Electrical Engineering Department ACADEMIC SESSION: - Dec40053 - Embedded System Applications
Electrical Engineering Department ACADEMIC SESSION: - Dec40053 - Embedded System Applications
(1)
(2)
(3)
(4)
2 OBJECTIVE
3 THEORY
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.
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
Set processor clock frequency to 16 MHz. Load LAB3.hex into your PIC18
and observe the output.
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
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
Observation:
State your observation of the circuit.
6 RESULT
7 DISCUSSION / TASK
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
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