100% found this document useful (1 vote)
191 views

CHP 3 - Pic Timer Programming in C

The document discusses timer programming in PIC microcontrollers. It describes the different timer modules in PIC18F4550 including Timer 0, 1, 2, and 3. It explains how to use timers as counters or timers, set the clock source, and use prescalers to divide the clock. The key registers for configuring each timer are also outlined, such as TMR0L, TMR0H, T0CON, and prescaler bits for Timer 0. Delays can be calculated using the timer overflow and prescaler settings along with the system clock.

Uploaded by

Tajuddin Razali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
191 views

CHP 3 - Pic Timer Programming in C

The document discusses timer programming in PIC microcontrollers. It describes the different timer modules in PIC18F4550 including Timer 0, 1, 2, and 3. It explains how to use timers as counters or timers, set the clock source, and use prescalers to divide the clock. The key registers for configuring each timer are also outlined, such as TMR0L, TMR0H, T0CON, and prescaler bits for Timer 0. Delays can be calculated using the timer overflow and prescaler settings along with the system clock.

Uploaded by

Tajuddin Razali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

[Type here]

DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]

CHP 3

PIC TIMER PROGRAMMING IN C

1
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]

3.1 Timer Registers


Timer and Counter
Basically, a timer is a clock that controls an event sequence at a fixed amount of time. Timers are
used for the precise delay generation and also used to trigger an activity before and after a
predetermined time and to measure the time elapsed between two successive events.

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.

Figure 1 : Timer Block

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).

How to Use Timer with PIC18F4550

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

The Timer0 module has the following features:

• Timer0 can work as both 8-bit and 16-bit modes timer/counter


• Software programmable Prescaler
• Select able clock source (internal or external)
• Interrupt on overflow

3
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]

Timer 1

• Timer1 can work as 16-bit timer or counter


• Readable and writable 8-bit registers (TMR1H and TMR1L)
• Selectable clock source (internal or external)
• Alternate clock source can be provided at Timer1 oscillator pins (T1OSO & T1OSI)
• Interrupt on overflow
• Timer1 can be multiplexed with other peripherals like ADC and generates special
event triggering for CCP (Capture, Compare and PWM) events.

Timer 2

• 8-bit Timer and Period registers (TMR2 and PR2, respectively)


• Software programmable prescaler (1:1, 1:4 and 1:16)
• Software programmable postscaler (1:1 – 1:16)
• Interrupt on TMR2 to PR2 match
• Optional use as the shift clock for the MSSP (Master Synchronous Serial Port)
module

Timer 3

The Timer3 module timer/counter incorporates these features:

• Timer3 can work as 16-bit timer or counter


• Readable and writable 8-bit registers (TMR3H and TMR3L)
• Selectable clock source (internal or external)
• Alternate clock source can be provided at Timer1 oscillator pins (T1OSO & T1OSI)
• Interrupt on overflow
• Timer3 can be multiplexed with other peripherals like ADC and generates special
event triggering for CCP (Capture, Compare and PWM) events.

4
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]

Clock source of pic microcontroller timers

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.

(I) Internal Clock Mode:

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.

(II) External Clock Mode:

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.

DELAY CALCULATION of timers

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).

Clock source frequency of crystal:

Fosc=20 MHz= 20000000 Hz

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.

FCPU=20 MHz/4 =5 MHz

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.

Prescaler Period (if Prescaler = 1:256)

Ftimer= 5 MHz/256 =19531.25Hz

5
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]

Single overflow of Timer0 will give this delay:

Ttimer = 1/19531.25 = 0.0000512 sec = 51.2 µs

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.

No. of count for 1 sec Delay = 1 sec/51.2 µs = 19531.25 = 4C4B H

The value to be filled in timer’s 16 bit register = FFFF – 4C4B= B3B4 H

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.

3.2 Register used for timers in PIC

Implementing Timer0 in PIC18F4550

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

Using Prescalar in Timer


For using prescaler, clear the PSA bit and assign the corresponding value in T0PS2, T0PS1 and T0PS0
registers.

For example, to assign a prescaler of value 2

7
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]

Implementing Counter Using Timer0 Module in PIC18F4550


Set the T0CS bit in T0CON register to select the Counter mode. Timer0 increments based on the pulse on
pin RA4/T0CKI. The T0SE bit in T0CON register determines the rising edge or falling edge. Setting this bit
will make it a rising edge and clearing it will select a falling edge. The procedure of delay calculation is
described in the chapter Timer Delay Implementation.

Register Configuration

TIMERs REGISTERS CONFIGURATION


Every Timer has certain registers associated, which must be configured for desired
operations.The Timer register can have the following bit length:
8 bit timers – These can count between 0-255
16 bit timers – These can count between 0-65536
32 bit timers – These can count between 0-4294967296

8
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]

The registers of Timer0 have been explained below.


(i) Timer 0 Control Register

TMR0ON: Timer0 on/off bit


This bit is set to high to enable the Timer0.
1 = Enable the Timer0
0 = to stop Timer0

T08BIT: 8/16 bit mode selection bit


This bit selects the Timer mode.
1 = Timer0 is configured as an 8-bit timer/counter.
0 = Timer0 is configured as a 16-bit timer/counter.

TMR0CS: Timer0 Clock Source set


Timer mode is selected by clearing the TMR0CS bit of the Control register.
1 = T0 Clk
0 = Fosc/4

TMR0SE: Timer0 source Edge select


The rising or falling transition of the incrementing edge for either input source is determined by
the TMR0SE bit in the Control register.
1 = Increment TMR0 on high to low transition
0 = Increment TMR0 on low to high transition

9
[Type here]
DEC40053 - PIC Timer Programming In C [CLO1,PLO4,CLS1,DK3]

PSA: Prescaler Assignment


The prescaler is enabled by clearing the PSA bit of the Control register.
1 = Prescaler not Assigned to TMR0
0 = Prescaler Assigned to TMR0

PS2, PS2, PS0: Prescaler Rate Select bits:


There are eight prescaler options for the Timer0 module ranging from 1:2 to 1:256. The prescaler
values are selectable via the PS 2:0 bits of the Control register. In order to have a 1:1 prescaler
value for the Timer0 module, the prescaler must be disabled by setting the PSA bit of the Control
register.

(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.

3.3 C Program for Timers in PIC


WORKING of pic microcontroller timers

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]

PROTEUS SIMULATIONS of timers

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]

CODE to generate delay with timers

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

while (INTCON.TMR0IF==0); // Wait until TMR0IF gets flagged


T0CON.TMR0ON=0; // Timer0 Off
INTCON.TMR0IF=0; // Clear Timer0 interrupt flag
PORTB=(PORTB<<1)|(PORTB>>7); // Circular right shift at PortB
}
}

12

You might also like