timer programming of pic 18 by prof.snehal laddha
TRANSCRIPT
![Page 1: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/1.jpg)
PIC18 Timer Programming in Assembly and C
![Page 2: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/2.jpg)
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
![Page 3: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/3.jpg)
Programming timers 0 and 1 Counter Programming
![Page 4: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/4.jpg)
PIC18 has two to five timers◦ Depending on the family number
These timers can be used as ◦ Timers to generate a time delay ◦ Counters to count events happening outside the
uC
![Page 5: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/5.jpg)
Every timer needs a clock pulse to tick Clock source can be
◦ 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 PIC18’s pins Counter
Timers are 16-bit wide◦ Can be accessed as two separate reg. (TMRxL &
TMRxH)◦ Each timer has TCON (timer Control) reg.
![Page 6: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/6.jpg)
TMR0L & TMR0H are 8-bit Reg.◦ MOVWF TMR0L◦ MOVFF TMR0L, PORTB
9-6
![Page 7: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/7.jpg)
Determine the timer operations modes
Example If T0CON=
0000 1000◦ 16-bit◦ No prescaler◦ Rising edge
T0CON (Timer0 Control) Register
![Page 8: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/8.jpg)
Part of INTCON
INTCON (Interrupt Control Register) has the TMR0IF Flag
![Page 9: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/9.jpg)
![Page 10: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/10.jpg)
1. 16-bit timer, 0000 to FFFFH.2. After loading TMR0H and TMR0L, the timer
must be started.3. Count up, till it reaches FFFFH, then it rolls
over to 0000 and activate TMR0IF bit.4. Then TMR0H and TMR0L must be reloaded
with the original value and deactivate TMR0IF bit.
![Page 11: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/11.jpg)
1. Load the value into the T0CON register2. Load reg. TMR0H followed by reg. TMR0L
with initial value3. Start the timer with instruction
BSF T0CON, TMR0ON4. Keep monitoring the timer flag (TMR0IF) to
see if it is raised.5. Stop the timer6. Clear the TMR0IF flag 37. Go Back to step 2
![Page 12: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/12.jpg)
![Page 13: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/13.jpg)
A square wave of 50% duty cycle on the PORTB.5 is created
Analyze the program
BCF TRISB,5MOVLW 0x08MOVWF T0CON
HERE MOVLW 0xFFMOVWF TMR0HMOVLW 0xF2MOVWF TMR0LBCF INTCON, TMR0IFBTG PORTB,5BSF T0CON, TMR0ON
AGAINBTFSS INTCON, TMR0IF
BRA AGAINBCF T0CON, TMR0ON BRA HERE
FFF2FFF3FFF4FFFF0000
TMR0IF=0TMR0IF=1
![Page 14: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/14.jpg)
Calculate the frequency of the wave generated on PIN PORTB 5.
BCF TRISB,5MOVLW 0x08MOVWF T0CONBCF INTCON,
TMR0IF
HERE MOVLW 0xFFMOVWF TMR0HMOVLW -D'48'MOVWF TMR0LCALL DELAYBTG PORTB,5BRA HERE
DELAYBSF T0CON, TMR0ON
AGAINBTFSS INTCON, TMR0IF
BRA AGAINBCF T0CON, TMR0ON BCF INTCON, TMR0IFRETURN
1111212
1
48
112
![Page 15: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/15.jpg)
General formula for delay calculation◦ T = 4/(10MHz) = 0.4 usecond
![Page 16: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/16.jpg)
Write a program to generate a square wave with a period of ms on pin PORTB.3 (XALT=10 Mhz)
T = 10 ms Time delay =
10ms/2 = 5 ms. We need 5ms/0.4us
= 12500 clocks FFFF - 30D4 +1
=CF2C TMR0H = CFH TMR0L= 2CH
![Page 17: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/17.jpg)
BCF TRISB,3MOVLW 0x08MOVWF T0CON
HEREMOVLW 0xCFMOVWF TMR0HMOVLW 0x2CMOVWF TMR0LBCF INTCON,TMR0IFCALL DELAYBTG PORTB,3BRA HERE
DELAYBSF T0CON,TMR0ON
AGAINBTFSSINTCON,TMR0IFBRA AGAINBCF T0CON,TMR0ON RETURN
![Page 18: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/18.jpg)
![Page 19: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/19.jpg)
The size of delay depend on ◦ The Crystal frequency◦ The timer’s 16-bit register.
The largest timer happens when 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◦ If T0CON=0000 0101, then T = 4*64/f
9-19
XTAL Osc ÷ 4 ÷ 64 TMRx
![Page 20: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/20.jpg)
Examine the following program and find the time delay in second.
Assume that XALT = 10 MHz.
BCF TRISB,2MOVLW 0x05MOVWF T0CON
HEREMOVLW 0x01MOVWF TMR0HMOVLW 0x08MOVWF TMR0LBCF INTCON,TMR0IFCALL DELAYBTG PORTB,2BRA HERE
![Page 21: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/21.jpg)
TMR0H:TMR0L=0108H=264 decimal 65,536-264=65,272 Now, 65,272x64x0.4us=1.671sec
![Page 22: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/22.jpg)
![Page 23: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/23.jpg)
![Page 24: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/24.jpg)
![Page 25: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/25.jpg)
Can be programmed in 16-bit mode only It has 2 bytes named as TMR1L and TMR1H It has also T1CON and TMR1IF 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.
9-25
![Page 26: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/26.jpg)
![Page 27: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/27.jpg)
![Page 28: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/28.jpg)
![Page 29: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/29.jpg)
Used to count event outside the PIC◦ Increments the TMR0H and TMR0L registers
T0CS in T0CON reg. determines the clock 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?◦ 8 bit, external clock source, no prescaler
If TMR1CS=1, the timer 1 counts up as clock pulses are fed into pin RC0
![Page 30: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/30.jpg)
Assuming that clock pulses are fed into pin T0CK1, write a program for counter 0 in 8-bit mode to count the pulses and display the state of the TMR0L count on PORTB.
![Page 31: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/31.jpg)
Port A i/p for clock
Port B o/p
Write bits inT0CON
Load TMR0L=0
Clear interrupt flag bit
Start timer
Display count on port B
Monitor timer 0 Flag bit
No overflow
Overflow
Stop timer go to initial count
![Page 32: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/32.jpg)
![Page 33: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/33.jpg)
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
![Page 34: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/34.jpg)
![Page 35: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/35.jpg)
Assume that a 1 Hz frequency pulse is connected to input for Timer1(RC0)
Write a program to display the counter values on PORTB and D in decimal.
Initial value=0 16-bit and no Prescaler
![Page 36: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/36.jpg)
![Page 37: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/37.jpg)
The PIC18 can have up to four or more 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
![Page 38: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/38.jpg)
![Page 39: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/39.jpg)
FFFF-3500=CAFF=51967+1=51968X0.4us=20ms
![Page 40: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/40.jpg)
![Page 41: Timer programming of PIC 18 by Prof.Snehal Laddha](https://reader033.vdocuments.us/reader033/viewer/2022061606/587f35521a28ab121d8b6d85/html5/thumbnails/41.jpg)