0% found this document useful (0 votes)
102 views13 pages

Electrical Engineering Department ACADEMIC SESSION: - Dec40053 - Embedded System Applications

This document provides instructions for a practical work on programming the timer and counter functions of a PIC microcontroller. It includes the learning outcomes, objectives, theory on timer and counter operation, required equipment, and procedures to write code to generate time delays using Timer0 as a timer and count external pulses using Timer0 as a counter. Students are asked to write C code, simulate the circuits in Proteus, and observe the output on an oscilloscope or 7-segment display.

Uploaded by

Keerthiga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views13 pages

Electrical Engineering Department ACADEMIC SESSION: - Dec40053 - Embedded System Applications

This document provides instructions for a practical work on programming the timer and counter functions of a PIC microcontroller. It includes the learning outcomes, objectives, theory on timer and counter operation, required equipment, and procedures to write code to generate time delays using Timer0 as a timer and count external pulses using Timer0 as a counter. Students are asked to write C code, simulate the circuits in Proteus, and observe the output on an oscilloscope or 7-segment display.

Uploaded by

Keerthiga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ELECTRICAL ENGINEERING DEPARTMENT

ACADEMIC SESSION: _______________


DEC40053 – EMBEDDED SYSTEM APPLICATIONS

PRACTICAL WORK 3 : PIC TIMER PROGRAMMING


PRACTICAL WORK
DATE :
LECTURER’S NAME:
GROUP NO. :
TOTAL
STUDENT ID & NAME : MARKS
(100%)

(1)

(2)

(3)

(4)

DATE SUBMIT : DATE RETURN :

Prepared By: Verified By:


(NORMAH BINTI JANTAN)
1 LEARNING OUTCOMES (LO):

1. : Construct and simulate real-time embedded system application based


on PIC16F/PIC18F microcontroller effectively

2 OBJECTIVE

1. Write C program for PIC timer and counter.


2. Draw and simulate PIC timer and counter in Proteus.
3. Build timer and counter hardware circuit.

3 THEORY

The Timer0 module incorporates the following features:

 Software selectable operation as a timer or counter in both 8-bit or 16-


bit modes
 Readable and writable registers
 Dedicated 8-bit, software programmable prescaler
 Selectable clock source (internal or external)
 Edge select for external clock
 Interrupt-on-overflow

The T0CON register (Figure 6) controls all aspects of the module’s operation,
including the prescale selection. It is both readable and writable.

Timer0 can operate as either a timer or a counter; the mode is selected with
the T0CS bit of the T0CON register. In Timer mode (T0CS = 0), the module
increments on every clock by default unless a different prescaler value is
selected. Timer0 incrementing is inhibited for two instruction cycles following a
TMR0 register write. The user can work around this by adjusting the value
written to the TMR0 register to compensate for the anticipated missing
increments.

The Counter mode is selected by setting the T0CS bit (= 1). In this mode,
Timer0 increments either on every rising or falling edge of pin RA4/T0CKI. The
incrementing edge is determined by the Timer0 Source Edge Select bit, T0SE
of the T0CON register; clearing this bit selects the rising edge. Figure 1 below
shows registers associated with Timer0.

Figure 1
Prescaler : - An 8-bit counter is available as a prescaler for the Timer0
module. The prescaler is not directly readable or writable; its value is set by
the PSA and T0PS<2:0> bits of the T0CON register which determine the
prescaler assignment and prescale ratio. Clearing the PSA bit assigns the
prescaler to the Timer0 module. When the prescaler is assigned, prescale
values from 1:2 through 1:256 in integer power-of-2 increments are
selectable.

Timer0 Interrupt :- The TMR0 interrupt is generated when the TMR0 register
overflows from FFh to 00h in 8-bit mode, or from FFFFh to 0000h in 16-bit
mode. This overflow sets the TMR0IF flag bit. The interrupt can be masked
by clearing the TMR0IE bit of the INTCON register. Before re-enabling the
interrupt, the TMR0IF bit must be cleared by software in the Interrupt Service
Routine.

Timer1/3/5 Operation :- The Timer1/3/5 module is a 16-bit incrementing


counter which is accessed through the TMRxH:TMRxL register pair. Writes to
TMRxH or TMRxL directly update the counter. When used with an internal
clock source, the module is a timer and increments on every instruction cycle.
When used with an external clock source, the module can be used as either a
timer or counter and increments on every selected edge of the external
source. Timer1/3/5 is enabled by configuring the TMRxON and TMRxGE bits
in the TxCON and TxGCON registers,
respectively
4 EQUIPMENT / TOOLS

