AN2475
AN2475
Application Note
AN2475/D
3/2003
Introduction
This document addresses the use of the internal timer and analog-to-digital
converter (ADC) modules of the MC68HC908QY/QT Family of microcontrollers
(MCUs). In the application, an analog signal is converted to a digital word
through the ADC to set the duty cycle of a pulse-width modulated (PWM)
signal. The PWM duty cycle varies from 0 to 100% in accordance with the
voltage applied at the ADC input. Hence, the PWM signal corresponds to the
digital conversion of the analog input. The PWM signal is fed back to build a
close-loop system for providing voltage regulation in a simple, very flexible
way.
© Freescale Semiconductor, Inc., 2004. All rights reserved. © Motorola, Inc., 2003
Voltage Conversion When the input voltage to the ADC equals VDD, the ADC converts the signal to
$FF (full scale). If the input voltage equals VSS, the ADC converts it to $00.
Input voltages between VDD and VSS are a straight-line linear conversion.
All other input voltages will result in $FF if greater than VDD and $00 if less
than VSS.
Conversion Time Sixteen internal clock cycles are required to perform one conversion. The ADC
starts a conversion on the first rising edge of the ADC internal clock
immediately following a write to the ADC status and control register (ADSCR).
If the internal clock is selected to run at 1 MHz, then one conversion will take
16 µs to complete. The highest sample rate at this frequency is 62.5 kHz.
Continuous In the continuous conversion mode, the ADC continuously converts the
Conversion versus selected channel filling the ADC data register (ADR) with new data after each
Single Conversion conversion. Data from the previous conversion will be overwritten whether that
data has been read or not. Conversions will continue until the ADCO bit is
cleared. The COCO bit (ADSCR, $003C) is set after each conversion and can
be cleared by writing the ADSCR or reading of the ADR.
In the single conversion mode, once the conversion is completed the ADC puts
the converted result in the ADR, sets a flag, and can generate an interrupt.
Pulse-Width By using the toggle-on-overflow feature with an output compare channel, the
Modulation (PWM) TIM module can generate a PWM signal. The composed value stored in the
TIM counter modulo registers determines the period of the PWM signal. The
channel pin toggles when the counter reaches that composed value. The time
between overflows is the period of the PWM signal.
The composed value of the TIM counter modulo registers and the selected
prescaler output determine the frequency of the PWM output. The frequency of
an 8-bit PWM signal is variable in 256 increments. Writing $00FF (255) to the
TIM counter modulo registers produces a PWM period equal to 256 times the
internal bus clock period if the prescaler select value is set to 000. Writing
$0080 (128) to the TIM channel registers produces a duty cycle of 128/256
or 50%.
Unbuffered PWM The PWM pulses are unbuffered because changing the pulse width requires
Signal Generation writing the new pulse width value over the old value currently in the TIM
channel registers. An unsynchronized write instruction to the TIM channel
registers for changing the pulse width value could cause incorrect operation for
up to two PWM periods. For example, writing a new value before the counter
reaches the old value but after the counter reaches the new value prevents any
compare during that PWM period. Also, using a TIM overflow interrupt routine
to write a new, smaller pulse width value may cause the compare to be missed.
The TIM may pass the new value before it is written.
Use the following methods to synchronize unbuffered changes in the PWM
pulse width on channel x:
Freescale Semiconductor, Inc...
NOTE: In PWM signal generation, do not program the PWM channel to toggle on
output compare. Toggling on output compare prevents reliable 0% duty cycle
generation and removes the ability of the channel to self-correct in the event of
software error or noise. Toggling on output compare also can cause incorrect
PWM signal generation when changing the PWM pulse width to a new, much
larger value.
Buffered PWM Channels 0 and 1 can be linked to form a buffered PWM channel whose output
Signal Generation appears on the TCH0 pin. The TIM channel registers of the linked pair
alternately control the pulse width of the output.
Setting the MS0B bit in the TIM channel 0 status and control register (TSC0)
links channel 0 and channel 1. The TIM channel 0 registers initially control the
pulse width on the TCH0 pin. Writing to the TIM channel 1 registers enables the
TIM channel 1 registers to synchronously control the pulse width at the
beginning of the next PWM period. At each subsequent overflow, the TIM
channel registers (0 or 1) that control the pulse width are the ones written to
last. TSC0 controls and monitors the buffered PWM function and TIM
channel 1 status and control register (TSC1) is unused. While the MS0B bit is
set, the channel 1 pin (TCH1) is available as general-purpose I/O.
NOTE: In buffered PWM signal generation, do not write new pulse width values to the
currently active channel registers. User software should track the currently
active channel to prevent writing a new value to the active channel. Writing
to the active channel registers is the same as generating unbuffered PWM
signals.
Since in this application the PWM duty cycle varies from 0 to 100% in
accordance with the voltage applied on the ADC, input buffered PWM allows
eliminating potentially harmful glitches when the pulse width setting is changed.
This is accomplished without requiring a synchronization method as in
unbuffered PWM signal generation.
Application Circuit
Freescale Semiconductor, Inc...
Figure 1 shows a typical application circuit involving the PWM feature. The
MC68HC908QY4 outputs a PWM waveform used to control the PMOS switch
and, therefore the effective DC regulated voltage applied on the load. The MCU
reads the instantaneous regulated output voltage through the ADC and
performs a comparison between the digital word obtained and an internal
digital reference previously defined. The load voltage is acquired by the ADC
through a feedback path cyclically thus, forcing the voltage regulation. If the
load demands more current the regulator output voltage decreases. If the
voltage input to the ADC is below the pre-set internal reference value the duty
cycle is reduced (decremented), thus increasing the driver current of the
converter compensating for the reduced voltage supplied to the load. On the
other hand, if the load demands less current than the nominal value the MCU
grows the duty cycle reducing the output voltage. When regulation is attained
the green light emitting diode (LED) is turned on. Conversely, turning on the red
LED indicates lack of regulation. Figure 2 illustrates the PWM waveform
modulated by an analog voltage signal applied on the ADC input whereas,
Figure 3 shows the regulated output voltage versus the load current for the
application circuit.
Regarding the application circuit in Figure 1 the diode, inductor, and shunt
capacitor create an energy storage reservoir to store enough energy to
maintain load voltage during the off-time of the transistor. The transistor serves
to replace lost energy during its off-time. Operation of the power switch can be
broken down into two periods.
• When the power switch is on, load current passes through the inductor
to the load and the diode will be reversed biased.
• When the power switch turns off, current must still pass through the
inductor. The current path through the off transistor is now open. The
diode will be forward biased, thus maintaining a closed current loop
through the load.
5V 5V
VDD
PTB0 TIMER
MC68HC908QY4
VREG
PTB1
GREEN ADC
LED
RED
Freescale Semiconductor, Inc...
LED
2.54
2.53
2.52
VOut (V)
2.51
2.50
2.49
2.48
20 40 60 80 100 120 140 160 180 200 220 240 260 280
ILoad (mA)
Software Description
In this application the PWM duty cycle varies from 0 to 100% in accordance
with the voltage applied on the ADC input. Actually PWM duty cycle varies from
approximately 6% to 94% since it is needed to set a PWM resolution limit for
proper operation of the close-loop system. User can redefine the PWM
resolution limit as explained below. The PWM output of timer channel 0 is
proportional to the analog converted value. When the input voltage increases,
the PWM duty cycle increases and vice versa.
loop in which the analog input voltage is converted onto a digital data by the
ADC and then compared to a target regulation value previously defined by the
user. The system reaches regulation if the analog input reading is in between
the upper and lower regulation limits also pre-defined by the user. The driver
transistor is a PMOS device. If the ADC value approaches the upper target
limit, the PWM duty cycle is increased (incremented). When the regulated
output voltage is below the target value but above the lower limit the PWM duty
cycle will be decremented. In case of the regulated output voltage being either
over the upper limit or below the lower limit, the PWM period will be either
enlarged or reduced, respectively. Therefore, the software will be seeking a
combination of PWM period and duty cycle to provide a regulated voltage
satisfying the pre-defined tolerances. PWM period checking range is from 256
to a minimum determined by the system resolution, as discussed below. A
software flowchart is shown in Figure 4.
Every time the period value is changed, the PWM duty cycle is redefined to
50%. In this application a PWM resolution limit is set to approximately
6% (lower) and 94% (upper) of period. If the regulation is not reached varying
PWM duty cycle between those limits, period value is altered again. This
sequence is performed until regulation is attained.
The PWM resolution limits are obtained by shifting the period value four times
to the right. Due to this shifting, the minimum possible period value is attained
to guarantee that resolution is 17 (decimal). However, the user can redefine the
PWM resolution limit altering the source by code taking into account the trade-
off between system resolution and minimum period attainable.
START
PWM INITIALIZATION
NO
Freescale Semiconductor, Inc...
DECREMENT PERIOD
YES
SET RESOLUTION (D) TO ~6%
NO
AD < LOWER UNIT?
INCREMENT PERIOD
TURN ON GREEN LED
AD = REGULATOR YES
VALUE?
NO
NO
NO
NO NO
YES
Software Listing
;*******************************************************************************************
;* Title: LoadRegulation.asm Copyright (c) 2002
;*******************************************************************************************
;* Author: Alfredo Olmos - Freescale SPS/BSTC
;* Andre V. Boas - Freescale SPS/BSTC
;* Marcus Espindola - Freescale SPS/BSTC
;*
;* Description: Generating a PWM Signal Modulated by an Analog Input Using HC908QY4 MCU.
;*
;* Documentation: HC908QY4 Data Sheet (MC68HC908QY4/D) for register and bit explanations
;*
Freescale Semiconductor, Inc...
;*******************************************************************************************
;* Equates and Data Table Includes
;*******************************************************************************************
include "MC68HC908QT4.equ"
;*******************************************************************************************
;* Constants and Variables for this file
;*******************************************************************************************
include "LoadRegulation.equ"
;*******************************************************************************************
;* Code Starts Here
;*******************************************************************************************
org FlashStart
mov #InitpwmH,TCH0H
mov #InitpwmL,TCH0L ;initial value for output compare (50%)
mov #tsc0Val,TSC0 ;enable buffered PWM in channel 0
lda TMODL
deca
clrh
sta TMODL ;Decrement period
ldx #$02
div
tax
stx PWMLow ;Set PWM Duty Cycle to ~50%
stx TCH0L
bra RegRes
lda ADR
cmp #RegHLim ;Verify if ADC value is greater than or
bge IncPeriod ;equal to Reg High Limit -> Inc Period if so
lda #$FF
bra SetPwmDc
lda ADR
cmp #RegLLim ;Verify if ADC value is lower than
bmi DecPeriod ;Reg Low Limit -> Decrement Period if so
lda #$FF
bra WaitReg
lda TMODL
inca
clrh
sta TMODL ;Increment period
ldx #$02
div
tax ;Set PWM Duty Cycle to ~ 50%
stx PWMLow
stx TCH0L
lda #$FF
bra WaitReg
;*******************************************************************************************
;* Interruptions
;*******************************************************************************************
Dummy:
rti
;*******************************************************************************************
;* Start of Vectors Definitions
;*******************************************************************************************
org $FFDE
fdb Dummy ;ADC conversion complete vector
org $FFE0
fdb Dummy ;Keyboard vector
org $FFF2
fdb Dummy ;TIM overflow vector
org $FFF4
fdb Dummy ;TIM Channel 1 vector
org $FFF6
fdb Dummy ;TIM Channel 0 vector
org $FFFA
fdb Dummy ;IRQ vector
org $FFFC
fdb Dummy ;SWI vector
org $FFFE
fdb Init ;Reset vector
;****************************************************************************************
;* Title: HC908QY4.equ Copyright (c) 2002
;****************************************************************************************
;* Author: Kazue Kikuchi
;*
;* Description: Register and bit name definitions for MC68HC908QY4 and MC68HC908QT4
;*
;* Documentation: MC68HC908QT4/D Advance Information
;* Include Files: none
;*
Freescale Semiconductor, Inc...
;*******************************************************************************************
;* Title: LoadRegulation.equ Copyright (c) **************************
;* Author: Alfredo Olmos - Freescale SPS/BSTC
;* Andre V. Boas - Freescale SPS/BSTC
;* Marcus Espindola - Freescale SPS/BSTC
;*
;* Description: Constants and variables definitions for MC68HC908QY4 and MC68HC908QT4.
;*
;* Documentation: HC908QY4 Data Sheet (MC68HC908QY4/D) for register and bit explanations
;*
Freescale Semiconductor, Inc...
;* Include Files:
;*
;* Assembler: P&E Microcomputer Systems - CASM for HC08
;*
;* Revision History: not yet released
;* Rev # Date Who Comments
;* ----- ----------- --------- --------------------------------------------
;* 0.1 19-Mar-03 Espindola Initial data entry
;*******************************************************************************************
;*******************************************************************************************
;* Freescale reserves the right to make changes without further notice to any product
;* herein to improve reliability, function, or design. Freescale does not assume any
;* liability arising out of the application or use of any product, circuit, or software
;* described herein; neither does it convey any license under its patent rights nor the
;* rights of others. Freescale 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 life, or for any other application in which the
;* failure of the Freescale product could create a situation where personal injury or
;* death may occur. Should Buyer purchase or use Freescale products for any such
;* intended or unauthorized application, Buyer shall indemnify and hold Freescale 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 Freescale was
;* negligent regarding the design or manufacture of the part.
;*
;* Freescale is a registered trademark of Freescale, Inc.
;*******************************************************************************************
;*******************************************************************************************
;* Constants and Variables for this file
;*******************************************************************************************
org RamStart
initCfg1: equ 12
;initCfg1: equ %00010001 ;Config1 Register value
; |||||||| CONFIG1 is a write once register
; |||||||+-COPD - 1 disable COP Watchdog
; ||||||+--STOP - 0 disable STOP instruction
; |||||+---SSREC - 0 4096 cycle STOP recovery
; ||||+----LVI5OR3 - 0 set LVI for 3V system
; |||+-----LVIPWRD - 1 disable power to LVI system
; ||+------LVIRSTD - 0 enable reset on LVI trip
; |+-------LVISTOP - 0 disable LVI in STOP mode
; +--------COPRS - 0 long COP timeout
Conclusion
In this application a PWM modulator is implemented using the
MC68HC908QY4 microcontroller. Typical PWM applications include motor
speed control, battery chargers, and switching voltage regulators, all of them
easily implemented using the 68HC08-based MCU’s. This low-cost, low pin
count family of microcontrollers brings to designers flexibility in the system
development process helping to reduce overall system costs and speed up
time-to-market. In addition, a variety of features and benefits make
MC68HC908QYx microcontrollers versatile for a wide range of systems like
home applications, computer peripherals, and automotive electronics.
E-mail:
[email protected]
Japan:
Freescale Semiconductor Japan Ltd.
Headquarters
ARCO Tower 15F Information in this document is provided solely to enable system and software
1-8-1, Shimo-Meguro, Meguro-ku, implementers to use Freescale Semiconductor products. There are no express or
implied copyright licenses granted hereunder to design or fabricate any integrated
Tokyo 153-0064 circuits or integrated circuits based on the information in this document.
Japan Freescale Semiconductor reserves the right to make changes without further notice to
0120 191014 or +81 3 5437 9125 any products herein. Freescale Semiconductor makes no warranty, representation or
[email protected] guarantee regarding the suitability of its products for any particular purpose, nor does
Freescale Semiconductor assume any liability arising out of the application or use of
Asia/Pacific: any product or circuit, and specifically disclaims any and all liability, including without
Freescale Semiconductor Hong Kong Ltd. limitation consequential or incidental damages. “Typical” parameters which may be
provided in Freescale Semiconductor data sheets and/or specifications can and do
Technical Information Center vary in different applications and actual performance may vary over time. All operating
2 Dai King Street parameters, including “Typicals” must be validated for each customer application by
Tai Po Industrial Estate customer’s technical experts. Freescale Semiconductor does not convey any license
Tai Po, N.T., Hong Kong under its patent rights nor the rights of others. Freescale Semiconductor products are
+800 2666 8080 not designed, intended, or authorized for use as components in systems intended for
[email protected] surgical implant into the body, or other applications intended to support or sustain life,
Information in this document is provided solely to enable system and software
implementers to use Freescale Semiconductor products. There are no express or
implied copyright licenses granted hereunder to design or fabricate any integrated
circuits or integrated circuits based on the information in this document.
Freescale Semiconductor reserves the right to make changes without further notice to
any products herein. Freescale Semiconductor makes no warranty, representation or
guarantee regarding the suitability of its products for any particular purpose, nor does
Freescale Semiconductor 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 Freescale Semiconductor 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. Freescale Semiconductor does not convey any license
under its patent rights nor the rights of others. Freescale Semiconductor 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 Freescale Semiconductor product
could create a situation where personal injury or death may occur. Should Buyer
purchase or use Freescale Semiconductor products for any such unintended or
unauthorized application, Buyer shall indemnify and hold Freescale Semiconductor
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 Freescale
Semiconductor was negligent regarding the design or manufacture of the part.
or for any other application in which the failure of the Freescale Semiconductor product
could create a situation where personal injury or death may occur. Should Buyer
For Literature Requests Only: purchase or use Freescale Semiconductor products for any such unintended or
Freescale Semiconductor Literature Distribution Center unauthorized application, Buyer shall indemnify and hold Freescale Semiconductor
P.O. Box 5405 and its officers, employees, subsidiaries, affiliates, and distributors harmless against all
Denver, Colorado 80217 claims, costs, damages, and expenses, and reasonable attorney fees arising out of,
1-800-441-2447 or 303-675-2140 directly or indirectly, any claim of personal injury or death associated with such
Fax: 303-675-2150 unintended or unauthorized use, even if such claim alleges that Freescale
[email protected] Semiconductor was negligent regarding the design or manufacture of the part.
AN2475/D
Rev. 0
3/2003
For More Information On This Product,
Go to: www.freescale.com