ceng -336 introduction to embedded systems …user.ceng.metu.edu.tr/~e1448331/corg/week6.pdfafter...

54
CENG-336 Introduction to Embedded Systems Development CENG CENG - - 336 336 Introduction to Embedded Introduction to Embedded Systems Development Systems Development Timers Timers

Upload: lamtuyen

Post on 21-May-2018

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336Introduction to Embedded

Systems Development

CENGCENG--336336Introduction to Embedded Introduction to Embedded

Systems DevelopmentSystems Development

TimersTimers

Page 2: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 2

DefinitionsDefinitions

� A counter counts (possibly asynchronous) input pulses from an external signal

� A timer counts pulses of a fixed, known frequency usually the system clock for the processor� A timer is basically a counter operated by the

processor clock.� It can count down a fixed number of clock cycles.� Almost all microcontrollers have built in timers.

Page 3: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 3

Definitions ...Definitions ...

� Physically, PIC Timer is a register whose value is continually increasing to 255, and then it starts all over again:�0, 1, 2, 3, 4 ..... 255, 0, 1, 2, 3, 4 ...... etc.

Page 4: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 4

Definitions ...Definitions ...� This incrementing is done in the background

of everything a microcontroller does.

� It is up to programmer to think up a way how (s)he will take advantage of this characteristic for his needs.

� One of the ways is increasing some variable on each timer overflow. � If we know how much time a timer needs to make

one complete round, then multiplying the value of a variable by that time will yield the total amount of elapsed time.

Page 5: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 5III-65

Uses of Programmable Timers and Event Uses of Programmable Timers and Event CountersCounters

1. 1. Generate realGenerate real--time interruptstime interrupts

2. Output precisely timed signals2. Output precisely timed signals

3. Programmable baud rate generator3. Programmable baud rate generator

4. Measure time between events4. Measure time between events

5. Count external events5. Count external events

6. Generate event caused interrupts6. Generate event caused interrupts

TimersTimers // Event CountersEvent Counters

Page 6: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 6

Timer applications�Frequency measurement�Waveform generation (e.g. sound)� time base (multi task system, sampling

system, ...)

TimersTimers // Event CountersEvent Counters ......

Page 7: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 7

Timer (8253 used in PC)Timer (8253 used in PC)

� Three identical timer elements � Actually they are counters of

the clock� Inputs are clock and gate� Output

� Changes or pulses when counting cycle is complete

� Modes� Interrupt on countdown� One-shot� Pulse rate generator� Square-wave generator� Software-triggered strobe� Hardware-triggered strobe

Timer 0

Timer 1

Timer 2

clockgateoutput

gateoutput

clock

clock

gateoutput

Registers0 - Counter 01 - Counter 12 - Counter 23 - Control for all

Page 8: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 8

Common Common TTimerimer AApplicationspplications� Combinations of

connection and mode� Examples

� Internal speaker

� periodic interrupt using countdown from system clock and square-wave mode

� counter input controls frequency

� 18HZ interrupt

� 4.77 MHz clock counted down by 12, then by 64K

� pulse-rate generator mode

Clk – constant rate

Gate - event

Out – to IRQ

Clk - event

Gate - on

Out - ignored

Clk – constant rate

Gate - on

Out – to IRQ

Time interval measurement

Event counter

Delay or periodic interrupt

Page 9: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 9

PIC TimersPIC Timers� Available in all PICs

� May generate interrupts on timer overflow

� Some 8 bits, some 16 bits, some have prescalers

� Some can connect to external clock, some to the processor clock, some to either.

� Read/write a number as the current count is possible for some.

Page 10: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 10

PIC TimersPIC Timers� Can use external pin as clock in / clock

out (i.e. for counting events)�Warning: Some Peripherals shares timer

resources.

Page 11: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 11

PIC TimersPIC Timers

� The device has three readable and writeable hardware timers that can increment automatically each instruction cycle (if no prescaler is used).

� All timers can cause an interrupt on overflow, and then restart from zero.

Page 12: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 12

PIC Timers PIC Timers

� Timer 0

timer/counter with prescale

� Timer 1

timer/counter with prescale

� Timer 2

timer only with prescale and postscale

� Watch Dog Timer (to be discussed later)

Page 13: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 13

