chapter 10 - v3.1.pdf

15
110 Chapter 10 Programmable Timer Subsystem The timer section in the M68HC11 is based ona 16-bit counter operating from the system E- clock. It provides basic real time functions with the following features: Timer overflow to extend the 16-bit capability of the timer section counter. Output compare function that can generate a variety of waveforms by comparing the 16- bit timer counter with the contents of a programmable register. Input capture function that can latch the value of the 16-bit timer counter on selected edges of external control signals. Programmable and periodic interrupt generator, called the real time interrupt. Pulse accumulator to count external events or act as a gated timer counting internal clock pulses. Computer operating properly (COP) watchdog timer. The timer subsystem is the most complex subsystem in the M68HC11 and it involves many control registers and control bits. All timer functions have interrupt controls and separate interrupt vectors. Figure 10.1 illustrates the timer subsystem block diagram. Timer subsystem registers: Data registers TCNT Timer count register TIC1-TIC3 Timer input capture registers 1 to 3 TIC1-TIC5 Timer output compare registers 1 to 5 PACNT Pulse accumulator count register Control registers TCTL1 Timer control register 1 (output compare specifications) TCTL2 Timer control register 2 (input capture edge specifications) TMSK1 Main timer interrupt mask register 1 (output compare and input capture interrupt enable bits) TMSK2 Miscellaneous timer interrupt mask register 2 (other interrupt enable bits) PACTL Pulse accumulator control register OC1M Action mask register OC1D Action data register Status registers TFLG1 Main timer interrupt flag register 1 (output compare and input capture flags) TFLG2 Miscellaneous timer flag register 2 (other system flags) For the names and positions of various control and status bits see also Fig 11.1 in your reference book (Spasov).

Upload: frew-frew

Post on 06-Dec-2015

232 views

Category:

Documents


1 download

TRANSCRIPT

110

Chapter 10

Programmable Timer Subsystem The timer section in the M68HC11 is based ona 16-bit counter operating from the system E-clock. It provides basic real time functions with the following features: • Timer overflow to extend the 16-bit capability of the timer section counter. • Output compare function that can generate a variety of waveforms by comparing the 16-

bit timer counter with the contents of a programmable register. • Input capture function that can latch the value of the 16-bit timer counter on selected

edges of external control signals. • Programmable and periodic interrupt generator, called the real time interrupt. • Pulse accumulator to count external events or act as a gated timer counting internal clock

pulses. • Computer operating properly (COP) watchdog timer. The timer subsystem is the most complex subsystem in the M68HC11 and it involves many control registers and control bits. All timer functions have interrupt controls and separate interrupt vectors. Figure 10.1 illustrates the timer subsystem block diagram. Timer subsystem registers: Data registers TCNT Timer count register TIC1-TIC3 Timer input capture registers 1 to 3 TIC1-TIC5 Timer output compare registers 1 to 5 PACNT Pulse accumulator count register Control registers TCTL1 Timer control register 1 (output compare specifications) TCTL2 Timer control register 2 (input capture edge specifications) TMSK1 Main timer interrupt mask register 1 (output compare and input capture

interrupt enable bits) TMSK2 Miscellaneous timer interrupt mask register 2 (other interrupt enable bits) PACTL Pulse accumulator control register OC1M Action mask register OC1D Action data register Status registers TFLG1 Main timer interrupt flag register 1 (output compare and input capture flags) TFLG2 Miscellaneous timer flag register 2 (other system flags) For the names and positions of various control and status bits see also Fig 11.1 in your reference book (Spasov).

111

Fig. 10.1 Main timer system block diagram

Port A Pins If the timer function is not enabled Port A pins can be used as general input/output pins. Otherwise port A pins have certain other functionality related to the specified timer subsection operation. Basic Timer The key to the operation of the M68HC11 is the 16-bit, free running counter called the TCNT. It is input is the system E-clock, which may be prescaled (divided) by 1, 4, 8 or 16. Starting from $0000 at system reset, the counter counts forever and the programmer can read its

