Author Topic: Pic MCU - Calculating Timer0 Overflow Rate  (Read 5061 times)

0 Members and 2 Guests are viewing this topic.

Offline kolbepTopic starter

  • Frequent Contributor
  • **
  • Posts: 606
  • Country: za
    • ShoutingElectronics.com
Pic MCU - Calculating Timer0 Overflow Rate
« on: September 27, 2014, 08:29:48 am »
Good Day.
I know there is a really simple answer to this one. But the flu has hit me to an extent where my brain does not want to do much.
What I am trying to do is work out how many times in 1 second Timer0 will generate an overflow interrupt, if the Osc is running at 16Mhz.
My intent is to make a phase angle dimmer, and I would like to calculate the time to fire the triac using  timer interrupts. But I am stumped.
What is the correct formula using:
Osc Frequency, Prescaler, Counter Bits (8/16), to work out the overlflows per second.

Here is how far I got, until I got stumped :

Thanks.
P

====================================
www.ShoutingElectronics.com Don't just talk about Electronics, SHOUT ABOUT IT! Electronics Blog Site and Youtube Channel
 

jucole

  • Guest
Re: Pic MCU - Calculating Timer0 Overflow Rate
« Reply #1 on: September 27, 2014, 09:35:01 am »
I think with most pics you need to divide the crystal clock by 4 to get the internal clock;  so - 16Mhz gives you an internal 4 Mhz "fosc/4" clock;  also I think the counter overflow occurs on the next clock cycle so in an 8 bit counter example it would appear to delay for 256 cycles, and then jump to your ISR.
 

Online jc101

  • Frequent Contributor
  • **
  • Posts: 890
  • Country: gb
Re: Pic MCU - Calculating Timer0 Overflow Rate
« Reply #2 on: September 27, 2014, 09:35:55 am »
Pop over to MikroE and download their free tool...

http://www.mikroe.com/timer-calculator/

...very handy.
 

Offline kolbepTopic starter

  • Frequent Contributor
  • **
  • Posts: 606
  • Country: za
    • ShoutingElectronics.com
Re: Pic MCU - Calculating Timer0 Overflow Rate
« Reply #3 on: September 27, 2014, 09:52:40 am »
thnks
the calculator is exactly what i needed
====================================
www.ShoutingElectronics.com Don't just talk about Electronics, SHOUT ABOUT IT! Electronics Blog Site and Youtube Channel
 

Offline miguelvp

  • Super Contributor
  • ***
  • Posts: 5550
  • Country: us
Re: Pic MCU - Calculating Timer0 Overflow Rate
« Reply #4 on: September 27, 2014, 06:00:42 pm »
I know you have the calculator now, but for overflow you should use 256 instead of 255 and 65536 instead of 65535, since those are the number of values your counter will have until the next iteration.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Pic MCU - Calculating Timer0 Overflow Rate
« Reply #5 on: September 28, 2014, 11:17:00 am »
Quote
What I am trying to do is work out how many times in 1 second Timer0 will generate an overflow interrupt

Another way to think of it is to calculate the intervals between interrupts. 16Mhz crystal means 4Mhz cpu frequency on the pic. each tick is 0.25us. At 1:2 prescaler and 8-bit counter (assuming no reloading), that's 0.25us * 2 * 256 = 128us. or 1000000us / 128us ~= 8000 times per second.

I don't know what your application is trying to do but as you can see, sometimes it yields fractional counts -> an impossibility. And over time, such fractional counts can lead to timing inaccuracy.

There are ways around that, if long-term timing accuracy is an issue for your application.
================================
https://dannyelectronics.wordpress.com/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->