Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microcontrollers in Practice (Mitescu M., 2005)

.pdf
Скачиваний:
827
Добавлен:
12.08.2013
Размер:
1.25 Mб
Скачать

80

6 Using the MCU Timers

 

 

Here is the code to does all this:

 

 

*MASTER SPI initialization routine

 

 

.DEF

TEMP1=R16

;definitions

 

 

.DEF

R_INT=R1

;use r1 only for this

 

 

 

 

;interrupt

 

 

.EQU

KTOVF=131

;constant to load in TCNT0

 

INIT_T0:

LDI

TEMP1,$03

; prescaler divide by 64

 

 

OUT

TCCR0,TEMP1

 

 

 

LDI

TEMP1,KTOVF

;init TCNT0

 

 

MOV

R_INT,TEMP1

 

 

 

OUT

TCNT0,R_INT

 

 

 

LDI

TEMP1,$02

 

 

 

OUT

TIFR,TEMP1

;clear flag if any

 

 

OUT

TIMSK,TEMP1

;enable interrupt

 

 

RET

 

;end of initializations

 

;The interrupt service routine starts here

 

T0_ISR

OUT

TCNT0,R_INT

;reload counter

 

 

....

 

 

 

 

RETI

 

;return from interrupt

SX 6.5

Starting from an 8-MHz clock, use Timer1 in output compare mode to generate on OC1A a 500-Hz clock having 50% duty cycle.

Solution

For a 500-Hz clock, OC1A must toggle two times faster, i.e. at 1 KHz. The initialization sequence must select the clock for TCNT1, by writing the bits [CS12:CS11:CS10] in TCCR1B with [0:1:0], which corresponds to a division factor of 1 for CK.

Besides that, the CTC bit in TCCR1B must be set to force clearing of TCNT1 after each compare match. The OCR1A register must be initialized with 8000, and the action upon OC1A must be set to ‘toggle’, by writing $40 in TCCR1A.

Finaly, the compare match interrupt for OC1A must be enabled by writing $40 in TIMSK. The interrupt routine is only needed to clear the OCF1A flag, because TCNT1 is automatically cleared at compare match, and OCR1A remains unchanged after initialization.

6.5 Distinctive Features of the Timer System of the 8051 Microcontrollers

81

The program sequence that executes the operations mentioned above is:

*MASTER SPI initialization routine

 

.DEF

TEMP1=R16

;definitions

 

.EQU

KH=$1F

;higher byte of constant

 

.EQU

KL=$40

;lower byte of constant

INIT_T1:

LDI

TEMP1,$09

;prescaler divide by 1

 

OUT

TCCR1B,TEMP1

;and CTC1=1

 

LDI

TEMP1,$40

;toggle OC1A

 

OUT

TCCR1A,TEMP1

 

 

LDI

TEMP1,KH

;write higher byte first !!

 

OUT

OCR1AH,TEMP1

 

 

LDI

TEMP1,KL

 

 

OUT

OCR1AL,TEMP1

 

 

LDI

TEMP1,$40

;clear flag

 

OUT

TIFR,TEMP1

 

 

OUT

TIMSK,TEMP1

;and enable interrupt

 

RET

 

 

6.5 Distinctive Features of the Timer System of the 8051 Microcontrollers

The timer of the 8051 family of microcontrollers does not have the output compare and the input capture features. In the standard configuration, there are two timers, named Timer0 and Timer1, each having as central element a 16-bit counter, called T0 and T1, respectively.

These are capable of counting, on an internal or external clock, and are accessible from the internal bus for read and write, as two 8-bit registers: TH0–TL0 for Timer0, TH1–TL1 for Timer1.

The only event reported by timers to the CPU is the timer overflow condition. The logic diagram of the circuit for the clock selection and control is presented

in the Fig. 6.2.

OSC

:12

 

 

C/T=0

CK source

 

 

 

C/T=1

 

Ti pin

 

 

INTi

 

CONTROL

 

 

GATE

 

 

TRi

i=0,1

 

Fig. 6.2. Logic diagram of the clock control circuit for the 8051 timer

82 6 Using the MCU Timers

6.5.1 The Control and Status Registers of the Timer

Timer1 can operate in three distinct modes and Timer0 in four modes. The control bits in register TMOD select the timer operating mode and the clock applied to the counter. The structure of TMOD is detailed below:

TMOD

7

6

5

4

3

2

1

0

 

GATE

C/T

M1

M0

GATE

C/T

M1

M0

RESET

0

0

0

0

0

0

0

0

The two nibbles of this register are identical. The lower nibble refers to Timer0, and the upper nibble refers to Timer1.

[M1:M0] Mode select bits. These bits control the operating mode of the timer as described in Table 6.8.

Table 6.8. Selection of the operating mode for 8051 timers

M1 M0 Operating mode

