Gerry Shand
03-14-2010, 10:23 AM
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
·