112

contents at any time. When it reaches $FFFF, it is reset to $0000 and the timer overflow flag (TOF in TFLG2) is set. This state can be detected either by polling or by an interrupt if timer overflow interrupt enable (TOI in TMSK2) bit is set. Clearing Timer Flag The flag bits in both of the status registers TFLG1 and TFLG2 are cleared by writing a 1 to the bit to be cleared. For example to clear TOF, you may use the following instructions: LDAA #$80 STAA TFLG2, X (assuming IX=$1000 and TFLG2 is the offset relative to

the base register address) Example: LDS #STACK LDAA #%10000000 STAA TFLG2 ; clear the TOF bit REPEAT LDAA #NTIMES STAA COUNTER ; init the counter variable SPIN1 TST TFLG2 BPL SPIN1 ; wait until TOF (bit-7 in TFLG2) is set LDAA #%10000000 STAA TFLG2 ; reset TOF DEC COUNTER ; decrement the counter BNE SPIN1 JSR RING_A_BELL ; ring a bell if the counter reaches to 0 BRA REPEAT ; continue by re-initializing the counter The above program rings a bell every NTIMES*65536 E-clock cycles assuming a prescaling factor of 1 (NTIMES*32.768 ms with a 2 Mhz E-clock). The same example with interrupt driven approach: Timer overflow interrupt vector: $FFDE:$FFDF Buffalo monitor vector jump table (pseudovector) for timer overflow interrupt: $D0:$ D2)

ORG $00D0 JMP ISR ; set the timer overflow interrupt pseudo vectors ORG PROG

LDS #STACK CLR DATA

LDAA #%10000000 STAA TFLG2 ; reset TOF flag STAA TMSK2 ; enable the timer interrupts (set TOI bit) CLI ; enable processor interrupts START BRA START ISR INC DATA ; increment DATA

LDAA DATA

113

CMPA #NTIMES ; check if it is equal to NTIMES BNE ENDIF

JSR RING_A_BELL ring a bell if DATA is equal to NTIMES

CLRA STAA DATA ; reinitialize DATA

ENDIF LDAA #%10000000 STAA TFLG2 ; reset TOF RTI Output Compare The output compare allows more accurate timing delays than the timer overflow flag. The following figures describe the output compare operation

10.2

10.3

114

A 16-bit timer output compare register, TOCn, may be written by the program with a double-byte store instruction, e.g., STD TOC5. The other 16-bit compare register is the free-running TCNT counter. A comparison is made at every bus clock cycle (E-clock cycle) and when the TOCn is identical to TCNT, the output compare flag, OCnF, is set. OCnF is also ANDed with an output compare interrupt eneable bit to generate an interrupt. The output compare functions are controlled by several registers as follows:

115

Example: Generate a 10 msec (which is less than the minimum timer overlow duration) pulse assuming a 2 Mhz E-clock. * Drive one-shot high pulse for 10 ms * with E = 2 MHz and prescale = 1 ORG $100 PWIDTH EQU 20000 LDD TCNT,X ;prevent premature STD TOC2,X ;OC2 compare PULSE BSET PORTA,X $40 ;drive PA6/OC2 high LDAA #$80 ;configure OC2 to clear STAA TCTL1,X ;and disconnect other OCx's LDAA #$40 ;clear OC2F if set STAA TFLG1,X LDD TCNT,X ;arm TOC2 for 10-ms trigger ADDD #PWIDTH-17 STD TOC2,X PULSE1

BRCLR TFLG1,X $40 PULSE1 ;wait for trigger by polling for OC2F high ;now OC2 becomes low automatically BCLR PORTA,X $40 ;clear internal latch bit for PA6 and LDAA #$40 ; STAA TFLG1,X ; then clear OC2F BCLR TCTL1,X $80 ;disconnect OC2 * BRA * ;end for now

116

