ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 3145
Скачиваний: 0
6.3 Distinctive Features of the General-Purpose Timer of HC11 |
71 |
interrupt masks that, when set to 1 by software, allow ICxF and OCxF to generate interrupts.
The ICxF and OCxF flags are cleared by writing 1 to the corresponding position of the TFLG1 register. The interrupt service routine must clear the flag that generated the interrupt, otherwise a new interrupt is generated, right after the execution of the RTI (Return from Interrupt) instruction.
6.3.1.3 The Output Compare Operating Mode
The software initialization sequence for the output compare timers is very similar to that required by the input capture timers. It starts by configuring the associated lines of PORT A as output lines by writing 1 to the corresponding bits of DDRA.
Then the software must specify the action to be taken on compare match. The register TCTL1 (Timer Control Register 1) serves this purpose.
TCTL1 |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
OM2 |
OL2 |
OM3 |
OL3 |
OM4 |
OL4 |
OM5 |
OL5 |
|
RESET |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
OMx and OLx are, respectively, Output Mode and Output Level control bits associated to the OCx output. The effect of these bits is described in Table 6.3.
Table 6.3. The effect of the control bits in TCTL1
OMx OLx Action taken on compare match
00 Timer disconnected from output pin
01 Toggle OCx output line
10 Clear OCx output line to 0
11 Set OCx output line to 1
The event flags associated to OCx and the interrupt mask bits are located in the registers TFLG1 and TMSK1, described in the previous section.
To increase the flexibility of the HC11 timer, the TOC1 timer has been provided with the capability to simultaneously control two or more of the PORTA lines associated to the timer, i. e. PORTA [3–7]. Two additional registers have been provided for this purpose, called OC1M (OC1 Mask) and OC1D (OC1 Data).
Only five bits are implemented in these registers, as follows:
OC1M |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
OC1M7 |
OC1M6 |
OC1M5 |
OC1M4 |
OC1M3 |
– |
– |
– |
|
RESET |
0 |
0 |
0 |
0 |
0 |
– |
– |
– |
72 |
6 Using the MCU Timers |
||||||||
OC1D |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
|
OC1D7 |
OC1D6 |
OC1D5 |
OC1D4 |
OC1D3 |
– |
– |
– |
||
RESET |
0 |
0 |
0 |
0 |
0 |
– |
– |
– |
|
•OC1M indicates the lines of PORTA lines to be affected at the next OC1 match, and OC1D contain the data to be written to PORTA.
OC1Mx = 1 – line x of PORTA will be written with OC1D value at the next
compare match.
OC1Mx = 0 – line x of PORTA will not be affected by OC1.
Note that TOCx interrupts may be used without affecting the associated outputs of PORTA (OMx = 0, OLx = 0), only for generating interrupts at precise time intervals.
6.3.1.4 Counting External Events
The main timer of HC11 cannot count on the external clock. To solve the problem of counting external events, an additional 8-bit counter, called the Pulse Accumulator, has been provided. This is a supplementary, simplified timer, without the input capture and output compare registers, but having the ability to count either external pulses, applied on an input pin, or an internal clock.
The line PA7 is used as the pulse accumulator input (PAI) for the external clock. When configured to count on the internal clock, this clock has a fixed frequency E/64, and the PAI line is used to enable/disable counting.
The control register of this timer is PACTL, which has the following structure:
PACTL |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
– |
PAEN |
PAMOD |
PEDGE |
– |
I4/O5 |
RTR1 |
RTR0 |
|
RESET |
0 |
0 |
0 |
0 |
0 |
– |
– |
– |
•PAEN – Pulse Accumulator Timer Enable. Writing 1 in this position enables the entire subsystem.
•PAMOD – Pulse Accumulator Mode
PAMOD = 0. The pulse accumulator operates as an event counter, counting pulses
applied to the PAI input.
PAMOD = 1. The pulse accumulator operates in Gated Time Accumulation mode,
counting on an internal clock obtained by dividing the system clock E by 64. When PAMOD = 1, counting of the internal clock pulses is enabled by the logic level on the PAI line. PAMOD works in conjunction with the bit PEDGE, as shown in Table 6.4.
•PEDGE – Pulse Accumulator Edge control. In the Event Counter operating mode (PAMOD = 0), this bit selects the edge of the input signal that increments the counter. In Gated Time Accumulation mode (PAMOD = 1), PEDGE selects the level of the signal on the PAI input, which inhibits counting of the internal clock.
6.3 |
Distinctive Features of the General-Purpose Timer of HC11 |
73 |
|||
Table 6.4. Pulse accumulator operating modes |
|||||
PAMOD |
PEDGE |
Action on timer |
|||
0 |
0 |
PA counts on falling edge of PAI |
|||
0 |
1 |
PA counts on rising edge of PAI |
|||
1 |
0 |
PAI = 0 inhibits counting. |
|||
1 |
1 |
PAI = 1 inhibits counting. |
|||
The other bits in PACTL refer to other subsystems, or are unimplemented. There are two status bits associated with the pulse accumulator timer interface,
located in TFLG2:
•PAOVF – Pulse Accumulator Overflow Flag. This is automatically set when PA overflows from $FF to $00, regardless of the clock (internal or external) selected for counting. PAOVF is cleared by writing 1 in the corresponding position (bit 5) of the TFLG2.
•PAIF – Pulse Accumulator Input Edge Flag. This bit is automatically set at the detection of an edge (selected by PEDGE ) of the signal on the PAI input. It is cleared by writing 1 in the corresponding condition (bit 4) of TFLG2.
These two flags have associated interrupt mask bits in the TMSK2 register. When PAOVFI = 1 (bit 5 from TMSK2), the setting of PAOVF generates an interrupt. Similarly, when PAII = 1 (bit 4 from TMSK2), an interrupt is generated at the occurrence of the selected edge of the PAI input.
6.3.2 Exercises Regarding the Use of the General-Purpose Timer of HC11
SX 6.1
Write the initialization routine that enables the interrupts at the detection of a rising edge of a signal applied on PA0.
Solution
PA0 is associated with the input capture timer IC3 (see Table 6.1). The initialization sequence must configure this bit of PORTA as the input, along with the following additional operations:
•Select the rising edge of the signal on PA0, by writing the bits [EDG3B:EDG3A] in TCTL2 with [0:1].
•Enable TIC3 interrupts by setting to 1 the local mask IC3I (IC3 interrupt enable) in TMSK1
74 6 Using the MCU Timers
Here is the program sequence that performs these operations:
ITIC3 |
BCLR |
DDRA,$01 |
;PA0 input |
LDAA |
#$01 |
;[EDG3B:EDG3A]=[0:1] |
|
STAA |
TCTL2 |
;select rising edge |
|
BSET |
TFLG1,$01 |
;clear IC3F if any |
|
BSET |
TMSK1,$01 |
;enable TIC3 interrupts |
SX 6.2
Knowing that the external oscillator frequency is 8 MHz, write the initialization sequence and the interrupt routine to generate a 500-Hz clock on PA5.
Solution
PA5 is associated with TOC3. The initialization sequence must configure PA5 as output, define the action to be performed on the OC3 output at compare match, by writing [OM3:OL3] bits from TCTL1, and enable TOC3 interrupts. The interrupt service routine must clear the interrupt flag, and prepare the next interrupt by writing a new value in TOC3. For an 8-MHz frequency of the external oscillator the internal E clock has a frequency of 2 MHz (0.5 microseconds/period).
Since [PR1:PR0] bits in TMSK2 are cleared at RESET, the prescaler is configured to divide E by 1. An output frequency of 500 Hz, corresponds to a period of 2 milliseconds, i.e. 4000 periods of the E clock. The interrupt routine must add the constant 4000 to the current value of the register TOC3, and write the TOC3 register with the result. Thus, the next moment when TCNT matches the contents of TOC3 comes after 4000 E clock periods, which is equivalent to 2 ms.
The output line associated to TOC3 must be programmed to toggle at every compare match, by writing the control bits [OM3:OL3] in TCTL1 with [0:1].
Here is the initiation sequence that matches these requirements:
ITOC3 |
BSET |
DDRA,$20 |
;PA5 output |
LDAA |
#$10 |
;[OM3:OL3]=[0:1] |
|
STAA |
TCTL1 |
;toggle output selected |
|
BSET |
TFLG1,$20 |
;clear IC3F if any |
|
BSET |
TMSK1,$20 |
;enable TOC3 interrupts |
And the interrupt service routine for TOC3 is:
TOC3SVC |
BSET |
TFLG1,$20 |
;clear OC3F |
|||
LDD |
TOC3 |
;get |
current |
value of TOC3 |
||
ADDD |
#4000 |
;add |
4000 - 2 ms more |
|||
STD |
TOC3 |
;update |
TOC3 |
|||
RTI |
;return |
from interrupt |
||||
6.4 Distinctive Feature of the Timer of the AVR Microcontrollers |
75 |
SX 6.3
Write the initialization sequence for the pulse accumulator timer, so that it generates an interrupt every tenth rising edge of the signal applied on PA7.
Solution
The required initialization sequence must perform the following operations:
•Configure PORTA bit 7 as the input line.
•Enable the pulse accumulator timer.
•Select the event counter operating mode.
•Select the rising edge of the input signal as the active edge.
•Initialize the counter PACNT with 246, so that the tenth pulse produces an overflow.
•Enable the PAOVF interrupt by setting the PAOVFI bit in TMSK2. The resulting initialization sequence looks like this:
INIT_PA |
BCLR |
DDRA,$80 |
;PA7 input |
LDAA |
#$50 |
;PAEN = 1, PAMOD = 0, PEDGE = 1 |
|
STAA |
PACTL |
;write control register |
|
LDAA |
#246 |
||
STAA |
PACNT |
;init counter |
|
BSET |
TFLG2,$20 |
;clear PAOVF if any |
|
BSET |
TMSK2,$20 |
;enable PAOVF interrupts |
The interrupt routine must do the following:
•Clear the PAOVF flag, by writing 1 in position 5 of TFLG2.
•Write the constant 246 to PANCT, so that the next overflow occurs at the tenth pulse on PAI.
Below is the listing of the interrupt service routine that does this:
PAOVF_SVC BSET |
TFLG2,$20 |
;clear |
PAOVF |
|
LDAA |
#246 |
;write |
246 to PACNT |
|
STAA |
PACNT |
|||
... |
||||
RTI |
;return from |
interrupt |
||
6.4 Distinctive Feature of the Timer of the AVR Microcontrollers
Unlike the HC11 family, where the timer subsystem remains the same for all family members, for the AVR there may be significant differences in the implementation of the timer from one family member to another. This section contains the description of the timer subsystem of the microcontroller AT90S8515.
This, in fact, contains two distinct timers, named Timer0 and Timer1.