go away spammer

Author Topic: Timing interrupts  (Read 2345 times)

0 Members and 1 Guest are viewing this topic.

Offline camerartTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Timing interrupts
« on: January 18, 2014, 05:31:32 AM »
Hi,

Could anyone post examples of 'Timing interrupts' in oshonsoft picbasic, please.

I'm trying to find a way to get seconds by using the internal chip timing instaed of waitms1000 in a loop.

Cheers, Camerart.
« Last Edit: January 19, 2014, 06:40:26 AM by camerart »

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,345
  • Helpful? 82
Re: Timing interrupts
« Reply #1 on: January 18, 2014, 12:33:25 PM »
In general, you will set up a timer to run at a particular, known rate, and read this timer whenever you need to know the current time.

For example, if you have a 16-bit timer, and can set it to tick every millisecond, then you have a timer that wraps around every 65.536 seconds. This is often long enough for any real need -- measuring time between two events is typically done by reading the initial time, and then reading the next time, and doing unsigned subtraction, which will cancel out any single wrap from "high value" to "low value" that might happen in between.

If you need a clock that "keeps time" for a long time, then you will need to install an interrupt handler, and increment the "current time" each time it fires. I don't know specifically about whatever chip you're using, but if you can, for example, use a 10-bit counter, and make it count from 0 .. 999, and interrupt every time it ticks over, then you can increment a variable from the interrupt handler, which would measure how many seconds have elapsed. You may need to disable interrupts while reading this variable, if the increment-a-long-variable function is not memory-atomic on your controller.

If you need both high resolution, AND long duration (more than possible with your native timer size,) then it actually becomes hard -- you have to set an interrupt at the middle of the timer interval as well as the end, and you have to read the timer value as well as the incremented value, and do some comparisons to avoid the race condition of the timer wrapping before you read it, but the interrupt handler not having run yet (or vice versa.)

Also, microcontroller built-in timers are not super accurate. If you want to "keep time" in the sense of a wallclock, you're much better off adding a high-quality, temperature-compensated real-time clock chip over I2C or SPI, such as the DS3231 (used in the ChronoDot.)

Offline camerartTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: Timing interrupts
« Reply #2 on: January 19, 2014, 06:56:34 AM »
Thanks for your explanation.

The chips I use mainly are 12F683 16F648A 16F819.

In the past I've used a few components including XTLs, giving 1second PIC input pins.

I'm not good at programming, and limited to one language, and a few chips.  I have made lots of little things with just these, and find this great.

When I get chance I like to add to my 'module' section (where the timer will sit) for use as and when a program needs them.

I almost understand what you say, but as for converting it into actual language is too difficult for me, this is why I would like someone to add actual picbasic code for me to copy.

Cheers, Camerart.

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,345
  • Helpful? 82
Re: Timing interrupts
« Reply #3 on: January 19, 2014, 02:39:37 PM »
I see. I have no experience in picbasic; sorry!

Offline camerartTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: Timing interrupts
« Reply #4 on: January 19, 2014, 05:48:54 PM »
Ok, thanks for trying.

I have been plodding on with snippets of code from a variety sources, It's a bit of a puzzle, but I might be getting there?

Camerart.

 


Get Your Ad Here