PIC Microcontroller and Embedded Systems: Eng. Husam Alzaq The Islamic Uni. of Gaza
PIC Microcontroller and Embedded Systems: Eng. Husam Alzaq The Islamic Uni. of Gaza
9-1
PIC Microcontroller and Embedded Systems Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey, February
2007.
9-2
Objective
List the Timers of PIC18 and their associated
registers Describe the various modes of the PIC18 timers Program the PIC18 timers in Assembly to generate time delays Program the PIC18 timers in Assembly as event counters
9-3
Outlines
Programming timers 0 and 1
Counter Programming
9-4
Introduction
PIC18 has two to five timers
Depending
These timers can be used as Timers to generate a time delay Counters to count events happening outside the uC
9-5
Internal 1/4th of the frequency of the crystal oscillator on OSC1 and OSC2 pins (Fosc/4) is fed into timer External: pulses are fed through one of the PIC18s pins Counter
Can be accessed as two separate reg. (TMRxL & TMRxH) Each timer has TCON (timer Control) reg.
9-7
T0CON Reg
Determine
9-8
Figure 9-3. INTCON (Interrupt Control Register) has the TMR0IF Flag
9-9
9-10
2.
3. 4.
5.
6. 7.
Load the value into the T0CON register Load reg. TMR0H followed by reg. TMR0L with initial value Start the timer with instruction BSF T0CON, TMR0ON Keep monitoring the timer flag (TMR0IF) to see if it is raised. Stop the timer Clear the TMR0IF flag 3 Go Back to step 2
9-12
9-13
BCF TRISB,5 MOVLW 0x08 MOVWF T0CON HERE A square wave of MOVLW 0xFF 50% duty cycle on MOVWF TMR0H the PORTB.5 is MOVLW 0xF2 created MOVWF TMR0L Analyze the BCF INTCON, TMR0IF program BTG PORTB,5 BSF T0CON, TMR0ON FFFF 0000 FFF2 AGAIN FFF3 FFF4 BTFSS INTCON, TMR0IF BRA AGAIN TMR0IF=1 TMR0IF=0 BCF T0CON, TMR0ON
Example 9-3
9-14
HERE
Example 9-5
Calculate the
MOVLW 0xFF MOVWF TMR0H MOVLW -D'48' MOVWF TMR0L CALL DELAY BTG PORTB,5 BRA HERE DELAY BSF T0CON, TMR0ON AGAIN BTFSS INTCON, TMR0IF BRA AGAIN BCF T0CON, TMR0ON BCF INTCON, TMR0IF RETURN
1 1 1 1 2 1 2 1 48 1 1 2
Figure 9-6. Timer Delay Calculation for XTAL = 10 MHz with No Prescaler
General formula for delay calculation
T
9-16
Example 9-8
Write a program to
T = 10 ms
Time delay =
10ms/2 = 5 ms. We need 5ms/0.4us = 12500 clocks FFFF - 30D4 +1 =CF2C TMR0H = CFH TMR0L= 2CH
9-17
TRISB,3 0x08 T0CON 0xCF TMR0H 0x2C TMR0L INTCON,TMR0IF DELAY PORTB,3 HERE
T0CON,TMR0ON
9-18
9-19
TMR0L=TMR0H=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
BCF TRISB,2 Example 9-13 MOVLW 0x05 MOVWF T0CON Examine the HERE following program MOVLW 0x01 and find the time MOVWF TMR0H delay in second. MOVLW 0x08 Assume that XALT = 10 MHz. MOVWF TMR0L BCF INTCON,TMR0IF CALL DELAY BTG PORTB,2 BRA HERE
The PIC uCs 9-21
9-22
oscillator to provide an additional clocking option. Used as a low-power clock source for the microcontroller in power-managed operation.
9-23
9-24
9-25
Figure 9-11. PIR1 (Interrupt Control Register 1) Contains the TMR1IF Flag
9-26
source,
If
T0CS = 1, the timer is used as a counter Counts up as pulses are fed from pin RA4 (T0CKI) What does T0CON=0110 1000 mean?
If TMR1CS=1, the timer 1 counts up as
9-27
External Source
options,
clock fed into T1CKI Clock from a crystal connected to T1OSIT1OSO (additional)
T1OSCEN=1 32 kHz Crystal is connected Used for saving power during SLEEP mode doesnt disable Timer1 while the main crystal is shut down
Internal Source
T1OSCEN=0
9-28
BSF TRISA,RA CLRF TRISB Example 9-23 MOVLW 0x68 MOVWF T0CON Assuming that clock pulses are HERE MOVLW 0x0 fed into pin T0CK1, MOVWF TMR0L write a program BCF INTCON,TMR0IF for counter 0 in 8BSF T0CON,TMR0ON bit mode to count AGAIN MOVFF TMR0L,PORTB the pulses and display the state of BTFSS INTCON,TMR0IF the TMR0L count BRA AGAIN on PORTB. BCF T0CON,TMR0ON GOTO HERE The PIC uCs 9-29
Example 9-24
Assume that a 1 Hz
frequency pulse is connected to input for Timer0(T0CKI) Write a program to display counter 0 on PORTB, C and D in decimal. Ser the initial value of TMR0L to -60.
The PIC uCs
NUME EQU 0x00 QU EQU 0x20 RMND_L EQU 0x30 RMND_M EQU 0x31 RMND_H EQU 0x32 MYDEN EQU D'10' BSF TRISA,RA4 MOVLW 0x68 MOVWF T0CON HERE MOVLW 0x0 MOVWF TMR0L BCF INTCON,TMR0I BSF T0CON,TMR0ON 9-30
Example 9-24
AGAIN MOVF TMR0L,W CALL BIN_ASC_CON BTFSS INTCON,TMR0IF BRA AGAIN BCFT0CON,TMR0ON GOTO HERE
BIN_ASC_CON MOVFF MOVWF MOVLW CLRF QU D_1 INCF SUBWF BC D_1 ADDWF DECF MOVFF MOVFF CLRF QU D_2 INCF SUBWF BC D_2 ADDWF DECF MOVFF MOVFF
PORTB,WREG NUME MYDEN QU NUME NUME QU NUME,RMND_L QU,NUME QU NUM NUM QU NUME,RMND_M QU,RMND_H
9-31
Example 9-26
Assuming that
clock pulses are fed into pin T0CKI and a buzzer is connected to pin PORTB.1 write a program for counter0 in 8-bit mode to sound the buzzer every 100 pulses
BCF TRISB,1 BSF TRISA,4 MOVLW 0x68 MOVWF T0CON MOVLW -D'100' MOVWF TMR0L BCF INTCON,TMR0IF BSF T0CON,TMR0ON AGAIN BTFSS INTCON,TMR0IF BRA AGAIN BCFT0CON,TMR0ON OVER BTG PORTB,1 CALL DELAY GOTO OVER 9-32
BSF TRISC,RC0 CLRF TRISB Example 9-27 CLRF TRISD MOVLW 0x02 Assume that a 1 Hz frequency pulse is MOVWF T1CON connected to input for HERE MOVLW 0x0 Timer1(RC0) MOVWF TMR1H Write a program to display the counter MOVLW 0x0 values on PORTB and D in MOVWF TMR1L decimal. BCF PIR1,TMR1IF Initial value=0 16-bit and no Prescaler BSF T1CON,TMR1ON
The PIC uCs
9-33
Example 9-27
AGAIN MOVFF TMR1H,PORTD MOVFF TMR1L,PORTB BTFSS PIR1,TMR1IF BRA AGAIN BCF PIR1,TMR1ON GOTO HERE
9-34
Chapter 9: Summary
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 uCs 9-35