frequency counter using silicon labs c8051f020 microcontroller embedded systems egre631 smitha...
TRANSCRIPT
FREQUENCY COUNTER USING
Silicon Labs C8051F020 microcontroller
Embedded Systems EGRE631
Smitha Gautham
Dept. of Electrical and Computer Engineering
Virginia Commonwealth University
Outline
• Application
• Theory
• Implementation
• Results and Discussions
• Summary and Future Work
Examples of Application
• Calibrate other equipment
• Guitar tuner (attach to a crystal)
• Measure rpm of wheel
Theory: Keeping track of time
• System clock is 22.45 MHz.
• Timer count = 22,450 → 1 ms
• Start counter
• Counter counts external clock pulses
• Every 1 ms → interrupt
• Count 1000 in ISR → 1 second delay
Theory: counting frequency
• Every second: stop counter
• Store value in counter register
• Counter registers:16 bits → count 65,535
• Higher frequencies: track counter overflows
Theory: LCD display
• Display the frequency on LCD
• Integer → string
• Pass string to LCD routine
Implementation: Overview of Microcontroller
Reference: Embedded programming , Chew Moi Tin and Gourab Sen Gupta
T4 is P0.4
Data port
Command port
Implementation: Hardware Schematic
Implementation: Actual Hardware Set-up
Complete Set-up MC and LCD
Implementation: Initializing cross-bars
• Timer 2 to count internal clock pulses
• Counter 4 to count external frequency
• Configuring the Crossbar registers
void init_crossbar (void)
{
XBR0 = 0x04; // UART 0 TX to P0.0, RX to P0.1
XBR1 = 0x40; // Sysclk out
XBR2 = 0x58; // Enable cross bar rout T4 to port pin
}
Implementation: Crossbars
XBR1=0x40
XBR0=0x04
XBR2=0x58
Implementation: Initializing Ports
• Configuring ports void init_ports(void)
{
P0MDOUT = 0x00; //configure P0 as input port
P0=0x04;
P1MDOUT = 0xFF; // P1 is push pull
P2MDOUT = 0xFF;// P2 as push pull
P3MDOUT = 0x00;
P5 = 0x00;
}
Implementation: Initializing Timer and Counter
CKCON=0x20
T2C0N=0X00
T4CON=0X03
Implementation: Initializing Timer and Counter
Implementation: Initialize timer
void init_timer(int cnt) { T2CON =0x00; //clear Timer 2 T4CON=0x03; //clear Timer4 config Timer 4 as counter CKCON= 0X20; //Timer 2 uses sys clk
TMR2RL=-cnt; //load count to get 1 ms delay TMR2=TMR2RL; TMR4RL=0x00; // clear Counter4 TMR4=TMR4RL; ET2=1; //Enable Timer2 interrupt TR2=1; // Run Timer2 T4CON= 0x0F;//Run Counter4 }
Implementation: ISR
void Timer2_ISR (void) interrupt 5 { unsigned int scnt;
TF2=0; //clear timer2 interrupt flag scnt++;
if (scnt==100) // ISR every 1ms, 1ms *100 gives .1 s { flag=1;
z=z+TMR4; //TMR4 value is repeatedly added to z TMR4=0x00; scnt=0; zcnt=zcnt+1; // to get 10 counts of .1 s
} if (zcnt==10) //.1s* 10 gives 1 s delay
{ T4CON =0x00; init_timer(mSEC_CNT);
}}
Implementation: Main Program
int main(void)
{
unsigned int arr4[12];
unsigned int *ptra;
Init();
EA=1;
init_timer(mSEC_CNT);
while(1)
{
if(flag==1)
{
flag=0;
if(zcnt==10)
// Initialize the microcontroller
// Enable Global Interrupts
// Initialize timer
//flag is enabled after .1s
// zcnt = 10 means 1 s is complete
Implementation: Main Program cont’d
{ zcnt=0; if ( z> 550000 ) // if value in z is >550K indicate { ptra=& arr4[0]; ptra="out of range";
z=0;if(ptra!='\0') lcd_disp(* ptra); lcd_init();
}
Implementation: Main Program cont’d
else sprintf(arr4, "%ld Hz", z); // convert z to string and display z=0; ptra= & arr4[0];
lcd_init(); lcd_disp(*ptra);
} }
}
}
Implementation: LCD Display
void lcd_init()
{ cmd_write(0x38); micro100_delay(30); // gives delay of 1 ms cmd_write(0x0E); // Display on Cursor off; 0000 1DCB micro_delay(1); cmd_write(0x01); //Clear the display micro_delay(1); cmd_write(0x06); //Entry mode 0000 01 I/D S micro_delay(1);
}
Implementation: LCD Display cont’d
void cmd_write(char cmd) { RS=0; micro_delay(1); RW=0; micro100_delay(10); // gives delay of 1 ms LCD_DAT_PORT = cmd; E=1; micro_delay(1); //gives a delay of 1 micro second E=0; }
Implementation: LCD Display cont’d
void data_write(char dat)
{
EA=1;
RS=1;
micro_delay(1);
RW=0;
micro100_delay(10);
LCD_DAT_PORT = dat; //Data is written
E=1;
micro_delay(1); //enable must be high for 300 ns to capture data
E=0;
}
Results
Range of few Hz to 100s of k HZ
Discussions
• Sampling time = 0.1 s• Number of times = 10• Total =1 s
• Register TMR4→ MAX 65,535 every 0.1 s• Z=z+TMR4 each time• Max Freq= 655,350 Hz
Discussions
• Theoretically cannot measure freq > 655,350 Hz• Display “0” beyond 550,000 Hz
Summary
• Inexpensive frequency counter implemented
• Can measure frequency from 1 Hz to 550 K Hz
• TMR4 is16 bits → max count of 65,535
• repeat 10 times per sec→ max frequency of 655,350
Future Work
• Expand range of frequency counter
• Other ways of implementations
•Explore use of other Timers/Counters
Thank youQuestions?