TimersTimers�� TIMER0TIMER0 is an 8-bit timer with an eight bit

prescaler, which can make the timer run 2 to 256 times slower than normal

�� TIMER1TIMER1 is a 16-bit timer (two 8-bit registers) with a 1:1 to 1:8 prescaler and some other features. Used by given C code to generate soft timer and sound

�� TIMER2TIMER2 is an 8-bit timer with 1:1 to 1:16 prescaler and a 1:1 to 1:16 postscaler It also has a period register.Used by given C code for PWM motor control

Page 14: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 14

PIC Timers / Timer0PIC Timers / Timer0

�� 8 bit timer/counter with 8 bit timer/counter with prescalerprescaler

�� Readable and writeableReadable and writeable

�� 88--bit software programmable bit software programmable prescalerprescaler

�� Internal or external clock setInternal or external clock set

�� Interrupt on overflow from 0xFF to 0x00Interrupt on overflow from 0xFF to 0x00

�� Edge Select for external clockEdge Select for external clock

Page 15: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 15

PrePre--SScalercaler�� Prescaler is a name for the part of a Prescaler is a name for the part of a

microcontroller which divides oscillator clock microcontroller which divides oscillator clock before it will reach logic that increases timer before it will reach logic that increases timer status. status.

�� The number which divides a clock is defined The number which divides a clock is defined through lowestthrough lowest--order three bits in order three bits in OPTIONOPTIONregister. register.

�� The highest divisor value is 256. This actually The highest divisor value is 256. This actually means that only at every 256th clock, timer means that only at every 256th clock, timer value would increase by one. value would increase by one.

�� This provides us with the ability to measure This provides us with the ability to measure longer time periods. longer time periods.

Page 16: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 16

PrePre--ScalerScaler ........

÷2 ÷2 ÷2 ÷2 ÷2 ÷2 ÷2 ÷2f

f/2 f/4 f/8 f/16 f/32 f/64 f/128

f/256

8 input to 1 output multiplexerSelects one of the inputs to connect to output

PrescalerPrescaler outputoutput

PS2, PS1, PS0PS2, PS1, PS0 : PreScaler select inputs:Binary number on these 3 bits determine which input (0..7) to be selected

Note: if prescaler is disabled, input (f) is directly connected to the counter

Input 0 Input 7

Input clock withfrequency: f

Page 17: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 17

PrePre--ScalerScaler ........

Page 18: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 18

�� After each count up to 255, timer resets its After each count up to 255, timer resets its value to zero and starts with a new cycle of value to zero and starts with a new cycle of counting to 255. counting to 255.

�� During each transition from 255 to zero, During each transition from 255 to zero, T0IF bit in INTCOT0IF bit in INTCONN register is set.register is set.� If interrupts are allowed to occur, this can be

taken advantage of in generating interrupts and in processing interrupt routine.

� It is up to programmer to reset T0IF bit in interrupt routine, so that new interrupt, or new overflow could be detected.

TimersTimers ......

Page 19: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 19

Page 20: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 20

�� The timer is controlled by a number of bits in The timer is controlled by a number of bits in the Option Register. the Option Register.

�� All processorsAll processors are nothing more than a are nothing more than a collection of gates.collection of gates.� While this may be hard to tell in a very complex

processor like a Pentium, the PIC is a very simple processor, and sometimes this actual simplicity makes itself obvious.

� The timer is one of those cases.

Controlling Controlling TimersTimers

Page 21: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 21

Option RegisterOption Register�� The The option registeroption register is used to control a is used to control a

number of processor features.number of processor features.

�� The leastThe least significant six bits of the option significant six bits of the option register control the timer logic.register control the timer logic.

�� The OPTION_REG register is a readable and The OPTION_REG register is a readable and writable register which contains various writable register which contains various control bits to configure control bits to configure � the TMR0/WDT prescaler, � the External INT Interrupt, � TMR0, and � the weak pull-ups on PORTB.

Page 22: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 22

Selecting ParametersSelecting Parameters�� In order to set up the timer, it is necessary to In order to set up the timer, it is necessary to

first decide the time interval needed.first decide the time interval needed.

�� The basic timer rate is one microsecond (with a 4 The basic timer rate is one microsecond (with a 4 MHz crystal).MHz crystal).

