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.

frequency measurement code gives different values

Status
Not open for further replies.

anjalimidhuna

Member level 3
Member level 3
Joined
Jan 4, 2014
Messages
62
Helped
2
Reputation
4
Reaction score
2
Trophy points
8
Location
india
Visit site
Activity points
358
Code:
void interrupt isr(void)
{
 if(TMR1IF==1)
 {
	Count++;
	tmr++;
	if(tmr==1000)
	{
		tmr=0;
		lcd++;
	}
	TMR1IF=0;
	TMR1H=0XFC;
	TMR1L=0X17;
 }
 if(CCP1IF==1)
 {	
	if(flag==0)
	{
		flag=1;
		Count=0;
	}
	else
	{
		if(Count>0)
		{
			freq=Count;	
			flag=0;
		}
	}
	CCP1IF=0;
 }
}
void main()
{
    T1CON=0b00000001;			// INITIALISE TIMER1 CONTROL REGISTER
    TMR1H=0XFC;					// LOAD VALUE TO HIGHER BYTE REGISTER
    TMR1L=0X17;					// LOAD VALUE TO LOWER BYTE REGISTRE
    INTCON=0b11000000;			// INITIALISE INTERRUPT CONTROL REGISTER
    PIE1=0b00000101;			// INITIALISE TIMER INTERRUPT CONTROL REGISTER
	 Lcd_Init();
	 adc_init();
 while(1)
 {
	if(lcd>=1)
	{
		LCD(Lcd_clear,ModCmd);
		Delay_ms(100);
		LCD(FstLin_FstColumn,ModCmd);
		freq=Count;
		freq=freq*2;
		if(freq>0)
		value=1000000/freq;
		Disp_STRING("FREQUENCY=");
		Disp_STRING(int_to_string(value,buf));  
		Disp_STRING("Hz");
		lcd=0;
	}
}
}

even for the same frequency it gives value like
499
333
249
.
.
.
.
.18
i am using PIC16F877A with hi tech c
 

I would guess a good place to start looking is the "Delay_ms(100);" line. I don't use Hitech C but if that is a delay created by a software loop it might disable interrupts until it has finished and hence cause the timer interrupts to be missed.

Brian.
 

Thax for your reply.
i just removed Lcd clear cmd and the delay but the problem is still there:sad:
 

Very strange program..!

you measure frequency by Timer1 counting or by CCP1 capture mode ?
you can not use both .
Entry pin is not the same ...

What about your hardware connections ?
How do you initialize variables (count, flag,tmr ) ?
what must be the exact Frequency input ?

Timer1 counting .. use software to pick up values of Timer1, no exact synchro with signal input..
Capture use internal hardware to pick up Timer1 value , exact synchro with edge of signal

In capture mode you only need CCP1 interrupt ...
the pupose of this mode is to catch the value of timer1
without using software..

but you need to set capture with front edge
and init timer1 inside the 1rst interrupt..

at the second interrupt
read timer1 value
stop interrupt

so you get the periode T between 2 front edges
then calculate F= 1/ T

or you can use Front edge and falling edge detection to measure
the duty cycle T1 ON , then T2 OFF => T =T1+T2 duty =T1 / T F=1/T
 

HI PAUL
actually i tried to find the frequency of the signal fed to pin rc2 when there is a transition interrupt occurred .the time period is calculated from the a count that is incremented with timer 1. Is this possible??? i have a trainer kit so not much idea about the h/w. i initialize the variables as global.
unsigned int Count=0,freq=0,value=0,lcd=0,tmr=0;
unsigned char buf[10];
bit flag;
frequecy fed to the circuit is ac mains with 50Hz.
 
Last edited:

hello,

frequecy fed to the circuit is ac mains with 50Hz.

do you mean, RC2 input is connected to an AC 50Hz sinusoidal voltage ?
be carreful ,risk of burning out for this input..
and dangerous for your life if no insulation !

you better have to build a litle TTL oscillator with a NE555 or
with a CD4060+Quartz (**broken link removed**)
for testing...


with CCP1 mode, timer1 counts the elapsed cycles between edges on RC2 input..
i did some comparative tests with a 16F877 mikroC
range 4.882629 Hz up to 3125 Hz
**broken link removed**
and
18F26K22_Freq_CCP_Timer1_Timer3_130925.c
you can have a look on it to get inspiration..

read in detail, the PIC16F datasheet and CCPx part to understand how it's working.
after choose the best strategie to get the best accuracy to your own range 1hZ ..100Hz

What is your FOsc ..(Quartz value ?)
 

See attached file. Frequency input is 100 Hz = 100 pulses per second. For 60 sec it is 100 * 60 = 6000. RPM = 6000. Modify the code for your PIC. See if it gives stable value.
 

Attachments

  • Tachometer.rar
    112.1 KB · Views: 76
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top