Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Experiment: 3
Timer Counter Applications with PIC micro-controller
Date: 11/03/2019
Objective
To write a C program in PIC C to generate a 15 second trigger at the output pin using
Timer 0 and verify the real time delay with an actual stop watch.
Design a frequency counter using Timer 1 features of the PIC microcontroller and
display the measured frequency on the LCD screen.
Introduction
A PIC microcontroller has special features that can be used to generate a delay using its internal
or external clock which uses its set frequency to trigger a delay. The delay can be set to
whatever the user’s requirements by setting the appropriate pre-scalar and bit value. “A pre-
scalar is an electronic counting circuit” (En.m.wikipedia.org, 2019) which uses integer division
to reduce high frequencies to low frequencies. By adjusting the pre-scalar value, the resolution
is determined at which the timer overflows but it is important to remember that the resolution
is inversely proportional to the range of the timer. By selecting a balance between the
resolution and the maximum range, a precise timer can be designed. Furthermore, a PIC
microcontroller can also be used as a counter where the minimum count value is always 1 and
the maximum count value will depend on the number of bits used.
Program Code
15 sec trigger code [16 bit]
#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
int i=0;
int lowt=0; //initializing variables
#INT_TIMER0
void interrupt_tim0() //interrupt function
{
i++; //increment i
if(i==9) //overflow
{
if(lowt==1)lowt=0; //saving of led status to variable lowt
else lowt=1;
i=0; //resetting overflow
}
if(lowt==0)output_high(PIN_B4); //if pin is low, change high
Page | 1
else output_low(PIN_B4); //otherwise(pin is high) change to low
}
main()
{ //intializing timer_0
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128); //intializing to use internal CLK,
Prescalar=128
enable_interrupts(INT_RTCC); //enabling timer internal
enable_interrupts(GLOBAL); //enabling global interrupt
output_low(PIN_B4); //setting first delay with pin to low
while(1); //loop, waiting for interrupt
}
main()
{
value=0; //initialize variable
lcd_init(); // Initialise the LCD
lcd_putc('\f'); // clear the screen
lcd_putc("Count Value = "); //display characters inside quotation
while(1) //loop
{
set_timer1(0); //Set timer1 count
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);//take external clock input
delay_ms(1000); // wait for some time to settle timer
setup_timer_1(T1_DISABLED); // disable timer 1 to read present count
value=get_timer1(); //read timer 1 count
lcd_gotoxy(11,4); //LCD XY position to display from
printf(lcd_putc,"%LU ",value); //display frequency count
}
} // end of program
Page | 2
Max
value
Min
value
Circuit Diagram
Page | 3
RS Port F Pin-1
RW Port F Pin-2
E Port F Pin-0
Data pins Port F Pin- 4 to 7
4 to 7
Setup
Figure 2: Experimental setup- PIC C interfaced with LCD screen and signal generator
Conclusion
For task 1, an 8 bit timer was implemented first but after measuring the delay with the
stopwatch, it was noticed that the trigger delay was not even approximating 15 seconds. The
reason for this error was that 8 bit timers can count up to a maximum of 256 overflows,
whereas the overflow for 15 seconds was 1145. Next, 16 bit timer was used as this could count
to a maximum value of 65536. After using 16 bit with 256 pre-scalar, we measured with the
stopwatch an average of 16.76 seconds delay given by the timer. In order to make the timer
more precise, we calculated for different pre-scalar values till we obtained an overflow value
closest to a whole number. This was because the microcontroller is input the rounded overflow
value, therefore the number closest to a whole number would minimize errors. After
implementing and running the timer with a pre-scalar value of 128, we successfully reduced
the error and got an average stopwatch reading of 15.11 seconds. Hence, it is important to
note that the overflow values does not exceed 2 to the power of bit value and a suitable pre-
scalar should be selected to get the most accurate results.
For task 2, the frequency counter could measure a maximum value of 65535 and a minimum
value of 1. Since timer 1 is a 16-bit counter its maximum possible count is 216 = 65536 and
minimum possible count is 20 = 1, therefore it could be said that the result is accurate. It could
Page | 4
be said that after the experiment the timer initialization in PIC 18F8722 was completely
understood.
Appendix
Page | 5
Page | 6
Page | 7