�� This oneThis one microsecond clock is divided by the microsecond clock is divided by the prescaler, which can be set to divide by 2, 4, 8prescaler, which can be set to divide by 2, 4, 8, 1, 16, 6, 32, 64, 128 or 256. 32, 64, 128 or 256.

�� The timer register itself has 8 bits, so it can count The timer register itself has 8 bits, so it can count to 256.to 256.

�� Thus, it is necessary to service the timer with Thus, it is necessary to service the timer with software at least every 256*256software at least every 256*256 microseconds, or microseconds, or 65.536 mi65.536 microcroseconds (assuming a 4 MHz clock).seconds (assuming a 4 MHz clock).

Page 23: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 23

Alternative Usage ...Alternative Usage ...

�� Alternative to the internal oscillator clock, Alternative to the internal oscillator clock, timer status can also be increased by the timer status can also be increased by the external clock on RA4/TOCKI pin. external clock on RA4/TOCKI pin. � Choosing one of these two options is done in

OPTIONOPTION register through T0CST0CS bit. � If this option of external clock was selected, it

would be possible to define the edge of a signal (rising or falling), on which timer would increase its value.

Page 24: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 24

ExampleExample�� In practice, one of the typical example that In practice, one of the typical example that

is solved via external clock and a timer is is solved via external clock and a timer is counting full turns of an axis of some counting full turns of an axis of some production machine, like transformer winder production machine, like transformer winder for instance. for instance.

�� Let's wind four metal screws on the axis of Let's wind four metal screws on the axis of a winder. These four screws will represent a winder. These four screws will represent metal convexity. metal convexity.

�� Let's place now the inductive sensor at a Let's place now the inductive sensor at a distance of 5mm from the head of a screw. distance of 5mm from the head of a screw.

Page 25: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 25

ExampleExample ......�� Inductive sensor will generate the falling Inductive sensor will generate the falling

signal every time the head of the screw is signal every time the head of the screw is parallel with sensor head. parallel with sensor head.

�� Each signal will represent one fourth of a full Each signal will represent one fourth of a full turn, and the sum of all full turns will be turn, and the sum of all full turns will be found in TMR0 timer. found in TMR0 timer.

�� Program can easily read this data from the Program can easily read this data from the timer through a data bus.timer through a data bus.

Page 26: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 26

ExExample ...ample ...

Page 27: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 27

Initializing the TimerInitializing the Timer

Page 28: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 28

�� Prescaler can be assigned either timer TMR0 Prescaler can be assigned either timer TMR0 or a watchdog.or a watchdog.� Watchdog is a mechanism which microcontroller uses to

defend itself against programs getting stuck.

�� Prescaler is accorded to timer TMR0, or to Prescaler is accorded to timer TMR0, or to watchdog timer troughwatchdog timer trough PSA bit in OPTION PSA bit in OPTION register.register.� By clearing PSA bit, prescaler will be accorded to timer

TMR0. When prescaler is accorded to timer TMR0, all instructions of writing to TMR0 register (CLRF TMR0, MOVWF TMR0, BSF TMR0,...) will clear prescaler.

� Prescaler change is completely under programmer's control, and can be changed while program is running.

Initializing the Timer ...Initializing the Timer ...

Page 29: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 29

Option RegisterOption Register ......

Page 30: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 30

All of the acronyms along the bottom of the diagram refer to bits in the option register, except for T0IF (Timer0 interrupt flag), which is a bit in the INTCON register.

Starting at the left, the processor clock is divided by four and fed into a gate. This division by four results in a single cycle per instruction execution. In the case of a 4MHz processor crystal, this conveniently results in a 1 MHz clock.

Page 31: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 31

When bit T0CS (Timer0 clock select) is false, the clock is fed into a prescaler. The prescaler ratio is set by bits PS2, PS1 and PS0. *Wrong*

If the PSA bit is false, the output of the prescaler is then routed to the TMR0 register, after synching with other internal clocks. This causes a 2 cycle delay, which is only apparent if we load the TMR0register with a value.

Page 32: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 32

Every cycle of the prescaler output increments TMR0 by one. We can read as well as write the contents of TMR0. Whenever TMR0 overflows, the T0IF bit in the INTCON register is set.

