Shop OBEX P1 Docs P2 Docs Learn Events
Measuring duration by using loops — Parallax Forums

Measuring duration by using loops

Hello. As I'm going through What's a Microcontroller book, I've noticed that there are many occasions where duration of time is roughly measured and controlled by use of loops. For example, by estimating one pass of through a loop to be 1/100th of a second, the for loop would be made to loop for 100 times to last for one second. I'm wondering if this method is used for the sake of simplicity in this introductory text and BS2, or if this is a common method in microcontroller programming.

I've found this technique used often in the servo programming chapter, where a position is needed to be held for specified number of seconds. I already see drawbacks to this technique if for example, some other actions need to be performed while servo position is being maintained, which may increase the time of each pass through the loop.

Are there other frequently used methods in the microcontroller programming world when developed for production use? Thank you for your help.

Comments

  • Short of an external timing source, timing loops (which may include PAUSE) is pretty much your only option for the stamp.

    The propellor has other options, including the ability to sense when a specific interval has passed.
  • Ok, I see. So this is a limitation of the Basic Stamp itself, not necessarily of the microcontrollers in general.

    Thank you.
  • Lots of different micros out there, each with its own limitations and abilities. That said, if you want to fiddle servos, Parallax Propellor is a good place to look. Prop Mini can easily control half a dozen and have six or seven processors left over to simultaneously handle other tasks.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2016-10-03 16:16
    The Stamp is deterministic, so simple loops for timing can be quite good. Many other processors, big and small, have interrupts that take priority over the main program, with the result that the timing of any coded loop is thrown off, unpredictable. Some processors turn off those interrupts in order to gain absolute control of timing, while others use interrupts from an external clock source to set the timing.

    The Stamp (and Propeller) have no interrupts, and therefore code loops are deterministic. The Stamp can run only one program thread at a time, so the watchword is "simple loops". Anything involving more than one task at a time must be considered in relation to the accuracy required. You might have to "tune" the loop to hit an exact period if that is necessary. In some situations it is quite adequate. You can always add an external real time clock chip to the Stamp circuit.
  • Thank you. That makes sense. I bought the Basic Stamp for the purpose of learning, and it's been perfect for that. I am surprised that a CPU can come without an internal clock though, as I expected it to be a necessity of being a CPU.
  • JonnyMacJonnyMac Posts: 9,156
    edited 2016-10-04 15:36
    Keep in mind that the BASIC Stamp has been around for quite a long time. The first (BS1) was released in 1993, and the BS2 -- which I assume you're using -- was released in 1995 (I believe). Both are based on very simple PIC processors, but with a bit of effort you can do big things. In 1997 I created a commercial product for a company called Flowtonex that used the BS2 at its core. It was an alarm timer that could time four channels at roughly 100ms resolution, and if an alarm event occurred it would use an internal modem to dial your pager service and send a code (remember, this was in 1997 when pagers were still in use). No, this is not great resolution, but for the intended purpose it was fine. Since the BS2 doesn't have a timer or interrupts (not all micros do), I had to go old-school and pad all of the paths through the code so that no matter which path was followed, the program loop was 100ms. It took a couple days of testing but it worked and the product was successful.

    Today I would code it in the Propeller and have as many channels as I have pins timed with sub-1ms resolution. Times change, as do products. The BASIC Stamp is a great way to get accustomed to programming. When your needs are beyond what the BASIC Stamp can handle, the Propeller is waiting in the wings (and boy is it a lot of fun).
  • Mason_L wrote: »
    Thank you. That makes sense. I bought the Basic Stamp for the purpose of learning, and it's been perfect for that. I am surprised that a CPU can come without an internal clock though, as I expected it to be a necessity of being a CPU.

    The bs2 does have an internal clock; what it doesn't have is a counter that is visible to the programmer.

    As JonnyMac says, learn about programming on BS2 and go on to a more powerful machine when your requirement dictate.

  • Tracy AllenTracy Allen Posts: 6,664
    edited 2016-10-04 18:17
    Say you press a button on the microwave oven to warm up a cup of coffee. It really doesn't matter if the time is 59 seconds or 61 seconds, but a microcontroller running in a loop will have no trouble hitting one minute within a much tighter spec. A lot of small embedded processors do just that. The microwave probably has a clock display though, so internally it may be picking its timing off of the 60 or 50 Hz line. Again, no need for an internal ticker. KISS. +1 that the BASIC Stamp is a very capable processor and a wonderful tool for learning and to do a lot with a little.
Sign In or Register to comment.