Sei sulla pagina 1di 7

School of Engineering and Physics

EE313 Microprocessor Applications

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
}

Frequency counter code


#include <18F8722.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
#include "lcd1.c" //include file for lcd

unsigned int16 value; //specifying data type range


void lcd_scroll_left()
{lcd_send_byte(0, 0x18); } //scrolling loop

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

Maximum and minimum readings measured by counter

Input signal Counter measurement

Page | 2
Max
value

Min
value

Circuit Diagram

LCD display Signal generator connected


connected to Port F to Port C, pin 0 and ground

Figure 1: PIC18F8722 board details and interfacing connections

Table 1: Connection pins between PIC and LCD display


Control lines of LCD on
PIC Port F pins
Millennium board

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.

Precaution / Hardware issues


 Always stop a program before running a new one.
 Check for loose wiring as it may interfere with experiment results
 Always disconnect the power to the Prototyping board before
connecting/disconnecting the ICD or changing the jumper wires to the Prototyping
board.
 There was some hardware issues regarding the ICD was not working resulting in no
communication with microcontroller and the LCD module could not power up. These
faulty components were then replaced in order to successfully complete the given
tasks.

Appendix

Page | 5
Page | 6
Page | 7

Potrebbero piacerti anche