0% found this document useful (0 votes)
6 views46 pages

Embedded Topik 3

The document provides an overview of PIC timer programming in C, focusing on the Timer0 register and its associated control register T0CON. It explains the functionality of timers in generating delays and counting events, along with programming examples for both timer and counter modes. Additionally, it covers timer delay calculations, prescaler usage, and practical coding examples for generating specific frequencies and toggling outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views46 pages

Embedded Topik 3

The document provides an overview of PIC timer programming in C, focusing on the Timer0 register and its associated control register T0CON. It explains the functionality of timers in generating delays and counting events, along with programming examples for both timer and counter modes. Additionally, it covers timer delay calculations, prescaler usage, and practical coding examples for generating specific frequencies and toggling outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

DEC5052

EMBEDDED SYSTEM APPLICATION

TOPIC 3
PIC TIMER PROGRAMMING IN C
LEARNING OUTCOME

i. Know Timer register


ii. Understand the register used for timer in
the PIC
iii. Apply C program for timer in PIC
TIMER REGISTER
TIMER OF THE PIC AND THEIR
ASSOCIATED REGISTER
 PIC18 microcontroller has two to five timers depending on the
family number
oTMR0 (Timer0)
oTMR1 (Timer1)
oTMR2 (Timer2)
TIMER OF THE PIC AND THEIR
ASSOCIATED REGISTER
 FUNCTIONS OF THE TIMER

Generate a time delay


As a counter to count events happening outside the microcontroller

Timers are 16-bit wide


• Can be accessed as two separate reg. (TMRxL & TMRxH)
• Each timer has T0CON (timer Control) reg.
TIMER0 REGISTER AND
PROGRAMMING
 Timer0 can be used as 8 bit or 16 bit timer
 The 16 bit register of Timer is accessed as low byte and high byte

TMR0H (Timer0 high byte) TMR0L (Timer0 low byte)


TIMER0 REGISTER AND
PROGRAMMING
 Example:
TMR0H = 0X35; // load 35H into TH0 register
TMR0L = 0X00; // load 00H into THL register
T0CON (TIMER0 CONTROL)
REGISTER
 Each timer has a control register called TCON, to set various timer
operation modes.
 ToCON is 8-bit register used to control of Timer0.
 The bits for T0CON are shown below
T0CON (TIMER0 CONTROL) REGISTER
T0CS (TIMER0 CLOCK SOURCE)
 To decide whether the clock source is internal (Fosc/4) or external
 T0SC = 0
Fosc/4 is used as clock source and in this case timer used as delay
generation
 T0SC = 1
Clock source is external which comes from RA4/T0CKI and in this case
timer used as counter
T1 T2 T3 T4

4T
TMR0IF (TIMER0 INTERRUPT
FLAG)
 TMR0IF bit is part of the INTCON (interrupt control) register
 When the timer reaches its maximum value of FFFFH, it rolls over to
0000 and TMR0IF is set to 1
TIMER : to generate
time delay
16 BIT TIMER PROGRAMMING
STEPS TO PROGRAM TIMER0 IN
16 BIT MODE
STEPS TO PROGRAM TIMER0 IN
16 BIT MODE
Example Timer 0 function

void T0Delay ()
{
T0CON = 0X08; 0b00001000 // Timer0, 16 bit mode, no prescaler
TMR0H = 0XFF; 0b11111111 //load TH0
TMR0L = 0XF2; 0b11110010 //load TL0
T0CONbits.TMR0ON = 1; //turn on timer

while (INTCONbits.TMR0IF == 0); //wait for TF0 to roll over


T0CONbits.TMR0ON = 0; //turn off timer
INTCONbits.TMR0IF = 0; //clear TF0
}
TIMER TMR0
 The timer TMR0 module is an 8-bit timer/counter with the
following features:
 8-bit timer/counter
 Readable and writable
 8-bit software programmable prescaler
 Internal (4 Mhz) or external clock select
 Interrupt on overflow from FFh to 00h
 Edge select (rising or falling) for external clock
DISTINGUISH PIC TIMER AS A
TIMER AND COUNTER
 CLOCK SOURCES OF THE TIMER

Internal Clock Pulse - (TIMER)


 1/4th of the frequency of the crystal oscillator on the OSC1 and
OSC2 pins (Fosc/4) is fed into the timer
 Usually used to generate a time delay

External Clock Pulse - (COUNTER)


 By choosing the external clock option, we feed pulses through
one of the PIC’s pins.
 Usually used as a counter
TIMER DELAY CALCULATION

TIMER DELAY CALCULATION

number of counts (hex) = (FFFF – YYXX) + 1

number of counts (decimal) = 65536 – NNNN

Delay = number of counts x timer period x prescaler

Delay
Example 1 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 (square wave of
50% duty cycle )