If the INTCON bit T0IE (Timer 0 interrupt enable) is set, this transition of the T0IF bit causes an interrupt, so we will always take care to clear T0IE.

Page 33: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 33

Page 34: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 34

PIC Timers / Timer1PIC Timers / Timer1

�� 1616--bit timer/counter with prebit timer/counter with pre--scalerscaler

�� Readable and writeableReadable and writeable

�� 1, 2, 4, 8 programmable pre1, 2, 4, 8 programmable pre--scalerscaler

�� Internal or external clock selectInternal or external clock select

�� External clock can be syn. or External clock can be syn. or asynasyn..

�� Interrupt on overflowInterrupt on overflow

�� Second crystal permittedSecond crystal permitted

Page 35: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 35

PIC Timers / Timer2PIC Timers / Timer2

�� 88--bit timer/counter with prebit timer/counter with pre--scalerscaler and and postpost--scalerscaler

�� Readable and writeableReadable and writeable

�� 1,1, 4 or 16 programmable pre4 or 16 programmable pre--scalerscaler

�� 44--bit programmable postbit programmable post--scalerscaler

�� Interrupt on overflowInterrupt on overflow

�� Output to port pinOutput to port pin

Page 36: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 36

Selecting ParametersSelecting Parameters�� In order to set up the timer, it is necessary to In order to set up the timer, it is necessary to

first decide the time interval needed.first decide the time interval needed.

�� The basic timer rate is one microsecond (with a 4 The basic timer rate is one microsecond (with a 4 MHz crystal). MHz crystal).

�� This oneThis one microsecond clock is divided by the microsecond clock is divided by the prescaler, which can be set to divide by 2, 4, 8prescaler, which can be set to divide by 2, 4, 8, 1, 16, 6, 32, 64, 128 or 256. 32, 64, 128 or 256.

�� The timer register itself has 8 bits, so it can count The timer register itself has 8 bits, so it can count to 256.to 256.

�� Thus, it is necessary to service the timer with Thus, it is necessary to service the timer with software at least every 256*256software at least every 256*256 microseconds, or microseconds, or 65.536 milliseconds (assuming a 4 MHz clock).65.536 milliseconds (assuming a 4 MHz clock).

Page 37: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 37

Selecting ParametersSelecting Parameters ......

�� The timer register itself can be used to The timer register itself can be used to divide by any arbitrary number by simplydivide by any arbitrary number by simplyreloading it whenever the register overflows, reloading it whenever the register overflows, and; and;

�� additional software counters canadditional software counters can be updated based be updated based on the timer, so it is possible to arrange any on the timer, so it is possible to arrange any desired time. desired time.

�� TheThe catch is that, the higher the resolution catch is that, the higher the resolution needed, the more frequently software mustneeded, the more frequently software mustservice its counters.service its counters.

Page 38: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 38

Selecting ParametersSelecting Parameters ......

�� Consider for a moment an application that Consider for a moment an application that requires a 10requires a 10--millisecond timer. millisecond timer.

�� If theIf the prescaler is set to divide by 64, the prescaler is set to divide by 64, the timer register can count to 16.384 timer register can count to 16.384 milliseconds.milliseconds.

�� If the timer register is preloaded with 100, If the timer register is preloaded with 100, then the timer will expire in 9.984then the timer will expire in 9.984milliseconds.milliseconds.�� (256(256--100)/256 = 9.984/16.384 (Check ?)100)/256 = 9.984/16.384 (Check ?)

Page 39: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 39

Selecting ParametersSelecting Parameters ......

�� If one were to use a prescaler division of 16, If one were to use a prescaler division of 16, it is possible to get a delay of exactlyit is possible to get a delay of exactly 10 10 milliseconds, however, it would require milliseconds, however, it would require servicing the timer in software every twoservicing the timer in software every twomilliseconds and maintaining a counter in milliseconds and maintaining a counter in software. software.

�� The programmer needs toThe programmer needs to consider how good consider how good is is ““good enoughgood enough””, balanced against the , balanced against the complexity and thecomplexity and the potential that time could potential that time could be taken from other tasks to watch the clock.be taken from other tasks to watch the clock.� Real-time Systems ?

Page 40: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 40

