



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1.1 List the timers of the PIC18 and their associated registers
1.2 Describe the various modes of the PIC18 timers
1.3 Program the PIC18 timers in C to generate time delays
2.1 MPLAB C18 Compiler
2.2 Personal Computer
The PIC18 has two to five timers depending on the family member. They are referred to as Timers 0,1,2,3 and 4. They can be used either as timers to generate a time delay or as counters to count events happening outside the microcontroller
3.1 REGISTERS AND PROGRAMMING TIMERS 0, 1, 2 AND 3
3.1.1 Basic registers of the timers
Many of the PIC 18 timers are 16 bits wide. Because the PIC 18 has an 8-bit architecture, each 16-bit timer is accessed as two separate registers of low byte (TMRxL) and high byte (TMRxH). Each timer also has the TCON (Timer control) register for setting modes of operation.
3.1.2 Timer0 programming
Timer0 can be used as an 8-bit or 16 bit timer. The 16-bit register of Timer0 is accessed as low byte and high byte, as shown in figure2.1. The low byte register is called TMR0L (TIMER0 Low byte) and the high byte register is referred to as TMR0H (TIMER0 high byte)
Figure 2.1 Timer0 High and Low register.
3.1.3 16-bit timer programming of Timer0.
The following are the characteristics and operations of 16-bit mode:
3.1.4 8-bit mode programming of Timer
Timer0 can also be used in 8-bit mode. The 8-bit mode allows only values of 00 to FFH to be loaded into the timers register TMRL0. After the timer is started, it starts to count up by incrementing the TMR0L register. It counts up until it reaches its limit of FFH. When it rolls over from FFH to 00, it sets HIGH the TMR0IF.
3.1.5 Timer1 programming
Timer1 is a 16- bit timer, and its 16-bit register is split into two bytes, referred to as TMR1L (Timer 1 low byte) and TMR1H (Timer1 high byte). See figure 2.2. Timer can be programmed in 16-bit mode only and unlike Timer0, it does not support 8-bit mode. Timer1 also has the T1CON (Timer 1 control) register in addition to the TMR1IF (Timer1 interrupt flag). The TMR1IF flag bit goes HIGH when TMR1H:TMR1L overflows from FFFF to 0000. Timer 1 also has the prescaler option, but it only supports factor of 1:1, 1:2, 1:4 and 1:8. See figure 2.3 for the timer1 block diagram and figure 2.4 for T1CON register options. The PIR1 register contains the TMR1IF flags. See figure2.5.
Figure 2.2 Timer1 High and Low Registers
Figure 2.5 PIR1 (Interrupt Control Register 1) Contains the TMR1IF Flag
3.1.6 Timer2 programming
Timer2 is an 8-bit timer. The 8-bit register of Timer2 is called TMR2. Timer2 also has an 8-bit register called the period register (PR2). We can set the PR2 register to a fixed value and Timer2 will increment from 00 until it matches the value in PR2. At that point, the equal signal will raise the TMR2IF flag and reset TMR2 to 00. The clock source for timer2 is Fosc/4 with the options of both prescaler and postcaler, as shown in figure 6. Notice from figure 2.6 that there is no external clock source for timer2.In the other words, it cannot be used as a counter.
Figure 2.6 Timer2 Block Diagram
3.1.7 Timer3 programming
Timer3 is a 16-bit timer that can be used as a timer or counter. Its 16-bit register is split into two bytes referred to as TMR3L and TMR3H. See figure 2.7. Timer3 can be programmed in 16-bit mode only and does not support 8-bit mode. We select various options of Timer3 using the T3CON (TIMER3 Control) register, as shown in figure 8. Timer3 has the prescaler options of 1:1, 1:2, 1:4, and 1:8, as shown in figure 2.8. Figure2.8 also shows the bits related to the CCP (compare/capture pulse-width-
modulation) feature of the PIC 18. Pulse width modulation (PWM) is an important concept used in DC motor control.
Figure 2.7 Timer3 Block Diagram
4.2 Steps to program 8-bit mode of Timer
To generate a time delay using Timer0 in 8-bit mode, take the following steps.