Shop OBEX P1 Docs P2 Docs Learn Events
Is there a register that counts the number of clock cycles that occur? — Parallax Forums

Is there a register that counts the number of clock cycles that occur?

horwattwhorwattw Posts: 1
edited 2014-11-17 13:20 in BASIC Stamp
I am working on a basketball scoreboard controlled by an infrared remote. There is a main loop that checks for input pulses from an infrared sensor, and then outputs the necessary displays to 7-segment LEDs. This seems to work okay. However, I am having trouble adding a way to tell how much time has passed, in order to display a clock that counts down. The main loop uses several PULSIN commands, which do not always last the same amount of time. This means that it is difficult to predict how long each execution of the main loop lasts for time counting purposes.

I'd hate to have to go to an assembly level board for this (or add a second board). Is there an easy solution?

Comments

  • tomcrawfordtomcrawford Posts: 1,126
    edited 2014-11-17 11:00
    If you cannot guarantee how long your main loop is going to take, I think you are going to need an external timing input.

    1. The easiest to connect to would be a GPS with 1 pps output; sadly, it would cost you tens of dollars. Check to make certain you would not have to program it to supply the output.
    2. There are Real Time Clock chips with square wave outputs (the DS1302 is *not* one of them); less than ten dollars for the chip and the crystal. You would have to program it to supply a 1 Hz output.
    3. Depending on your power supply, you might be able to obtain a 60 Hz reference. Depending on how fast your main loop runs, you might be able to divide it in software to obtain a 1 Hz reference. Alternately you could divide it externally by 6 or 10 with a single 749x to obtain something you can work with.
    4. Do not succumb to the temptation of using a 555 or similar analog timer because you cannot obtain consistent results over temperature and aging. In My Opinion and Experience.
  • JonnyMacJonnyMac Posts: 9,014
    edited 2014-11-17 11:33
    Have you considered the Propeller? You can do what you want without resorting to Assembly code. There are lots of IR objects available, and keeping track of time using the Propeller is very simple. This is not a knock at the BASIC Stamp (I used to be called the "King of BASIC Stamps" when I worked at Parallax); what you're doing is on the complex side and doing it in PBASIC may be asking too much.
  • Mike GreenMike Green Posts: 23,101
    edited 2014-11-17 13:20
    To specifically answer your question, there are registers on the microcontroller chips used to make the Stamps that can keep track of the number of clock cycles, but they're not directly accessible to PBasic programs. They're used internally to implement statements like PAUSE, PULSOUT, PULSIN and other statements that do timing of various sorts. tomcrawford's suggestions are all usable. Like JonnyMac, I'd suggest using a Propeller for your project. If you want to stick with the Stamp, the scheme needed depends on how much precision you need. If you only need seconds-to-go, then pretty much any Real Time Clock will do. You can compute the time to the end of the quarter (or whatever) and, at least once a second, read the actual time from the RTC and compute the seconds-to-go for display from that and the time to the end of the quarter. If you need tenths of a second, you will want some kind of external counter. An MC14060B is a 14-bit counter with a built-in oscillator that can run off a 32768Hz clock crystal to give a 2Hz clock. The datasheet shows a typical crystal oscillator circuit.
Sign In or Register to comment.