NEW DATABASE - 350 MILLION DATASHEETS FROM 8500 MANUFACTURERS
AN2658 AIN15 AN2719 STM8/128-EVAL - Datasheet Archive
Application note Using the analog to digital converter of the STM8 microcontroller Introduction The purpose of this application
AN2658 AN2658 Application note Using the analog to digital converter of the STM8 microcontroller Introduction The purpose of this application note is to explain how to use the Analog to Digital Converter implemented in the STM8 microcontroller family. It provides useful information on how to configure the ADC registers and microcontroller resources and use the ADC in different modes. The STM8 firmware library, containing source code of all the examples described in this application note, can be downloaded from the STMicroelectronics website: www.st.com. October 2008 Rev 1 1/20 www.st.com Contents AN2658 AN2658 Contents 1 ADC hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.1 1.2 2 General properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 ADC operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Summary of features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1 2.2 Clock selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.3 Conversion triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.4 ADC speed/sampling time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.5 Voltage reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.6 Input analog channel selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.7 3 Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Data storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Configuring ADC registers for conversion . . . . . . . . . . . . . . . . . . . . . . 10 3.1 Selecting the operating mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.1.1 Single conversion mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.1.2 Continuous conversion mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.1.3 Conversion on external trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.2 3.3 Analog input selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.4 Storing converted values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.5 4 Conversion speed selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 ADC interrupt management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Practical application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.1 Areas of use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.2 Hardware connection examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.3 Methods for precision improvement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5 Design recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 6 Displaying the ADC conversion result using LEDs . . . . . . . . . . . . . . . 17 6.1 6.2 2/20 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 AN2658 AN2658 Contents 6.3 7 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 ADC conversion triggered by TIM1 timer . . . . . . . . . . . . . . . . . . . . . . . 18 7.1 7.2 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 7.3 8 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3/20 ADC hardware description 1 ADC hardware description 1.1 AN2658 AN2658 General properties STM8 family microcontrollers include an Analog to Digital Converter which has up to 16 multiplexed inputs. The ADC resolution is 10 bits. The number of external analog inputs depends on the package size of the chosen STM8 family device. The ADC is a successive approximation Analog to Digital Converter. Conversion can be performed in continuous mode or single mode. The digital result is then stored in registers. Because the ADC resolution is 10 bits and the ADC data register bit length is 16, the ADC result is stored in two 8 bit registers, and the data is either right or left aligned (this is selectable). The event used to start conversion can be generated by software or by the STM8 microcontroller's internal timer 1 (any type of timer event can be used). The start of A/D conversion can also be triggered by an external pin. The reference voltage for the ADC depends from package. It comes either from an external source - in this case the reference must be connected to two external pins - or is connected internally to analog power supply pins. The value of this reference voltage is limited to within the range from 2.75 V up to the voltage applied on the VDDA input. The measured voltage must be between Vref+ and Vref-. Resolution can be increased by so-called analog zooming - using a smaller reference voltage around the range of values to be measured. If needed, an interrupt can be generated at the end of conversion. The ADC is driven by a clock derived from the MCU master clock through a programmable divider. This allows you to select the ADC clock speed according to your application requirements. 4/20 AN2658 AN2658 ADC hardware description Figure 1. ADC block diagram VREF+ VREFVDDA VSSA Data bus Power/Analog pins Data register (10-bits) Interrupt End of conversion AIN0 AIN1 fADC Analog to digital converter Clock prescaler fMASTER GPIOs AIN15 AIN15 ADC registers ADC_ETR trigger Timer 1 5/20 ADC hardware description 1.2 AN2658 AN2658 ADC operation Figure 2. and Figure 3. describe ADC operation in single and continuous mode. Figure 2. Single conversion mode CLK ADON ADC inactive next conversion EOC tSTAB (7µs) conversion time (14 clocks) 1 sync clock 3 clocks sampling time 10 clocks conversion Software clears EOC bit Start conversion ADON=1 or trigger Software reads ADC data Power-on ADC ADON=1 Figure 3. Start conversion ADON=1 or trigger End of conversion Continuous conversion mode CLK ADON EOC tSTAB (7µs) conversion time (14 clocks) next conversion time (14 clocks) Software reads ADC data End of conversion Power-on ADC ADON=1 6/20 Start conversion ADON=1 or trigger Next conversion starts immediately Software clears EOC bit AN2658 AN2658 2 Summary of features Summary of features Table 1. below summarizes the main features of the STM8 ADC. Table 1. STM8 ADC features Feature STM8 ADC Resolution Clock speed fADC = 1 to 2 or 4 MHz (refer to datasheet) Triggers Software, timer or external pin trigger Operating modes Single or continuous mode Conversion speed min. 3.5 us @ 4 MHz fADC Reference voltage external pins: 2.75 V ÷ VDDA range, analog zooming possible (or VDDA - in some packages) Number of input channels up to 16 channels Data format 2.1 10-bit optional left or right alignment Resolution The STM8 ADC converter has 10-bit resolution. A special extra feature of the STM8 is that, to read the conversion results faster, and if you can accept lower resolution; you can read only 8 of the 10 bits, provided that you select the appropriate register alignment. The register for configuring right or left data alignment is ADC_CR2. 2.2 Clock selection The A/D conversion speed is given by the ADC clock signal. This clock speed must be between 1 MHz and 2 or 4 MHz (refer to datasheet). The ADC clock frequency is selected by dividing the frequency of the master clock. The register for clock speed selection is ADC_CR1. 2.3 Conversion triggers ADC conversion can be triggered by a Timer1 TRGO (trigger output) event or by a rising edge on the ADC_ETR external pin. Conversion can also be triggered by software, by setting the ADON bit. The register for trigger selection is ADC_CR2. 7/20 Summary of features 2.4 AN2658 AN2658 ADC speed/sampling time The ADC speed (and also sampling time) influences the measurement precision and must be chosen depending on the output impedance of the measured voltage source and the required conversion precision. The sampling time cannot be set independently from the ADC clock. The ADC speed can be chosen from fMASTER/2 down to fMASTER/18 where fmaster is the microcontroller internal clock. The maximum speed of ADC conversion in the STM8 ADC is 3.5 µS. The register for selecting the ADC clock division factor is ADC_CR1. 2.5 Voltage reference The ADC reference voltage must be connected to the external pins or in some packages is connected internally to analog power supply. The reference voltage must be within the analog power supply range and the minimum reference voltage value is 2.75V. 2.6 Input analog channel selection The STM8 ADC has up to 16 analog inputs connected to GPIO input pins. There are fewer analog inputs on some devices (this depends on the device package). Pins with ADC function must be configured as floating input and the input Schmitt trigger on these pins should be disabled (this reduces power consumption). The register for selecting the input analog channel is ADC_CSR. 8/20 AN2658 AN2658 2.7 Summary of features Data storage The 10 bits of ADC converted data are stored in two 8-bit registers. The STM8 ADC has selectable data alignment. You can choose to left or right align the 10-bit ADC result in the two 8-bit data registers. You must read the data registers in a particular order to ensure that the data retrieved from the registers is consistent (resulting from the same conversion). When the first data register is read, the second data register is latched so it can be read later and to prevent it being overwritten by the next conversion. The order in which you must read the data registers depends on the alignment mode. In right alignment mode, the LSB must be read first and then the MSB. In left alignment mode, the MSB must be read first and then the LSB. Figure 4. Data alignment Left alignment mode: ADC_DRH ADC_DRL 7 0 7 D7 D6 D9 D8 0 D5 D4 D3 D2 D1 D0 Right alignment mode: ADC_DRH 7 D9 D8 0 D7 D6 D5 D4 D3 D2 ADC_DRL 7 0 D1 D0 9/20 Configuring ADC registers for conversion 3 Configuring ADC registers for conversion 3.1 AN2658 AN2658 Selecting the operating mode The ADC converter can operate in different modes according to application requirements. The following sections list the various operating modes, the register settings required to use the ADC in each mode and the practical uses in each case. 3.1.1 Single conversion mode In this mode, the ADC performs only one conversion. Conversion start is triggered either by software writing to the ADON bit (twice) or by a signal from Timer1 or from an external signal on the ADC_ETR pin. A new start of conversion must be generated before the next single conversion (either by external trigger or by software). Register settings: ADC_CR1: CONT = 0 Practical use: This mode can be used to collect an analog value from an external sensor (for example to read a temperature). 3.1.2 Continuous conversion mode In this mode, the ADC performs conversions cyclically. When one conversion has finished, then the next conversion starts immediately. Data must be collected from the ADC_DR data registers before they are overwritten by the next conversion. Register settings: ADC_CR1: CONT = 1 Practical use: To collect a set of analog values from an external analog signal (for example to read an AC voltage waveform). 3.1.3 Conversion on external trigger Conversion can be started by writing twice to the ADON control bit or by external trigger. The external trigger source event can be a Timer1 TRGO event or a rising edge on the external ADC_ETR pin. 3.2 Conversion speed selection The conversion speed of an ADC depends proportionally on the ADC clock frequency, the sampling time duration and on the ADC resolution. The ADC clock speed must be selected by selecting the division factor to be applied to the internal master clock. The sampling time is not customizable and depends on the ADC clock. 3.3 Analog input selection The device has up to 16 external analog inputs connected to GPIO input pins. Pins to be used as ADC input function must be configured as floating inputs and the input Schmitt 10/20 AN2658 AN2658 Configuring ADC registers for conversion triggers on these pins should be disabled to avoid unnecessary power consumption. If the application changes the analog input channel during conversion then the current conversion is stopped and the next conversion starts on the new channel. 3.4 Storing converted values The converted ADC value is stored in the ADC_DRH and ADC_DRL registers. Because the converted value has a 10 bit length and the data registers have 2x8 bit length, it is possible to select left or right alignment of the conversion result in the data registers. In left alignment the 8 most significant bits are stored in a single register, in right alignment the 8 least significant bits are stored in a single register. This can be chosen to suit the application. Data register reading is buffered - when the first register is read, the second register is automatically stored in a shadow register to be read later. This prevents reading data register values from 2 different conversions. The first register to be read depends on the data alignment. You must read the MSB first in left alignment mode and read LSB first in right alignment mode. Practical usage: Left alignment is useful for fast read access to the conversion result in 8bit resolution. Right alignment is good for fast read access when measuring low level signals. In cases where the result does not affect the high order bits - then you can read only the LSB byte. 3.5 ADC interrupt management Event-driven programming methods can be used for efficiency or for special software requirements. With this technique, when the main program is running, it is notified of state changes by events. You can enable the ADC interrupt to give the main program fast asynchronous event notification at the end of conversion(s). This means the main program does not need to poll the ADC conversion status flags but only has to process the ADC data results (for example after storing a set of conversions in a buffer). The reception of the ADC converted data values is performed by an interrupt routine in the background. 11/20 Practical application AN2658 AN2658 4 Practical application 4.1 Areas of use Temperature measurement: Device thermal protection Process calibration Fan control Power supply measurement: Auto save configuration Battery charging/protection Measurement of physical values in other types of application: Household: automatic lights dimmers, weather stations, thermometers, security sensors, . Industry: lights, thermostats, humidity control, . 4.2 Automation: from sensors with analog outputs Electrical quantity measurements: voltage, current, capacity, resistance, . Hardware connection examples Figure 5. Simple unipolar DC signal measurement UIN AINx STM8 VREFVSS 12/20 AN2658 AN2658 Practical application Figure 6. External preamplifier usage (with high impedance input) UIN + AINx STM8 R1 R2 VREFVSS Figure 7. Differential DC signal measurement with preamplifier R2 - UIN AINx R1 + R3 STM8 R4 VREFVSS Figure 8. AC signal (FFT) measurement UIN DC filter AINx STM8 VREFVSS 13/20 Practical application Figure 9. AN2658 AN2658 Analog zooming AINx UIN = (VREF+, VREF-) STM8 VREF+ UREF VREFVSS Figure 10. Multiple channel measurement UIN0 UIN1 AIN0 AIN1 UINx AINx VREFVSS 14/20 STM8 AN2658 AN2658 4.3 Practical application Methods for precision improvement Averaging samples: Averaging decreases speed but can give improved accuracy Analog zooming (use appropriate VREF voltage and VREF offset): Select reference voltage between input signal ranges Gives full ADC range - min. voltage per bit White noise added to measured signal: wobbling of input signal over several bits gives possibility to use averaging (if input signal is very stable) White noise gives independence from sampling frequency Digital filtering (50/60 Hz suppression from DC value) Set proper sampling frequency (The trigger from Timer1 is useful in this case) Perform software post processing on sampled data (comb filter) Fast Fourier Transform (FFT) for AC measurements: To show harmonic parts in measured signal Slower due to more computational power requirements Calibration of ADC: offset, gain, bit weight calibration Decreases internal ADC errors Internal ADC structure must be known Good hardware design: Grounding Reference voltage filtering Power supply filtering Preamplifier usage Frequency independence . Refer to AN2719 AN2719 for more details. 15/20 Design recommendations 5 AN2658 AN2658 Design recommendations The following is a checklist of the main design rules for using the ADC. Grounding of analog/digital power: Star topology VDDA, VSSA filtering: RC, LC filtering Avoid noise from noisy digital power Vref selection - offset, value, precision: Reference voltage source precision and stability to meet the required precision in the application and the and ADC's capability Reference voltage source value and precision according to the expected measurement range Source impedance vs. input impedance knowledge: Use input buffers for measured signal Impedance relation with required conversion speed External preamplifier usage: For low (and also high) level signals Amplifier speed and precision properties Amplifier dependency from frequency 16/20 Select appropriate ADC mode, speed, trigger Software methods: averaging, FFT, . AN2658 AN2658 Displaying the ADC conversion result using LEDs 6 Displaying the ADC conversion result using LEDs 6.1 Overview This section describes how to use the ADC continuous conversion mode and display the result on LEDs. The example shows how to use continuous conversion mode and observe the effect of changing the voltage values on the analog channel and observing the accuracy of the ADC conversion. 6.2 Hardware description Figure 11 shows a typical connection between the STM8 AIN9 pin and a potentiometer. Figure 11. Connection to a potentiometer 5V AIN9 10 K STM8 6.3 Firmware description The STM8 firmware library includes the ADC driver that supports the ADC through a set of functions. In this example the ADC is configured in continuous conversion mode with interrupt enabled. In the interrupt routine, the range of the converted value defines the number of LEDs to light up each time a conversion occurs. Increasing the voltage applied to channel 9 of the ADC, using the potentiometer, increases the number of LEDs that light up (up to 4). This firmware is provided as ADC example 1 in the STM8 firmware library, available from the STMicroelectronics microcontroller website. 17/20 ADC conversion triggered by TIM1 timer 7 ADC conversion triggered by TIM1 timer 7.1 AN2658 AN2658 Overview This section describes how to start and ADC conversion triggered by the TIM1 trigger output. There are several ways of starting ADC conversions in the STM8 including any event that can be generated by the multipurpose timer (TIM1). This gives the you a large choice when implementing your application. 7.2 Hardware description Figure 11 shows a typical connection between the STM8 AIN9 pin and a potentiometer. Tis allows you to test the ADC by changing the input voltage to be converted. The conversion result is displayed on four LEDs. This example hardware is implemented in the STM8 evaluation board STM8/128-EVAL STM8/128-EVAL. 7.3 Firmware description The STM8 firmware library includes the ADC driver that supports the ADC through a set of functions. ADC is configured in continuous conversion mode with interrupt enabled. The conversion is triggered by the TIM1 TRGO event. In the interrupt routine, the range of the converted value defines the number of LEDs to light up each time a conversion occurs. This firmware is provided as ADC example 2 in the STM8 firmware library, available from the STMicroelectronics microcontroller website. 18/20 AN2658 AN2658 8 Revision history Revision history Table 2. Document revision history Date Revision 28-Oct-2008 1 Changes Initial release. 19/20 AN2658 AN2658 Please Read Carefully: Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries ("ST") reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any time, without notice. All ST products are sold pursuant to ST's terms and conditions of sale. Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and services described herein. No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein. UNLESS OTHERWISE SET FORTH IN ST'S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER'S OWN RISK. Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any liability of ST. ST and the ST logo are trademarks or registered trademarks of ST in various countries. Information in this document supersedes and replaces all information previously supplied. The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners. © 2008 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 of America www.st.com 20/20