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.

Reading TMR1 as a 16bit variable

Status
Not open for further replies.

Suraj143

Active Member
Guys can somebody tell me how to read TMR1 as a single 16bit variable?

I use 628A on 4Mhz.I just making a frequency counter.I have a 1sec time period.I'm counting how many pulses are collected to TMR1 register pair & show on display.

I use mikroC

Code:
void interrupt(){
                 if(PIR1.TMR1IF==1){
                 PIR1.TMR1IF = 0;
                 pulse_Hi++;
                 }
               if(PIR1.TMR2IF ==1){           //Count 1Sec
                 PIR1.TMR2IF =0;
               ISR_Count++;
                 if(ISR_Count==100){
                     ISR_Count = 0;
                      pulse_Hi = 0;
                      Freq_Hi = TMR1H;
                      Freq_Low = TMR1L;
                      TMR1H = 0;
                      TMR1L = 0;
                      Freq_Hi =Freq_Hi * 256 ;
                      Frequency = Freq_Hi | Freq_Low;
                      Dig1 = Frequency/10000;
                }
           }
}
 
Last edited:
Try not to use math in interrupts

Make sure you use an "int" for your frequency..

Then put these lines in...
Code:
frequency = 0;     // this clears the variable
frequency = ((int)TMR1H << 8) + TMR1L;    // cast TMR1H to an int (so its big enough)  "<< 8 " shift it to the top byte, then add TMR1L.

Shifting is much faster than *...
 
Last edited:
Looks like you have it sorted.
Timer 1 has 2 registers tmr1h and tmr1l, these contain the high byte and low byte of the count word, the only thing you need to be carefull of is tmr1l overflowing after its been read, you'll then have an invalid read, 2 ways round this 1 is read twice and make sure both values are the same, or 2 stop the counter before the read.
Timer 1 can be used as a 19 bit counter if you use the prescaler, but you cant read the lower 3 bits.
 
Guys thanks it worked nicely.

Now I need to expand my counting frequency range upto 10Mhz.I need to make my frequency variable as a "long Int".

How do I combine "Freq_Hi" variable with 16bit "Frequency" variable?

In other words my 10Mhz final frequency must be in the "Frequency" variable.

Code:
void interrupt(){
               if(PIR1.TMR1IF ==1){     //65536 pulses overflowed
                    PIR1.TMR1IF ==0;
                      Freq_Hi++;
                    }
               else{                   //Count 1Sec period
                 PIR1.TMR2IF =0;
               ISR_Count++;
                 if(ISR_Count==100){
                  Display_Register= Display_Register |  0b00000001 ; //set update flag
                     ISR_Count = 0;
                      Frequency = 0;     // clear the last frequency
                      Frequency = ((int)TMR1H << 8) + TMR1L;    //pack to 16 bit
                      TMR1H = 0;
                      TMR1L = 0;
                }
           }
}
 
Last edited:
I dont know if I got what you said right, however you have a 16 bit counter and you want to count to 10 million, I'd use the prescale with timer 1 to reduce the count but you dont have to.

16 bit Timer1 will overflow at just over 152 times per second at 10mc's, so you need to make sure your interrupt routine is quicker than 152hz, if you read the counter every interrupt add the contents of the counter to a 32 bit long word using a 32 bit addition sub, then clear the counter, at the end of each second you'll have a 32 bit value of frequency, you can then transfer this to the displayed value and clear it.

Cant help with the code I'm an asm man.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top