Pseudo Timer Interrupt
StephenMoore
Posts: 188
I am trying to do some real time machine motion control and am trying to use two separate background timers. Does anyone have an idea for trapping timer timeouts. The following program fails since in DUTY mode the carry bit is only high for one clock cycle. I would like to do a bunch of math and motor control while the timer is running and stop when it runs out.
CON _CLKMODE = XTAL1 + PLL16X _XINFREQ = 5_000_000 myPin = 1 VAR long myStack[10], timerValue byte cog, flag OBJ pst :"Parallax Serial Terminal" PUB Main pst.Start(115200) cog := cognew(testTimer(myPin, @timerValue), @myStack) waitcnt(clkfreq * 2 + cnt) repeat pst.str(string($0D, "Enter timeout value: ")) timerValue := pst.DecIn repeat while (timerValue <> 0) 'Do a bunch of motor control here PUB testTimer(Pin, timerAddr) | timer timer := 0 flag := 0 repeat if timer <> long[timerAddr] timer := long[timerAddr] frqa := 1 phsa := $FFFF_FFFF - timerValue ctra := 111 << 26 + Pin 'Start timer in DUTY cycle mode repeat while (flag == 0) flag := ina[Pin] 'Trap timer timeout here long[timerAddr] := 0
Comments
So you'd monitor the phsx value until it's negative and then wait for it to become positive again.
In my Tachyon Forth I have stepper motors running and I need to have multiple timeouts and so I use a similar scheme which is built-in to the kernel, I just say "#2000 5 TIMEOUT" to set timer 5 for a 2 second timeout and then use "5 TIMEOUT?" to test that timer. Of course this timer task can do a lot more but this feature alone is very useful.
That is what I am trying to do. I actually need two timers. One for overall motion move and one that times out at regular intervals for doing numerical integration for overall rotations. I will try to set up a cog with this feature and post it. It won't be ultra precise but if I look at PHSA or PHSB register values I should be able to get within a few hundred clock cycles.
Regards,
sm
The idea is the timer cog will control pulse output on ctra (and encoder pulse counting on ctrb eventually). The main program controls time of execution.
I am not sure I am allocating cog stack space properly in the Timer object...