1. PC installed software:
a. MPLABX IDE integrates with XC8 compiler.
b. PROTEUS Professional

2. PIC Trainer

5 PROCEDURE

5.1 To begin, create a folder named PW3 on your desktop. Inside the folder,
create sub folder as Figure 2 below. Create a new MPLAB project named
LAB3 and save it in the Project sub folder.

Figure 2
5.2 TASK A. Timer as a time delay.

Using Appendix 1 as reference, find the value for T0CON for creating time
delays using setting as below:
 Timer0 OFF
 Timer0 is configured as a 16-bit timer/counter
 Internal instruction cycle clock
 Increment on low-to-high transition on T0CKI pin
 Timer0 prescaler is assigned
 1:256 Prescale value

T0CON = 0b = H

Find values to be loaded into TMR0H and TMR0L for creating 500 ms delay
using steps below.

Step 1
Time for 1 cycle, = 1
1Tcy 
Fosc  4  prescaler
Step 2
Get the number of counts by Dividing the desired time delay by 1Tcy
NOC = Desired time delay =
1Tcy

Step 3
Perform 65,536 - n, where n is the decimal value we got in Step 2.

Step 4
Convert the result of Step 3 to hex, where yyxx is the initial hex value to be
loaded into the timer’s registers.

Step 5
Set TMR0H = yy and TMR0L = xx.

5.3 Write a program in Figure 3 into your Untitled window. Use the T0CON,
TMR0H and TMR0L value you obtain in step 2. Fill the red box using suitable
instructions. Save it as PW3_TIMER0.c than build the program until
successful. If any error occurs, fix it.
Figure 3: Timer0 programming

5.4 Construct circuit shown in Figure 4 in Proteus.


Figure 4:LED circuit

Select oscilloscope from instrument icon.

Set processor clock frequency to 16 MHz. Load LAB3.hex into your PIC18
and observe the output.

Measure time delay generated by Timer0 using the oscilloscope on Channel


A.

TASK B. Timer As a Counter

Using Appendix 1 as reference, find the value for T0CON as a counter using
setting as below:

 Timer0 OFF
 Timer0 is configured as a 8-bit timer/counter
 Transition on T0CKI pin
 Increment on low-to-high transition on T0CKI pin
 Timer0 prescaler is NOT assigned
 1:2 Prescale value

T0CON = 0b = H

5.5
Create a new file. Write a program in Figure 5 into your Untitled window. Fill
the red box using suitable instructions. Then build the program until
successful. If any error occurs, fix it. Save the file as PW3_COUNTER.c

Figure 5: Counter0 programming

5.6 Construct the circuit shown in Figure 6.

Figure 6: Counter circuit


Set processor clock frequency to 16 MHz. Load PW3_COUNTER.hex into
your PIC18 and observe the output.
Change the LED with 7 Segment shown in Figure 7.

R11
200
U1
2 15
RA0/AN0/C1IN- RC0/T1OSO/T13CKI U2
3 16
RA1/AN1/C2IN- RC1/T1OSI/CCP2B
4 17 7 13
RA2/AN2/C2IN+/VREF-/CVREF RC2/CCP1/P1A A QA
5 18 1 12
RA3/AN3/C1IN+/VREF+ RC3/SCK/SCL B QB
6 23 2 11
RA4/T0CKI/C1OUT RC4/SDI/SDA C QC
7 24 6 10
RA5/AN4/SS/HLVDIN/C2OUT RC5/SDO D QD
14 25 4 9
RA6/OSC2/CLKO RC6/TX/CK BI/RBO QE
13 26 5 15
RA7/OSC1/CLKI RC7/RX/DT RBI QF
3 14
LT QG
33 19
RB0/AN12/FLT0/INT0 RD0/PSP0
34 20 74LS248
RB1/AN10/INT1 RD1/PSP1
35 21
RB2/AN8/INT2 RD2/PSP2
36 22
RB3/AN9/CCP2A RD3/PSP3
37 27
RB4/KBI0/AN11 RD4/PSP4 LS1
38 28
RB5/KBI1/PGM RD5/PSP5/P1B
39 29
RB6/KBI2/PGC RD6/PSP6/P1C
40 30
RB7/KBI3/PGD RD7/PSP7/P1D
8
RE0/RD/AN5
9 SOUNDER
RE1/WR/AN6
10
RE2/CS/AN7
1
RE3/MCLR/VPP
PIC18F4520

