Write a PIC18 program in C language to measure the frequency of a square wave an
ID: 3567418 • Letter: W
Question
Write a PIC18 program in C language to measure the frequency of a square wave and display it on the LCD. See the picture for more details. Thank you.
LAB 9 TIMER/COUNTER PROGRAMMING IN C OBJECTIVES To program the PIC timer and counter To measure the frequency of a square wave. REFERENCE Mazidi, McKinlay, and Causey "PIC Microcontroller and Embedded Systems," Chapter 9. MATERIALS PICDEM 2 PLUS board MPLAB Oscilloscope ACTIVITY 1 Write a program in C language to measure the frequency of a square wave and display it on the LCD display To measure the frequency, you can set up TIMERI as a timer to generate 1 second interval. Meanwhile you can set up TIMER0 as a 16-bit counter and count the number of eycles of the square wave in the 1-second interval. You then convert the count from Hex into ASCII code and display the value on LCD Please note that in the last lab you set up TIMERO as an 8-bit counter, and this time you set up TIMERO as a 16-bit counter. The highest frequency you can measure this time is 2 -1 65,535 Hz. Make sure that your program measures the frequency continuously and correctly, ic., you need to consider counter overflow. When TIMERO overflows, display "Invalid Frequency" on LCD. This program is similar to your last assignment, but this time you write in C instead of assembly. You may use many C built-in functions for data conversions. l toa nat-itoaExplanation / Answer
void main (void)
{
int lowbyte;
int highbyte;
OSCCON = 0b01110000; // Setup 8MHz Internal Clock
TRISB = 0b00001000; // setup I/O on PortB
ADCON1 = 0x7F; // Setup all A/D inputs as digital inputs
CCP1CON = 0b00000101; // CCP = Capture Mode Every Rising Edge
T3CON = 0b10001001; // Timer3 config
LCD_Initialise();
while(1)
{
lowbyte = CCPR1H;
highbyte = CCPR1L;
LCD_Write_Data(highbyte); // Write high byte
LCD_Write_Data(lowbyte); // Write Low byte
Delay10KTCYx(200); // Delay
LCD_Write_Com(0x01); // clears LCD
}
}
In this code I am basically trying to input a frequency and write the values of the CCPR1H & CCPR1L straight to the LCD display so that I can see them changing, i.e. ensure they are working, but all im getting out is a 16-bit zero (00000000).
Ive read most of the datasheets, but find them to be a bit vaugue and confusing, any help or guidance would be much appreciated.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.