chapter2 - timers

26
Timers Chapter 2

Upload: adamwaiz

Post on 28-Oct-2014

929 views

Category:

Documents


10 download

TRANSCRIPT

Page 1: Chapter2 - Timers

Timers

Chapter 2

Page 2: Chapter2 - Timers

Objectives

• Upon completion of this chapter, you will be able to:– List the timers of the PIC18 and their

associated registers– Describe the various modes of the PIC18

timers– Program the PIC18 counters in C as event

counters

Page 3: Chapter2 - Timers

Timer0

• 16-bit wide• Consists of a low-byte (TMR0L) and a

high-byte (TMR0H) register• Can be used as 8-bit or 16-bit timer

Low byte (8-bit)High byte (8-bit)

Page 4: Chapter2 - Timers

Timer0 (cont’d)Important Registers:

i) T0CON (Timer0 Control Register)• To start & stop Timer0 and other configurations

ii) TMR0H:TMR0L (for counting purposes)• Act as counting buffer

iii) INTCON (Interrupt Control Register)D0D7

Page 5: Chapter2 - Timers

T0CON (Timer0 Control) Register

Page 6: Chapter2 - Timers

Example 1

• What is the value of T0CON if the Timer0 settings are as below ?– 16-bit timer– No pre-scaler– Internal clock (from oscillator) source– Increment on positive-edge

Answer: T0CON = 00001000

Page 7: Chapter2 - Timers

Example 2• Calculate the amount of time delay generated by the Timer0

with the following specification:– XTAL = 10MHz– TMR0H:TMR0L = FFF2H

Solution:TCY = 4/10MHz = 0.4us (Each tick consume 0.4us)How many tick? (FFFF-FFF2) + 1 = 14 ticksTime delay = 14 x 0.4us = 5.6us

FFF2 FFF3 FFF4 FFFE FFFF 0000

TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=1

1 2 1413

Page 8: Chapter2 - Timers

Example 3Write a C 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!

• To find register value for TMR0H:TMR0L – FFFF - register value + 1 = 1388H ticks– register value = EC78H

@

Simply take the negative value of the tick counts-1388H = EC78H This is much more easier!

Page 9: Chapter2 - Timers

Example 3 (cont’d)

Page 10: Chapter2 - Timers

Exercise 1Write a C 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

Page 11: Chapter2 - Timers

Exercise 1 (cont’d)

Page 12: Chapter2 - Timers

Timer1

• 16-bit wide• Consists of a low-byte (TMR1L) and a

high-byte (TMR1H) register• Can be used as 16-bit timer only!

Low byte (8-bit)High byte (8-bit)

Page 13: Chapter2 - Timers

Timer1 (cont’d)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

Page 14: Chapter2 - Timers

T1CON (Timer1 Control) Register

Page 15: Chapter2 - Timers

Exercise 2Write a C program to create pulses with a frequency of 2500Hz with 50% duty cycle on pin PORTB.1. Use Timer1 to create the delay. (Assume XTAL = 20MHz)

Solution:• T = 1/2500 = 400us (HIGH: 200us; LOW: 200us)• How many ticks in 200us delay?

– 200us/0.2us = 1000 ticks = 03E8H ticks!

• Therefore, register counts = - 03E8H = FC18H

Page 16: Chapter2 - Timers

Exercise 2 (cont’d)

Page 17: Chapter2 - Timers

Exercise 2 (cont’d)

Approx.1000 ins. cycles

Approx.1000 ins. cycles

Page 18: Chapter2 - Timers

Timer0 & Timer1 as Counter

• Can used as Counters• Counter0 (Timer0 counter):

– Count pulses on T0CKI (RA4) pin• Counter1 (Timer1 counter):

– Count pulses on T13CKI (RC0) pin

Page 19: Chapter2 - Timers

Example - Counter

• Please refer Example 9-35, 9-36 & 9-37 in the textbook

Page 20: Chapter2 - Timers

Timer2

• 8-bit wide• Consists of a PR2 (Period Register 2)• TMR2 will increment from 00 until reaches

PR2 value before TMR2IF flag is set• Consists of prescaler and postscaler• No counter function

Page 21: Chapter2 - Timers

Timer2 (cont’d)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

Page 22: Chapter2 - Timers

Timer2 (cont’d)

Page 23: Chapter2 - Timers

Timer3

• 16-bit wide• Consists of a low-byte (TMR3L) and a

high-byte (TMR3H) register• Enable the CCP Mode for PWM

Application

Page 24: Chapter2 - Timers

Timer3 (cont’d)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)

Page 25: Chapter2 - Timers

Timer3 (cont’d)

Page 26: Chapter2 - Timers

End of Chapter 2