One Output Compare Controlling Up to Five Outputs The Output Compare 1 channel has special features that are controlled by the OC1M and OC1I registers. Output Compare 1 can simultaneously switch up to five outputs.

OC1M and OC1D work together to define the action taken on port A, bits 7-3. OC1M is a mask register and a 1 in a bit position in the mask means that the corresponding data bit in the data register, OC1D, is transferred to the output bit in port A. The transfer from OC1D to port A occurs when a successful output comparison is made. Thus up to five bits can be simultaneously changed by one output comparison. Note that Output Compare 1 can be used with another output compare channel to produce very short duration pulses (as short as one E-clock) (see the example program below). Forced Output Compares CFORC register bit is ORed with the output compare flag. Therefore, writing a one to this register forces a comparison action to occur at the output pins. This forced comparison does not set the output compare flag and therefore no interrupt will be generated.

,

The above figure illustrates some of the capabilities of the timer subsection. Using the output compare function, it is possible to generate a one shot or a fixed or variable duty cycle continuous waveforms.

117

Example: A 2 µs (very short duration) pulse generation at every 32.768 ms on port A bit-6.

TCNT EQU $0E TFLG1 EQU $23 TCTL1 EQU $20 TOC1 EQU $16 TOC2 EQU $18 OC1M EQU $0C OC1D EQU $0D

PROG EQU $C000 DATA EQU $D000 STACK EQU $DFFF

REGS EQU $1000

ORG PROG LDS #STACK LDX #REGS

LDD TCNT, X ; grab the value of the TCNT register STD TOC1, X ADDD #4 ; set TOC2 to compare 4 cycles later STD TOC2, X

LDAA #%11000000 ; reset output compare flags STAA TFLG1, X LDAA #%01000000 ; init the data and mask registers STAA OC1D, X STAA OC1M, X LDAA #%10000000 ; setup OC2 to reset the bit STAA TCTL1, X

SPIN

BRCLR TFLG1, X %01000000 SPIN ; wait until OC2F is set LDAA #%11000000 ; reset OC1F, OC2F STAA TFLG1, X BRA SPIN ; continue

118

Input Capture

The input capture hardware is shown above. Three 16-bit Timer Input Capture registers, TIC1-TIC3, latch the value of the free-running counter in response to a program-selected, external signal. For example the length of a positive pulse can be measured by capturing the time at the rising edge and then again at the falling edge.

The above figure illustrates again some of the capabilities of the timer subsection. Using the input capture function, it is possible to make pulse width measurements, period measurements or count pulses.

119

120

Example: Write a subroutine, which determines the period of a square wave in units of clock cycles (assume that the period is <65536 cycles).

REGS EQU $1000 TCTL2 EQU $21 TFLG1 EQU $23 TIC1 EQU $10

FIRST RMB 2

GET_PERIOD PSHX LDX #REGS

LDAA #%00010000 ; init IC1 for rising edge STAA TCTL2, X LDAA #%00000100 ; reset IC1 flag STAA TFLG1, X

SPIN1 BRCLR TFLG1, X %00000100 SPIN1 ; wait for the next rising edge by waiting IC1F LDD TIC1, X ; get the count that was latched STD FIRST ; store it LDAA #%00000100 ; reset IC1 flag STAA TFLG1, X

SPIN2 BRCLR TFLG1, X %00000100 SPIN2 ; wait for the next rising edge LDD TIC1, X ; get the count that was latched SUBD FIRST ; store it BPL EXIT ; correct if the result is negative ADD #$FFFF

EXIT PULX RTS

Stepper Motor Control Using Output Compare Stepper motors are electric motors that rotate from one position to the next in dicsrete intervals (steps). A step refers to a fraction of a full 360 degrees of the motor shaft (or rotor) rotation. The primary stepper motor application is position control. A common stepper motor type is the four-phase stepper motor.

121

In four-phase stepper motors, the direction (polarity) of the current through the windings determines which position the motor steps to next. A high level of current called the rush current is required to cause shaft rotation. Each change of polarity at the terminal of a winding is called a step of phase shift. A sequence of logic pulses steps the motor from one position to the next.