00 Mode 0 – 13-bit timer/counter

01 Mode 1 – 16-bit timer/counter

10 Mode 2 – 8-bit auto reload timer/counter

11 Mode 3 for Timer0, Timer 1 stopped

C/T – Counter/Timer select bit.

C/T = 1 selects the external clock applied on T0 for Timer0, or T1 for Timer1. C/T = 0 selects an internal clock having the frequency fOSC/12.

GATE – Gated operation control bit.

GATE = 1 Counting is enabled by a logic level HIGH on the input pin INTi,

associated with the timer

GATE = 0. Counting is only conditioned by the TRi bit in TCON (Timer Control Register) register.

TCON – The timer control register has the following structure:

TCON

7

6

5

4

3

2

1

0

 

TF1

TR1

TF0

TR0

IE1

IT1

IE0

IT0

 

 

 

 

 

 

 

 

 

RESET

0

0

0

0

0

0

0

0

TFi – Timer i overflow flag. These bits are set by hardware when the counter overflows, and are automatically cleared at the execution of the associated interrupt service routine.

TRi – Timer Run control. This is set and cleared by software to start/stop counting. Refer to Fig. 6.2 for a description of the effect of this control bit.

ITI, IEi – These control bits are not related to the timer system.

6.5 Distinctive Features of the Timer System of the 8051 Microcontrollers

83

The overflow flags TF0, TF1 can generate interrupt requests, if the interrupts are enabled by setting the bits ET0, ET1 in the IE (Interrupt Enable) register. The IE register controls all the possible interrupts of 8051 and has the following structure:

IE

7

6

5

4

3

2

1

0

 

EA

ES

ET1

EX1

ET0

EX0

 

 

 

 

 

 

 

 

 

RESET

0

0

0

0

0

0

0

0

EA – Enable All. This is the global interrupt mask.

EA = 0 all interrupts are disabled.

EA = 1 each interrupt can be enabled/disabled individually.

ES – Enable Serial Port Interrupt

ET1 – Enable interrupts from Timer1.

ET1 = 1 The flag TF1 generates an interrupt request when set.

EX1 – Enable external interrupts from INT1

ET0 – Enable interrupts from Timer0 (TF0)

EX0 – Enable external interrupts from INT0

6.5.2 Description of the Timer Operating Mode 0

In operating mode 0, the counter Ti is 13-bits wide, with 5 bits in TLi, and 8 bits in THi. The clock can be either internal (timer mode) or external (counter mode). Selection between the internal or external clock is made by means of the C/T control bit in the TMOD register.

The overflow condition occurs at the transition from status $1FFF to $0000, and is indicated by setting the corresponding flag TFi to 1.

6.5.3 Description of the Timer Operating Mode 1

The logical diagram describing the 8051 timers operating in modes 0 and 1 is presented in Fig. 6.3. The only difference between mode 1 and mode 0 is that, in mode 1, the counter is 16-bit wide, and the overflow condition occurs at the transition from status $FFFF to $0000.

CK source

TLi

 

THi

 

 

TFi

 

 

 

Interrupt

 

5/8 bits

8 bits

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

EA

 

 

 

 

i=0,1

 

 

ETi

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Fig. 6.3. 8051 timer in mode 0/1

84 6 Using the MCU Timers

6.5.4 Description of the Timer Operating Mode 2

In mode 2, the lower half of the counter (TLi) operates as an 8-bit counter, while the upper half (THi) acts as a register that holds the reload value for TLi.

At overflow, the value written to THi is automatically transferred in TLi, and counting continues from this value. The logic diagram for the operating mode 2 of the 8051 timer is presented in Fig. 6.4. The clock source for the counter is selected as shown in Fig. 6.2.

CK source

TLi

 

 

TFi

 

 

 

 

Interrupt

 

8 bits

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Reload

 

 

 

 

 

 

 

 

 

 

 

 

EA

 

 

 

 

 

 

 

 

 

THi

 

 

 

 

 

 

 

i=0,1

 

 

 

 

 

 

 

 

8 bits

 

ETi

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Fig. 6.4. The 8051 timer operating in mode 2

6.5.5 Description of the Timer Operating Mode 3

This operating mode is specific only for Timer0. In mode 3, the counter T0 is split into two 8-bit counters that count on different clocks. The lower half, TL0, operates in a way similar to modes 0, and 1, but the length of the counter is limited to 8 bits. At overflow, the flag TF0 is set, and an interrupt is generated if ET0 = 1.

The upper half of T0, called TH0, acts like a second 8-bit counter, which counts a fixed frequency fOSC/12 clock. At overflow, TF1 is set, and an interrupt can be generated if ET1 = 1. The logic diagram of the timer in this operating mode is presented in Fig. 6.5.

OSC

:12

