Timer problems
Kram11
Posts: 8
Can anyone help?
· I am trying to understand how I can use timers within a program without pausing the code as waitcnt seems to. I have therefore tried to sample the cnt value and compare it to my timed valve required. If the current cnt value plus my timed valve is greater I set a bit and display on the screen.
I seem to be getting a -ve value from the cnt, is this correct?
Is there an easier way of using timers
OBJ
········ tv··· :······ "TV_Text"·
VAR
·long Delay_Start
·byte Delay_Done
·byte Time_Set
·byte Timer1_Complete
·long Start_Point1
CON
··· _CLKMODE = XTAL1 + PLL16X
··· _XINFREQ = 5_000_000
·
·
PUB Main
··· tv.start(12)
repeat
···· tv.Str(string("Delay Time: ",13))
····
····· if ina[noparse][[/noparse]4]· ==1
······· Time_Set:=0·············
·······
· Check_Time
·········· tv.dec(Delay_Start)
··········· tv.out(13)
···········
···········
···· Timer1_On(9_000,1)
·········· tv.dec(Timer1_Complete)
·········· tv.out(13)
·········· tv.dec(Start_Point1)
····
·· tv.out($01)· ' back to home
····
PUB Check_Time
····· Delay_Start := cnt
·····
PUB Timer1_On (Delay1,Start1)
······ if Time_Set==0
········· Start_Point1:=cnt ' check the start time
··· Time_Set:=1· ' the timer is now running
··· if Delay1>(Start_Point1+cnt)
····· Timer1_Complete:=1
··· else
····· Timer1_Complete:=0
······
······
· I am trying to understand how I can use timers within a program without pausing the code as waitcnt seems to. I have therefore tried to sample the cnt value and compare it to my timed valve required. If the current cnt value plus my timed valve is greater I set a bit and display on the screen.
I seem to be getting a -ve value from the cnt, is this correct?
Is there an easier way of using timers
OBJ
········ tv··· :······ "TV_Text"·
VAR
·long Delay_Start
·byte Delay_Done
·byte Time_Set
·byte Timer1_Complete
·long Start_Point1
CON
··· _CLKMODE = XTAL1 + PLL16X
··· _XINFREQ = 5_000_000
·
·
PUB Main
··· tv.start(12)
repeat
···· tv.Str(string("Delay Time: ",13))
····
····· if ina[noparse][[/noparse]4]· ==1
······· Time_Set:=0·············
·······
· Check_Time
·········· tv.dec(Delay_Start)
··········· tv.out(13)
···········
···········
···· Timer1_On(9_000,1)
·········· tv.dec(Timer1_Complete)
·········· tv.out(13)
·········· tv.dec(Start_Point1)
····
·· tv.out($01)· ' back to home
····
PUB Check_Time
····· Delay_Start := cnt
·····
PUB Timer1_On (Delay1,Start1)
······ if Time_Set==0
········· Start_Point1:=cnt ' check the start time
··· Time_Set:=1· ' the timer is now running
··· if Delay1>(Start_Point1+cnt)
····· Timer1_Complete:=1
··· else
····· Timer1_Complete:=0
······
······
Comments
Two remnarks:
(1) Understand that CNT is a wrap-around counter, counting up to POSX and then down from NEGX; a whole cycle takes 40 seconds @ 80 MHz...
Checking
as you did is generally a good idea, but it must rather read:
You cannot detect delays longer than 25 seconds with this simple technique.
Why is this unprecise? Because you use a lot of SPIN operations inbetwee, so you your accuracy will be araumd some hundred µs.