Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
12/02/09 11:20
Read: times


 
#171384 - Using timer 2 for frequency measurement
Hello members,

I am trying to measure a frequency(100Hz for test purpose) using 89C52 with timer 2 in capture mode. I have given the frequency to the uC P1.1. Now as my calculations tell me, if i start counting on one negative edge, and then on the next edge, the counter value should be 2400h as the time period of the 100Hz frequency is 10 mili seconds(ie 10msec/1.058 micro sec = 2400h). and then i am sending this value out of my serial port to my PC....but i am not getting the desired value. the values range from 2340h to 24C0h...

I do not understand why such a vague value is received. Is there any way to correct this. I want to measure 1 Hz to 100 Hz



#include<reg52.h>
#include<serial.h>
#include<generic.h>

char T2_higher_byte = 0x00,T2_lower_byte = 0x00;
char set_higher = 0x24,set_lower = 0x00;
/************************************************************************
The timer is triggered if either an overflow occurs
or the T2EX(P1.1) receives an 1 to 0 transition.
I am using a frequency of 100Hz for test purpose.
*************************************************************************/
void timer2_ISR (void) interrupt 5
{
if(EXF2 == 1)
   {
   TL2 = 0;//resetting lower timer register so that it starts from begining
   TH2 = 0;//resetting higher timer register so that it starts from begining
   T2_higher_byte = RCAP2H;//storing the captured higher byte 
   T2_lower_byte = RCAP2L;//storing the captured lower byte
   }
TF2 = 0;            /* Clear the interrupt request */
EXF2 = 0;			/* Clear the edge trigger flag */
}
/************************************************************************/
void initialize_timer2()
{
ET2 = 1;                      /* Enable Timer 2 Interrupts */
CP_RL2 = 1;					  /* Making the timer edge triggered */
EXEN2 = 1;					  /* Enabling pin 1.1 */
TR2 = 1;                      /* Start Timer 2 Running */
}
/************************************************************************/
void serial_interrupt() interrupt 4
{
	if(TI)
		{
		TI = 0;
		}	
	if(RI)
		{
		RI = 0;
		}
}
/************************************************************************/
void main()
{
initialize_serial();	 /* initializing serial interrupt */
SBUF = 0XA4;			 /* Sending dummy value */
initialize_timer2();	 /* initializing timer interrupt */
while(1)
     {
	 sec_wait(1);//wait for 1 sec
     TR2 = 0;	 //Disabling timer 2
     SBUF = 0XA1;//sending dummy value
     msec_wait(1);//wait to give time for serial sending
     SBUF = T2_higher_byte;//sending the higher byte
     msec_wait(1);//wait to give time for serial sending
     SBUF = T2_lower_byte;//sending the lower byte 
     T2_higher_byte = 0;//resetting both higher variables
     T2_lower_byte = 0;//resetting both lower variables 
     TR2 = 1;//enabling timer 2 again
	 }
}    
/************************************************************************/

 



List of 13 messages in thread
TopicAuthorDate
Using timer 2 for frequency measurement            01/01/70 00:00      
   Latencies            01/01/70 00:00      
      Big differences            01/01/70 00:00      
         I'm using a PLC and a Oscilloscope !!            01/01/70 00:00      
            2% jitter...            01/01/70 00:00      
            Try a free-running timer and average 10 periods            01/01/70 00:00      
               Dear Per            01/01/70 00:00      
                  You may use timer2 if you want            01/01/70 00:00      
      Latency...            01/01/70 00:00      
   Frequency Measure            01/01/70 00:00      
   now using timer 0 and ext intr 0            01/01/70 00:00      
      giving up without proof that ...            01/01/70 00:00      
      Your calculations are OK            01/01/70 00:00      

Back to Subject List