TMR0 as a Counter

Status
Not open for further replies.

Suraj143

Active Member
Hi I want to use TMR0 as a counter.I'm feeding input through RA4 TMR0 input.

Is it ok if i use pre scaller 1:1 & what about the TMR0 value? do I need to load 255 everytime?

IF I load 255 to TMR0 when a pulse comes it will overflow TMR0 & goes to ISR.

Any ideas

Thank you.
 
From what you said you are using timer0 as if it were an interrupt pin. You could just use a pin with interrupt on change for this.

Do you want to count N events then interrupt. Or do you want to know how many events happen in a given period of time? What are you trying to do.
 
3v0 said:
Do you want to count N events then interrupt. Or do you want to know how many events happen in a given period of time? What are you trying to do.

This is exactly what I'm going to do.

The given period time I can use TMR2.After this timer overflows I can count how many times the event happens.

The problem is to calculate the events happening I'm going to use TMR0.I cannot use RB0 interrupts its driving displays so I'm using TMR0 input.
 

To find out how many events happen in a given time period.

Use two timers. Use one to count events. Use the other to know when to stop counting.

If you pick TIMER0 to count events and TIMER2 to interupt when you want to stop counting:

Code:
This assumes that you will never count to more them 0xFFFF

Setup timer0 as 16 bit timer.
Set the prescaler for the TIMER0 as 1:1.  
Load TMR0L and TMR0H registers with zero.
Remaining TIMER0 setup.

Setup TIMER2 to timeout after the time for counting is over.

When TIMER2 interupts (or the IF bit is set) turn off TIMER0.
The count you want is now int TMR0H TMR0L registers.

Develop the code in steps. First get timer0 to count events on the input pin. Then figure out how to setup timer2 to time out in the period of time you need.
Then combine the two.
 
Hi 3v0 thanks for your great answer.Thats how I planned to do in my method.

Just a quick question I'm using PIC16F series the TMR0 is an 8bit one not a 16 bit one.It cannot setup as 16bit timer.

Thanks
 
Suraj143 said:
Hi 3v0 thanks for your great answer.Thats how I planned to do in my method.

Just a quick question I'm using PIC16F series the TMR0 is an 8bit one not a 16 bit one.It cannot setup as 16bit timer.

Thanks

Can you use TMR1 which is 16 bit. If not declare a var COUNTS in you code and inc it each time TMR0 overflow/interrupts. You count will be COUNT*0xFF + TMR0.
 
Suraj143 said:
What about adding 255 to TMR0 everytime?

So when a pulse comes it will generate the T0IF flag.

You said you wanted to count pulses.
Why do you need to know when each pulse happens??
 
I don't see why do you need it to be that way. Just enable INT. It is selectable to be rising edge or falling edge. If any edge detected, then interrupt generated.
 
Hi to both of you I missed one thing.

When TMR0 used as a counter when ever it detects a pulse it will update the TMR0.If the PIC is doing some other work for example when its doing multiplex no need to check everytime the T0IF bit it will automatically update the input pulses while doing multiplex routine.

Tell me am i right?

Now the only problem is TMR0 doesn't support 1:1 pre scaller it has 1:2 prescaller.So the result I have to double every time so the final result is all the time is even numbers.

Thanks
 
Text to keep the posting gods happy!
 
Hi 3V0

Now I totally understood.TMR0 is independent & separate form the CPU.

Note that I'm using T0IF only to detect whether the signal input is exceed than our max count.

I'll use PSA = 1 so when any time a pulse is available it will automatically update the TMR0 register.So need to double the result every time.In this case its working as 1:1 PS.

Thank you very much 3V0.
 
Text to keep posting gods happy.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…