0% found this document useful (0 votes)
132 views49 pages

Chapter 3 Timer

Here is the code: #include <p18f45k22.h> #include "delays.h" void main() { TRISB = 0; // Configure PORTB pins as output while(1) { PORTB = 0x10; // Toggle PORTB.4 Delay10KTCYx(5000); // Delay 50ms using Timer0 prescaled by 4 } }

Uploaded by

Fahmi LJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views49 pages

Chapter 3 Timer

Here is the code: #include <p18f45k22.h> #include "delays.h" void main() { TRISB = 0; // Configure PORTB pins as output while(1) { PORTB = 0x10; // Toggle PORTB.4 Delay10KTCYx(5000); // Delay 50ms using Timer0 prescaled by 4 } }

Uploaded by

Fahmi LJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

CHAPTER 3

WEEK 7 -9
3.0 PIC TIMER PROGRAMMING
IN C
3.1 Know Timer Register
3.2 Understand The Register used for timers in
the PIC
3.3 Apply C Program for Timer in PIC
Timer Registers
 List the Timer and their associated registers
 Various modes of PIC timers
Timers in PIC18F45K22

Timer 8-bit 16-bit


0 YES YES
1/3/5 NO YES
2/4/6 YES NO
Timer0
 Software selectable operation as a
Timer/Counter in both 8-bit/16-bit modes
 Readable and writable registers
 Dedicated 8-bit, software peogrammable
prescaler
 Selectable clock source (internal/external)
 Control by T0CON register
Timer0
• 16-bit wide
• Consists of a low-byte (TMR0L) and a high-
byte (TMR0H) register
• Can be used as 8-bit or 16-bit timer

High byte (8-bit) Low byte (8-bit)


Timer0 Block Diagram – 8 bit
Timer0 Block Diagram – 16 bit

Timer
mode
Timer0 Operation
• Can be a Timer/ Counter. TOCS = 0 : Timer
mode.
• Timer mode: by default increments on every
clock unless a different prescaler is selected.
• TOCS=1 : Counter mode
• Timer0 increments either on every rising or
falling edge of pin RA4/TOCK1
Timer0 (cont’d)
• Important Registers:
i) T0CON (Timer0 Control Register)
• To start & stop Timer0 and other
configurations
ii) TMR0H:TMR0L (for counting purposes)
• Act as counting buffer
iii) INTCON (Interrupt Control Register)
T0CON (Timer0 Control) Register
The T0CON register (Register 11-1) controls all aspects of the module’s
operation, including the prescale selection. It is both readable and writable.
Example 1
• What is the value of T0CON if the Timer0
settings are as below ?
– 16-bit timer
– No pre-scaler
– Internal clock (from oscillator) source
– Increment on positive-edge

Answer: T0CON =
Example 1
• What is the value of T0CON if the Timer0
settings are as below ?
– 16-bit timer
– No pre-scaler
– Internal clock (from oscillator) source
– Increment on positive-edge

Answer: T0CON = 00001000


PIC18 Period
Steps to program Timer0
(16-bit mode)
1. Configure the T0CON register indicating which mode (8-bit or
16- bit) to be used and the selected prescaler option.
2. Load register TMR0H followed by register TMR0L with initial
count values.
3. Start the timer with the instruction “T0CONbits.TMR0ON = 1”
4. Keep monitoring the timer flag (T0CONbits.TMR0IF) to see if it is
raised. Get out of the loop when TMR0IF becomes high.
5. Stop the timer with the instruction “T0CONbits.TMR0ON = 0”.
6. Clear the TMR0IF flag for the next round (T0CONbits.TMR0IF =
0).
7. Go back to Step 2 to load TMR0H and TMR0L again.
Steps to program Timer0
(8-bit mode)
1. Load the T0CON value register indicating 8-bit mode is selected.
2. Load the TMR0L registers with the initial count value.
3. Start the timer.
4. Keep monitoring the TMR0IF to see if it is raised. Get out of the
loop when TMR0IF becomes HIGH.
5. Clear the TMR0IF flag for the next round.
6. Start the timer.
7. Go back to Step 2 to load TMR0L again.
• Notice that when we choose the 8-bit option, only the TMR0L
register is used and the TMR0H has a zero value during the count
up.
Values for TMR0H and TMR0L
Assume XTAL=10 MHz and no prescaler:
1. Divide the desired time delay by 0.4 μs.
2. Perform 65,536 - n, where n is the decimal value
we got in Step 1.
3. Convert the result of Step 2 to hex, where yyxx
is the initial hex value to be loaded into the
timer’s registers.
4. Set TMR0H = yy and TMR0L = xx.
Values for TMR0H and TMR0L
Timer delay calculation for XTAL=10 MHz
with no prescaler.
Example 2
Calculate the amount of time delay generated by the Timer0 with the
following specification:
– XTAL = 10MHz
– TMR0H:TMR0L = FFF2H

Solution:

Frequency = 10 MHz/4 = 2.5 MHz


Period = 1 / 2.5 MHz = 0.4 µs
Time delay = Number of counts x Period

Number of counts = (FFFF-FFF2) + 1 = 14