This causes the shaft (rotor) to rotate in precise angular increments per step. The phase sequence may begin anywhere but it must continue in the specified order. For example, the following waveform rotates the motor for 10 steps clockwise.

During the period when no rotation occurs, the motor windings must be supplied with a lower level of current called the hold current. Since high currents are required to drive the stepper motor, a MOSFET latch with open drain outputs may be used to supply pulses to the windings. TPIC2406 Intelligent Power Quad MOSFET Latch by Texas Instruments is one such example.

122

Stepper Motor Control Utility Subroutines * Subroutine INITOC4 * Initializes OC4 to drive four-phase stepper motor * Calling Register * IX = register block address * No return registers except that CCR affected ORG $180 INITOC4

PSHA ;preserve registers PSHB BCLR TCTL1,X $0C ;OM4:OL4=0:0 LDD TCNT,X ;avoid premature compare STD TOC4,X LDAA #$10 ;clear OC4F is set STAA TFLG1,X STAA TMSK1,X ;OC4I=1 to enable interrupt CLI PULB ;restore registers PULA RTS ;return

123

* Listing 11.5 * Service routine RTOC4 * Drives stepper motor to next step. * It uses a look-up table to find the output pattern of the current step. * Then it drives the outputs and moves the pointer to the next step. * It also increments a step counter (STEPCNT) for use by an application program to keep * track of number of steps driven. * Driver Outputs: * MT1 <--- PB0 * MT2 <--- INV PB0 * MT3 <--- PB1 * MT4 <--- INV PB1 * RUSH <-- PB2 * Global variables * STEPLEN = time duration of step * STEPPTR = pointer to step in sequence * STEPCNT = step counter STEPLEN EQU $0A STEPPTR EQU $0C STEPCNT EQU $0D RTOC4 LDX #REGBAS ;point to registers LDD STEPLEN ;schedule next step ADDD TOC4,X STD TOC4,X BCLR TFLG1,X $EF ;clears flag OC4F LDY #STEPTAB ;point to sequence for this step LDAB STEPPTR ABY LDAA PORTB,X ;get previous drive outputs and ANDA #$FC ;modify by clearing last two bits ORAA 0,Y ;then change them as per table STAA PORTB,X ;and update drive outputs INC STEPPTR ;stepptr ++ LDAB STEPPTR ;if stepptr == 4 CMPB #4 ;(note stepper mtr has four phases) BNE SKIP CLR STEPPTR ;then stepptr = 0 SKIP INC STEPCNT ;update stepper mtr count RTI ;return * Stepper motor sequence look-up table * PB2 is high to supply rush current STEPTAB STEP1 FCB %101 STEP2 FCB %111 STEP3 FCB %110 STEP4 FCB %100

124

* Listing 11.6 * Demonstration of utility routines to drive * stepper motor for 16 steps using EVB board. * REMINDER, see template listing in Appendix E for * necessary overhead, such as segment definition, * stack setup, and interrupt vectors * ---------------------------------------------------------- ORG $0A STEPLEN FCB $01, 00 ;period of time for each step STEPPTR RMB 1 ;points to step sequence in STEPTAB STEPCNT RMB 1 ;for use by application program to * ;keep track of number of steps * Remove above definitions from Listing 11.5 * ---------------------------------------------------------- ORG $100 * Stepper motor control part LDD STEPLEN ;init stepper motor step time CLR STEPPTR ;define first step LDX #REGBAS ;point to registers BSET PORTB,X $04 ;supply rush current JSR INITOC4 ;initialize OC4 for stepper motor control CLR STEPCNT ;init step counter * OC4 interrupt drives program here LOOP LDAA STEPCNT ;for 16 steps CMPA #16 BNE LOOP BCLR TMSK1,X $10 ;then disable OC4 interrupt BCLR PORTB,X $04 ;and supply hold current BRA * ;end of demo for now