Setting up the TimerSetting up the Timer

�� To set up the timer, one must first disable To set up the timer, one must first disable interrupts so that an interrupt doesninterrupts so that an interrupt doesn’’tt occur occur when the timer expires. when the timer expires.

�� Then, enable the timer and assign the Then, enable the timer and assign the prescaler to theprescaler to the timer. timer.

�� Establish the prescaler value, and finally, load Establish the prescaler value, and finally, load the timer register.the timer register.

Page 41: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 41

Setting up the TimerSetting up the Timer ......�� The bits for enabling the timerThe bits for enabling the timer,,

�� Assigning the prescaler to the timer, Assigning the prescaler to the timer,

�� TheThe bits that set the prescaler division ratiobits that set the prescaler division ratio

are all in the same register. are all in the same register.

�� Thus, theseThus, these values may be setvalues may be set� bit by bit, or � by simply loading the Option register with a value

(assuming it is possible to determine benign values for the other bits).

Page 42: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 42

Setting up the TimerSetting up the Timer ......

�� Whenever the timer expires, the T0IF bit in Whenever the timer expires, the T0IF bit in the INTCON register will be set. the INTCON register will be set.

�� WeWe must clear this bit, reload the timer register, must clear this bit, reload the timer register, and then execute the code that is to beand then execute the code that is to be done at done at this time.this time.

Page 43: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 43

Setting up the TimerSetting up the Timer ......

Page 44: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 44

� In code, the setup portion might look something like:banksel INTCON

bcf INTCON,T0IE ; Mask timer interrupt

banksel OPTION_REG

bcf OPTION_REG,T0CS ; Enable timer

bcf OPTION_REG,PSA ; Prescaler to timer

bcf OPTION_REG,PS2 ; \

bsf OPTION_REG,PS1 ; >- 1:16 prescale

bsf OPTION_REG,PS0 ; /

movlw D’100’ ; Timer will count

movwf TMR0 ; 156 (256-100) counts

Setting up the TimerSetting up the Timer ......

Page 45: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 45

Setting up the TimerSetting up the Timer ......

� Clearly, the individual bits in the option register could all be set with a “single store”.

� If we didn’t care about the RB0 interrupt, the weak pull-ups, or the transition of RA4,then instead of five bit manipulations we could have said:

� movlw B’10000011’ ; Set up prescaler and� movwf OPTION_REG ; timer

Page 46: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 46

�� The execution loop might look something like:The execution loop might look something like:

main

btfss INTCON,T0IF ; Did timer overflow?

goto main ; No, hang around some more

movlw D’100’ ; Timer will count

movwf TMR0 ; 156 (256-100) counts

bcf INTCON,T0IF ; reset overflow flag

call DoCode ; Execute main code

goto main ; Go back and wait

Setting up the TimerSetting up the Timer ......

Page 47: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 47

OperationOperation

�� Timer modeTimer mode is selected by clearing the T0CS is selected by clearing the T0CS bit (OPTION<5>).bit (OPTION<5>).

�� In timer mode, the Timer0 moduleIn timer mode, the Timer0 module will will increment every instruction cycle (without increment every instruction cycle (without prescaler).prescaler).

�� If the TMR0 register is written, theIf the TMR0 register is written, theincrement is inhibited for the following two increment is inhibited for the following two instruction cycles.instruction cycles.� The user can work around this by writing an

adjusted value to the TMR0 register.

Page 48: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 48

Operation ...Operation ...

�� Counter modeCounter mode is selected by setting the T0CS bit is selected by setting the T0CS bit (OPTION<5>).(OPTION<5>).

�� In counter mode, Timer0 willIn counter mode, Timer0 will increment either on increment either on every rising or falling edge of the T0CKI pin. every rising or falling edge of the T0CKI pin.

�� The incrementing edge is determinedThe incrementing edge is determined by the by the Timer0 Source Edge Select the T0SE bit Timer0 Source Edge Select the T0SE bit (OPTION<4>). (OPTION<4>).

�� Clearing the T0SE bitClearing the T0SE bit selects the rising edge. selects the rising edge.

Page 49: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 49

TMR0 InterruptTMR0 Interrupt�� TMR0 interrupt is generated when the TMR0 TMR0 interrupt is generated when the TMR0

