Shop OBEX P1 Docs P2 Docs Learn Events
Accurate timing on a Basic Stamp? — Parallax Forums

Accurate timing on a Basic Stamp?

Is there any way to get an accurate time on the Basic Stamp? Following the tutorials in “What’s a Microcontroller” I understand the concept of using a For...Next loop to approximate time, but that requires that you know how long the instructions inside the loop take to execute. I’m interested in some kind of timer that will set a Pin to HIGH on a defined interval. I could then construct my programs inside a Do Loop that would monitor the Pin. I saw that Parallax offers a timer chip, but wondering of there is any ‘native’ on-board functionality that will do this.

Thanks in advance for your input,
Tom

Comments

  • you can start with Tracy Allen website:

    http://www.emesystems.com/OLDSITE/BS2index.htm#misc
  • What are you trying to syn. with your timing loop. Of course the more code that needs to exacute on a single loop the longer the time.
  • Mike GreenMike Green Posts: 23,101
    edited 2020-05-25 04:10
    There's no built-in timer in the Basic Stamps. The most accurate time "standard" is the PAUSE statement which you can use, along with a careful analysis of instruction execution time as described by Tracy Allen, to create an interval timer working on the order of seconds or large fractions of a second. For more accurate or smaller intervals, you really have to build an external timebase using a CD4040B or other counter driven by a 32KHz watch crystal. You could also drive an optocoupler from the power line (carefully) to get a 60Hz time standard.
  • Thanks for the input everyone. I’m not really trying to build anything, just exploring the capabilities of the board. I’ve decided to use a 555 timer chip to build a timing circuit (using a BS2 pin to read the output). I’m curious to see how it acts though because I’ll have to leave the signal HIGH long enough to ensure that the BS2 doesn’t miss it because it is busy executing the commands in the Do...Loop.
  • Tracy AllenTracy Allen Posts: 6,656
    edited 2020-05-27 02:07
    Tom or Tony, which BASIC Stamp or board are you using?

    One simple expedient is to connect a capacitor to a pin through a 100 ohm resistor, and also connect the capacitor to 5 volts Vdd through a 1 M resistor. At one point in your DO loop, make the pin a low output to discharge the capacitor, and them return the pin to input, so the cap can start to charge up through the 1M. The instructions in the loop continue and have to finish before the capacitor can charge up to the 1.4V threshold. The loop sits there waiting for that to happen, whence the loop cycles. Timing of the loop is regulated externally by the RC time constant, even if there happens to be wobble in the instruction path. A bit longer time can be had by discharging down from 5V to 1.4V. Choose the capacitor appropriately. That said, the 555 timer would be more accurate.

    Many real time clock chips have a square wave output at 1 Hz, and that can be useful for what you want to do, especially if you also need the most accurate real time or alarms.
  • ok, here is an update and a request for help. I have the 555 circuit working fine. It is providing output and I can control the timing via a pot. I've hooked it up to a scope (Parallax USB scope) and it shows the output (3.3v) on a regular cycle. I run a wire from the 555 output to BOE Pin3, wired up a LED on both the BOE and the breadboard, and display the output via a simple DEBUG ? IN3 command in a Do...Loop (with a PAUSE 10 in the loop as well). It works great, both LED's flash in tandem and the value of IN shows 1 or 0 sync'ed with the LEDs (exactly what I was after). The problem is that when I remove the scope, the LED on the BOE goes out and the value of IN3 very quickly fluctuates between 1 and 0 (which explains why the LED is not "on"). Even when this occurs, the LED on the breadboard continues to blink on it's "normal" cycle so I presume the issue is not the timing circuit. I tried to attach a movie file so you can see what is occurring, but I got a message that file type is not allowed so I included a simple picture of the setup.

    Any thoughts would be appreciated.

    Thanks,
    Tom
    640 x 480 - 123K
  • More information... there is definitely some interaction with the scope going on. When I plug only a positive lead into the circuit or the positive rail on my breadboard, I get the rapid high/low on the BOE pin. When I plug ANY negative scope lead into the negative rail the output is correct (works with ch1, ch2 or the external trigger).
  • It sounds like maybe an issue with a lack of common ground. Do you have a direct connection from the 555 circuit ground to the BOE ground?
  • kwinnkwinn Posts: 8,697
    It sounds like maybe an issue with a lack of common ground. Do you have a direct connection from the 555 circuit ground to the BOE ground?

    +1
  • Exactly right! When I run a wire from the breadboard ground to the BOE ground everything works fine. I presume this is because both the scope and the BOE are grounded through the computer (e.g. common ground), which is why it works with the scope attached. If that is not the reason, please let me know.

    Thanks for your help!
    Tom
  • kwinnkwinn Posts: 8,697
    Tom-n-Tony wrote: »
    Exactly right! When I run a wire from the breadboard ground to the BOE ground everything works fine. I presume this is because both the scope and the BOE are grounded through the computer (e.g. common ground), which is why it works with the scope attached. If that is not the reason, please let me know.

    Thanks for your help!
    Tom

    You presume correctly.
  • The PIC16F57 does have one built in 8 bit timer counter module. It is on pin 2, but on the basic stamp version it is tied low Vss. This is per the data sheet if the timer is not in use.
  • Tom n Tony, please see my post on "Accessories".

    I need software for a Parallax USB Oscilloscope. Help?

    turbineguy
Sign In or Register to comment.