Presentation is loading. Please wait.

Presentation is loading. Please wait.

Timers Chapter 2.

Similar presentations


Presentation on theme: "Timers Chapter 2."— Presentation transcript:

1 Timers Chapter 2

2 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

3 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)

4 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) D0 D7

5 T0CON (Timer0 Control) Register

6 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 =

7 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 ticks Time delay = 14 x 0.4us = 5.6us FFF2 FFF3 FFF4 FFFE FFFF 0000 TMR0IF=0 TMR0IF=1 1 2 14 13

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

9 Example 3 (cont’d)

10 Exercise 1 Write 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 = ticks = 3D090H ticks! (out of range!!) With 1:4 prescaller 250000/4 = ticks = F424H ticks! Therefore, register counts = -F424H = 0BDCH

11 Exercise 1 (cont’d)

12 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)

13 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

14 T1CON (Timer1 Control) Register

15 Exercise 2 Write 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

16 Exercise 2 (cont’d)

17 Exercise 2 (cont’d) Approx.1000 ins. cycles Approx.1000 ins. cycles

18 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

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

20 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

21 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

22 Timer2 (cont’d)

23 Timer3 16-bit wide Consists of a low-byte (TMR3L) and a high-byte (TMR3H) register Enable the CCP Mode for PWM Application

24 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)

25 Timer3 (cont’d)

26 End of Chapter 2 Assignment Timer


Download ppt "Timers Chapter 2."

Similar presentations


Ads by Google