Scheibuman
Member
Hello everyone,
My name is Dirk, I'm 50 years old, I'm from Germany and I'm new to the forum.
I have been looking for some time for a solution to program microcontrollers that I can understand in order to implement a few small ideas.
After some research I came across Matrix - Flowcode, but I found it too complicated. Then I came across Pixace, which I found very appealing because of BASIC.
In this very good forum I found a lot of code snippets to realize my current project, but unfortunately I encountered timing problems.
About my project:
I would like to have a motor extend and retract a component by reversing the polarity. The motor should extend for x seconds when the exit button is pressed briefly, but stop immediately when it is pressed again, i.e. interrupt the time loop. If you press the other button during the x seconds, the motor should retract for x seconds.
The same applies to the exit button.
However, the motor should also extend when a certain frequency is reached and retract again when the frequency falls below a certain level.
The frequency range is 0 to a maximum of 250 Hz, whereby the motor should always extend above 155 Hz, i.e. frequencies above 155 Hz are not important for me.
So let's just assume that the motor should extend at 120 Hz and retract at 20 Hz.
To evaluate the frequency, I wanted to use the Pulsin command. But this simply takes too much time. At 4Mhz and "open loop" programming, my program needs approx. 3 seconds for 255 runs without the Pulsin command. To measure this, I let an LED toggle in the Do-Loop instruction after 255 passes.
If I include the Pulsin command in the loop, the program needs 2:52 minutes(!) for the 255 runs at 0 Hz. At 10 Hz a good 25 seconds and at 120 Hz around 3 seconds again.
My problem is that the motor should only run for x seconds. I have to define a time somehow. At 0 Hz, however, the program always needs at least 0.8 seconds to run through once, and it gets faster at higher frequencies. The internal timer does not adhere to the specified time as soon as no frequency is present.
I have chosen a 14M2+ as µC, as I need 3 inputs and 3 outputs.
Running it at 16 or even 32 Mhz does not bring any noticeable time advantage, as Pulsin still needs the same time to determine the time between the edges.
Is it possible to limit the Pulsin command with a TimeOut parameter? Because it would be enough for me if it only fills the variable from 15 Hz and does not measure frequencies below that.
I am attaching the code I hope you don't stone me because of my bad English and the bad code (I don't know what is worse).
My name is Dirk, I'm 50 years old, I'm from Germany and I'm new to the forum.
I have been looking for some time for a solution to program microcontrollers that I can understand in order to implement a few small ideas.
After some research I came across Matrix - Flowcode, but I found it too complicated. Then I came across Pixace, which I found very appealing because of BASIC.
In this very good forum I found a lot of code snippets to realize my current project, but unfortunately I encountered timing problems.
About my project:
I would like to have a motor extend and retract a component by reversing the polarity. The motor should extend for x seconds when the exit button is pressed briefly, but stop immediately when it is pressed again, i.e. interrupt the time loop. If you press the other button during the x seconds, the motor should retract for x seconds.
The same applies to the exit button.
However, the motor should also extend when a certain frequency is reached and retract again when the frequency falls below a certain level.
The frequency range is 0 to a maximum of 250 Hz, whereby the motor should always extend above 155 Hz, i.e. frequencies above 155 Hz are not important for me.
So let's just assume that the motor should extend at 120 Hz and retract at 20 Hz.
To evaluate the frequency, I wanted to use the Pulsin command. But this simply takes too much time. At 4Mhz and "open loop" programming, my program needs approx. 3 seconds for 255 runs without the Pulsin command. To measure this, I let an LED toggle in the Do-Loop instruction after 255 passes.
If I include the Pulsin command in the loop, the program needs 2:52 minutes(!) for the 255 runs at 0 Hz. At 10 Hz a good 25 seconds and at 120 Hz around 3 seconds again.
My problem is that the motor should only run for x seconds. I have to define a time somehow. At 0 Hz, however, the program always needs at least 0.8 seconds to run through once, and it gets faster at higher frequencies. The internal timer does not adhere to the specified time as soon as no frequency is present.
I have chosen a 14M2+ as µC, as I need 3 inputs and 3 outputs.
Running it at 16 or even 32 Mhz does not bring any noticeable time advantage, as Pulsin still needs the same time to determine the time between the edges.
Is it possible to limit the Pulsin command with a TimeOut parameter? Because it would be enough for me if it only fills the variable from 15 Hz and does not measure frequencies below that.
I am attaching the code I hope you don't stone me because of my bad English and the bad code (I don't know what is worse).
Code:
'Simple Flap
'Take2
Start:
setfreq m4 'Pimp my Pulsein 32 Mhz don't work...
'Labeling
'Inputs
symbol inSpeed = C.0
symbol inSWout = pinC.1
symbol inSWin = pinC.2
'Outputs
symbol outMotorUp = B.2
symbol MotorUp = pinB.2
symbol outMotorDown = B.3
symbol MotorDown = pinB.3
symbol outBeeper = B.4
'Buttondetection
symbol SWoutTemp = b0
symbol SWoutMem = b1
symbol SWoutL2H = b2
symbol SWinTemp = b3
symbol SWinMem = b4
symbol SWinL2H = b5
'Constants
symbol conMotorOnTime = b8
symbol conMotorOnLoop = b10
'Variables
symbol vSpeed = b9
symbol vTime = b6
symbol LoopTime = b11
'Setting Constants
conMotorOnTime = 6 '(3 sec at 32Mhz)
'Declare the outputs
output outMotorUp, outMotorDown, outBeeper
'set outputs to low
low outMotorUp
low outMotorDown
low outBeeper
'#terminal 4800
Main:
do ' Detect pressed buttons (low->high)
SWoutTemp = inSWout 'Copy Out-Switch-State to temp
SWoutL2H = SWoutTemp andnot SWoutMem 'Compare Temp with Mem to Detect Button state
SWoutMem = SWoutTemp 'write Temp to Mem
SWinTemp = inSWin
SWinL2H = SWinTemp andnot SWinMem
SWinMem = SWinTemp
LoopTime = LoopTime Max 254 + 1 'Counts the loops
' If low to high (L2H) is detected, select direction and start or stop motor
if SWoutL2H = 1 then
pinB.1 = 0 'Program time indicator
MotorDown = 0 'Stop Motor Down, because motor can not up und down at the same time
MotorUp = not MotorUp 'Toggle MotorUp
let vTime = Time + conMotorOnTime
elseif SWinL2H = 1 then '
MotorUp = 0 'Stop Motor Up, because motor can not up und down at the same time
MotorDown = not MotorDown 'Toggle MotorDown
let vTime = Time + conMotorOnTime
endif
'sertxd ("SWoutTemp: ",#SWoutTemp)
if Time => vTime then 'Check Time if Time goes by:
low outMotorUp ' Stop both MotorOuts
MotorDown = 0
Low outMotorDown
MotorUp = 0
vTime = 0
endif
if LoopTime = 255 then 'Programm has looped 255 times
toggle B.1 'light up or down red LED
LoopTime=0 'Reset LoopCounter
endif
pulsin inSpeed,1,vSpeed 'Check actual speed and slow down the code...
loop