#include <p18f4580.h>
void T0Delay ()
{
void T0Delay (void); T0CON = 0X08;
TMR0H = 0XFF;
TMR0L = 0XF2;
void main (void)
T0CONbits.TMR0ON = 1;
{
TRISB=0; while (INTCONbits.TMR0IF == 0);
T0CONbits.TMR0ON = 0;
INTCONbits.TMR0IF = 0;
while (1)
}
{
PORTB=0x55;
T0Delay ();
PORTB=0xAA;
Example 1 Cont. 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 (square wave of
50% duty cycle )

void T0Delay ()
{
T0CON = 0X08;
TMR0H = 0XFF;
TMR0L = 0XF2;
T0CONbits.TMR0ON = 1;

while (INTCONbits.TMR0IF == 0);


T0CONbits.TMR0ON = 0;
INTCONbits.TMR0IF = 0;
}
Write a C18 program to toggle only the PORTC.3 bits
continuously with some delay. Use Timer0, 16 bit mode, and
Example 2

no prescaler options to generate the delay (square wave of


50% duty cycle ). Set the register TMR0H = 35H and TMR0L =
00H.
#include <p18f4580.h>

void T0Delay ()
void T0Delay (void); {
#define LED PORTCbits.RC3 T0CON = 0X08;
TMR0H = 0X35;
TMR0L = 0X00;
void main (void)
T0CONbits.TMR0ON = 1;
{
TRISB=0; while (INTCONbits.TMR0IF == 0);
T0CONbits.TMR0ON = 0;
INTCONbits.TMR0IF = 0;
while (1)
}
{
LED=1;
T0Delay ();
LED=0;
Example 2 Cont. In example above, calculate the amount of time delay
generated by the timer. Assume that XTAL = 10Mhz
FINDING VALUES TO BE LOADED INTO THE
TIMER
Assume that XTAL = 10Mhz, write a program to generate a
square wave with a period of 10 ms on pin PORTB.3
Example 3

#include <p18f4580.h>
#define LED PORTBbits.RB3 void T0Delay ()
void T0Delay (void); {
T0CON = 0X08;
TMR0H = 0XCF;
void main (void)
TMR0L = 0X2C;
{ T0CONbits.TMR0ON = 1;
TRISB=0;
while (1) while (INTCONbits.TMR0IF == 0);
T0CONbits.TMR0ON = 0;
{
INTCONbits.TMR0IF = 0;
LED=1; }
T0Delay ();
LED=0;
Assuming that XTAL = 10Mhz, modify the program in example
3 to generate a square wave of 2khz frequency on pin
Example 4

PORTB.3
PRESCALER AND GENERATING A
LARGE TIME DELAY
 Size time delay depend on two factors (a) the crystal frequency and
(b) the timer 16 bits register – TMR0H & TMR0L
 So the largest time delay is achieved by making both TMR0H &
TMR0L zero.
 We can use the prescaler option in T0CON register to increase the
delay by reducing the period.
 The prescaler option of T0CON allow us to devide the instruction
clock by a factor of 2 to 256 as was shown in figure above
T0CON (Timer0 control) register
Prescaler and generating a
large time delay
Prescaler and generating a
large time delay
Prescaler and generating a
large time delay
Write a C18 program generate a frequency of 50Hz frequency
on pin PORTB.7. Use Timer0, 16 bit mode, with prescaler =
Example 5

128. Assume XTAL = 128.


Write a C18 program generate a frequency of 50Hz frequency
on pin PORTB.7. Use Timer0, 16 bit mode, with prescaler =
Example 5

128. Assume XTAL = 10Mhz.

#include <p18f4580.h>
#define LED PORTBbits.RB7
void T0Delay (void); void T0Delay ()
{
void main (void) T0CON = 0X06;
{ TMR0H = 0XFF;
TRISB=0; TMR0L = 0X3D;
while (1) T0CONbits.TMR0ON = 1;
{
LED=1; while (INTCONbits.TMR0IF == 0);
T0Delay (); T0CONbits.TMR0ON = 0;
LED=0; INTCONbits.TMR0IF = 0;
T0Delay (); }
}
}
Write a C18 program to toggle the PORTB.4 bit continuosly
every 50ms. Use Timer 0, 16 bit mode, the 1:4 precaler to
Example 6

create time delay. Assume XTAL = 10MHz


Example 6 (cont.) Write a C18 program to toggle the PORTB.4 bit continuosly
every 50ms. Use Timer 0, 16 bit mode, the 1:4 precaler to
create time delay. Assume XTAL = 10MHz
TIMER : COUNTER
COUNTER PROGRAMMING
 Last section, you learned timer of PIC18 to generate time delay
 These timer can also be used as counter to counts events
happening outside the PIC18.
 Remember
 Timer as time delay, pic 18’s crystal is used as source frequency
 Timer as counter, pulse outside of PIC18 that increment TH, TL
register
T0CS BIT IN T0CON REGISTER
 T0SC 0= // timer get pulse get pulse from the
crystal oscillator connected to the OSC1 and
OSC2 pins (Fosc/4)
 T0SC = 1 // timer is used as counter and gets
its pulse from outside the PIC18 (fed from pin
RA4). The pin called T0CK1 (Timer0 clock input)
T0CS BIT IN T0CON REGISTER
T0CS BIT IN T0CON REGISTER
Example 7 Assume that 1-Hz external clock is being fed into pin T0CK1
(RA4). Write a C18 program for Counter0 in 8 bit mode to
count up and display the state TMR0L count on PortB. Start
the count at 0H
Example 7 Cont. Assume that 1-Hz external clock is being fed into pin T0CK1
(RA4). Write a C18 program for Counter0 in 8 bit mode to
count up and display the state TMR0L count on PortB. Start
the count at 0H
Assume that 1-Hz external clock is being fed into pin T0CK1
(RA4). Write a C18 program for Counter0 in 16 bit mode to
Example 8

count the pulse and display the TMR0H and TMR0L register
on PORTD and PORTB. Start the count at 0H

You might also like