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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/24/00 12:38
Read: times


 
#2857 - RE: Frequency measurement with C515
Dennis,
I have used the Keil C51 compiler and I am familiar with interrupt latency from the assembly generated from this compiler.
With Keil you do not have to worry about interrupt latency times until you start reaching frequencies around 15kHZ +/- something.
The problem may be in the way you are determining your timing. It is only a guess without seeing part of your code but this problem is really pretty straight forward. Here is an example of one way to do it.

In this example you have an unkown frequency connected to P1.0 which is also the timer 2 external counter input.
In your startup routine;
Configure T2 as an external counter. I leave that to you to read. Do not start the counter.
Set timer 0 to mode one. Do not start timer.
//At 12mHz T0 increments every 1us
//We will set T0 to go off every
//10ms for simpicity. Here are the
//defines for the value 10,000
#define T0_LOW 0xF0
#define T0_HIGH 0xD8

//declare a bit for each of your flags
bit bRead_Frequency = 0x20;
bit breading_in_progress = 0x21;

//declare 1 second timer
data unsigned char yone_second_timer;
//Declare frequency variable
data unsigned int ufrequency;

void main(void)
{
//init T0 do not start
//init counter two, do not start

//Set flags properly
bRead_Frequency = 0;
breading_in_progress = 0;

while(1)//do forever
{
if(!breading_in_progress )
{
get_frequency();
}
if( bRead_Frequency )
{
ufrequency = read_frequency();
}
}//end while
}//end main

/*****************************
Get Frequency
******************************/
void get_frequency(void)
{
//100*10ms = 1sec
yone_second_timer = 100;

//Clear this flag to be sure
bRead_Frequency = 0;

//Set flag
breading_in_progress = 1;

//Start T0
TR0 = 1;

//Start counter 2
TR2 = 1;

}//End get_frequency
/****************************/


/****************************
Read Frequency
****************************/
unsigned int read_frequency(void)
{
//Clear flags
breading_in_progress = 0;
bRead_Frequency = 0;

//Return 16 bit count in T2 counter
return ((TH2 << 8) + TL2);
}//End read_frequency
/****************************/

/****************************
Timer one is my time base
and this is the interrupt service
routine.
****************************/

timer_0_isr() interrupt 1 using 2
{
TL0 = T0_LOW; //Reload timer 0
TH0 = T0_HIGH;

if(!(--yone_second_timer))
{
//Stop external count
//Frequency is now in TH2, TL2
//We will retrieve it in another
//routine
TR2 = 0;

// In this example you do not need
// T0 to keep running so stop it.
TR0 = 0;

//Set flag to tell main program to
//get count from T2
bRead_Frequency = 1;
}

}//End timer_0_isr

Examine the code to be sure it is bug free. This should give you an idea of a good way to read a frequency. The starting and stopping of the timers introduces some error as well as does the interrupt latency. In this example T2 should stop in the timer 0 isr within 60us worse case, which means you should not miss more than one count if your are below about 16kHz. You can get the ISR to run faster but your application does not require.

Again, double check for bugs, I wrote this off the top of my head and this should be used only as an example.
Good Luck!
Allen

List of 9 messages in thread
TopicAuthorDate
Frequency measurement with C515            01/01/70 00:00      
RE: Frequency measurement with C515            01/01/70 00:00      
RE: Frequency measurement with C515            01/01/70 00:00      
RE: Frequency measurement with C515            01/01/70 00:00      
RE: Frequency measurement with C515            01/01/70 00:00      
RE: Frequency measurement with C515            01/01/70 00:00      
RE: Frequency measurement with C515            01/01/70 00:00      
RE: Frequency measurement with C515            01/01/70 00:00      
RE: Frequency measurement with C515            01/01/70 00:00      

Back to Subject List