frequency counter using silicon labs c8051f020 microcontroller embedded systems egre631 smitha...

28
FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia Commonwealth University

Upload: rafe-barnett

Post on 03-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

FREQUENCY COUNTER USING

Silicon Labs C8051F020 microcontroller

Embedded Systems EGRE631

Smitha Gautham

Dept. of Electrical and Computer Engineering

Virginia Commonwealth University

Page 2: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Outline

• Application

• Theory

• Implementation

• Results and Discussions

• Summary and Future Work

Page 3: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Examples of Application

• Calibrate other equipment

• Guitar tuner (attach to a crystal)

• Measure rpm of wheel

Page 4: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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

Page 5: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Theory: counting frequency

• Every second: stop counter

• Store value in counter register

• Counter registers:16 bits → count 65,535

• Higher frequencies: track counter overflows

Page 6: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Theory: LCD display

• Display the frequency on LCD

• Integer → string

• Pass string to LCD routine

Page 7: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Implementation: Overview of Microcontroller

Reference: Embedded programming , Chew Moi Tin and Gourab Sen Gupta

T4 is P0.4

Data port

Command port

Page 8: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Implementation: Hardware Schematic

Page 9: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Implementation: Actual Hardware Set-up

Complete Set-up MC and LCD

Page 10: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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

}

Page 11: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Implementation: Crossbars

XBR1=0x40

XBR0=0x04

XBR2=0x58

Page 12: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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;

}

Page 13: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Implementation: Initializing Timer and Counter

CKCON=0x20

T2C0N=0X00

Page 14: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

T4CON=0X03

Implementation: Initializing Timer and Counter

Page 15: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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 }

Page 16: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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);

}}

Page 17: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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

Page 18: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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();

}

Page 19: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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);

} }

}

}

Page 20: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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);

}

Page 21: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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; }

Page 22: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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;

}

Page 23: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Results

Range of few Hz to 100s of k HZ

Page 24: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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

Page 25: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Discussions

• Theoretically cannot measure freq > 655,350 Hz• Display “0” beyond 550,000 Hz

Page 26: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

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

Page 27: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Future Work

• Expand range of frequency counter

• Other ways of implementations

•Explore use of other Timers/Counters

Page 28: FREQUENCY COUNTER USING Silicon Labs C8051F020 microcontroller Embedded Systems EGRE631 Smitha Gautham Dept. of Electrical and Computer Engineering Virginia

Thank youQuestions?