PWM Using Hco5
PWM Using Hco5
by AN1734/D
AN1734
Introduction
By using the 16-bit timer counter and its output compare function, a
PWM of desired duty cycle and frequency can be quickly and easily
implemented. This method uses modules common in most Motorola
microcontrollers and requires only a small amount of processing
overhead.
PWM
A PWM signal is a signal with a fixed frequency and variable on and off
times. In other words, the period of the signal remains constant, but the
amount of time that the signal remains high and low can vary within a
period. The duty cycle is the ratio of on time (tOn) to the total period
(t = tOn + tOff).
tOn tOff
AN1734
2 MOTOROLA
Application Note
Output Compare Method
tOff
tOn
The 16-bit timer and its output compare function can be used to produce
a specific output level at a particular time. When the output of the timer
compare (TCMP) pin is alternated between low and high logic levels, the
result is a periodic waveform. Values in the output compare register are
used to cause a delay between output events. Specifically, tOn and tOff
of a desired duty cycle can be calculated and programmed into the
output compare function.
AN1734
MOTOROLA 3
Application Note
2. Set or clear the output level bit (OLVL) in the timer control register
(TCR). This will determine the TCMP pin's output level for the next
valid compare.
3. Set the output compare interrupt enable bit (OCIE) in the TCR to
enable output compare interrupts.
4. Clear the interrupt mask.
5. On timer interrupt:
a. Complement the OLVL bit for next compare.
b. Compute and store new value for output compare register.
Period: t = 1/f
On time: tOn = DC * t
Next, the frequency of the timer subsystem should be calculated. For the
HC05 Family of MCUs to find the MCU internal operating frequency, fop,
divide the oscillator frequency, fosc, by 2. The timer prescaler divides this
frequency further by 4. Therefore, the timer subsystem frequency, ftim, is
fosc/8.
AN1734
4 MOTOROLA
Application Note
Output Compare Method
Once the timer period, ttim, is found, compute the number of timer cycles
required to cause a specific delay. This delay can be used to time an
output. In the case of generating a PWM signal, the high and low levels
of the output need to be timed. Specifically, the on and off delay values
should be calculated. These delays reflect the number of timer cycles
necessary to produce a pulse of a specific duration.
The delay values are proportional to the on and off times of the desired
PWM signal, and are defined as:
Using the previous example of a 4-MHz oscillator, 2-kHz PWM, and 50%
duty cycle, DOn and DOff would each require a decimal value of 125.
The delay values DOn and DOff can be added to the timer counter, and
the result will be stored in the output compare register. On a valid
compare (when the contents of the counter match the output compare
register), an output compare interrupt can be generated. This interrupt
can then be used to prepare the MCU for the next pulse segment delay
and its associated output level.
In MCUs equipped with output compare (TCMP) pins, the logic level to
be output on a valid compare can be preset. In this way, an output
compare timer interrupt will set the output level of the TCMP pin
immediately. A new value for the output compare register to time the
current output level can then be loaded, and the next triggered output
level set. In this manner, a highly accurate PWM output can be achieved,
as the output level is set automatically on a valid compare.
If a microcontroller does not have a TCMP pin, the same procedure for
setting up output compares can be followed. The timer interrupt service
routine can be programmed to manually set the PWM output level at the
pin of an I/O port. In this case, the software would set the current pulse
segment's output level and delay, rather than the next segment's output
level and the current segment's delay.
AN1734
MOTOROLA 5
Application Note
NOTE: When using this method, one must consider the latency of entering the
interrupt routine when calculating the necessary delays. The time
between a valid compare and the manual setting of the output level in
this case is not always negligible.
AN1734
6 MOTOROLA
Application Note
Output Compare Method
Timing limits
fosc Minimum delay( s) Minimum timer change
fop (MHz) s)
top ( ftim (kHz) s)
ttim (
(MHz) = (interrupt cycles)*top = (interrupt cycles)/4
8 4 0.25 1000 1 13 13
4 2 0.5 500 2 26 13
2 1 1 250 4 52 13
1 0.5 2 125 8 104 13
AN1734
MOTOROLA 7
Application Note
When a new A/D value is ready, the software determines the difference
between the new DOn (the ADDR value) and the current DOn. If the new
A/D value does differ from the DOn, it can be determined whether the
duty cycle should increase or decrease.
AN1734
8 MOTOROLA
Application Note
Sample Application Motor Controller
24 V
VDD
FAN
1N4002 MOTOR
470 WINDING
VDD
VRH
10 k AN0
FAN: PANAFLO DC BRUSHLESS,
0.1 F FBP 08B24L
24 Vdc, 0.09A
When the duty cycle is altered through the potentiometer, the new DOn
and DOff is calculated. However, before these new values are recorded,
they must be checked against the upper and lower delay limits. If they
exceed these limits, no change is made in the delays.
Because an 8-bit delay value is used, the upper limit on delays is $FF.
There is also a lower limit on delays. The time it takes to process the
timer output compare interrupt routine determines this limit. Output
compare interrupts should not happen faster than they can be serviced.
In this example, the worst case interrupt service time is 52 cycles. With
an internal operating frequency of 2 MHz, the time required is 26 s.
Because the 16-bit timer runs at 500 kHz, its period is 2 s. Therefore,
the minimum delay this interrupt routine can tolerate is 13 "ticks" of the
free-running timer (13 * 2 s = 26 s). Keeping track of this lower limit
ensures an accurate and clean output waveform.
AN1734
MOTOROLA 9
Application Note
Conclusion
AN1734
10 MOTOROLA
Application Note
Conclusion
INITIALIZE ADC
GET INITIAL VALUE
SET UP INITIAL DUTY
CYCLE
LOOP
NO
YES
YES
IS NEW VALUE
CALL DUTY UP THE SAME AS OLD CALL DUTY DOWN
VALUE OF
ON DELAY?
NO
SUBTRACT OLD
ON DELAY FROM
NEW VALUE
IS RESULT
YES NO
POSITIVE?
(NEW > OLD)
AN1734
MOTOROLA 11
Application Note
DUTY DOWN
DELTA = |DELTA|
YES
EXCEEDED $FF?
NO
DLIMIT1
YES
ADC DATA < DELAY MIN? OFF DELAY = DELAY MAX
ON DELAY = DELAY MIN
NO
STORE NEW
ON DELAY,
STORE NEW
OFF DELAY
RTS
AN1734
12 MOTOROLA
Application Note
Conclusion
DUTY UP
NO
OFF DELAY =
OFF DELAY DELTA
DLIMIT2
YES NEW OFF DELAY <
ON DELAY = DELAY MAX
DELAY MIN
OFF DELAY = DELAY MIN
NO
RECORD NEW
OFF DELAY
ON DELAY =
ADC DATA
RTS
AN1734
MOTOROLA 13
Application Note
TIMERINT
TIMER INTERRUPT
GO HIGH GO LOW
SET OLVL BIT FOR CLEAR OLVL BIT FOR
NEXT OUTPUT LEVEL NEXT OUTPUT LEVEL
CLEAR OCF
RTI
AN1734
14 MOTOROLA
Application Note
Code Listing
Code Listing
************************************************************************************
* DCVAR.ASM *
* This program illustrates the use of the 16-bit free-running timer to *
* generate a Pulse Width Modulated (PWM) signal using a Motorola HC05 *
* MCU. *
* *
* This example was written for the M68HC05P9 microcontroller. As shown *
* in the accompanying application note, this program is used in *
* conjunction with a potentiometer connected to an ADC channel to control *
* the duty cycle of a fixed-frequency PWM signal, which appears on the *
* TCMP pin. *
* *
* The loop portion of the program reads in the value of the A/D data *
* register. When the contents of the data register do not match the *
* OnDelay of the program, an adjustment is made to OnDelay and OffDelay *
* and the duty cycle of the PWM signal is changed. *
* *
* OnDelay is determined and controlled by the value in the A/D data *
* register (ADDR). *
* *
* The PWM signal is achieved by keeping track of two variables, OnDelay *
* and OffDelay. These two values determine the amount of time the output *
* signal is kept high or low. The timer routine reads the free-running *
* timer, adds the delay amount to it, and stores the result in the output *
* compare register. When the free-running timer matches the value in the *
* output compare register, the TCMP pin is toggled, and new delay values *
* are computed. *
* *
* The maximum delay value is $FF, due to the use of an 8-bit value. *
* The minimum delay value is $0D, determined by the time it takes to *
* execute the timer interrupt routine. In this case it is around 52 bus *
* cycles, which at an internal operating frequency of 2 MHz is ~26 s. *
* Because the timer frequency is the operating frequency divided by 4, *
* the minimum delay value for the timer becomes the interrupt latency *
* divided by 4. *
***********************************************************************************
* Equate statements for assembler
***********************************************************************************
* Registers
TCR EQU $12 ;Timer control register
TSR EQU $13 ;Timer status register
OCH EQU $16 ;Output compare registers
OCL EQU $17 ;(High/Low Bytes)
ACRH EQU $1A ;Alternate counter registers
ACRL EQU $1B ;(High/Low Bytes)
ADDR EQU $1D ;A/D data register
ADSCR EQU $1E ;A/D status and control register
AN1734
MOTOROLA 15
Application Note
* Bit positions
OCIE EQU $06 ;OCIE bit position in TCR
OLVL EQU $00 ;OLVL bit position in TCR
CCF EQU $07 ;CCF bit position in ADSCR
* PWM definitions
* Minimum achievable delay is determined by the timer interrupt latency
* Maximum achievable delay is determined from period - (minimum delay)
DELAYMIN EQU $0D ;Minimum achievable delay
;determined by interrupt latency
DELAYMAX EQU $ED ;Maximum delay
;(HALFPERIOD*2) - (DELAYMIN)
***********************************************************************************
* RAM Variables
***********************************************************************************
ORG RAMSPACE ;Start of user RAM
OnDelay RMB 1 ;PWM variables
OffDelay RMB 1
ADCData RMB 1
Delta RMB 1
TempA RMB 1 ;Temporary storage variable
***********************************************************************************
* Main Program
* Setup timer interrupts and analog-to-digital converter
***********************************************************************************
ORG ROMSPACE ;Start of user ROM
Setup LDA #$20
STA ADSCR ;Turn on A/D and select channel AN0
LDA #HALFPERIOD
STA OnDelay ;Record delay variables
STA OffDelay
Init BRCLR CCF,ADSCR,Init
LDA ADDR ;Get initial potentiometer reading
STA ADCData ;Record value
BSR UpdateDC ;Update the PWM duty cycle
BSET OLVL,TCR ;Set to output high on first compare
LDX ACRH ;Get the current timer value,
LDA ACRL ;store in X and A
AN1734
16 MOTOROLA
Application Note
Code Listing
***********************************************************************************
* Increase the duty cycle; increase OnDelay, decrease OffDelay
***********************************************************************************
DutyUp STA Delta ;Record difference
LDA OffDelay
CMP Delta ;Compare OffDelay and difference
BLO Dlimit2 ;If OffDelay < difference, at minimum
SUB Delta ;If OffDelay >= difference, go ahead
CMP #DELAYMIN ;See if minimum delay was violated
BLO Dlimit2 ;If so, record the limit
AN1734
MOTOROLA 17
Application Note
************************************************************************************
* Timer Interrupt Routine
* This routine services the output compare interrupt.
* The latency of this routine determines the minimum delay possible.
************************************************************************************
* NOTE:
* If other timer interrupts are active, need to arbitrate them
* before servicing the output compare interrupt
************************************************************************************
TimerInt BRSET OLVL,TCR,GoLow ;Determine current output level
GoHigh BSET OLVL,TCR ;Setup OLVL for high next time
LDA OCL ;Setup next output compare time
ADD OffDelay ;Add off delay offset
BCC NoCarry ;Compensate for 8-bit carry
BRA Carry
GoLow BCLR OLVL,TCR ;Setup OLVL for low next time
LDA OCL ;Setup next output compare time
ADD OnDelay ;Add on delay offset
BCC NoCarry ;Compensate for 8-bit carry
Carry INC OCH
NoCarry LDX TSR ;Clear OCF bit
STA OCL ;Ready for next compare
RTI ;Return from interrupt
************************************************************************************
* Vector Definitions
************************************************************************************
ORG TIMERVEC ;Timer interrupt vector
FDB TimerInt
ORG RESETVEC ;Reset vector
FDB Setup
AN1734
18 MOTOROLA
Application Note
Code Listing
AN1734
MOTOROLA 19
Application Note
R E Q U I R E D
A G R E E M E N T
N O N - D I S C L O S U R E
Motorola reserves the right to make changes without further notice to any products herein. Motorola makes no warranty, representation or guarantee regarding the suitability of its
products for any particular purpose, nor does Motorola assume any liability arising out of the application or use of any product or circuit, and specifically disclaims any and all liability,
including without limitation consequential or incidental damages. "Typical" parameters which may be provided in Motorola data sheets and/or specifications can and do vary in different
applications and actual performance may vary over time. All operating parameters, including "Typicals" must be validated for each customer application by customer's technical experts.
Motorola does not convey any license under its patent rights nor the rights of others. Motorola products are not designed, intended, or authorized for use as components in systems
intended for surgical implant into the body, or other applications intended to support or sustain life, or for any other application in which the failure of the Motorola product could create a
situation where personal injury or death may occur. Should Buyer purchase or use Motorola products for any such unintended or unauthorized application, Buyer shall indemnify and hold
Motorola and its officers, employees, subsidiaries, affiliates, and distributors harmless against all claims, costs, damages, and expenses, and reasonable attorney fees arising out of,
directly or indirectly, any claim of personal injury or death associated with such unintended or unauthorized use, even if such claim alleges that Motorola was negligent regarding the
design or manufacture of the part. Motorola and are registered trademarks of Motorola, Inc. Motorola, Inc. is an Equal Opportunity/Affirmative Action Employer.
AN1734/D