PIC Microcontroller and Embedded Systems Muhammad Ali Mazidi
PIC Microcontroller and Embedded Systems Muhammad Ali Mazidi, Rolin Mc. Kinlay and Danny Causey Eng. Husam Alzaq The Islamic Uni. Of Gaza The PIC u. Cs 9 -1
Chapter 9: PIC 18 Timer Programming in Assembly and C PIC Microcontroller and Embedded Systems Muhammad Ali Mazidi, Rolin Mc. Kinlay and Danny Causey, February 2007. The PIC u. Cs 9 -2
Objective r List the Timers of PIC 18 and their associated registers r Describe the various modes of the PIC 18 timers r Program the PIC 18 timers in Assembly to generate time delays r Program the PIC 18 timers in Assembly as event counters The PIC u. Cs 9 -3
Outlines r Programming timers 0 and 1 r Counter Programming The PIC u. Cs 9 -4
Introduction r PIC 18 has two to five timers m Depending on the family number r These timers can be used as m Timers to generate a time delay m Counters to count events happening outside the u. C The PIC u. Cs 9 -5
Section 9. 1: Programming timers 0 and 1 r Every timer needs a clock pulse to tick r Clock source can be m Internal 1/4 th of the frequency of the crystal oscillator on OSC 1 and OSC 2 pins (Fosc/4) is fed into timer m External: pulses are fed through one of the PIC 18’s pins Counter r Timers are 16 -bit wide m Can be accessed as two separate reg. (TMRx. L & TMRx. H) m Each timer has TCON (timer Control) reg. The PIC u. Cs 9 -6
Timer 0 registers and programming r TMR 0 L & TMR 0 H are 8 -bit Reg. m MOVWF TMR 0 L m MOVFF TMR 0 L, PORTB The PIC u. Cs 9 -7
T 0 CON Reg r Determine the timer operations modes r Example r If T 0 CON= 0000 16 -bit m No prescaler m Rising edge m The PIC u. Cs Figure 9 -2. T 0 CON (Timer 0 Control) Register 9 -8
TMR 0 IF flag bit r Part of INTCON The PIC u. Cs Figure 9 -3. INTCON (Interrupt Control Register) has the TMR 0 IF Flag 9 -9
Figure 9 -4. Timer 0 Overflow Flag The PIC u. Cs 9 -10
Characteristics and operations of 16 -bit mode 1. 16 -bit timer, 0000 to FFFFH. 2. After loading TMR 0 H and TMR 0 L, the timer must be started. 3. Count up, till it reaches FFFFH, then it rolls over to 0000 and activate TMR 0 IF bit. 4. Then TMR 0 H and TMR 0 L must be reloaded with the original value and deactivate TMR 0 IF bit. The PIC u. Cs 9 -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 3. 4. 5. 6. 7. with initial value Start the timer with instruction BSF T 0 CON, TMR 0 ON Keep monitoring the timer flag (TMR 0 IF) to see if it is raised. Stop the timer Clear the TMR 0 IF flag 3 Go Back to step 2 The PIC u. Cs 9 -12
Figure 9 -5. Timer 0 16 -bit Block Diagram The PIC u. Cs 9 -13
BCF MOVLW MOVWF HERE r A square wave of MOVLW 50% duty cycle on MOVWF the PORTB. 5 is MOVLW created MOVWF r Analyze the BCF program BTG BSF FFFF 0000 FFF 2 AGAIN FFF 3 FFF 4 BTFSS BRA TMR 0 IF=1 TMR 0 IF=0 BCF Example 9 -3 The PIC u. Cs TRISB, 5 0 x 08 T 0 CON 0 x. FF TMR 0 H 0 x. F 2 TMR 0 L INTCON, TMR 0 IF PORTB, 5 T 0 CON, TMR 0 ON INTCON, TMR 0 IF AGAIN T 0 CON, TMR 0 ON 9 -14
HERE Example 9 -5 r Calculate the frequency of the wave generated on PIN PORTB 5. BCF TRISB, 5 MOVLW 0 x 08 MOVWF T 0 CON BCF INTCON, TMR 0 IF The PIC u. Cs MOVLW 0 x. FF MOVWF TMR 0 H MOVLW -D'48' MOVWF TMR 0 L CALL DELAY BTG PORTB, 5 BRA HERE DELAY BSF T 0 CON, TMR 0 ON AGAIN BTFSS INTCON, TMR 0 IF BRA AGAIN BCF T 0 CON, TMR 0 ON BCF INTCON, TMR 0 IF RETURN 1 1 2 1 48 1 1 2
Figure 9 -6. Timer Delay Calculation for XTAL = 10 MHz with No Prescaler r General formula for delay calculation m T = 4/(10 MHz) = 0. 4 usecond The PIC u. Cs 9 -16
Example 9 -8 r Write a program to generate a square wave with a period of ms on pin PORTB. 3 (XALT=10 Mhz) r T = 10 ms r Time delay = r r The PIC u. Cs 10 ms/2 = 5 ms. We need 5 ms/0. 4 us = 12500 clocks FFFF - 30 D 4 +1 =CF 2 C TMR 0 H = CFH TMR 0 L= 2 CH 9 -17
Example 9 -8, Cont. BCF MOVLW MOVWF HERE MOVLW MOVWF BCF CALL BTG BRA The PIC u. Cs TRISB, 3 0 x 08 T 0 CON 0 x. CF TMR 0 H 0 x 2 C TMR 0 L INTCON, TMR 0 IF DELAY PORTB, 3 HERE DELAY BSF AGAIN BTFSS BRA BCF RETURN T 0 CON, TMR 0 ON INTCON, TMR 0 IF AGAIN T 0 CON, TMR 0 ON 9 -18
Example 9 -8, Cont. The PIC u. Cs 9 -19
Prescaler and generating larger delay r The size of delay depend on m The Crystal frequency m The timer’s 16 -bit register. r The largest timer happens when TMR 0 L=TMR 0 H=0 r Prescaler option is used to duplicate the delay by dividing the clock by a factor of 2, 4, 8, 16, 32, 64 , 128, 256 m The PIC u. Cs If T 0 CON=0000 0101, then T = 4*64/f XTAL Osc ÷ 4 ÷ 64 TMRx 9 -20
BCF TRISB, 2 Example 9 -13 MOVLW 0 x 05 MOVWF T 0 CON r Examine the HERE following program MOVLW 0 x 01 and find the time MOVWF TMR 0 H delay in second. MOVLW 0 x 08 r Assume that XALT = 10 MHz. MOVWF TMR 0 L BCF INTCON, TMR 0 IF CALL DELAY BTG PORTB, 2 BRA HERE The PIC u. Cs 9 -21
Figure 9 -7. Timer 0 8 -bit Block Diagram The PIC u. Cs 9 -22
Figure 9 -8. Timer 1 High and Low Registers r Can be programmed in 16 -bit mode only r It has 2 bytes named as TMR 1 L and RMR 1 H r It has also T 1 CON and TMR 1 IF r The module incorporates its own low-power oscillator to provide an additional clocking option. r Used as a low-power clock source for the microcontroller in power-managed operation. The PIC u. Cs 9 -23
Figure 9 -9. Timer 1 Block Diagram The PIC u. Cs 9 -24
Figure 910. T 1 CON (Timer 1 Control ) Register The PIC u. Cs 9 -25
Figure 9 -11. PIR 1 (Interrupt Control Register 1) Contains the TMR 1 IF Flag The PIC u. Cs 9 -26
SECTION 9. 2: Counter Programming r Used to counts event outside the PIC m Increments the TMR 0 H and TMR 0 L registers r T 0 CS in T 0 CON reg. determines the clock source, If T 0 CS = 1, the timer is used as a counter m Counts up as pulses are fed from pin RA 4 (T 0 CKI) m What does T 0 CON=0110 1000 mean? m r If TMR 1 CS=1, the timer 1 counts up as clock pulses are fed into pin RC 0 The PIC u. Cs 9 -27
Using external Crystal for Timer 1 clock External Source r Timer 1 comes with two options, clock fed into T 1 CKI • T 1 OSCEN=0 m Clock from a crystal connected to T 1 OSIT 1 OSO (additional) Internal Source m • T 1 OSCEN=1 • 32 k. Hz Crystal is connected • Used for saving power during SLEEP mode doesn’t disable Timer 1 while the main crystal is shut down The PIC u. Cs 9 -28
BSF TRISA, RA CLRF TRISB Example 9 -23 MOVLW 0 x 68 MOVWF T 0 CON r Assuming that clock pulses are fed HERE MOVLW 0 x 0 into pin T 0 CK 1, MOVWF TMR 0 L write a program for BCF INTCON, TMR 0 IF counter 0 in 8 -bit BSF T 0 CON, TMR 0 ON mode to count the pulses and display AGAIN MOVFF TMR 0 L, PORTB the state of the BTFSS INTCON, TMR 0 IF TMR 0 L count on BRA AGAIN PORTB. BCF T 0 CON, TMR 0 ON GOTO HERE The PIC u. Cs 9 -29
Example 9 -24 r Assume that a 1 Hz frequency pulse is connected to input for Timer 0(T 0 CKI) r Write a program to display counter 0 on PORTB, C and D in decimal. r Ser the initial value of TMR 0 L to -60. The PIC u. Cs NUME EQU 0 x 00 QU EQU 0 x 20 RMND_L EQU 0 x 30 RMND_M EQU 0 x 31 RMND_H EQU 0 x 32 MYDEN EQU D'10' BSF TRISA, RA 4 MOVLW 0 x 68 MOVWF T 0 CON HERE MOVLW 0 x 0 MOVWF TMR 0 L BCF INTCON, TMR 0 I BSF T 0 CON, TMR 0 ON 9 -30
Example 9 -24 AGAIN MOVF TMR 0 L, W CALL BIN_ASC_CON BTFSS INTCON, TMR 0 IF BRA AGAIN BCFT 0 CON, TMR 0 ON GOTO HERE The PIC u. Cs BIN_ASC_CON MOVFF MOVWF MOVLW CLRFQU D_1 INCF SUBWF BC D_1 ADDWF DECF MOVFF CLRFQU D_2 INCF SUBWF BC D_2 ADDWF DECF MOVFF PORTB, WREG NUME MYDEN QU NUME, RMND_L QU, NUME QU NUME, RMND_M QU, RMND_H 9 -31
Example 9 -26 r Assuming that clock pulses are fed into pin T 0 CKI and a buzzer is connected to pin PORTB. 1 write a program for counter 0 in 8 -bit mode to sound the buzzer every 100 pulses The PIC u. Cs BCF TRISB, 1 BSF TRISA, 4 MOVLW 0 x 68 MOVWF T 0 CON MOVLW -D'100' MOVWF TMR 0 L BCF INTCON, TMR 0 IF BSF T 0 CON, TMR 0 ON AGAIN BTFSS INTCON, TMR 0 IF BRA AGAIN BCFT 0 CON, TMR 0 ON OVER BTG PORTB, 1 CALL DELAY GOTO OVER 9 -32
BSF TRISC, RC 0 CLRF TRISB Example 9 -27 CLRF TRISD MOVLW 0 x 02 r Assume that a 1 Hz frequency pulse is MOVWF T 1 CON connected to input for HERE MOVLW 0 x 0 Timer 1(RC 0) MOVWF TMR 1 H r Write a program to display the counter values MOVLW 0 x 0 on PORTB and D in MOVWF TMR 1 L decimal. BCF PIR 1, TMR 1 IF r Initial value=0 r 16 -bit and no Prescaler BSF T 1 CON, TMR 1 ON The PIC u. Cs 9 -33
Example 9 -27 AGAIN MOVFF TMR 1 H, PORTD MOVFF TMR 1 L, PORTB BTFSS PIR 1, TMR 1 IF BRA AGAIN BCF PIR 1, TMR 1 ON GOTO HERE The PIC u. Cs 9 -34
Chapter 9: Summary Next: Chapter 10 PIC 18 Serial Port Programming in Assembly and C r The PIC 18 can have up to four or more r r r timers/counters. Depending on the family member Timers: Generate Time Delays (using Crystal) Counters: Event counter (using Pulse outside) Timers are accessed as two 8 -bit registers, TMRLx and TMRHx Can be used either 8 -bit or 16 -bit Each timer has its own Timer Control register The PIC u. Cs 9 -35
- Slides: 35