View Full Version : Using 2 Hz Timebase for Program Control

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):
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

Tick PIN 6иииииииииииииииииииииииииииииииииииииииииииии 'INPUT PULSE FROM TIMER

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


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
и CountReg=CountReg+1иииииииииииииииииииииииииииииии иии 'INCREMENT COUNTER REGISTER ONCE PER SECOND
и TimeReg=0иииииииииииииииииииииииииииииииииииииииии иии 'RESET TIME REGISTER
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

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
GOTO Mainииииииииииииииииииииииииииииииииииииииииииииии 'GO BACK TO THE BEGINNING

Any thoughts or suggestions?



03-14-2010, 11:20 PM

Although written for the BS1, would this link help:


Reference .pfd page 97; AppNote page 167

cheers, David

Tracy Allen
03-15-2010, 01:14 AM
I see a couple of possibilties there. One is that the loop takes too long updating the LCD etc. and does not in time to Main:. On the other hand, sometimes the loop runs through so fast that it gets back to Main: before the input tick changes, so there is a run-on. What are you observing that you describe colorfully as, "Getting some wonky results so I am on the right track but getting run over because I am just sitting there."? Is the pulse train a square wave, or short pulses? It seems too me more likely to be the run-on problem. Maybe the following would help...

tickold VAR bit
ticknow VAR bit
screen VAR bit ' for decision of which screen to display

tickold = tick ' initialize
ticknow = tick
LOOP WHILE ticknow=tickold ' stay in this loop until change in tick
tickold = ticknow ' update the comparison value
TimeReg = TimeReg + 1 // 4 ' automatically a cycle of 4
CountReg=CountReg + 1 // 10 ' automatically a cycle of 10
screen = CountReg / 5 ' This bit variable is 0 for countreg= {0 to 4} and 1 for countreg= {5 to 9}
' .. and so on

Tracy Allen
www.emesystems.com (http://www.emesystems.com)

Gerry Shand
03-16-2010, 10:43 AM
Hi Tracy and David:

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.


Gerry Shand