Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] frequency reading with timer0 and timer1

Status
Not open for further replies.

nikouz

Newbie level 6
Newbie level 6
Joined
Feb 5, 2021
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
119
Hi all

i am having an assignment to count frequency with TMR1 on a PIC 16F88. I am using MPLAB and XC8. I am posting the code which is only an interrupt. TMR0 is producing a square wave 50% duty cycle which is from stimulus seton RA4 pin. My Fosc is 4MHz and no prescale used.



#ifndef _PIC12
extern unsigned int freq;
void __interrupt() isr(void)
{
/* This code stub shows general interrupt handling. Note that these
conditional statements are not handled within 3 seperate if blocks.
Do not use a seperate if block for each interrupt flag to avoid run
time errors. */
if(TMR1IF==1) {
TMR1ON = 0;
TMR1IF = 0;
freq = TMR0;
freq = (freq<<10)-240;
TMR1 = 0xFFFF - 1000; // 1ms meassuring period
TMR0 = 0;
TMR1ON = 1;
}

First of all we had to make the code just to measure pulses in 1ms.So the line "freq = (freq<<10)-240" of code was not existing on that point. So when i run the code i put watch on freq and indication was most of time 10 and once every some pauses 11 wich expected. After i been asked to show the frequency as Hz so i add the red line without the -240 there. This method was more less consuming in space in program and data memory. This way has a the issue that in the reality i multiply by 1024 so my result is 10240 instead of 10000 and here came the -240 which as i understand is not the right way to correct this 240 difference and here is that i am asking help. How to do it.



Thank you for your time.
 

I'm really confused by what you are doing.
To measure frequency with TMR1 you need to do this:
1. configure TMR1 to count external pulses on the input pin.
2. configure TMR0 as a timebase, the 'window' during which pulses are counted.
3. configure interrupts so TMR0 (and if necessary, the prescaler) generate regular timed interrupts.
4. In the ISR, read TMR1, this is your frequency, reset it to zero and if necessary reload TMR0 to get the right period (1mS).

If you do that, you will get a direct frequency measurement with no adding or subtracting needed. It will update every time TMR0 generates an interrupt.

Brian.
 

I'm really confused by what you are doing.
To measure frequency with TMR1 you need to do this:
1. configure TMR1 to count external pulses on the input pin.
2. configure TMR0 as a timebase, the 'window' during which pulses are counted.
3. configure interrupts so TMR0 (and if necessary, the prescaler) generate regular timed interrupts.
4. In the ISR, read TMR1, this is your frequency, reset it to zero and if necessary reload TMR0 to get the right period (1mS).

If you do that, you will get a direct frequency measurement with no adding or subtracting needed. It will update every time TMR0 generates an interrupt.

Brian.
yes it is confusing because lecturer gave the assignment part by part thats why. I didn't understand very well your instruction but i will give it a go and i will contact back if something is not clear.
thank you
 

You have to understand how a frequency counter works. It counts how many input pulses are seen within a fixed time period. As an example, although you wouldn't be able to create a 1 second delay with TMR0, if TMR0 generated an interrupt once per second and you counted pulses within that second, you would have the frequency in Hz, the number of pulses per second.

It makes sense to use TMR1 to do the pulse counting because it is a 16 bit register, if you used TMR0 to count it would only give results up to 255. Additionally, if you also count roll-over interrupts fro TMR1 you can further increase the resolution. In theory you can measure frequencies from 1Hz to about 50MHz using this method.

Brian.
 

Ok brian

I think i ve got it what you mean. I will try and come back today or tomorrow.

Really thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top