Chapter 9 PIC 18 Timer Programming in Assembly
Chapter 9 PIC 18 Timer Programming in Assembly 1
Objective � List the Timers of PIC 18 and their associated registers � Describe the various modes of the PIC 18 timers � Program the PIC 18 timers in Assembly to generate time delays � Program the PIC 18 timers in Assembly as event counters 2
Introduction � PIC 18 has two to five timers � These timers can be used as ◦ Depending on the family number ◦ Timers to generate a time delay ◦ Counters to count events happening outside the u. C 3
Programming timers 0 and 1 � Every timer needs a clock pulse to tick � Clock source can be ◦ Internal 1/4 th of the frequency of the crystal oscillator on OSC 1 and OSC 2 pins (Fosc/4) is fed into timer ◦ External: pulses are fed through one of the PIC 18’s pins Counter � Timers are 16 -bit wide ◦ Can be accessed as two separate reg. (TMRx. L & TMRx. H) ◦ Each timer has TCON (timer Control) reg. ◦ TMR 0 L & TMR 0 H are 8 -bit Reg. 4
T 0 CON Reg Determine the timer operations modes Example If T 0 CON= 0000 1000 ◦ 16 -bit ◦ No prescaler ◦ Rising edge -5
TMR 0 IF flag bit � Part of INTCON Timer 0 Overflow Flag 6
Characteristics and operations of 16 -bit mode 1. 2. 3. 4. 16 -bit timer, 0000 to FFFFH. After loading TMR 0 H and TMR 0 L, the timer must be started. Count up, till it reaches FFFFH, then it rolls over to 0000 and activate TMR 0 IF bit. Then TMR 0 H and TMR 0 L must be reloaded with the original value and deactivate TMR 0 IF bit. 7
Steps to program Timer 0 in 16 -bit mode 1. 2. 3. 4. 5. 6. 7. Load the value into the T 0 CON register Load reg. TMR 0 H followed by reg. TMR 0 L 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 8
Example A square wave of 50% duty cycle on the PORTB. 5 is created Analyze the program BCF TRISB, 5 MOVLW 0 x 08 MOVWF T 0 CON HERE MOVLW 0 x. FF MOVWF TMR 0 H MOVLW 0 x. F 2 MOVWF TMR 0 L BCF INTCON, TMR 0 IF BTG PORTB, 5 BSF T 0 CON, TMR 0 ON AGAIN BTFSS INTCON, TMR 0 IF BRA AGAIN BCF T 0 CON, TMR 0 ON BRAHERE FFF 2 FFF 3 FFF 4 FFFF TMR 0 IF=0 0000 TMR 0 IF=1 9
Example HERE MOVLW 0 x. FF MOVWF TMR 0 H MOVLW -D'48' Calculate the MOVWF TMR 0 L frequency of the CALL DELAY wave generated on BTG PORTB, 5 PIN PORTB 5. BRA HERE DELAY BCF TRISB, 5 BSF T 0 CON, TMR 0 ON MOVLW 0 x 08 AGAIN MOVWF T 0 CON BTFSS INTCON, TMR 0 IF BCF INTCON, BRA AGAIN TMR 0 IF BCF T 0 CON, TMR 0 ON BCF INTCON, TMR 0 IF RETURN 1 1 1 13
Timer Delay Calculation for XTAL = 10 MHz with No Prescaler General formula for delay calculation ◦ T = 4/(10 MHz) = 0. 4 usecond 11
Example Write a program to generate a square wave with a period of 10 ms on pin PORTB. 3 (XALT=10 Mhz) T = 10 ms Time delay = 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 12
Example , Cont. BCF MOVLW MOVWF HERE MOVLW MOVWF BCF CALL BTG BRA 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 13
Prescaler and generating larger delay � � � The size of delay depend on ◦ The Crystal frequency ◦ The timer’s 16 -bit register. The largest timer happens when TMR 0 L=TMR 0 H=0 Prescaler option is used to duplicate the delay by dividing the clock by a factor of 2, 4, 8, 16, 32, 64 , 128, 256 ◦ If T 0 CON=0000 0101, then T = 4*64/f XTAL Osc ÷ 4 ÷ 64 TMRx 14
Example Examine the following program and find the time delay in second. Assume that XALT = 10 MHz. BCF TRISB, 2 MOVLW 0 x 05 MOVWF T 0 CON HERE MOVLW 0 x 01 MOVWF TMR 0 H MOVLW 0 x 08 MOVWF TMR 0 L BCF INTCON, TMR 0 IF CALL DELAY BTGPORTB, 2 BRA HERE DELAY BSF T 0 CON, TMR 0 ON AGAIN BTFSS INTCON, TMR 0 IF BRAAGAIN BCF T 0 CON, TMR 0 ON RETURN 15
Timer 0 8 -bit Block Diagram 1. 2. 3. 4. 4. 5. 6. Load the value into the T 0 CON register indicating 8 bit mode is selected Load reg TMR 0 L with initial value Start the timer with instruction Keep monitoring the timer flag (TMR 0 IF) to see if it is raised. Stop the timer with instrucction BCF T 0 CON, TMR 0 ON Clear the TMR 0 IF flag for the next round Go Back to step 2 16
Timer 1 High and Low Registers � 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. � Used as a low-power clock source for the microcontroller in power-managed operation. 17
Timer 1 Block Diagram 18
T 1 CON (Timer 1 Control ) Register 919
PIR 1 (Interrupt Control Register 1) Contains the TMR 1 IF Flag 20
Counter Programming � Used to counts event outside the PIC ◦ Increments the TMR 0 H and TMR 0 L registers � T 0 CS in T 0 CON reg. determines the clock source, ◦ If T 0 CS = 1, the timer is used as a counter ◦ Counts up as pulses are fed from pin RA 4 (T 0 CKI) ◦ What does T 0 CON=0110 1000 mean? � If TMR 1 CS=1, the timer 1 counts up as clock pulses are fed into pin RC 0 21
Timer 1 comes with two options, ◦ clock fed into T 1 CKI �T 1 OSCEN=0 ◦ Clock from a crystal connected to T 1 OSI-T 1 OSO (additional) �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 Internal Source � Using external Crystal for Timer 1 External Source clock 22
Example Assuming that clock pulses are fed into pin T 0 CK 1, write a program for counter 0 in 8 -bit mode to count the pulses and display the state of the TMR 0 L count on PORTB. BSF TRISA, RA CLRF TRISB AGAIN MOVFF TMR 0 L, PORTB MOVLW 0 x 68 BTFSS INTCON, TMR 0 IF MOVWF T 0 CON BRA AGAIN HERE MOVLW 0 x 0 BCF T 0 CON, TMR 0 ON MOVWF TMR 0 L GOTO HERE BCF INTCON, TMR 0 IF BSF T 0 CON, TMR 0 ON 23
Example Assume that a 1 Hz frequency pulse is connected to input for Timer 0(T 0 CKI) Write a program to display counter 0 on PORTB, C and D in decimal. Ser the initial value of TMR 0 L to -60. 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 IF BSF T 0 CON, TMR 0 ON 24
Example AGAINMOVF TMR 0 L, W CALL BIN_ASC_CON BTFSS INTCON, TMR 0 IF BRA AGAIN BCF T 0 CON, TMR 0 ON GOTO HERE 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 RETURN PORTB, WREG NUME MYDEN QU NUME, RMND_L QU, NUME QU NUME, RMND_M QU, RMND_H 25
Example 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 BCF BSF MOVLW MOVWF BCF BSF TRISB, 1 TRISA, 4 0 x 68 T 0 CON -D'100' TMR 0 L INTCON, TMR 0 IF T 0 CON, TMR 0 ON AGAIN BTFSS INTCON, TMR 0 IF BRA AGAIN BCF T 0 CON, TMR 0 ON OVER BTG PORTB, 1 CALL DELAY GOTO OVER 26
- Slides: 26