R2
10k

R1
200

Figure 7: Counter circuit with 7- Segment

Observation:
State your observation of the circuit.
6 RESULT

7 DISCUSSION / TASK

TASK 1: Program Timer0 to generate time delay

Design an embedded system to control a running light using three LEDs


(green, yellow and red) using PIC18F4550. The LEDs are “active LOW”. Use
Timer0 to generate time delay. Construct a program to turn ON and OFF the
LEDs as below:

1. Turn on only green LED for 0.5 second (500ms)


2. Turn on only yellow LED for 1 second
3. Turn on only red LED for 2 second
4. Repeat Step 1

TASK 2: Program Timer0 as a counter

Design a conveyor system used for loading tennis ball into a box. Each box
can be loaded 10 tennis balls. A sensor (active LOW switch) will be used to
count the total number of tennis balls that fall into the box. When all the balls
fall into the box, buzzer will turn ON for 1 second.
(Use delay_ms to produce delay time of 1 second and 7 Segment to show the
process of counting)
Appendix 1
DEC 40053 –EMBEDDED SYSTEM APPLICATIONS
PRACTICAL WORK 1 / 2 / 3 / 4 / 5 / 6
NO Group No. REG. NO. NAME OF STUDENT Programme
S1
S2
S3
S4

PRACTICAL SKILL ASSESSMENT RUBRIC


CLO 3: Construct and simulate real-time embedded system application based on PIC16F/PIC18F microcontroller effectively.
(P4, PLO5)
SCORE Score
NO CRITERIA
1-2 3-4 5-6 7-8 9-10 S1 S2 S3 S4
Program code The program The program The program The program
is incomplete code is code is complete code is complete code is complete
with complete with with a little with a little and well
inconsistent some inconsistence inconsistent organized.
Write code typing inconsistence code typing and code typing and
1. program and not well code typing and well organized well organized
organized well organized after being with a minimum
after being after being assisted by assist by lecturer
assisted by assisted by lecturer
lecturer lecturer
The program The program The program The program The program
code still have code have code have three code have one code successfully
an error. three or more or more error. or two error. run
Compile and
Student error. Student Student can Student can
2. execute
cannot fixed can fixed the fixed the error fixed the error
program
the error after error after with a minimum with a minimum
being assisted being assisted assist by lecturer assist by
by lecturer. by lecturer. lecturer
Student The circuit The circuit Can construct a Can construct a
cannot fixed connection connection have hardware circuit hardware circuit.
Construct the circuit have three or three or more one or two error. or
circuit using connection by more error. error. Student or Can produce
3. hardware or itself after Student can can fixed the Can produce a neat schematic
software being assisted fixed the error error with a schematic in in Proteus
(Proteus) by lecturer. after being minimum assist Proteus one or
assisted by by lecturer two error.
lecturer.
Unsuccessful Successful Successful Successful Successful
program with program and program and program and program and
completely manage to get manage to get manage to get manage to get
incorrect the correct the correct the correct the correct
output on output on output on output on output on
hardware or hardware or hardware or hardware or hardware or
Demonstrate Proteus. Proteus after Proteus with a Proteus. Proteus.
circuit using being assist by minimum assist
4. hardware or lecturer. by lecturer.
software
Student Student having Student Student Student
(Proteus)
having difficulties to understand understand understand the
difficulties to understand most of the most of the program and
understand the program program and program and can
the program. demonstrate can can demonstrate
the circuit. demonstrate demonstrate the circuit.
the circuit. the circuit.

MARKS(NO(1+2+3+4)) = /50

TOTAL MARKS/100
6 RESULT
CIRCUIT/OSCILOSCOPE DISPLAY
TASK A- TIMER0
T0CON = 0b = H

TASK B- COUNTER
(USING LED)

T0CON = 0b = H

7 TASK B
(USING 7 SEGMENT)

T0CON = 0b = H

TASK 1
Write C Code and print the circuit diagram

T0CON = 0b = H

TASK 2
Write C Code and print the circuit diagram.

T0CON = 0b = H

8 ENHANCEMENT ASSESSMENT

Describe the role of TMR0IF(TMR0 Overflow Interrupt Flag) bit in INTCON


(Interrupt Control) Register.

You might also like