SlideShare a Scribd company logo
3
Most read
6
Most read
7
Most read
Using Timers of Microchip PIC18F
Microcontrollers
Corrado Santoro
ARSLAB - Autonomous and Robotic Systems Laboratory
Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy
santoro@dmi.unict.it
L.A.P. 1 Course
Corrado Santoro Using Timers in PIC18F MCUs
What is a “Timer”?
It is a circuit to let a software have the “knowledge of flow
of time”
It is a composed of:
A clock source; usually the system clock or an external
signal;
A programmable frequency divisor, called prescaler, to
divide clock source frequency, if needed;
Some SFRs which hold a 8-, 16- or 32-bit value that is
incremented in hardware using the clock source.
Some SFRs which give some state information,
e.g overflow (zero crossing).
PIC18F family has 7 timers, called TIMER0, TIMER1, ...,
TIMER5, TIMER6
Each timer has different characteristics and may be used
together with other peripherals.
Corrado Santoro Using Timers in PIC18F MCUs
The TIMER0 of PIC18
TIMER0 is a 8/16 bit timer/counter (figure shows the 8bit mode);
TMR0L is the SFR containing the value that is incremented;
All the parts to the left are the clock source circuits.
T0CON (Timer 0 Control) register is used to program the timer, and
includes the bits shown in figure (T0CS, PSA, T0PS, etc.)
Corrado Santoro Using Timers in PIC18F MCUs
The 16-bit version of TIMER0
In 16-bit mode, two SFR are used TMR0L and TMR0H;
In write operations, TMR0H must be written before TMR0L;
In read operations, TMR0L must be read before TMR0H;
However, XC8 offers a single 16-bit variable TMR0 which includes both
low and high part of TMR0.
Corrado Santoro Using Timers in PIC18F MCUs
The 16-bit version of TIMER0
The T0CON (Timer 0 Control) SFR includes all the bits which
control TIMER0 functioning.
Corrado Santoro Using Timers in PIC18F MCUs
TIMER0: Selecting clock source
Clock source can be internal or external and is controlled by
bit T0CS:
T0CS = 0; → clock source is internal and is taken from
Fosc/4.
T0CS = 1; → clock source is external and is taken from
T0CKI pin; in this case T0SE controls the edge of the
signal which triggers increment.
Corrado Santoro Using Timers in PIC18F MCUs
TIMER0: Dividing clock frequency
In some cases, the clock coming from the oscillator could be
too fast for our applications: we can lower it by using the
frequency prescaler.
The prescaler is a circuit which divides the signal frequency by
2, 4, 8, 16, ..., 256.
The prescaler is activated by bit PSA:
PSA = 0; → prescaler is selected, frequency division is
controlled by bits T0PS.
PSA = 1; → prescaler is not selected.
Corrado Santoro Using Timers in PIC18F MCUs
TIMER0: Dividing clock frequency
When the prescaler is activated (PSA = 0), division is
performed as:
T0PS = 111, division 1:256
T0PS = 110, division 1:128
T0PS = 101, division 1:64
....
T0PS = 000, division 1:2
Corrado Santoro Using Timers in PIC18F MCUs
TIMER0: controlling depth and on/off
Finally, T0CON includes these other two bits:
TMR0ON, turns on/off the timer;
T08BIT, selects 8 (value “1”) or 16 (value “0”) bit mode.
Corrado Santoro Using Timers in PIC18F MCUs
A case-study: a timer to flash a LED
We want to use the system clock, T0CS = 0;
In our board, we have FOSC = 64MHz, therefore the basic frequency is
FOSC/4 = 16MHz, the P = 62.5ns;
Let’s use the prescaler and divide the frequency by 256, so PSA = 0;
T0PS = 0b111;
The timer increments using a period P = 62.5ns · 256 = 16µs.
Corrado Santoro Using Timers in PIC18F MCUs
A case-study: a timer to flash a LED
... the timer increments using a period
P = 62.5ns · 256 = 16µs.
Let us suppose we want a period of half a second 500ms
Therefore 500·10−3
16·10−6 = 31250
A delay of 500ms implies 31250 counts
Corrado Santoro Using Timers in PIC18F MCUs
A case-study: a timer to flash a LED
✞
int main(void)
{
TRISBbits.TRISB0 = 0; // output
T0CONbits.TMR0ON = 0; // stop the timer
T0CONbits.T08BIT = 0; // timer configured as 16-bit
T0CONbits.T0CS = 0; // use system clock
T0CONbits.PSA = 0; // use prescaler
T0CONbits.T0PS = 0b111; // prescaler 1:256 (’0b’ is a prefix for binary)
TMR0 = 0; // clear timer value
T0CONbits.TMR0ON = 1; // start the timer
for (;;) {
unsigned int t;
t = TMR0;
if (t >= 31250) { // equivalent of 500 ms
TMR0 = 0;
LATBbits.LATB0 = !LATBbits.LATB0;
}
}
}
✡✝ ✆
Corrado Santoro Using Timers in PIC18F MCUs
Case-study 2: more LEDs flashing
Let us suppose we want to:
flash led in RB0 at a period of 500 ms
flash led in RB1 at a period of 750 ms
Do we need two timers?? NO!
1 compute the greatest common divisor, which is 250ms
2 use it as your “timer period”
3 toggle RB0 after two periods
4 toggle RB1 after three periods
Corrado Santoro Using Timers in PIC18F MCUs
Case-study 2: more LEDs flashing
Using the same set-up of the previous example, since our
period is 250ms
we have 250·10−3
16·10−6 = 15625
A delay of 250ms implies 15625 counts
Corrado Santoro Using Timers in PIC18F MCUs
Case-study 2: more LEDs flashing
✞
int main(void)
{
char c0 = 0, c1 = 0; // why char? because they are 8 bits
TRISBbits.TRISB0 = 0; // output
TRISBbits.TRISB1 = 0; // output
T0CONbits.TMR0ON = 0; // stop the timer
T0CONbits.T08BIT = 0; // timer configured as 16-bit
T0CONbits.T0CS = 0; // use system clock
T0CONbits.PSA = 0; // use prescaler
T0CONbits.T0PS = 0b111; // prescaler 1:256 (’0b’ is a prefix for binary)
TMR0 = 0; // clear timer value
T0CONbits.TMR0ON = 1; // start the timer
for (;;) {
unsigned int t;
t = TMR0;
if (t >= 15625) { // equivalent of 250 ms
TMR0 = 0;
++c0; ++c1;
if (c0 == 2) { // flash led 0
LATBbits.LATB0 = !LATBbits.LATB0;
c0 = 0;
}
if (c1 == 3) { // flash led 1
LATBbits.LATB1 = !LATBbits.LATB1;
c1 = 0;
}
}
}
}
✡✝ ✆
Corrado Santoro Using Timers in PIC18F MCUs
Timer Overflow
In our examples, we check the timer value and, after
reaching a certain maximum, we clear it
However, what does it happen if we don’t modify TMR0?
At a certain point, the TMR0 reaches its maximum possible
value, which is 255 (0xff) at 8 bit and 65535 (0xffff) at 16 bit
The next increment will overflow TMR0, which thus goes
to zero
This event is signalled by the hardware by setting a
proper bit in a SFR
The bit is called T0IF and belongs to register INTCON
The bit set by the hardware and cleared by software
Corrado Santoro Using Timers in PIC18F MCUs
Timer Overflow
We can exploit the overflow event as follows.
Instead of clearing TMR0 and waiting for reaching our MAX
(15625 in the example), we can:
Set TMR0 to “65536 − MAX” (“65536 − 15625 = 49911” in
our example)
Wait for overflow by checking T0IF
Clear T0IF
Corrado Santoro Using Timers in PIC18F MCUs
Case-study 2: LED flashing with overflow
✞
int main(void)
{
char c0 = 0, c1 = 0; // why char? because they are 8 bits
TRISBbits.TRISB0 = 0; // output
TRISBbits.TRISB1 = 0; // output
T0CONbits.TMR0ON = 0; // stop the timer
T0CONbits.T08BIT = 0; // timer configured as 16-bit
T0CONbits.T0CS = 0; // use system clock
T0CONbits.PSA = 0; // use prescaler
T0CONbits.T0PS = 0b111; // prescaler 1:256 (’0b’ is a prefix for binary)
TMR0 = 49911; // initial timer value
INTCONbits.T0IF = 0; // clear the overflow bit initially
T0CONbits.TMR0ON = 1; // start the timer
for (;;) {
if (INTCONbits.T0IF == 1) { // overflow!
TMR0 = 49911; // reload timer
INTCONbits.T0IF = 0; // clear overflow
++c0; ++c1;
if (c0 == 2) { // flash led 0
LATBbits.LATB0 = !LATBbits.LATB0;
c0 = 0;
}
if (c1 == 3) { // flash led 1
LATBbits.LATB1 = !LATBbits.LATB1;
c1 = 0;
}
}
}
}
✡✝ ✆
Corrado Santoro Using Timers in PIC18F MCUs
Timer Overflow
Let’s consider the expression: “65536 − MAX”:
We notice that 65536, in 16-bit arithmetic, does not exist
and is equivalent to 0
therefore, “65536 − MAX = −MAX”
Corrado Santoro Using Timers in PIC18F MCUs
Case-study 2: LED flashing with overflow
✞
int main(void)
{
char c0 = 0, c1 = 0; // why char? because they are 8 bits
TRISBbits.TRISB0 = 0; // output
TRISBbits.TRISB1 = 0; // output
T0CONbits.TMR0ON = 0; // stop the timer
T0CONbits.T08BIT = 0; // timer configured as 16-bit
T0CONbits.T0CS = 0; // use system clock
T0CONbits.PSA = 0; // use prescaler
T0CONbits.T0PS = 0b111; // prescaler 1:256 (’0b’ is a prefix for binary)
TMR0 = -15625; // initial timer value
INTCONbits.T0IF = 0; // clear the overflow bit initially
T0CONbits.TMR0ON = 1; // start the timer
for (;;) {
if (INTCONbits.T0IF == 1) { // overflow!
TMR0 = -15625; // reload timer
INTCONbits.T0IF = 0; // clear overflow
++c0; ++c1;
if (c0 == 2) { // flash led 0
LATBbits.LATB0 = !LATBbits.LATB0;
c0 = 0;
}
if (c1 == 3) { // flash led 1
LATBbits.LATB1 = !LATBbits.LATB1;
c1 = 0;
}
}
}
}
✡✝ ✆
Corrado Santoro Using Timers in PIC18F MCUs
Comparing the techniques
Let’s compare (1)
✞
unsigned int t;
t = TMR0;
if (t >= 15625) { // equivalent of 250 ms
TMR0 = 0;
✡✝ ✆
to (2)
✞
if (INTCONbits.T0IF == 1) { // overflow!
TMR0 = -15625; // reload timer
INTCONbits.T0IF = 0; // clear overflow
✡✝ ✆
(1) uses a 16-bit comparison, (2) uses a single-bit
comparson → less code since the CPU is 8-bit
(2) uses polling but can be easily transformed into a
interrupt-based code since overflows can be
programmed to generate interrupts
Corrado Santoro Using Timers in PIC18F MCUs
Using Timers of Microchip PIC18F
Microcontrollers
Corrado Santoro
ARSLAB - Autonomous and Robotic Systems Laboratory
Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy
santoro@dmi.unict.it
L.A.P. 1 Course
Corrado Santoro Using Timers in PIC18F MCUs

More Related Content

PDF
Avr timers
PDF
1. FPGA architectures.pdf
PPTX
8051 Timers and Counters
PPT
Microprocessor
PPT
PDF
Programming with PIC microcontroller
PDF
Smart traffic light controller using verilog
PPT
8051 MICROCONTROLLER
Avr timers
1. FPGA architectures.pdf
8051 Timers and Counters
Microprocessor
Programming with PIC microcontroller
Smart traffic light controller using verilog
8051 MICROCONTROLLER

What's hot (20)

PPTX
2 Digit Object counter
PDF
SPI Protocol in LPC2148
PDF
Router 1X3 – RTL Design and Verification
PPT
PIC Microcontrollers.ppt
PPT
PIC timer programming
PPTX
8051 Microcontroller ppt
PPTX
8251 USART
PPTX
Discrete Input module block diagram and wiring in PLC
DOCX
Project report on the Digital clock using RTC and microcontroller 8051
PPTX
Lpc 1768 timers
PPTX
Basics of ATmega32
PPTX
Micro controller 8051 Interrupts
PPTX
8051 microcontroller features
PPTX
PPT on 8085 Microprocessor
PPTX
Four way traffic light conrol using Verilog
PPTX
Introduction to microprocessor
PPTX
PIC 16F877 micro controller by Gaurav raikar
PPT
PIC 16F877A by PARTHIBAN. S.
PPTX
I/O system in intel 80386 microcomputer architecture
PPT
Interfacing stepper motor
2 Digit Object counter
SPI Protocol in LPC2148
Router 1X3 – RTL Design and Verification
PIC Microcontrollers.ppt
PIC timer programming
8051 Microcontroller ppt
8251 USART
Discrete Input module block diagram and wiring in PLC
Project report on the Digital clock using RTC and microcontroller 8051
Lpc 1768 timers
Basics of ATmega32
Micro controller 8051 Interrupts
8051 microcontroller features
PPT on 8085 Microprocessor
Four way traffic light conrol using Verilog
Introduction to microprocessor
PIC 16F877 micro controller by Gaurav raikar
PIC 16F877A by PARTHIBAN. S.
I/O system in intel 80386 microcomputer architecture
Interfacing stepper motor
Ad

Similar to Using Timers in PIC18F Microcontrollers (20)

PPTX
PIC-Chapter_10.pptx
PPTX
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PPTX
Timers
PPT
lecture 12 counter_microcontroller2.ppt
PDF
timers.pdf
PDF
Using Timer2 in Microchip MCUs
PPTX
timer counter (1).pptx
PPT
8051 microcontroller timer summary presentation
PPT
Microcontroller Timer Counter Modules and applications
PDF
AVR_Course_Day7 timers counters and interrupt programming
PPTX
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
PPTX
pic microcontroller unit 5 detailed .pptx
PPT
8051e
PPTX
6-Interrupts Programming-27-03-2024.pptx
PPTX
5th unit embedded system and iot design timer and controller
PPT
UNIT-5.ppt
PPTX
8051 timer counter
PDF
Timers and counters of microcontroller 8051
PDF
8051 Timers / Counters
PPTX
5-Timer Mode 2 Programming-18-03-2024.pptx
PIC-Chapter_10.pptx
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
Timers
lecture 12 counter_microcontroller2.ppt
timers.pdf
Using Timer2 in Microchip MCUs
timer counter (1).pptx
8051 microcontroller timer summary presentation
Microcontroller Timer Counter Modules and applications
AVR_Course_Day7 timers counters and interrupt programming
8051 MICROCONTROLLER TIMER AND ITS APPLICATIONS
pic microcontroller unit 5 detailed .pptx
8051e
6-Interrupts Programming-27-03-2024.pptx
5th unit embedded system and iot design timer and controller
UNIT-5.ppt
8051 timer counter
Timers and counters of microcontroller 8051
8051 Timers / Counters
5-Timer Mode 2 Programming-18-03-2024.pptx
Ad

More from Corrado Santoro (20)

PDF
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
PDF
The I2C Interface
PDF
Using Timer1 and CCP
PDF
Pulse Width Modulation Signal Generation with MCUs
PDF
Handling Asynchronous Events in MCUs
PDF
Presentation @ Miniscuola WOA 2015
PDF
Presentation @ WOA 2015
PDF
Using Ready-for-PIC and SDR Libraries
PDF
Introduction to Erlang
PDF
Introduction to shell scripting
PDF
Pillole di C++
PDF
Analog to digital converter
PDF
Exercises with timers and UART
PDF
UART MCU
PDF
Handling Interrupts in Microchip MCUs
PDF
Programming the Digital I/O Interface of a PIC microcontroller
PDF
Introduction to microcontrollers
PDF
How does a Quadrotor fly? A journey from physics, mathematics, control system...
PDF
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
The I2C Interface
Using Timer1 and CCP
Pulse Width Modulation Signal Generation with MCUs
Handling Asynchronous Events in MCUs
Presentation @ Miniscuola WOA 2015
Presentation @ WOA 2015
Using Ready-for-PIC and SDR Libraries
Introduction to Erlang
Introduction to shell scripting
Pillole di C++
Analog to digital converter
Exercises with timers and UART
UART MCU
Handling Interrupts in Microchip MCUs
Programming the Digital I/O Interface of a PIC microcontroller
Introduction to microcontrollers
How does a Quadrotor fly? A journey from physics, mathematics, control system...
Integrating Cloud Services in Behaviour Programming for Autonomous Robots

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Cell Types and Its function , kingdom of life
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Computing-Curriculum for Schools in Ghana
PDF
Insiders guide to clinical Medicine.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Classroom Observation Tools for Teachers
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Basic Mud Logging Guide for educational purpose
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O5-L3 Freight Transport Ops (International) V1.pdf
RMMM.pdf make it easy to upload and study
Cell Types and Its function , kingdom of life
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Computing-Curriculum for Schools in Ghana
Insiders guide to clinical Medicine.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Classroom Observation Tools for Teachers
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
O7-L3 Supply Chain Operations - ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Complications of Minimal Access Surgery at WLH
Basic Mud Logging Guide for educational purpose
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
human mycosis Human fungal infections are called human mycosis..pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

Using Timers in PIC18F Microcontrollers

  • 1. Using Timers of Microchip PIC18F Microcontrollers Corrado Santoro ARSLAB - Autonomous and Robotic Systems Laboratory Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy santoro@dmi.unict.it L.A.P. 1 Course Corrado Santoro Using Timers in PIC18F MCUs
  • 2. What is a “Timer”? It is a circuit to let a software have the “knowledge of flow of time” It is a composed of: A clock source; usually the system clock or an external signal; A programmable frequency divisor, called prescaler, to divide clock source frequency, if needed; Some SFRs which hold a 8-, 16- or 32-bit value that is incremented in hardware using the clock source. Some SFRs which give some state information, e.g overflow (zero crossing). PIC18F family has 7 timers, called TIMER0, TIMER1, ..., TIMER5, TIMER6 Each timer has different characteristics and may be used together with other peripherals. Corrado Santoro Using Timers in PIC18F MCUs
  • 3. The TIMER0 of PIC18 TIMER0 is a 8/16 bit timer/counter (figure shows the 8bit mode); TMR0L is the SFR containing the value that is incremented; All the parts to the left are the clock source circuits. T0CON (Timer 0 Control) register is used to program the timer, and includes the bits shown in figure (T0CS, PSA, T0PS, etc.) Corrado Santoro Using Timers in PIC18F MCUs
  • 4. The 16-bit version of TIMER0 In 16-bit mode, two SFR are used TMR0L and TMR0H; In write operations, TMR0H must be written before TMR0L; In read operations, TMR0L must be read before TMR0H; However, XC8 offers a single 16-bit variable TMR0 which includes both low and high part of TMR0. Corrado Santoro Using Timers in PIC18F MCUs
  • 5. The 16-bit version of TIMER0 The T0CON (Timer 0 Control) SFR includes all the bits which control TIMER0 functioning. Corrado Santoro Using Timers in PIC18F MCUs
  • 6. TIMER0: Selecting clock source Clock source can be internal or external and is controlled by bit T0CS: T0CS = 0; → clock source is internal and is taken from Fosc/4. T0CS = 1; → clock source is external and is taken from T0CKI pin; in this case T0SE controls the edge of the signal which triggers increment. Corrado Santoro Using Timers in PIC18F MCUs
  • 7. TIMER0: Dividing clock frequency In some cases, the clock coming from the oscillator could be too fast for our applications: we can lower it by using the frequency prescaler. The prescaler is a circuit which divides the signal frequency by 2, 4, 8, 16, ..., 256. The prescaler is activated by bit PSA: PSA = 0; → prescaler is selected, frequency division is controlled by bits T0PS. PSA = 1; → prescaler is not selected. Corrado Santoro Using Timers in PIC18F MCUs
  • 8. TIMER0: Dividing clock frequency When the prescaler is activated (PSA = 0), division is performed as: T0PS = 111, division 1:256 T0PS = 110, division 1:128 T0PS = 101, division 1:64 .... T0PS = 000, division 1:2 Corrado Santoro Using Timers in PIC18F MCUs
  • 9. TIMER0: controlling depth and on/off Finally, T0CON includes these other two bits: TMR0ON, turns on/off the timer; T08BIT, selects 8 (value “1”) or 16 (value “0”) bit mode. Corrado Santoro Using Timers in PIC18F MCUs
  • 10. A case-study: a timer to flash a LED We want to use the system clock, T0CS = 0; In our board, we have FOSC = 64MHz, therefore the basic frequency is FOSC/4 = 16MHz, the P = 62.5ns; Let’s use the prescaler and divide the frequency by 256, so PSA = 0; T0PS = 0b111; The timer increments using a period P = 62.5ns · 256 = 16µs. Corrado Santoro Using Timers in PIC18F MCUs
  • 11. A case-study: a timer to flash a LED ... the timer increments using a period P = 62.5ns · 256 = 16µs. Let us suppose we want a period of half a second 500ms Therefore 500·10−3 16·10−6 = 31250 A delay of 500ms implies 31250 counts Corrado Santoro Using Timers in PIC18F MCUs
  • 12. A case-study: a timer to flash a LED ✞ int main(void) { TRISBbits.TRISB0 = 0; // output T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use system clock T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 (’0b’ is a prefix for binary) TMR0 = 0; // clear timer value T0CONbits.TMR0ON = 1; // start the timer for (;;) { unsigned int t; t = TMR0; if (t >= 31250) { // equivalent of 500 ms TMR0 = 0; LATBbits.LATB0 = !LATBbits.LATB0; } } } ✡✝ ✆ Corrado Santoro Using Timers in PIC18F MCUs
  • 13. Case-study 2: more LEDs flashing Let us suppose we want to: flash led in RB0 at a period of 500 ms flash led in RB1 at a period of 750 ms Do we need two timers?? NO! 1 compute the greatest common divisor, which is 250ms 2 use it as your “timer period” 3 toggle RB0 after two periods 4 toggle RB1 after three periods Corrado Santoro Using Timers in PIC18F MCUs
  • 14. Case-study 2: more LEDs flashing Using the same set-up of the previous example, since our period is 250ms we have 250·10−3 16·10−6 = 15625 A delay of 250ms implies 15625 counts Corrado Santoro Using Timers in PIC18F MCUs
  • 15. Case-study 2: more LEDs flashing ✞ int main(void) { char c0 = 0, c1 = 0; // why char? because they are 8 bits TRISBbits.TRISB0 = 0; // output TRISBbits.TRISB1 = 0; // output T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use system clock T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 (’0b’ is a prefix for binary) TMR0 = 0; // clear timer value T0CONbits.TMR0ON = 1; // start the timer for (;;) { unsigned int t; t = TMR0; if (t >= 15625) { // equivalent of 250 ms TMR0 = 0; ++c0; ++c1; if (c0 == 2) { // flash led 0 LATBbits.LATB0 = !LATBbits.LATB0; c0 = 0; } if (c1 == 3) { // flash led 1 LATBbits.LATB1 = !LATBbits.LATB1; c1 = 0; } } } } ✡✝ ✆ Corrado Santoro Using Timers in PIC18F MCUs
  • 16. Timer Overflow In our examples, we check the timer value and, after reaching a certain maximum, we clear it However, what does it happen if we don’t modify TMR0? At a certain point, the TMR0 reaches its maximum possible value, which is 255 (0xff) at 8 bit and 65535 (0xffff) at 16 bit The next increment will overflow TMR0, which thus goes to zero This event is signalled by the hardware by setting a proper bit in a SFR The bit is called T0IF and belongs to register INTCON The bit set by the hardware and cleared by software Corrado Santoro Using Timers in PIC18F MCUs
  • 17. Timer Overflow We can exploit the overflow event as follows. Instead of clearing TMR0 and waiting for reaching our MAX (15625 in the example), we can: Set TMR0 to “65536 − MAX” (“65536 − 15625 = 49911” in our example) Wait for overflow by checking T0IF Clear T0IF Corrado Santoro Using Timers in PIC18F MCUs
  • 18. Case-study 2: LED flashing with overflow ✞ int main(void) { char c0 = 0, c1 = 0; // why char? because they are 8 bits TRISBbits.TRISB0 = 0; // output TRISBbits.TRISB1 = 0; // output T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use system clock T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 (’0b’ is a prefix for binary) TMR0 = 49911; // initial timer value INTCONbits.T0IF = 0; // clear the overflow bit initially T0CONbits.TMR0ON = 1; // start the timer for (;;) { if (INTCONbits.T0IF == 1) { // overflow! TMR0 = 49911; // reload timer INTCONbits.T0IF = 0; // clear overflow ++c0; ++c1; if (c0 == 2) { // flash led 0 LATBbits.LATB0 = !LATBbits.LATB0; c0 = 0; } if (c1 == 3) { // flash led 1 LATBbits.LATB1 = !LATBbits.LATB1; c1 = 0; } } } } ✡✝ ✆ Corrado Santoro Using Timers in PIC18F MCUs
  • 19. Timer Overflow Let’s consider the expression: “65536 − MAX”: We notice that 65536, in 16-bit arithmetic, does not exist and is equivalent to 0 therefore, “65536 − MAX = −MAX” Corrado Santoro Using Timers in PIC18F MCUs
  • 20. Case-study 2: LED flashing with overflow ✞ int main(void) { char c0 = 0, c1 = 0; // why char? because they are 8 bits TRISBbits.TRISB0 = 0; // output TRISBbits.TRISB1 = 0; // output T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use system clock T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 (’0b’ is a prefix for binary) TMR0 = -15625; // initial timer value INTCONbits.T0IF = 0; // clear the overflow bit initially T0CONbits.TMR0ON = 1; // start the timer for (;;) { if (INTCONbits.T0IF == 1) { // overflow! TMR0 = -15625; // reload timer INTCONbits.T0IF = 0; // clear overflow ++c0; ++c1; if (c0 == 2) { // flash led 0 LATBbits.LATB0 = !LATBbits.LATB0; c0 = 0; } if (c1 == 3) { // flash led 1 LATBbits.LATB1 = !LATBbits.LATB1; c1 = 0; } } } } ✡✝ ✆ Corrado Santoro Using Timers in PIC18F MCUs
  • 21. Comparing the techniques Let’s compare (1) ✞ unsigned int t; t = TMR0; if (t >= 15625) { // equivalent of 250 ms TMR0 = 0; ✡✝ ✆ to (2) ✞ if (INTCONbits.T0IF == 1) { // overflow! TMR0 = -15625; // reload timer INTCONbits.T0IF = 0; // clear overflow ✡✝ ✆ (1) uses a 16-bit comparison, (2) uses a single-bit comparson → less code since the CPU is 8-bit (2) uses polling but can be easily transformed into a interrupt-based code since overflows can be programmed to generate interrupts Corrado Santoro Using Timers in PIC18F MCUs
  • 22. Using Timers of Microchip PIC18F Microcontrollers Corrado Santoro ARSLAB - Autonomous and Robotic Systems Laboratory Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy santoro@dmi.unict.it L.A.P. 1 Course Corrado Santoro Using Timers in PIC18F MCUs