Shop OBEX P1 Docs P2 Docs Learn Events
How can a person figure out a true second without using a external clock — Parallax Forums

How can a person figure out a true second without using a external clock

MarkdahplumberMarkdahplumber Posts: 7
edited 2013-02-27 07:36 in BASIC Stamp
Hello I am using a 4 digit display and was wondering if there is a way to get my code to read as close to a second as possible with using only the BS2 and not any other source because it doesn't have to be perfect but would be nice to get somewhat close
and I am only counting seconds for now up to 9999

I know the BS2 has a certain time for a instruction but my question is how does a instruction work in a branch or gosub
example:

start:
gosub time
goto start

time:
debug cls
return <
the total loop for this code in my eyes is 6 instructions so will it be BS2 instruction time X 6 = time?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-25 21:08
    The Stamps do not have an accurate clock that's accessible to your program. The PAUSE statement uses the internal clock as well as time-dependent statements like PULSOUT, SERIN, SEROUT, and others. Instructions all take different amounts of time and each operation (+ - * / etc.) takes time. Some of the instruction times are discussed here. Click on "app-notes" link. It's possible to use a PAUSE 1000 for a fairly accurate one second delay. You then have to minimize the number of instructions between each PAUSE and adjust the PAUSE time to account for as much of the other statements' execution time as possible. Some of that will have to be by trial and error. It's a lot of work and it's so much easier and more accurate to use some kind of external RTC (Real Time Clock).

    Here's one RTC on a little breakout board that should be easy to interface to using the SHIFTOUT and SHIFTIN statements and 3 I/O pins.
  • MarkdahplumberMarkdahplumber Posts: 7
    edited 2013-02-25 21:56
    Okay thanks Mike for that fast response and info
    I was doing trial and error but couldn't get it quite close enough and thought maybe you professionals would know a little trick but then again
    it would seem impossible to get it with out external source but I have it down to only 12 seconds off in 3 hours so not bad for a rookie
    plus this experiment is not going anywhere but just learning different things

    thanks again
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-25 22:07
    There are all sorts of schemes for adding some kind of external time standard to a Stamp. You can make an oscillator / buffer with a cheap quad gate and a 32KHz crystal, then divide that by 4096 with this to get a 1/8th second time standard. You can divide that further to get a 1 second standard if you want.

    The Propeller has direct access to a count of the system clock which is usually 80MHz. It's pretty trivial to execute a section of code once a second with accuracy as good as the crystal.
  • dredre Posts: 106
    edited 2013-02-26 09:54
  • Mike GreenMike Green Posts: 23,101
    edited 2013-02-26 10:51
    David, Thanks for the reference. The 74HC4060N is the IC I was thinking of to give a 1/2 second reference with a 32KHz crystal. The page is marked 167 with the first page shown marked 71 in the excerpt you indicated.
  • ercoerco Posts: 20,256
    edited 2013-02-26 17:13
    Hello I am using a 4 digit display and was wondering if there is a way to get my code to read as close to a second as possible with using only the BS2 and not any other source because it doesn't have to be perfect but would be nice to get somewhat close
    and I am only counting seconds for now up to 9999

    I'm sure you can get "somewhat close" with nothing but a BS2 and a carefully calibrated loop. Might be voltage/temp dependant, but reasonable. Have you built your hardware displays yet, are they working?
  • ercoerco Posts: 20,256
    edited 2013-02-26 22:03
    Here's some quick clock code using the DEBUG screen.
    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    
    main:
    DO
    FOR B6=1 TO 12    'hours
    FOR B5=0 TO 59    'minutes
    FOR B4=0 TO 59    'seconds
    
    DEBUG DEC B6
    IF B5<10 THEN DEBUG ":0" ELSE DEBUG ":"
    DEBUG DEC B5
    IF B4<10 THEN DEBUG ":0" ELSE DEBUG ":"
    DEBUG DEC B4,CR
    PAUSE 975   'calibration for ~1 second minus execution time
    
    NEXT
    NEXT
    NEXT
    LOOP
    
  • ercoerco Posts: 20,256
    edited 2013-02-27 07:30
    I let that program run for a while and it gained less than one second over 2 hours. Your 9999 second limit is ~2.8 hours, so I'd call that "somewhat close". You could fine tune it more by adding a PULSOUT somewhere and recalibrating, but that would be gilding the lily. :)
  • RDL2004RDL2004 Posts: 2,554
    edited 2013-02-27 07:36
    Here is a useful little circuit for time. Your program could just check for a a change of the 1 Hz output and increment a count. As long as you check often (many times per second) it will be pretty accurate. Sorry for the low quality of the image, but it's something I found on the web a long time ago.

    I used this circuit as part of a frequency counter with a 4.194304 MHz crystal, worked fine.
    413 x 361 - 35K
Sign In or Register to comment.