Embedded System Spring 2011 PIC 18 Timer Programming
Embedded System Spring, 2011 PIC 18 Timer Programming in Assembly and C Eng. Wazen M. Shbair
Today’s Lecture o List the Timers of PIC 18 and their associated registers o Describe the various modes of the PIC 18 timers o Program the PIC 18 timers in Assembly to generate time delays o Program the PIC 18 timers in Assembly as event counters IUG- Embedded System 2
Introduction o PIC 18 has two to five timers n Depending on the family number o These timers can be used as n Timers to generate a time delay n Counters to count events happening outside the u. C 3
Programming timers 0 and 1 o Every timer needs a clock pulse to tick o Clock source can be n Internal 1/4 th of the frequency of the crystal oscillator on OSC 1 and OSC 2 pins (Fosc/4) is fed into timer n External: pulses are fed through one of the PIC 18’s pins Counter o Timers are 16 -bit wide n Can be accessed as two separate reg. (TMRx. L & TMRx. H) n Each timer has TCON (timer Control) reg. 4 9 -4
Timer 0 registers and programming o TMR 0 L & TMR 0 H are 8 -bit Reg. n MOVWF TMR 0 L n MOVFF TMR 0 L, PORTB 5
T 0 CON (Timer 0 control) register o Each timer has a control register called TCON, to set various timer operation modes. o To. CON is 8 -bit register used to control of Timer 0. o Example n n If T 0 CON= 0000 16 -bit No prescaler Rising edge 6
Figure 9 -2. T 0 CON (Timer 0 Control) Register
T 0 CS(Timer 0 clock source) o This bit used to decide whether the clock source is internal or external. o If T 0 CS=0 Then Fosc/4 is used as clock source, used for delay generation. o If T 0 CS=1 the clock source is external and comes from an external source, used as event counter. 8
TMR 0 IF flag bit o TMR 0 IF is (Timer 0 interrupt flag) is a part of the INTCON (interrupt control) register. o When te timer reaches its maximum value of FFFFH, it rolls over to 0000, and TMR 0 IF is set to 1 INTCON (Interrupt Control Register) has the TMR 0 IF Flag 9
Figure 9 -4. Timer 0 Overflow Flag 10
Characteristics and operations of 16 -bit mode q 16 -bit timer, 0000 to FFFFH. q After loading TMR 0 H and TMR 0 L, the timer must be started. q Count up, till it reaches FFFFH, then it rolls over to 0000 and activate TMR 0 IF bit. q Then TMR 0 H and TMR 0 L must be reloaded with the original value and deactivate TMR 0 IF bit. 11
Steps to program Timer 0 in 16 -bit mode to generate time delay 1. Load the value into the T 0 CON register 2. Load reg. TMR 0 H followed by reg. TMR 0 L with initial value 3. Start the timer with instruction BSF T 0 CON, TMR 0 ON 4. Keep monitoring the timer flag (TMR 0 IF) to see if it is raised. 5. Stop the timer 6. Clear the TMR 0 IF flag 3 7. Go Back to step 2 12 9 -12
13
14
15
Figure 9 -6. Timer Delay Calculation for XTAL = 10 MHz with No Prescaler o General formula for delay calculation n T = 4/(10 MHz) = 0. 4 usecond 16
Finding values to be loaded into the timer o If we know the amount of timer delay , how to find the values needed for the TMR 0 H and TMR 0 L registers. 17
Finding values to be loaded into the timer 18
19
Prescaler and generating larger delay o The size of delay depend on n The Crystal frequency n The timer’s 16 -bit register. o The largest timer happens when TMR 0 L=TMR 0 H=0 o Prescaler option is used to duplicate the delay by dividing the clock by a factor of 2, 4, 8, 16, 32, 64 , 128, 256 n If T 0 CON=0000 0101, then T = 4*64/f XTAL Osc ÷ 4 ÷ 64 TMRx 9 -20 20
Prescaler and generating larger delay IUG-Embedded System 2011 IUGEmbedded System 21
IUG-Embedded System 2011 IUGEmbedded System 22
IUG-Embedded System 2011 IUGEmbedded System 23
Timer 1 Programming o o Can be programmed in 16 -bit mode only It has 2 bytes named as TMR 1 L and RMR 1 H It has also T 1 CON and TMR 1 IF The module incorporates its own low-power oscillator to provide an additional clocking option. o Used as a low-power clock source for the microcontroller in power-managed operation. 9 -24
Figure 9 -10. T 1 CON (Timer 1 Control ) Register IUG-Embedded System 2011 IUGEmbedded System 25
Figure 9 -11. (Interrupt Control Register) Contains the TMR 1 IF Flag 9 -26
Counter Programming o Used to counts event outside the PIC n Increments the TMR 0 H and TMR 0 L registers o T 0 CS in T 0 CON reg. determines the clock source, n If T 0 CS = 1, the timer is used as a counter n Counts up as pulses are fed from pin RA 4 (T 0 CKI) n What does T 0 CON=0110 1000 mean? o If TMR 1 CS=1, the timer 1 counts up as clock pulses are fed into pin RC 0 9 -27
Internal Source External Source 9 -28 IUG-Embedded System 2011 IUGEmbedded System 28
Using external Crystal for Timer 1 clock o Timer 1 comes with two options, n clock fed into T 1 CKI o T 1 OSCEN=0 n Clock from a crystal connected to T 1 OSIT 1 OSO (additional) o T 1 OSCEN=1 o 32 k. Hz Crystal is connected o Used for saving power during SLEEP mode doesn’t disable Timer 1 while the main crystal is shut down
IUG-Embedded System 2011 IUGEmbedded System 30
IUG-Embedded System 2011 IUGEmbedded System 31
IUG-Embedded System 2011 IUGEmbedded System 32
References o Jie Hu , ECE 692 Embedded Computing Systems , Fall 2010. o PIC Microcontroller And Embedded Systems: using Assembly and C for PIC 18, M. Mazidi, R. Mc. Kinlay and D. Causey, Prentice Fall, 2008. o Eng. Husam Alzaq, Embedded System Course, IUG, 2010 IUG- Embedded System 33
- Slides: 33