Download presentation
Published byRafe Barnett Modified over 9 years ago
1
FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller
Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia Commonwealth University
2
Outline Application Theory Implementation Results and Discussions
Summary and Future Work
3
Examples of Application
Calibrate other equipment Guitar tuner (attach to a crystal) Measure rpm of wheel
4
Theory: Keeping track of time
System clock is 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
5
Theory: counting frequency
Every second: stop counter Store value in counter register Counter registers:16 bits → count 65,535 Higher frequencies: track counter overflows
6
Theory: LCD display Display the frequency on LCD Integer → string Pass string to LCD routine
7
Implementation: Overview of Microcontroller
T4 is P0.4 Data port Command port Reference: Embedded programming , Chew Moi Tin and Gourab Sen Gupta
8
Implementation: Hardware Schematic
9
Implementation: Actual Hardware Set-up
Complete Set-up MC and LCD
10
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 }
11
Implementation: Crossbars
XBR0=0x04 XBR1=0x40 XBR2=0x58
12
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; }
13
Implementation: Initializing Timer and Counter
CKCON=0x20 T2C0N=0X00
14
Implementation: Initializing Timer and Counter
T4CON=0X03
15
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 }
16
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);
17
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
18
Implementation: Main Program cont’d
{ zcnt=0; if ( z> ) // if value in z is >550K indicate ptra=& arr4[0]; ptra="out of range"; z=0; if(ptra!='\0') lcd_disp(* ptra); lcd_init(); }
19
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); }
20
Implementation: LCD Display
void lcd_init() { cmd_write(0x38); micro100_delay(30); // gives delay of 1 ms cmd_write(0x0E); // Display on Cursor off; DCB micro_delay(1); cmd_write(0x01); //Clear the display cmd_write(0x06); //Entry mode I/D S }
21
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; }
22
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; }
23
Range of few Hz to 100s of k HZ
Results Range of few Hz to 100s of k HZ
24
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
25
Discussions Theoretically cannot measure freq > 655,350 Hz
Display “0” beyond 550,000 Hz
26
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
27
Future Work Expand range of frequency counter
Other ways of implementations Explore use of other Timers/Counters
28
Thank you Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.