Time delay = 14 x 0.4us = 5.6us
Example 3
• Write a C program to toggle all the bits of PORTB continuously with
1ms delay. Use Timer0, 16-bit mode, no prescaler options to
generate the delay. (Assume XTAL=20MHz)
• Solution:
• TCY = 4/20MHz = 0.2us (Each tick consume 0.2us)
• How many ticks in 1ms delay?
– 1ms/0.2us = 5000 ticks = 1388H ticks!
• To find register value for TMR0H:TMR0L
– FFFF - register value + 1 = 1388H ticks
– register value = EC78H
@
Simply take the negative value of the tick counts
-1388H = EC78H
Solution
Exercise
• Write a C program to toggle only the
PORTB.4 bit continuously every 50ms. Use
Timer0, 16-bit mode and the 1:4 prescaler
to create the delay. (Assume XTAL =
20MHz)
Solution
Solution:
• TCY = 4/20MHz = 0.2us (Each tick consume
0.2us)
• How many ticks in 50ms delay?
– 50ms/0.2us = 250000 ticks = 3D090H ticks!
(out of range!!)
• With 1:4 prescaller
– 250000/4 = 62500 ticks = F424H ticks!
• Therefore, register counts = -F424H = 0BDCH
Exercise (cont’d)
Prescaler
• To produce a larger time delay
• Use prescaler option in T0CON register to
increase the delay by reducing the period
• Lower 3 bits of the T0CON register give the
option the number that can be divided.
Example 3
Example 4
Calculate the amount of time delay generated by the Timer0 with the following
specification:
– XTAL = 10MHz
– TMR0H:TMR0L = 0108H
– prescaler 64

Solution:

Frequency = 10 MHz / 4 / 64 = 39062.5 Hz


Period = 1 / 39062.5 Hz = 25.6 µs
Time delay = Number of counts x Period
Number of counts = (FFFF-0108) + 1 = FEF8H = 65,272.
Time delay = FEF8 x 25.6 µs = 1.671 s

Another solution:
Example 5
Example 6
Calculate the amount of time delay generated by the Timer0 with the
following specification:
– XTAL = 10MHz
– TMR0H:TMR0L = 85EEH
– prescaler 4

Solution:
Timer1
• 16-bit wide
• Consists of a low-byte (TMR1L) and a high-
byte (TMR1H) register
• Can be used as 16-bit timer only!

High byte (8-bit) Low byte (8-bit)


Timer1 (cont’d)
Important Registers:
i) T1CON (Timer1 Control Register)
• To start & stop Timer1 and other
configurations
ii) TMR1H:TMR1L (for counting purposes)
• Act as counting buffer
iii) PIR1 (Peripheral Interrupt Request
Register 1)
T1CON (Timer1 Control) Register
Exercise 2
Write a C program to create pulses with a frequency of 2500Hz
with 50% duty cycle on pin PORTB.1. Use Timer1 to create the
delay. (Assume XTAL = 20MHz)

Solution:
• T = 1/2500 = 400us (HIGH: 200us; LOW: 200us)
• How many ticks in 200us delay?
– 200us/0.2us = 1000 ticks = 03E8H ticks!
• Therefore, register counts = - 03E8H = FC18H
Exercise 2 (cont’d)
Exercise 2 (cont’d)
Timer0 & Timer1 as Counter
• Can used as Counters
• Counter0 (Timer0 counter):
– Count pulses on T0CKI (RA4) pin
• Counter1 (Timer1 counter):
– Count pulses on T13CKI (RC0) pin
Timer2
• 8-bit wide
• Consists of a PR2 (Period Register 2)
• TMR2 will increment from 00 until reaches PR2
value before TMR2IF flag is set
• Consists of prescaler and postscaler
• No counter function
Timer2 (cont’d)
Important Registers:
i) T2CON (Timer2 Control Register)
• To start & stop Timer2 and other
configurations
ii) PR2 (to set the counting value)
• If TMR2 = PR2; TMR2IF flag is set
iii) PIR1 (Peripheral Interrupt Request
Register 1)
Timer2 (cont’d)
Timer3

• 16-bit wide
• Consists of a low-byte (TMR3L) and a high-
byte (TMR3H) register
• Enable the CCP Mode for PWM Application
Timer3 (cont’d)
Important Registers:
i) T3CON (Timer3 Control Register)
• To start & stop Timer3 and other
configurations
ii) TMR3H:TMR3L (for counting purposes)
• Act as counting buffer
iii) PIR2 (Peripheral Interrupt Request
Register 2)
Timer3 (cont’d)
PIC18 Timer Overall

TIMER 8-bit 16-bit

0 YES YES

1 NO YES

2 YES NO

3 NO YES
delays.h Function Library
Time Delays using the delays.h
• To determine the time generated by the
delays.h functions use the following
equation.
4 _ = instruction time, TCY
Clock Frequency
• If the clock frequency is 4 MHz then the
instruction time is 1.0 μs so if a
Delay10TCYx(8) is used in a program it
causes an 80 μs time delay.
Delay calculation examples.
delay functions: TCY: instruction cycle. 1 TCY = 4/(48M Hz) = 1/12 us.
Delay1TCY(10); delay for (1 instruction cycle)*10. Delay time = (1/12)*e-6
*10 = 0.83 us
Delay10TCYx(100); delay for (10 instruction cycles)*100.
• In this command, x is standing for multiply.Delay time = 10 *(1/12)*e-
6 * 100 = 83 us
• Delay100TCYx(10); delay for (100 instruction cycle)*10. Delay time =
100 *(1/12)*e-6 * 10 = 83us
• Delay1KTCYx(3); delay for (1000 instruction cycle)*3. Delay time =
1000 *(1/12)*e-6 * 3 = 0.25 ms .
• Delay10KTCYx(30); delay for (10000 instruction cycle)*30. Delay time
= 10000 *(1/12)*e-6 * 30 = 25 ms notice the range of variables in
brackets is from 1 to 255. A value of 0 will make the variable in to 256.
Example
• Write a C18 program
to toggle all the bits of
PORTB continuously
with some delay.
Use Timer0, 16-bit
mode, and no
prescaler options to
generate the delay.
Example 2
• Write a C18 program to
toggle only the PORTB.4
bit continuously every 50
ms.
Use Timer0, 16-bit mode,
the 1:4 prescaler to
create the delay. Assume
XTAL = 10MHz

You might also like