Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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.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.
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.
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.
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.
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
Suraj143 said:What about adding 255 to TMR0 everytime?
So when a pulse comes it will generate the T0IF flag.
Text to keep the posting gods happy!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.
YesIf 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 posting gods happy.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.