Part Number Hot Search : 
GL901RX LPC1343F 01501 11007 SGM8552 LR246 TDA5030A 1N5820
Product Description
Full Text Search
 

To Download AN1712 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  AN1712/0304 1/14 rev. 1.0 AN1712 application note generating a high resolution sinewave using st7 pwmart by microcontroller division applications introduction the purpose of this application note is to present a software technique for generating a high resolution sinewave using st7 pwmart, tunable in frequency and average amplitude. this application has been implemented using the st72321j9 microcontroller. the pwmart (au- toreload timer peripheral embedded in the microcontroller) is used to generate a pwm signal and this pwm signal is then filtered by low pass filter (simple rc circuit) to generate a sine- wave. 1
2/14 generating a high resolution sinewave using st7 pwmart 1 generating a sinusoid this section highlights the main features of the st7 pwmart used to generate a pwm signal which is then filtered by low pass filter (a simple rc circuit in this example) to generate a si- nusoid. please refer to the st7 datasheet for more details. the st7 pwmart consists of an 8-bit auto reload counter with compare/capture capabilities and a 7-bit clock prescaler. 1.1 pwm generation the free running 8-bit counter is fed by the output of the prescaler, and is incremented on every rising edge of the clock signal. it is possible to read or write the contents of the counter on the fly by reading or writing the counter access register (artcar). when a counter over- flow occurs, the counter is automatically reloaded with the contents of the artarr register (the prescaler is not affected). the counter clock frequency is given by: the timer counter?s input clock (f input ) feeds the 7-bit programmable prescaler, which selects one of the 8 available taps of the prescaler, as defined by cc[2:0] bits in the artcsr reg- ister. thus the division factor of the prescaler can be set to 2 n (where n = 0, 1,..7). this f input frequency source is selected through the excl bit of the artcsr register and can be either the f cpu or an external input frequency f ext . the clock input to the counter is enabled by the tce (timer counter enable) bit in the artcsr register. when tce is reset, the counter is stopped and the prescaler and counter contents are frozen. when tce is set, the counter runs at the rate of the selected clock source. the timer compare function is based on four different comparisons with the counter (one for each pwmx output). each comparison is made between the counter value and an output com- pare register (ocrx) value. this ocrx register can not be accessed directly, it is loaded from the duty cycle register (pwmdcrx) at each overflow of the counter. this double buffering method avoids glitch generation when changing the duty cycle on the fly. pwm mode allows up to four pulse width modulated signals to be generated on the pwmx output pins with minimum core processing overhead. this function is stopped during halt mode. each pwmx output signal can be selected independently using the corresponding oex bit in the pwm control register (pwmcr). when this bit is set, the corresponding i/o pin is 2 cc[2:0] f input f counter = 2
3/14 generating a high resolution sinewave using st7 pwmart configured as output push-pull alternate function. the pwm signals all have the same fre- quency which is controlled by the counter period and the artarr register value. when a counter overflow occurs, the pwmx pin level is changed depending on the corre- sponding opx (output polarity) bit in the pwmcr register. when the counter reaches the value contained in one of the output compare register (ocrx) the corresponding pwmx pin level is restored. note: the reload values will also affect the value and the resolution of the pwm output signal duty cycle. to obtain a signal on a pwmx pin, the contents of the ocrx register must be greater than the contents of the artarr register. the resolution for the pwmx duty cycle is: figure 1. pwm auto-reload timer function on overflow, the ovf flag of the artcsr register is set and an overflow interrupt request is generated if the overflow interrupt enable bit, oie, in the artcsr register, is set. the ovf flag must be reset by the user software. this interrupt is used as a time base in the application. 256 - artarr f counter f pwm = 256 - artarr 1 resolution = duty cycle register (pwmdcrx) auto-reload register (artarr) 00 255 counter pwmx output with oex=1 with oex=0 and opx=0 and opx=1
4/14 generating a high resolution sinewave using st7 pwmart 1.2 sinewave generation at the start of the program: ? the pwmdcr0 register is initialized to obtain a 50% duty cycle ? the number of samples in a sinewave cycle is defined ? the counter reload value (artarr) is initialized in the software provided with this application note, predefined initialization values for each fre- quency can be selected in the define.h file. more than 18 samples in a sinewave cycle should be selected to generate a sinewave with total harmonic distortion of less than 5%. so, depending on the number of samples in a sin- ewave cycle, the duty cycle register (pwmdcr0) is modified, taking care that no 0% and 100% pwm is generated for any of the sinewave samples (because the pwm duty cycle must be between ~99% to ~1% to generate an undistorted sinewave). the pwm duty cycle is changed after a certain number ( ? counter ? ) of overflow cycles. so, the sinewave frequency depends on three parameters, ? the pwm frequency (f pwm ) ? the number of samples in a sinewave cycle ? the number of overflow cycles after which the pwm duty cycle changes ( ? counter ? ) so, the sinewave frequency can be given by: see also figure 2. this pwm signal must be filtered with an external rc network selected for the filtering level re- quired to generate a sinusoid. the cut off frequency of low pass rc filter is given as: so, the value of r and c must be chosen in such a way that the output sinewave frequency should be less than this high cut off frequency (f h ). the instantaneous value of sinewave depends on the duty cycle of pwm. so: f sine = 1 t pwm * number of samples * counter f h = 1 2 * pi * r * c v sine max,min = max, min pwm duty cycle * v dd v sine average = average pwm duty cycle * v dd
5/14 generating a high resolution sinewave using st7 pwmart figure 2. output at pwm0 pin and general calculation for finding sinewave frequency 10 pwm cycles each number of samples = 4 f pwm = f counter /(256-artarr) = 20 khz (if f counter = 4mhz) counter=10 f sine = 1/(50us*4*10) = 500 hz duty cycle = ~50% time vdd maximum duty cycle = ~87% average duty cycle = ~50% minimum duty cycle = ~13% duty cycle = ~50% one sinewave cycle time pwmdcr0 register value 00 255 artarr=56 pwmdcr0 = average value = 155 (pwm duty cycle = ~50%) pwmdcr0 = average value+75 pwmdcr0 = average value-75 10(=counter) * t pwm output at pwm0 pin output of rc circuit
6/14 generating a high resolution sinewave using st7 pwmart 2 improving sinewave resolution sinewave resolution depends on three factors: ? t pwm ? the number of samples in a sinewave cycle ? ? counter ? value by changing any of these parameters you can get a different resolution. if f counter is fixed, you can only change t pwm by varying the artarr value. the minimum change you can do to artarr is 1. so, the minimum change in t pwm is t counter . for ex- ample: if f counter is 4 mhz and artarr value is 56, then t pwm is 50us. if the number of samples in a sinewave cycle is 40 and counter is 10, f sine will be 50 hz. if you change the artarr value to 57, t pwm will change to 49.75us. so, f sine will be ~50.25 hz (assuming the number of samples and counter value are fixed). so, the resolution is ~0.25 hz. now to improve resolution, change artarr = 36, counter = 7 and number of samples in a sinewave cycle = 52. assuming f counter is still 4mhz, t pwm will be 55us. in this case f sine will be 49.95 hz, which gives improved resolution (~0.05hz).
7/14 generating a high resolution sinewave using st7 pwmart 3 software configuration the software is developed in ? c ? language and gives you the option of using the st7 software library or not. a header file ? define.h ? is supplied. this header file defines the sin structure. the sin structure has the three user defined datatypes for storing sinewave patterns depending on the number of samples in a sinewave cycle, the ? index ? which is used for counting the current sinewave sample and another datatype which is used to indicate whether the current sample is related to the upper half or lower half of the sinewave. depending on the number of samples in a sinewave cycle, sample values are initialized for the sinewave envelope (x(n)=asin((2*pi*n)/n)). where a is the sinewave amplitude, pi is 3.1416, n is the n th sample of the sinewave, n is the number of samples in a sinewave cycle. n should be more than 18 in order to generate a sinewave with total harmonic distortion of less than 5%. the value of a should be such that no 0% or 100% duty cycle is generated for any of the sinewave samples. there are other define types for the sinewave frequency from 45 to 65hz with a resolution of less than 0.1hz. these define types contain five parameters: ? ? no_of_samples_half ? for defining the number of samples in half a sinewave cycle ? ? counter ? value which defines the number of overflow cycles after which the pwm duty cycle changes ? initialization value of artarr for f pwm , ? initialization value of pwmdcr0 a 50% duty cycle ? ? average_amp ? to define the average sinewave amplitude. a sinewave with a resolution of less than 0.1hz is obtained by varying the ? no_of_samples_half ? , the ? counter ? value and artarr. the average sinewave ampli- tude is also software configurable by the ? average_amp ? parameter. it should be noted that the value of a and the value of the ? average_amp ? is chosen in such a way that no 0% or 100% duty cycle is generated for any of the sinewave samples. 3.1 sin structure initialization the sin structure has the three user defined datatypes for storing the sinewave pattern ac- cording to the number of samples in a sinewave cycle, the ? index ? which is used for counting the current sinewave sample and another datatype which is used for indicating whether the current sample is related to the upper half or lower half of the sinewave.
8/14 generating a high resolution sinewave using st7 pwmart figure 3. loading of sample values and initialization of sin structure 3.2 auto-reload timer initialization the counter is initialized by: ? writing to the artarr register to set the pwm frequency. ? setting the fcrl (force counter re-load), the tce (timer counter enable) and oie (over- flow interrupt enable) bits in the artcsr register. in this case, the f input is cpu clock (f cpu ) and f counter = f input (= 4mhz in this particular application). ? enable pwm0 and configure the polarity in the pwmcr register. ? initialize the pwmdcr0 register to define the pwm duty cycle. loadvalueintosinstructur load sample values (already defined) into value[no_of_samples_half] of sin structure (the no of samples in half sin wave is already defined) initialize sinewave for up- per half return initialize index=0
9/14 generating a high resolution sinewave using st7 pwmart figure 4. pwmart timer initialization 3.3 pwmart interrupt service routine this is the interrupt service routine for the pwmart interrupt. every time an overflow occurs, an interrupt is generated (because overflow interrupt is enabled). the pwmart duty cycle is changed after every ? counter ? number of overflow cycles. the duty cycle can vary from ~99% to ~1% depending on the sinewave sample values. ar_timer_init start the pwm timer counter, force counter reload is on & overflow interrupt is enabled enable pwm0 and con- figure the polarity return set the frequency of pwmart by initializing the artarr initialize the duty cycle of sinewave to 50%
10/14 generating a high resolution sinewave using st7 pwmart figure 5. pwmart interrupt service routine art_interrupt clears the overflow interrupt flag counter = counter+1 return is counter >= counter? no yes counter = 0 upper half cycle of sinewave? load the duty cycle register for up- per half cycle of sinewave. this is achieved by adding the current sam- ple value into average_amp. yes no load the duty cycle register for lower half cycle of sinewave. this is achieved by subtracting the current sample value from average_amp. index = index+1 is index= no_of_samples_half? index = 0 & change the po- larity of sinewave cycle yes no
11/14 generating a high resolution sinewave using st7 pwmart 3.4 main routine the main routine calls loadvalueintosinstructur and ar_timer_init. after this, the initializa- tion interrupts are enabled (rim is executed) so that the microcontroller can go into the inter- rupt routine and an infinite while loop is called. figure 6. main routine main loadvalueintosinstructur enableinterrupts ar_timer_init + while(1)
12/14 generating a high resolution sinewave using st7 pwmart 4 hardware configuration this application runs on an st72f321 microcontroller. the pwm0 channel is used to gen- erate the pwm signal which is then filtered by low pass filter (a simple rc circuit in this ex- ample) to generate a sinusoid. the r ext used is 1.8k and c ext is 0.47uf. the values of r ext and c ext decides the filtering level. the selected crystal for this example has a frequency of 8 mhz which gives f cpu = 4 mhz be- cause the pll is disabled and slow mode is not selected. it gives the f counter = 4 mhz for the reset value of counter clock control bits in artcsr register. figure 7. generation of sinewave: application circuitry st7 8 mhz pwm0/pb3 /\/\/\/\/\/\ r ext =1.8k v dd_0, v dd_1, v dd_2 v dd v ss_0, v ss_1, v ss_2 27 pf 27 pf osc1 osc2 c ext =0.47 uf v pp /iccsel c = 0.1 uf
13/14 generating a high resolution sinewave using st7 pwmart 5 software all the source files in ? c ? language with the option of using the st7 software library or not (st7 software library version 1.1) are given in the zip file with this application note. the source files are for guidance only. stmicroelectronics shall not be held liable for any di- rect, indirect or consequential damages with respect to any claims arising from use of this soft- ware.
14/14 generating a high resolution sinewave using st7 pwmart 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 co nsequences 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 publicati on are subject to change without notice. this publication supersedes and replaces all information previously supplied. stmicroelectronics prod ucts are not authorized for use as critical components in life support devices or systems without express written approval of stmicroelectro nics. the st logo is a registered trademark of stmicroelectronics. all other names are the property of their respective owners ? 2004 stmicroelectronics - all rights reserved stmicroelectronics group of companies australia ? belgium - brazil - canada - china ? czech republic - finland - france - germany - hong kong - india - israel - italy - japan - malaysia - malta - morocco - singapore - spain - sweden - switzerland - united kingdom - united states www.st.com


▲Up To Search▲   

 
Price & Availability of AN1712

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X