EE313 Lab 3
EE313 Lab 3
EE313 Lab 3
Aim
To comprehend the timer initialization in PIC 18F8722
Introduction
Frequency is characterized as number of cycles every second. It can likewise be characterized as
reciprocal of absolute time 'T'.
Frequency counters are test instruments used to give exact estimations of the recurrence of a
sign. Frequency counters are test instruments utilized in numerous applications related with radio
frequency engineering to quantify the recurrence of signs precisely.
A prescaler is an electronic checking circuit used to decrease a high recurrence electrical sign to
a lower recurrence by whole number division. The reason for the prescaler is to permit the clock
to be timed at the rate a client wants.
In this lab students are required to find the best prescaler to create a trigger at every fifteen
seconds. Also, students are required to display both the lab partners name on the lcd display. And
finally students are required to generate the frequency in the lcd that they have set in the
frequency generator.
Results
Task 1 (15 sec delay)
Prescaler=256
8bit Timer0 mode
#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
int16 i=0;
int lowt=0;
#INT_TIMER0
void interrupt_tim0(){ // program automatically jump on ISR when overflow occurred
i++;
if(i==1145){ // check for about 15sec
if(lowt==1)lowt=0;
else lowt=1;
i=0;
}
if(lowt==0)output_high(PIN_B4); // LED OFF
else output_low(PIN_B4); // LED ON
}
main() {
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_BIT);//Timer 0 setup
enable_interrupts(INT_RTCC);//enable timer0
enable_interrupts(GLOBAL);//enable global interrupts
output_low(PIN_B4); // LED ON
while(1); // Wait for the interrupt
}
Prescaler =128
8bit Timer0 mode
#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
int16 i=0;
int lowt=0;
#INT_TIMER0
void interrupt_tim0(){ // program automatically jump on ISR when overflow occurred
i++;
if(i==2289){ // check for about 15sec
if(lowt==1)lowt=0;
else lowt=1;
i=0;
}
if(lowt==0)output_high(PIN_B4); // LED OFF
else output_low(PIN_B4); // LED ON
}
main() {
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128|RTCC_8_BIT);//Timer 0 setup
enable_interrupts(INT_RTCC);//enable timer0
enable_interrupts(GLOBAL);// enable global interrupts
output_low(PIN_B4); // LED ON
while(1); // Wait for the interrupt
}
Prescaler=64
8bit Timer0 mode
#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
int16 i=0;
int lowt=0;
#INT_TIMER0
void interrupt_tim0(){ // program automatically jump on ISR when overflow occurred
i++;
if(i==4578){ // check for about 15sec
if(lowt==1)lowt=0;
else lowt=1;
i=0;
}
if(lowt==0)output_high(PIN_B4); // LED OFF
else output_low(PIN_B4); // LED ON
}
main() {
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64|RTCC_8_BIT);//Timer 0 setup
enable_interrupts(INT_RTCC);//enable timer0
enable_interrupts(GLOBAL);// enable global interrupts
output_low(PIN_B4); // LED ON
while(1); // Wait for the interrupt
}
16bit Timer0 mode
Prescaler=256
#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
int i=0;
int lowt=0;
#INT_TIMER0
void interrupt_tim0(){ // program automatically jump on ISR when overflow occurred
i++;
if(i==5){ // check for about 15sec
if(lowt==1)lowt=0;
else lowt=1;
i=0;
}
if(lowt==0)output_high(PIN_B4); // LED OFF
else output_low(PIN_B4); // LED ON
}
main() {
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);//Timer 0 setup
enable_interrupts(INT_RTCC);//enable timer0
enable_interrupts(GLOBAL); enable global interrupts
output_low(PIN_B4); // LED ON
while(1); // Wait for the interrupt
}
#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
int i=0;
int lowt=0;
#INT_TIMER0
void interrupt_tim0(){ // program automatically jump on ISR when overflow occurred
i++;
if(i==9){ // check for about 15sec
if(lowt==1)lowt=0;
else lowt=1;
i=0;
}
if(lowt==0)output_high(PIN_B4); // LED OFF
else output_low(PIN_B4); // LED ON
}
main() {
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);//Timer 0 setup
enable_interrupts(INT_RTCC);//enable timer0
enable_interrupts(GLOBAL); enable global interrupts
output_low(PIN_B4); // LED ON
while(1); // Wait for the interrupt
}
#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
int i=0;
int lowt=0;
#INT_TIMER0
void interrupt_tim0(){ // program automatically jump on ISR when overflow occurred
i++;
if(i==18){ // check for about 15sec
if(lowt==1)lowt=0;
else lowt=1;
i=0;
}
if(lowt==0)output_high(PIN_B4); // LED OFF
else output_low(PIN_B4); // LED ON
}
main() {
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64);//Timer 0 setup
enable_interrupts(INT_RTCC);//enable timer0
enable_interrupts(GLOBAL);// enable global interrupts
output_low(PIN_B4); // LED ON
while(1); // Wait for the interrupt
}
Task 2
Name Display Code
#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#include "lcd.c" // inbuilt LCD library
main() {
lcd_init(); // Initialise the LCD
lcd_putc('\f'); // clear the screen
lcd_putc("EE313 LAB 3\n AVISHAY NARAYAN");
}//end program
#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#include "lcd.c" // inbuilt LCD library
main() {
lcd_init(); // Initialise the LCD
lcd_putc('\f'); // clear the screen
lcd_putc("EE313 LAB 3\n ALVISH NAIDU");
}//end program
Fig [2]: Name of second lab partner
Frequency Counter Code
#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#include "lcd.c" // inbuilt LCD library
unsigned int16 value;
main() {
lcd_init(); // Initialise the LCD
lcd_putc('\f'); // clear the screen
lcd_putc("FREQUENCY:");
while(1)
{
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);//take external clock input T1CKI
delay_ms(1000); // wait for some time to settle timer
setup_timer_1(T1_DISABLED); // disable timer 1 to read present count
lcd_gotoxy(1,16);
printf(lcd_putc,"%LU",value); //LCD display of Frequency count
}
}// end of program
Conclusion
After completing this lab, students have learnt how to use the different prescalers to generate
square waves (triggers) as the output and have also learnt the different codes in PCW compiler
for PIC C to generate square waves (triggers) as the output and also to show the frequency that is
generated by a signal generator.
Reference
Fig [1]: Name of first lab partner
Fig [2]: Name of second lab partner
Fig [3]: Signal Gen 15KHz Square Wave
Fig [4]: LCD Frequency Count Display
Fig [5]: Signal Gen 50KHz Square Wave
Fig [6]: LCD Frequency Count Display
Fig [7]: Signal Gen 30KHz Square Wave
Fig [8]: LCD Frequency Count Display
[1]"Prescaler - an overview | ScienceDirect Topics", Sciencedirect.com, 2020. [Online]. Available:
https://www.sciencedirect.com/topics/engineering/prescaler. [Accessed: 22- Mar- 2020].
[2]"What is a Frequency Counter & Counter Timer » Electronics Notes", Electronics-notes.com, 2020.
[Online]. Available: https://www.electronics-notes.com/articles/test-methods/frequency-counter-
timer/what-is-a-frequency-counter-timer.php. [Accessed: 22- Mar- 2020].