C/T=0

 

 

 

 

 

 

 

TL 0

TF 0

Interrupt

 

 

 

 

 

 

C/T=1

 

8 bits

T0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

pin

TR 0

 

Control

 

EA

 

 

 

 

 

 

 

 

 

 

GATE

 

 

 

 

ET 0

 

INT0

 

 

 

 

 

 

pin

 

 

 

 

 

 

 

 

 

 

TH 0

TF 1

Interrupt

 

 

 

 

8 bits

 

 

 

 

 

 

TR 1

Control

 

EA

ET 1

Fig. 6.5. The 8051 Timer in mode 3

6.5 Distinctive Features of the Timer System of the 8051 Microcontrollers

85

6.5.6 Using Timer1 as a Baud Rate Generator

Timer1 is used to generate the communication clock for the serial port. When the serial port is configured in mode 1 or 3, the baud rate is determined by the Timer1 overflow rate, according to the following formulas (SMOD is bit 7 in PCON):

When SMOD = 0 Baud = (Timer1_Overflow_Rate)/32

When SMOD = 1 Baud = (Timer1_Overflow_Rate)/16

Both internal or external clock sources may be selected; the only thing that counts is the overflow rate. In practice, for the usual baud rates, the use of the internal clock is recommended, and to program Timer1 in mode 2, autoreload. The software initialization sequence loads TH1 with the reload value and starts the timer. No further action is required for the software.

Timer0 can be configured to operate in mode 3, and use the control signals TF1, TR1, while Timer1 is used as baud rate generator.

Compared to the AVR and HC11 timers, the general-purpose timer of the 8051 is weaker, for at least two reasons:

The only event related to the timer reported to the CPU is timer overflow

The timer cannot directly control any of the MCU I/O lines.

These minuses have been corrected in the next generation of microcontrollers derived from 8051. The 80x52 family of microcontrollers includes an additional timer, called Timer2, which solves these problems.

6.5.7 Exercises for Programming the 8051 Timer

SX 6.6

Starting from an oscillator frequency of 11.059 MHz, write an initialization sequence to use Timer1 as a baud rate generator for 9600 baud.

Solution

The frequency of the internal clock is: fCOUNT = fOSC/12 = 0.92158 MHz. The frequency of the UART clock is: fUART = 16×Baud_rate = 16×9600 = 153 600 Hz.

The resulting overflow rate for Timer1 is:

Timer1_Overflow_Rate = fCOUNT/ fUART = 6 .

This gives the reload value for Timer1:

(TH1) = 255 − 6 + 1 = 250 = 0xFA .

The other initializations required refer to:

SMOD (bit 7 of PCON) must be set to 1

Select operating mode 2 for Timer1 (M1 = 1, M0 = 0)

Select the internal clock for Timer1 (C/T = 0)

Start the timer (TR1 = 1)

86 6 Using the MCU Timers

The resulting values for the registers involved are: PCON = 80h, TMOD = 20h, and TCON = 40h.

Here is the initialization sequence required:

INIT_T1:

MOV

PCON,#80H

;SMOD=1

MOV

TMOD,#20H

;C/T=0, M1=1, M0=0

MOV

TH1,#0FAH

;auto reload value

MOV

TCON,#40H

;TR1=1 -- start counting

RET

 

 

SX6.7

Starting from an oscillator clock frequency of 20 MHz, write the initialization sequence and the interrupt service routine to configure Timer0 in operating mode 1, to generate interrupts at 10-ms intervals.

Solution

In operating mode 1, with C/T = 0, T0 is a 16-bit counter, using the internal clock with a frequency of fCLOCK = fOSC/12, which corresponds to a period TCLOCK = 0.6 µs. The required 10-ms interval corresponds to a number of 10 000/0.6 = 16 666 TCLOCK periods. To overflow in 16 666 periods, the timer must start counting at the value: 65 535 − 16 666 + 1 = 48 870 = 0BEE6H.

The other initializations required concern selecting the operating mode 1 (M1 = 0, M0 = 1) in TMOD, and starting the timer, by setting TR1 = 1 in TCON. Finally the initialization sequence must enable a Timer0 interrupt, by setting the bits EA and ET0 in register IE. The interrupt service routine must reload the value 0BEE6H in TH0:TL0.

Here is the required initialization sequence:

INIT_T0:

 

 

MOV

TMOD,#01H

;C/T=0, M1=0, M0=1, Timer0

MOV

TH0,#0BEH

;TH0

MOV

TL0,#0E6H

;TL0

MOV

TCON,#20H

;TR0=1 -- start counting

MOV

IE,#82H

;enable interrupts

RET

 

 

The interrupt service routine must reload the initialization values in TH0:TL0. Since each MOV instruction takes two cycles to execute, the reload value must be decreased by 4, and becomes 0BEE2H.

