0% found this document useful (0 votes)
10 views24 pages

Lecture 8

The document provides an overview of the timers in PIC18 microcontrollers, detailing their registers, modes, and programming methods for generating time delays. It explains the operation of Timer0 and Timer1, including their configurations and examples of assembly and C programs for delay generation. The document emphasizes the use of internal and external clock sources and the role of prescalers in extending delay durations.

Uploaded by

vinod SALUNKHE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views24 pages

Lecture 8

The document provides an overview of the timers in PIC18 microcontrollers, detailing their registers, modes, and programming methods for generating time delays. It explains the operation of Timer0 and Timer1, including their configurations and examples of assembly and C programs for delay generation. The document emphasizes the use of internal and external clock sources and the role of prescalers in extending delay durations.

Uploaded by

vinod SALUNKHE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

PIC18 Supporting Devices

Mr. Vinod Salunkhe


Objective
• List the Timers of PIC18 and their associated registers
• Describe the various modes of the PIC18 timers
• Program the PIC18 timers in Assembly to generate time delays
Outlines
• Programming timers 0
• Programming timers 1
Introduction
• What is MICROCONTROLLER??????
• It is device which has its own:-
CPU (microprocessor)
RAM
ROM
I/O ports
Timer
ADC and other peripherals
• PIC18 has two to five timers
• Depending on the family number
• These timers can be used as
• Timers to generate a time delay
• Counters to count events happening outside the uC
Section 1.1: Programming timers
0 and 1
• Every timer needs a clock pulse to tick
• Clock source can be
• Internal  1/4th of the frequency of the crystal oscillator on OSC1 and
OSC2 pins (Fosc/4) is fed into timer
• External: pulses are fed through one of the PIC18’s pins  Counter
• Timers are 16-bit wide
• Can be accessed as two separate reg. (TMRxL & TMRxH)
• Each timer has TCON (timer Control) reg.
Timer0 registers and
programming
• TMR0L & TMR0H are 8-bit Reg.
• MOVWF TMR0L
• MOVFF TMR0L, PORTB
TMR0IF flag bit
• Part of INTCON

Figure 1-3. INTCON (Interrupt Control


Figure 1-4. Timer0 Overflow
Flag
Characteristics and operations
of 16-bit mode
1. 16-bit timer, 0000 to FFFFH.
2. After loading TMR0H and TMR0L, the timer must be started.
3. Count up, till it reaches FFFFH, then it rolls over to 0000 and
activate TMR0IF bit.
4. Then TMR0H and TMR0L must be reloaded with the original
value and deactivate TMR0IF bit.
Steps to program Timer0 in 16-bit
mode to generate time delay
1. Load the value into the T0CON register
2. Load reg. TMR0H followed by reg. TMR0L with initial value
3. Start the timer with instruction
BSF T0CON, TMR0ON
4. Keep monitoring the timer flag (TMR0IF) to see if it is raised.
5. Stop the timer
6. Clear the TMR0IF flag 3
7. Go Back to step 2
Figure 1-5. Timer0 16-bit Block
Diagram
Example 1-3-Write a subroutine to create a time
delay
void delay (char cx)
{
int i;
T0CON = 0x83; /* enable TMR0, select instruction clock, prescaler set
to 16 */
for (i = 0; i < cx; i++)
{
TMR0 = 15535; /* load 15535 into TMR0 so that it rolls over in 50000
clock cycles */
INTCONbits.TMR0IF = 0;
while(!(INTCONbits.TMR0IF)); /* wait until TMR0 rolls over */
}
return;
}
Figure 1-6. Timer Delay Calculation for XTAL =
10 MHz with No Prescaler
• General formula for delay calculation
• T = 4/(10MHz) = 0.4 usecond
Write an 8051 C program to toggle all the bits of
port P1 continuously with some delay in
between. Use Timer 0, 16-bit mode to generate
the delay.
• Solution: void T0Delay()
#include <reg51.h> {
void T0Delay(void); TMOD=0x01; TL0=0x00;
void main(void) TH0=0x35;
{ TR0=1;
while (1) while (TF0==0); TR0=0;
{ TF0=0;
P1=0x55; }
T0Delay();
P1=0xAA;
T0Delay();
}
}
Prescaler and generating larger
delay
• The size of delay depend on
• The Crystal frequency
• The timer’s 16-bit register.
• The largest timer happens when TMR0L=TMR0H=0
• Prescaler option is used to duplicate the delay by dividing the
clock by a factor of 2,4, 8,16, 32,64 ,128,256
• If T0CON=0000 0101, then T = 4*64/f

XTAL Osc ÷4 ÷ 64 TMRx


Figure 1-7. Timer0 8-bit Block
Diagram
Figure 1-8. Timer1 High and Low
Registers
• Can be programmed in 16-bit mode only
• It has 2 bytes named as TMR1L and RMR1H
• It has also T1CON and TMR1IF
• The module incorporates its own low-power oscillator to provide an
additional clocking option.
• Used as a low-power clock source for the microcontroller in power-
managed operation.
Figure 1-9. Timer1 Block
Diagram
Figure 1-
10. T1CON
(Timer 1
Control )
Register
Figure 1-11. PIR1 (Interrupt
Control Register 1) Contains the
TMR1IF Flag
Write an 8051 C program to toggle all bits of P2
continuously every 500 ms. Use Timer 1, mode 1 to
create the delay.

• Solution: void T1M1Delay(void)


#include <reg51.h> {
void T1M1Delay(void); TMOD=0x10;
void main(void) TL1=0xFE;
{ TH1=0xA5;
unsigned char x; TR1=1;
P2=0x55; while (TF1==0);
while (1) TR1=0; TF1=0;
{ }
P2=~P2;
for (x=0;x<20;x++)
T1M1Delay();
}
}
Summary
• The PIC18 can have up to four or more
timers/counters. Depending on the family member
• Timers: Generate Time Delays (using Crystal)
• Timers are accessed as two 8-bit registers, TMRLx and
TMRHx
• Can be used either 8-bit or 16-bit
• Each timer has its own Timer Control register

You might also like