AN590 STMICROELECTRONICS | Alldatasheet

Document overview

  • Manufacturer or author: Provided By ALLDATASHEET.COM(FREE DATASHEET DOWNLOAD SITE)
  • PDF pages: 7

Technical content

PWM signal with duty cycle proportional to an analog input voltage is presented. Figure 1. Auto-reload Timer Block Diagram

clock fosc and the prescaler ratio. The PWM signal period is controlled by the Reload register. The duty cycle is defined by the Compare register CP (see Figure 2). PWM output is set on the overflow of TC and reset when TC=CP. ratio as small as possible to achieve a maximum resolution. PWM of 31.25kHz. A resolution of 1/64 allows to increase FPWM to 125kHz. Table 1. Prescaler Programming Ratio

Figure 2. PWM Timer Operation We want to generate a periodic signal at a frequency of 12 kHz and a duty cycle of 37% . The CPU frequency (quartz frequency) is 4 MHz.

PWM GENERATION WITH ST62 AUTO-RELOAD TIMER Program example: – SC0 is not programmed, so it keeps its reset value 00h (all flags cleared) – The PWM signal starts as soon as the last instruction (ldi MC) is executed Program example RC .def 0D9h,0FFh,0FFh ;reload/capture register CP .def 0DAh,0FFh,0FFh ;compare register MC .def 0D5h,0FFh,0FFh ;mode control register SC0 .def 0D6h,0FFh,0FFh ;status/control register 0 SC1 .def 0D7h,0FFh,0FFh ;status/control register 1 LR . def 0DBh,0FFh,0FFh ;load register ldi CP, 186 ;compare register = 186d ldi RC, 145 ;reload register = 145d ldi SC1,001h ;clock source= CPU clock divided by 3 ;prescaler ratio = 1 ldi MC, 11100000b ;auto-reload mode,interrupts disabled ;PWMOUT enabled, start timer Example 2: Target: generate a PWM signal of frequency 31.25 kHz with a duty cycle proportional to an input analog voltage varying between 0V and VCC : 0V corresponds to 0% duty cycle, VCC cor- responds to 100% duty cycle. The CPU clock is 8 MHz: Referring to example 1, we find that the prescaler ratio must be 1, the value in RC must be 0 and the value in CP is taken directly from the A/D output (varying from 0 to 255d depending upon the analog input): RC = 0 CP = 0...255d prescaler ratio = 1 F IN = 8 MHz FPWM = 8 MHz / 256 = 31.25 kHz Duty cycle = CP / 256d Resolution = 1/256d = 0.39 % We need to implement a software loop which repetitively converts the analog input to a digital value and copies this digital value into CP:

PWM GENERATION WITH ST62 AUTO-RELOAD TIMER Program example A .def 0FFh,0FFh,0FFh ;Accumulator PA .def 0C0h,0FFh,0FFh ;Data Register Port A PADIR .def 0C4h,0FFh,0FFh ;Data Direction Reg. Port A PAOPT .def 0CCh,0FFh,0FFh ;Option Register Port A RC .def 0D9h,0FFh,0FFh ;reload capture register CP .def 0DAh,0FFh,0FFh ;compare Register MC .def 0D5h,0FFh,0FFh ;mode control register SC0 .def 0D6h,0FFh,0FFh ;status control register 0 SC1 .def 0D7h,0FFh,0FFh ;status control register 1 LR .def 0DBh,0FFh,0FFh ;load register ADCC .def 0D1h,0FFh,0FFh ;adc control register ADC .def 0D0h,0FFh,0FFh ;adc result Register ;port A must be an IOP3 type (port with analog input mode): ldi PADIR,000h ldi PAOPT,001h ;PA0 is the analog input ldi PA, 001h ;PA1..PA7 are input with pull-up ldi SC1,00000000b ;PSC divides by 1 ldi CP,080h ;compare register = 128d ;(initial duty = 50%) ldi RC,000h ;reload register = 0 ;(count up from 0 to 255) ldi MC,11100000b ;auto-reload mode,interrupt disabled ;PWMOUT enabled, start timer with ;duty cycle 50% at start-up adc_loop ldi ADCC,030h ;start A/D conversion wt_eoc jrr 6,ADCC,wt_eoc ;wait for end-of-conversion ld A,ADC ;save result into Accumulator ld CP,A ;copy it into CP register jp adc_loop ;do it again

PWM GENERATION WITH ST62 AUTO-RELOAD TIMER The length of the loop “adc_loop” is the interval at which the duty cycle of the PWM signal will be updated. In this example, this loop lasts around 76µs, so the PWM duty cycle is updated every 2 or 3 PWM cycles. Calculation of the 80µs: - Instructions ldi, ld and jp last 4 cycles of 13 clock periods -> 4 x 4 x 13 8MHz periods = 26µs - Instruction jrr lasts as long as the A/D takes to do the conversion, which is around 50µs at 8MHz -> total length of adc_loop at 8 MHz = approx. 76µs This last example shows that, as requested, the CPU is not used to generate the PWM signal, but only to start/stop it and to update the duty cycle.

PWM GENERATION WITH ST62 AUTO-RELOAD TIMER “THE PRESENT NOTE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH A NOTE AND/OR THE USE MADE BY CUSTOMERS OF THE INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.” Information furnished is believed to be accurate and reliable. However, STMicroelectronics assumes no responsibility for the consequences of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. No license is granted by implication or otherwise under any patent or patent rights of STMicroelectronics. Specifications mentioned in this publication are subject to change without notice. This publication supersedes and replaces all information previously supplied. STMicroelectronics products are not authorized for use as critical components in life support devices or systems without the express written approval of STMicroelectronics. The ST logo is a registered trademark of STMicroelectronics  2003 STMicroelectronics - All Rights Reserved. Purchase of I2C Components by STMicroelectronics conveys a license under the Philips I2C Patent. Rights to use these components in an I2C system is granted provided that the system conforms to the I2C Standard Specification as defined by Philips. STMicroelectronics Group of Companies Australia - Brazil - Canada - China - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - U.S.A. http://www.st.com