Shop OBEX P1 Docs P2 Docs Learn Events
Using 2 Hz Timebase for Program Control — Parallax Forums

Using 2 Hz Timebase for Program Control

Gerry ShandGerry Shand Posts: 45
edited 2010-03-16 03:43 in BASIC Stamp
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
·

Comments

  • dredre Posts: 106
    edited 2010-03-14 16:20
    Gerry,

    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 AllenTracy Allen Posts: 6,662
    edited 2010-03-14 18:14
    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
    Main:
    DO  
      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
  • Gerry ShandGerry Shand Posts: 45
    edited 2010-03-16 03:43
    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.

    Regards,

    Gerry Shand
Sign In or Register to comment.