Embedded Topik 3
Embedded Topik 3
TOPIC 3
PIC TIMER PROGRAMMING IN C
LEARNING OUTCOME
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
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;
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
#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
count the pulse and display the TMR0H and TMR0L register
on PORTD and PORTB. Start the count at 0H