MOV TH0,#0BEH ;TH0

MOV TL0,#0E2H ;TL0

6.6 PWM Timers. Principles of Operation

87

6.6 PWM Timers. Principles of Operation

A PWM signal is, basically, a signal with the duty cycle dynamically controlled. If this signal is passed through a low-pass filter, the output of the filter is the analog signal VOUT = K × A, where A is the amplitude of the PWM pulses, and K is the duty cycle.

This is a simple and cheap D/A converter, and therefore most recent microcontrollers include a dedicated PWM timer, or have the main timer designed with the capability to generate PWM signals.

The Motorola 68HC11 series K microcontrollers include a dedicated PWM timer, consisting of a free-running up-counter, PWCNT, whose content is permanently compared with two programmable registers, called PWPER and PWDTY. Refer to the block diagram of this timer, presented in Fig. 6.6.

PWPER defines the period of the output signal, and PWDTY controls the duty cycle of the PWM output. When the contents of the counter PWCNT match the contents of PWDTY, the control logic changes the polarity of the output signal, and when PWCNT reaches the value in PWPER, the counter is automatically cleared.

The register PWCTL contains control bits to select the frequency of the input clock for PWCNT, the polarity of the output signal, and enable the entire PWM system.

The operation of the PWM timer is synthetically presented in Fig. 6.7.

The series K microcontrollers 68HCHC11 include four 8-bit PWM channels. These can be configured to operate as two 16-bit PWM timers. The advantage of the structure presented in Fig. 6.6 is that it allows fine-tuning of the period of the output signal in a wide range.

The AVR family of microcontrollers uses Timer1 to generate PWM signals. The PWCNT counter is implemented using the least significant 8, 9, or 10 bits of TCNT1. There is no PWPER register, so that the period of the output signal can only be adjusted by selecting the frequency of the input clock.

The length of the PWCNT counter is software selectable, by means of the bits [PWM11:PWM10] in register TCCR1A. The functions of the PWDTY register are executed by the OCR1 register. The difference is that when operating as a PWM

 

 

 

 

 

 

 

 

 

 

Reset counter

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

PWPER

 

 

 

 

 

COMP1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Prescaller

 

 

PWCNT

 

 

 

 

 

 

 

Output

PWM out

 

 

 

 

 

 

 

 

 

 

logic

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

COMP2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

PWDTY

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

PWCTL

 

 

 

 

 

 

 

 

 

 

 

Internal bus

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Fig. 6.6. Simplified block diagram of the PWM timer of 68HC11 series K

88 6 Using the MCU Timers

PWCNT

PWPER

PWDTY

Time

PWMOUT

Time

Fig. 6.7. Functional diagram of the PWM timer of 68HC11 series K

timer, TCNT1 is forced to be reversible. It counts up from $0000 to a TOP value, determined by the length of the counter (8, 9, or 10 bits). When it reaches the TOP value, it starts counting down to zero. The polarity of the output signal is changed

Prescaller

Up Down

PWCNT COMP

Output PWMOUT

logic

PWDTY

PWCTL

Internal bus

Fig. 6.8. Simplified block diagram of the PWM AVR timer

PWCNT

TOP

PWDTY

Time

PWMOUT

Time

Fig. 6.9. Functional diagram of the PWM AVR timer

6.7 Watchdog Timers

89

in opposite directions, when TCNT1 matches the value of OCR1 when counting upwards, and downwards. Refer to Fig. 6.8, and 6.9 for details of the operation of the PWM AVR timer.

This PWM system is far less flexible than that of the HC11, but it is simple and cheap, so that it has been implemented in many AVR microcontrollers.

6.7 Watchdog Timers

The block diagram of a watchdog timer is presented in Fig. 6.10. The system consists of a counter, having the overflow time programmable in a range from a few milliseconds to a few seconds. When the watchdog overflows, a hardware RESET is generated.

CLK

COUNTER

 

MUX

 

OVERFLOW

MCU RESET

 

 

 

 

DETECT

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

RESET COUNTER

CONTROL

REGISTER

Internal bus

Fig. 6.10. Block diagram of the watchdog timer

If the watchdog is enabled, the program running on the microcontroller must be organized so that, periodically, at time intervals shorter than the overflow time, it resets the watchdog’s counter, otherwise a hardware RESET is generated.

The control register is used to enable the watchdog, to select the overflow time, and to reset the counter.

6.7.1 The Watchdog of HC11

The watchdog system of HC11 is called the COP Timer (Computer Operating Properly Timer).

The watchdog overflow time is selected by means of the control bits [CR1:CR0] (COP Rate select) in the OPTION register, as shown in Table 6.9. [CR1:CR0] can only be written during the first 64 E clock cycles, after RESET. This artifice is intended to prevent unintentional modification of the watchdog settings in case of program runaway.