Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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.
 
Suraj143 said:
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.
 
Suraj143 said:
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.
 
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
 
Suraj143 said:
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.
Yes​
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?

Yes if I understand you correctly. The counter updates automaticaly on every input pulse. This is done without any action from the PIC CPU. The CPU is free to run other code while the counter is counting.

Think of the timer module as a device that is seperate from the CPU. The timer works independantly of the CPU. The CPU can set and read the timer registers, the timer can set the IF flag and/or generate an interrupt. That is the connection between the CPU and the timer.

You can setup the timers and forget about them till you see the IF flag change or service a timer generated interrupt.​


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.

Not a problem. If you set the Prescaler Assignment bit (PSA bit) to 1 Timer0 will not use the prescaler. Same results as if the prescaler had a 1:1 setting.​

Code:
bit 3 PSA: Prescaler Assignment bit
1 = Prescaler is assigned to the WDT
0 = Prescaler is assigned to the Timer0 module

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.
 
Suraj143 said:
Hi 3V0

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

YES, that makes it very useful.​

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

When the timer overflows (goes from 0xFF to 0x00) it will set the IF flag and interrupt if enabled to do so. So load the timer with 0xFF - maxCount. Then it wil set the IF in maxCount events.​

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.

Correct and glad to help.​
Thank you very much 3V0.
Text to keep posting gods happy.
 
Status
Not open for further replies.

Latest threads

Back
Top