0% found this document useful (0 votes)
432 views

PIC18 Timer Programming

The document explains the assembly language programming for timer and counter modules in PIC18 microcontrollers. It lists the different timers available in PIC18 and their registers. It describes how to program the timers to generate delays and counters to count external events. It provides examples of programming timers 0, 1, 2 and 3 in various modes like 8-bit, 16-bit with and without prescalers to create time delays and generate waveforms.

Uploaded by

Nor Azlan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
432 views

PIC18 Timer Programming

The document explains the assembly language programming for timer and counter modules in PIC18 microcontrollers. It lists the different timers available in PIC18 and their registers. It describes how to program the timers to generate delays and counters to count external events. It provides examples of programming timers 0, 1, 2 and 3 in various modes like 8-bit, 16-bit with and without prescalers to create time delays and generate waveforms.

Uploaded by

Nor Azlan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

Explain the assembly language programming for the timer and counter module

List the timers of the PIC18 and their associated register Describe the various modes of the PIC18 timers Program the PIC18 timers in Assembly Language to generate delays Program the PIC18 counters in Assembly as event counters

The PIC18 has from two (2) to five (5) timers depending on the family member. They are referred to as Timer 0, 1, 2, 3 and 4. What Timers can do? 1) Generate a time delay 2) As a Counter to count events happening outside the microcontroller. PIC18F4580 has 4 Timers.

Every timer needs a clock pulse to tick. The clock source can be internal or external. Internal clock: The 1/4th of the frequency of the crystal oscillator on the OSC1 and OSC2 pins (Fosc/4) is fed into the timer. Therefore, it is used for time delay generation. This is called a timer. External clock: Fed pulses thru one of the PIC18s pins: This is called a counter.

Many of the PIC18 timers are 16 bits wide. Each 16-bit timer is accessed as two separate register, low byte (TMRxL) and high byte (TMRxH) Each timer also has the TCON (Timer Control) register for setting modes of operation.

The Timer0 module incorporates the following features: Software selectable operation as a timer or counter in both 8-bit or 16-bit modes Readable and writable registers Dedicated 8-bit, software programmable prescaler Selectable clock source (internal or external) Edge select for external clock Interrupt-on-overflow

Timer0 can be used as an 8-bit or 16-bit timer The 16-bit register of Timer0 is accessed as low byte (TMR0L) and high byte (TMR0H)

Timer0 High and Low Registers

Find the value for T0CON if we want to program Timer0 in 16-bit mode, no prescaller. Use PIC18s Fosc/4 crystal oscillator for the clock source, increment on positive-edge.

T0CON = 0000 1000

With 64 prescaller

T0CON = 0000 0101

T0CS or bit 5 is used to decide whether the clock source is internal (Fosc/4) or external T0CS = 0 ; The Fosc/4 is used as clock source T0CS = 1; The clock source is external and comes from the RA4/T0CKI (pin 6)

Used as an event counter

External Source

Internal Source

Find the timers clock frequency and its period for various PIC18 based systems, with the following crystal frequency. Assume that no prescaller is used.

a) 10 MHz b) 16 MHz c) 4 MHz

2.5 MHz @ 0.4 uS 4 MHz @ 0.25 uS 1 MHz @ 1 uS

TMR0IF Flag Bit


D7 D0

Tomer0 Overflow Flag

It allows values of 0000H to FFFFH to be loaded into the registers TMR0H and TMR0L After loaded, the timer must be started (BSF T0CON, TMR0ON) It start to count up until it reaches its limit of FFFFH. When it rolls over from FFFFH to 0000H, it sets HIGH a flag bit (TMR0IF) Repeat the process: 1)Reload the TMR0H and TMR0L 2)TMR0IF flag must be reset to 0

1) Load the value into the T0CON register 2) Load register TMR0H followed by register TMR0L 3) Start the timer 4) Keep monitoring the timer flag (TMR0IF) 5) Stop the timer 6) Clear the TMR0IF flag for the next round 7) Go back to the step 2)

Calculate the amount of time delay generated by the timer. Assume that XTAL = 10MHz

Solution: T = 4/10MHz = 0.4us (Each tick consume 0.4us) How many tick? (FFFF-FFF2) + 1 = 14 Decimal ( ticks) Time delay = 14 x 0.4us = 5.6us for half the pulse
Rolls Over from FFFF to 0 1 FFF2 TMR0IF=0 FFF3 2 FFF4 FFFE 13 FFFF 14 0000

TMR0IF=0 TMR0IF=0

TMR0IF=0 TMR0IF=0 TMR0IF=1

For TMR0H = B8 and TMR0L = 3E, calculate the delay generated Assume XTAL = 10MHz (FFFF B83E + 1) = 47C2H = 18370 x 0.4uS = 7.348mS or 65536 47166 = 18370 x 0.4uS = 7.348mS

