nikouz
Newbie level 6
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 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.