register overflows from FFh to 00h. register overflows from FFh to 00h.

�� ThisThis overflow sets bit T0IF (INTCON<2>). overflow sets bit T0IF (INTCON<2>).

�� InInterrupt can be masked by clearing bit T0IEterrupt can be masked by clearing bit T0IE(INTCON<5>). (INTCON<5>).

�� Bit T0IF must be cleared in software by the Bit T0IF must be cleared in software by the Timer0 module interrupt service routineTimer0 module interrupt service routinebefore rebefore re--enabling this interrupt. enabling this interrupt.

�� TMR0 interrupt cannot awaken the processor TMR0 interrupt cannot awaken the processor fromfrom SLEEP since the timer is shutSLEEP since the timer is shut--off off during SLEEP. during SLEEP.

Page 50: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 50

Timer0 & External ClockTimer0 & External Clock

�� When an external clock input is used for When an external clock input is used for TMR0, it must meet certain requirements TMR0, it must meet certain requirements � These requirements ensure the external clock can

be synchronized with the internal phase clock (TOSC).

� Also, there is a delay in the actual incrementing of TMR0 after synchronization.

Page 51: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 51

TMR0 PreScalerTMR0 PreScaler

�� An 8An 8--bit counter is available as a prebit counter is available as a pre--scaler scaler for the Timer0 module, or as a postfor the Timer0 module, or as a post--scaler scaler for thefor the Watchdog Timer. Watchdog Timer.

�� For simplicity, this counter is being referred For simplicity, this counter is being referred to as to as ““prescalerprescaler”” inin the Timer0 description. the Timer0 description.

�� Thus, a prescaler assignment for the Timer0 Thus, a prescaler assignment for the Timer0 module means that there ismodule means that there is no postscaler for no postscaler for the Watchdog Timer, and vicethe Watchdog Timer, and vice--versa.versa.

Page 52: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 52

�� The PSA and PS2:PS0 bits (OPTION<3:0>) The PSA and PS2:PS0 bits (OPTION<3:0>) determine the prescaler assignment and determine the prescaler assignment and prescaleprescale ratio.ratio.

�� When assigned to the Timer0 module, all When assigned to the Timer0 module, all instructions writing to the TMR0 register instructions writing to the TMR0 register (e.g.,(e.g., CLRF TMR0, MOVWF TMR0, BSF CLRF TMR0, MOVWF TMR0, BSF TMR0,xTMR0,x ....etc.) will clear the prescaler.....etc.) will clear the prescaler.

�� When assigned to WDT,When assigned to WDT, aa CLRWDTCLRWDTinstruction will clear the prescaler along with instruction will clear the prescaler along with the Watchdog Timer. the Watchdog Timer.

�� The prescaler is notThe prescaler is not readable or writable.readable or writable.

TMR0/WDT PreScaler ...TMR0/WDT PreScaler ...

Page 53: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 53

Timer0 Initialization Timer0 Initialization (Internal Clock Source)(Internal Clock Source)

CLRF TMR0 ; Clear Timer0 registerCLRF INTCON ; Disable interrupts and clear T0IFBSF STATUS, RP0 ; Bank1MOVLW 0xC3 ; PortB pull-ups are disabled,MOVWF OPTION_REG ; Interrupt on rising edge of RB0

; Timer0 increment from internal clock; with a prescaler of 1:16.

BCF STATUS, RP0 ; Bank0;** BSF INTCON, T0IE ; Enable TMR0 interrupt

;** BSF INTCON, GIE ; Enable all interrupts

;

; The TMR0 interrupt is disabled, do polling on the overflow bit

;

T0_OVFL_WAIT

BTFSS INTCON, T0IFGOTO T0_OVFL_WAIT

; Timer has overflowed

Page 54: CENG -336 Introduction to Embedded Systems …user.ceng.metu.edu.tr/~e1448331/CORG/Week6.pdfAfter each count up to 255, timer resets its value to zero and starts with a new cycle of

CENG-336 Introduction to Embedded Systems Development 54

Watchdog TimersWatchdog Timers

� Watchdog timers are used to guard a system against lock-up due to software errors or soft failures in hardware. � Often included in CPU supervisor circuits.

� Retriggering usually done in the main program loop.