PDA

View Full Version : Set Port problem



ae
12-07-2009, 08:43 PM
Hi All,

I will use a tmr spin in a serate cog·which is blinking a led.
The Led is not blinking but the TmrTick· is working ! How can I correct this code?

{{··Main Program }}
CON
· _clkmode = xtal1 + pll16x······ ' use crystal x 16
· _xinfreq = 5_000_000··········· ' 5 MHz cyrstal (sys clock = 80 MHz)
OBJ
· Tmr··· : "LedTmr.spin"
PUB main | i
· tmr.Start
·
'The main loop
·repeat
····i:=1·· 'Wait Idle
{{ End Of Main Program }}


{{ LedTmr.spin }}
CON
· LED·· = 27 'P27
· Ms500 = 50
VAR
· long· Stack[16]·················· 'Stack space for new cog
· byte· Cog·························· 'Hold ID of cog in use, if any
· byte· LEDCntr

PUB Start: Success
{{Start new blinking process in new cog; return TRUE if successful}}
· Stop
· LEDCntr := 0
· OUTA[LED]:=0·
· DIRA[LED]~~· 'Output for Led·
· Success := (Cog := cognew(PauseMs(10), @Stack) + 1) '10 ms·timer
··
PUB Stop
{{Stop toggling process, if any.}}
· if Cog
··· cogstop(Cog~ - 1)

PRI TmrTick·
· if LEDCntr>0···
··· LEDCntr--
· else
··· !OUTA[LED]· '500 ms LED togle
··· LEDCntr:=Ms500
···

PUB PauseMs(Period)|ClkCycles
{{Pause execution for Period (in units of 1 ms).}}
· ClkCycles := ((clkfreq / 1000 * Period) - 4296) #> 381···· 'Calculate 1 ms time unit
· repeat
··· waitcnt(ClkCycles + cnt)·································· 'Wait for designated time·············
··· TmrTick
{{ End Of·LedTmr.spin Program }}

kuroneko
12-07-2009, 08:57 PM
Each cog has it's own set of dira/outa registers. In the start method you manipulate them in the context of the caller cog (not the one you subsequently start and which toggles the LED). What you have to do is move the two lines relating to dira/outa handling into the PauseMs() method. Actually, dira setup will be sufficient.

Post Edited (kuroneko) : 12/7/2009 1:04:55 PM GMT

ae
12-07-2009, 09:15 PM
Thank you very much your quick reply.