problem understanding timers :/

Thread Starter

sayf alawneh

Joined Aug 4, 2014
20
here is my problem first of all am using pic 16f877a
timer 0 is 8 bit timer and timer 1 is 16 bit timer ,ok
if am gonna use one of them as a frequency counter from an external source what is the difference between using any of those 2 timers for frequency counting purpose ????
using an 8 bit timer or 16 bit timer how can that effect my frequency meter????
also how can i know the maximum frequency i can measure using those timers????
and what is the relation between the max measured frequency and the timer being 8 or 16 bits ???? :/
forgive me guys i know u may think those r silly questions but am still a newbie and they cause me headache
and asking stupid question is better than being stupid for ever :(
 

Shagas

Joined May 13, 2013
804
8 bit timer means that the timer can count up to 256.
16 bit timer mean that it can count up to 65535.

Lets say you have a cpu clock of 1Mhz . If you don't use a prescalar (google it if you don't know what it is ) then you have a timer speed of 1Mhz.
So if you wanted to generate a frequency of say... 2Khz then you would have a problem with the 8 bit timer because 1000000/256 = 3906 Hz which means that 3906 would be the lowest frequency that you can generate.
Lets say you are using a 16Mhz clock and 8 bit timer and you want to generate 15Hz. You would need a big prescalar ( I think the maximum on pic is 1024? )
so you would get a timer clock of 16,000,000/1024 = 15,625 Hz. So 15625/256 = 61 Hz. So the timer would overflow too fast and you would need to use the 16 bit timer.

16 Bit timer is usually used when a higher timing resolution and or range is required.
I suggest you do some projects with both and I can guarantee you that you will run into problems very fast and understand when to use which.
 

Thread Starter

sayf alawneh

Joined Aug 4, 2014
20
8 bit timer means that the timer can count up to 256.
16 bit timer mean that it can count up to 65535.

Lets say you have a cpu clock of 1Mhz . If you don't use a prescalar (google it if you don't know what it is ) then you have a timer speed of 1Mhz.
So if you wanted to generate a frequency of say... 2Khz then you would have a problem with the 8 bit timer because 1000000/256 = 3906 Hz which means that 3906 would be the lowest frequency that you can generate.
Lets say you are using a 16Mhz clock and 8 bit timer and you want to generate 15Hz. You would need a big prescalar ( I think the maximum on pic is 1024? )
so you would get a timer clock of 16,000,000/1024 = 15,625 Hz. So 15625/256 = 61 Hz. So the timer would overflow too fast and you would need to use the 16 bit timer.

16 Bit timer is usually used when a higher timing resolution and or range is required.
I suggest you do some projects with both and I can guarantee you that you will run into problems very fast and understand when to use which.
thank u ^_^
the maximum prescaler on pic is 256
but am still wondering about the maximum frequency i can generate i read it they say its 50 million hertz
i need to make a counter in other words i wanna use the timer to count pulses from an external source let us say that the external source is a pwm generated from another pic microcontroller and this pwm is fed to the pic am gonna use to count those pulses coming from the other pic
so what max pulses or frequency i can count thats my question :/
 

tindel

Joined Sep 16, 2012
936
I think you're looking at it backwards. I think you need to spec the max frequency that you want to count. However, If you're chip is operating at 50MHz then theoretically the fastest you could count is 25MHz due to Nyquist frequency, but you won't be able to achieve this frequency because you're uC will also be used to do other things. I'd think 10MHz would be the best you could hope for... and that's probably pushing it. I haven't seen many PWM's operate over 1MHz though so that's in your favor.

The algorithm would be something like this in my mind.
  • Setup your timer to interrupt at least 2 times the maximum frequency you want to count (this way you're above nyquist frequency and don't get aliasing), preferably 5x
  • After an interrupt read the state of the input, and immediately return to the main routine.
  • If a state has changed from 0 to 1 (or 1 to 0, if you prefer), then increment the counter.
  • Use a second timer interrupt to reset (aka gate) the counter at your desired time interval (usually 0.01s, 0.1s, 1s, or 10s)

P.S. if you want any accuracy at all you'll need to base your timers off of a crystal and NOT the internal RC oscillator that most chips have these days.
 
Top