Write a program to toggle all the bits of PORTB continuously with 1ms delay. Use Timer0, 16-bit mode, no prescaler options to generate the delay. (Assume XTAL=20MHz)
Solution: TCY = 4/20MHz = 0.2us (Each tick consume 0.2us) How many ticks in 1ms delay? 1ms/0.2us = 5000 ticks = 1388H ticks!

TMR0H = ECH To find register value for TMR0H:TMR0L FFFF - register value + 1 = 1388H ticks TMR0L = 78H register value = EC78H @ Simply take the negative value of the tick counts -1388H = EC78H
For source code, please modify Example 3

The time delay depends on two factors, a) The crystal frequency b) The timers 16-bit register We can use the prescaler option in the T0CON register to increase the delay by reducing the period Prescaler option from 2 to 256
XTAL Osc 4 64 TMRx

Find the timers clock frequency and its period with the following crystal frequency. Assume that a prescaler of 1:64 is used. a) 10 MHz b) 16 MHz

Solution: a) 10MHz/4 = 2.5MHz 2.5MHz/64 = 39062.5Hz T = 1/39062.5Hz = 25.6uS b) 16MHZ/4 = 4MHz 4MHZ/64 = 62500Hz T = 1/62500Hz = 16uS

Write a program to toggle only the PORTB.4 bit continuously every 50ms. Use Timer0, 16-bit mode and the 1:4 prescaler to create the delay. (Assume XTAL = 20MHz)
Solution: TCY = 4/20MHz = 0.2us (Each tick consume 0.2us) How many ticks in 50ms delay? 50ms/0.2us = 250000 ticks = 3D090H ticks! (out of range!!) With 1:4 prescaller 250000/4 = 62500 ticks = F424H ticks! Therefore, register counts = -F424H = 0BDCH

For source code, please modify Example 3

Set the T0CON value register indicating 8-bit mode Load the TMR0L register only! Start the timer Keep monitoring the timer flag (TMR0IF) Stop the timer Clear the timer flag Reload TMR0L
Please refer Example 9-16

The Timer1 timer/counter module incorporates these features: Software selectable operation as a 16-bit timer or counter Readable and writable 8-bit registers (TMR1H and TMR1L) Selectable clock source (internal or external) with device clock or Timer1 oscillator internal options Interrupt-on-overflow Module Reset on CCP Special Event Trigger Device clock status flag (T1RUN)

16-bit wide Consists of a low-byte (TMR1L) and a highbyte (TMR1H) register Can be used as 16-bit timer only!

High byte (8-bit)

Low byte (8-bit)

Important Registers:
i) T1CON (Timer1 Control Register) To start & stop Timer1 and other configurations ii) TMR1H:TMR1L (for counting purposes) Act as counting buffer iii) PIR1 (Peripheral Interrupt Request Register 1)
D7

Write a program to generate a square wave of 50Hz frequency on pin PORTB.5. Use Timer1 and the maximum prescaler allowed

Solution: # T = 1/50Hz = 20mS (Square wave) # wave = 20mS/2 = 10mS # 10mS/0.4uS/8 = 3125 or C325H # Timer1 Register = F3CBH

Can used as Counters Counter0 (Timer0 counter):


Count pulses on T0CKI (RA4) pin

Counter1 (Timer1 counter):


Count pulses on T13CKI (RC0) pin

Please refer Example 9-22, 9-23, 9-24, 925,9-26 & 9-27 in the textbook

The Timer2 module timer incorporates the following features: 8-Bit Timer and Period registers (TMR2 and PR2, respectively) Readable and writable (both registers) Software programmable prescaler (1:1, 1:4 and 1:16) Software programmable postscaler (1:1 through 1:16) Interrupt on TMR2-to-PR2 match Optional use as the shift clock for the MSSP module

Important Registers:
i) T2CON (Timer2 Control Register) To start & stop Timer2 and other configurations ii) PR2 (to set the counting value) If TMR2 = PR2; TMR2IF flag is set iii) PIR1 (Peripheral Interrupt Request Register 1)
D7

Please refer Example 9-38 and 9-39

The Timer3 module timer/counter incorporates these features: Software selectable operation as a 16-bit timer or counter Readable and writable 8-bit registers (TMR3H and TMR3L) Selectable clock source (internal or external) with device clock or Timer1 oscillator internal options Interrupt-on-overflow Module Reset on CCP Special Event Trigger

Important Registers:
i) T3CON (Timer3 Control Register) To start & stop Timer3 and other configurations ii) TMR3H:TMR3L (for counting purposes) Act as counting buffer iii) PIR2 (Peripheral Interrupt Request Register 2)

Please refer Example 9-42, 9-43 and 9-44

The PIC18 can have up to four or more timers/counters. Depending on the family member Timers: Generate Time Delays (using Crystal) Counters: Event counter (using Pulse outside) 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

Never leave that till to-morrow which you can do to-day Quote by Benjamin Franklin

You might also like