CHP 3 - Pic Timer Programming in C
CHP 3 - Pic Timer Programming in C
CHP 3
1
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
Timers in Microcontroller
The timer inside a microcontroller is a free running binary counter. The counter increments for
each pulse applied to it. The counter counts continuously from 0 to (2^n)-1 where n is the
number of bits. In PIC18F4550, there are 8-bit and 16-bit timers. The timer takes the internal
clock as a reference clock, while the counter counts external clocks or pulses applied through
port pins. So basically timer is a counter with an internal clock.
The main advantage of timers and counters is that it works independent of microcontroller CPU
and the timer values can be read whenever needs. Basically, a standard microcontroller consists
of 1 or more hardware timer modules of different bit lengths.
Basically, a standard microcontroller consists of 1 or more hardware timer modules of different bit
lengths.
• 8-bit timers- capable of counting 0-255
• 16-bit timer – capable of counting 0-65535
The initial values of the timer register can be set by the user and can be used to generate required
counts. Below is a functional diagram of a timer module.
2
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
Counters
Timers are also called counters because they are used to count external events. Timers are
mainly used for counting or measuring external events.
For example in the case of a visitor counter, the sensor placed for detecting the presence of a
person goes high when someone crosses the door. The output of the sensor is connected to the
Timer Clock Input Pin of the microcontroller. The timer register inside the microcontroller
increments each time when a person crosses the door. The value can be later read by the CPU.
Prescalar
Prescalar is a configurable clock-divider circuit. It can be used to divide the clock frequency input
to the timer module. For example, if the instruction clock is 5MHz and we use a prescaler of 2 to
divide it which effectively make the clock 2.5MHz. So each counting time will increase from 0.2
µs to 0.4 µs.
In PIC microcontroller, timer module provides 256, 128, 64, 32, 16, 8, 4, 2 and 1. 1 is actually
prescaler bypassed.
Overflow
Overflow means the counter reached its maximum output and roll over to zero. The
microcontroller has an overflow flag to indicate the overflow of the counter and generates
overflow interrupts. This gives an additional option to count the number of times the counter
overflowed and it extends the range of the counter. For example, if we need to count up to 512,
we can use the 8-bit timer and overflow flag. After two overflows, the count must be 512
(256+256).
PIC18F4550 consists of four hardware timers namely Timer 0, Timer 1, Timer 2, Timer 3.
Timer 2 is an 8-bit timer and all others are 16-bit timers.
Timer 0
3
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
Timer 1
Timer 2
Timer 3
4
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
The simplest is the TIMER0. For Timer configurations, it is important to know that how time delay
is calculated by the timer. Firstly the Timer clock source is set.
The clock can be internal or external. In the internal clock mode Timer0 operates as timer
and uses the internal(FCPU) clock with or without pre-scalar. Prescaler is an integer value which
divides the CPU clock to give Timer Clock i.e Timer Clock = FCPU/pre-scalar. When the Pre-scalar
is set to one or bypassed then the timer runs on the same clock as the CPU is running.
In this mode Timer0 operates as counter and counts on every rising or falling edge of the
clock connected to the Timer’s clock pin.
Now we calculate the time delay of 1 sec using 20MHz crystal oscillator with PIC microcontroller.
PIC 18F has ability for external as well as internal clock source but we are using Timer0 with
internal clock (Timer mode).
The PIC internally divides FOSC by 4 to get FCPU. Based on this we have clock cycle and instruction
cycle. The clock cycle is simply 1/FOSC while instruction cycle is 1/FCPU.
PIC18 has the ability to generate interrupt on overflow. It means that a bit called Timer0 Interrupt
Flag (TMR0IF) is set when TMR0 makes transition from 255 to 0.
5
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
This means that when timer runs, it will take 51 µs to increment its value at every count. Now
we have to calculate the value to be filled in Timer register to generate 1 sec delay.
These values are filled in the Timer register and it rolls over up to FFFF. The values are reloaded
again to start timer for same delay.
The Functioning of the timer0 can be understood from this block diagram.
The Timer 0 stores the value TMR0 registers, TMR0H is the high byte and TMR0L is the lower
byte. The higher byte will be useful when using 16-bit mode. Timer0 is worked as timer and
counter. In timer mode, the module increments on every clock by default, unless a prescaler
value is written into TMR0 register. Clear the T0CS bit(5th bit) of T0CON register to select the
timer mode.
6
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
Register Configuration
7
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
Register Configuration
8
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
9
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
(ii) TMR0H
(iii) TMR0L
Both registers are separately accessible thus Timer0 can work in both 8-bit and 16-bit
modes. In these registers, pre-calculated value for delay is filled.
Timer0 can operate as a timer or as a counter. When the clock source is the instruction
cycle clock, it operates as a timer and when the clock source is the T0CKI pin, it operates
as a counter. When PIC18f452 reads the TMR0L register, the upper half of Timer0 is
latched into the TMR0H register. This makes sure that the PIC18 always reads a 16-bit
value that its upper byteand lower byte belong to the same time.
10
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
For Programming:
• Firstly through T0CON register, we will select the Pre-scaler, Clock option and Mode of
Timer0.
• Then we fill the higher byte of Timer value in TMR0H and then fill lower byte value
in TMR0L
• Now set the TMR0ON bit to start the timer.Wait until the TMR0IF flag gets high
• As TMR0IF gets high, we set it to zero and stop the timer by clearing the TMR0ON bit.To
start the Timer0 again repeat this process of placing higher byte and lower byte with help
of while loop.
The time delay has been established here by glowing a set of 8 LEDs one by one with a delay of
1 sec. The code is also given below.
11
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]
void main()
{
TRISB=0; // Configure PortB as output Port.
PORTB=0x01; // Turn on LED on PORT B pin0
T0CON=0x07; // Prescaler= 1:256, 16-bit mode, Internal Clock
while(1)
{ // Values calculated for 1 second delay with 20MHz crystal
TMR0H=0xB3; // Placing Lower byte in TMR0L
TMR0L=0xB4; // Placing Lower byte in TMR0L
T0CON.TMR0ON=1; // Timer0 On
12