Using 2 Hz Timebase for Program Control
Gerry Shand
Posts: 45
Hey Everyone:
Trying some code here without too much luck. What I am trying to do is count a 2 Hz pulse train to give me a fairly accurate seconds count to count the elapsed time a switch has been closed and to toggle a LCD display. Getting some wonky results so I am on the right track but getting run over because I am just sitting there.
Here is the code (edited to prevent a cure to insomnia):
'
'DEFINE VARIABLES
'
SecReg VAR Word········································ 'REGISTER FOR SECONDS
HourReg VAR Word······································· 'REGISTER FOR HOURS
Contactor VAR Switches.BIT7···························· 'SYSTEM·IS ON
CountReg VAR Nib······································· 'COUNTER FOR TOGGLING DISPLAY
TimeReg VAR Nib········································ 'TIME REGISTER FOR SECONDS
'
'DEFINE PINS
'
Tick PIN 6············································· 'INPUT PULSE FROM TIMER
'
'INITIALIZATION
'
DIRS=%0000000000011011································· '1 = OUTPUT
PAUSE 10··············································· '10 MS PAUSE TO WAIT FOR THINGS TO SETTLE DOWN
CountReg=0············································· 'SET COUNTER REGISTER TO ZERO
TimeReg=0·············································· 'SET TIME REGISTER TO ZERO
'
'MAIN PROGRAM
'
Main:
This first block takes the 2 Hz pulse train and is supposed to convert it into one second pulses
IF Tick=TimeReg.BIT0 THEN······························ 'INCREMENT TIME REGISTER
· TimeReg=TimeReg+1
ELSEIF TimeReg=4 THEN
· CountReg=CountReg+1·································· 'INCREMENT COUNTER REGISTER ONCE PER SECOND
· TimeReg=0············································ 'RESET TIME REGISTER
ENDIF
This block increments the seconds register by one second whenever the system is on. There is another inner loop here to log hours.
IF Contactor=1 AND TimeReg=0 THEN······················ 'INCREMENT TIMER IF CONTACTOR IS ON AND
· SecReg=SecReg+1······································ '1 SEC. HAS ELAPSED
· IF SecReg=3600 THEN·································· 'A MULTIPLIER OF 3600 IS REQUIRED
· HourReg=HourReg+1
· SecReg=0
· ENDIF
ENDIF
This block toggles the display
IF CountReg=10 THEN···································· 'TOGGLE BETWEEN DISPLAY SCREENS EVERY·5
· CountReg=0··········································· 'SECONDS AND RESET - CONTINUOUS LOOPING
· ELSEIF CountReg=<4 THEN
· GOSUB Display_Run_Time
· ELSEIF CountReg>4 THEN
· GOSUB Display_Set_Time
ENDIF
GOTO Main·············································· 'GO BACK TO THE BEGINNING
Any thoughts or suggestions?
Thanks.
Gerry
·
Trying some code here without too much luck. What I am trying to do is count a 2 Hz pulse train to give me a fairly accurate seconds count to count the elapsed time a switch has been closed and to toggle a LCD display. Getting some wonky results so I am on the right track but getting run over because I am just sitting there.
Here is the code (edited to prevent a cure to insomnia):
'
'DEFINE VARIABLES
'
SecReg VAR Word········································ 'REGISTER FOR SECONDS
HourReg VAR Word······································· 'REGISTER FOR HOURS
Contactor VAR Switches.BIT7···························· 'SYSTEM·IS ON
CountReg VAR Nib······································· 'COUNTER FOR TOGGLING DISPLAY
TimeReg VAR Nib········································ 'TIME REGISTER FOR SECONDS
'
'DEFINE PINS
'
Tick PIN 6············································· 'INPUT PULSE FROM TIMER
'
'INITIALIZATION
'
DIRS=%0000000000011011································· '1 = OUTPUT
PAUSE 10··············································· '10 MS PAUSE TO WAIT FOR THINGS TO SETTLE DOWN
CountReg=0············································· 'SET COUNTER REGISTER TO ZERO
TimeReg=0·············································· 'SET TIME REGISTER TO ZERO
'
'MAIN PROGRAM
'
Main:
This first block takes the 2 Hz pulse train and is supposed to convert it into one second pulses
IF Tick=TimeReg.BIT0 THEN······························ 'INCREMENT TIME REGISTER
· TimeReg=TimeReg+1
ELSEIF TimeReg=4 THEN
· CountReg=CountReg+1·································· 'INCREMENT COUNTER REGISTER ONCE PER SECOND
· TimeReg=0············································ 'RESET TIME REGISTER
ENDIF
This block increments the seconds register by one second whenever the system is on. There is another inner loop here to log hours.
IF Contactor=1 AND TimeReg=0 THEN······················ 'INCREMENT TIMER IF CONTACTOR IS ON AND
· SecReg=SecReg+1······································ '1 SEC. HAS ELAPSED
· IF SecReg=3600 THEN·································· 'A MULTIPLIER OF 3600 IS REQUIRED
· HourReg=HourReg+1
· SecReg=0
· ENDIF
ENDIF
This block toggles the display
IF CountReg=10 THEN···································· 'TOGGLE BETWEEN DISPLAY SCREENS EVERY·5
· CountReg=0··········································· 'SECONDS AND RESET - CONTINUOUS LOOPING
· ELSEIF CountReg=<4 THEN
· GOSUB Display_Run_Time
· ELSEIF CountReg>4 THEN
· GOSUB Display_Set_Time
ENDIF
GOTO Main·············································· 'GO BACK TO THE BEGINNING
Any thoughts or suggestions?
Thanks.
Gerry
·
Comments
Although written for the BS1, would this link help:
http://www.parallax.com/Portals/0/Downloads/appnt/stamps/bs1Appnotes.pdf
Reference .pfd page 97; AppNote page 167
cheers, David
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Thanks for the code suggestion. I will implement it in the coming week and let you know how it turns out.
A little further background:
a. The 2 Hz signal comes from the Application Note that David described for the BS-1. So it should be a square wave with a 50% duty cycle. I modified the code to allow more efficient·GOTO usage in the program·but ran into what appears to be a run on condition. See c. below.
b. I just got the components to put this circuit together from Digi-Key as Active Electronics' cupboard in Edmonton has been pretty bare lately - and their service is hit and miss. Depends who is on the order desk that day and the mood they are in.
c. So I thought I could save some time by using the 1 Hz signal on my Parallax Professional Development Board and just change a few scaling numbers to compensate for the 1 Hz in lieu of the 2 Hz. But Tracy brings up a good point and I have not thought to scope the output·- it may not be a 50% duty cycle. The phenomena I see appears to be a run on condition because when the display toggles, the seconds jump in groups of about 5 - I use the debug function and changed the LCD display to show seconds instead of hours for the run time. This has caught me before on other projects. Thanks for the reminder as sometimes pain is not a good teacher.
So stay tuned for further details. If anyone has any comments about the reliability of the 2 hz circuit or has a better circuit, let me know. Right now I am just using standard component values called out for in the schematic but there is a caveat about oscillator start up so some more tweaking may be in order. So much for a simple project.
Regards,
Gerry Shand