Cortex_m3_stm32 Embedded Learning Note (16): ADC Experiment (Analog-to-digital conversion)

Source: Internet
Author: User

Before learning a number of modes of electricity, A/D conversion of the knowledge, also Baidu a lot of information is difficult to understand. Forget, first self-righteous, and so on after the study of specialized courses and then again. (You must learn to go home in winter vacation.) Well, that's what it's all about. Reading so much information, the sense of A/D conversion is the voltage (or other analog: such as pressure, image, etc.) into the digital, d/A is the reverse, and the ADC is a/D converter, he can collect the external voltage into the digital. This section of the experiment uses an ADC to capture the external voltage converted to a digital display on the screen.

The STM32 has a single ADC (the STM32F101/102 series has only 1 ADCs), which can be used independently or in dual mode (increased sample rate). The STM32 ADC is a 12-bit successive approximation analog-to-digital converter . It has 18 channels and can measure 16 external and 2 internal signal sources. A/D conversion of each channel can be performed in a single, continuous, sweep, or discontinuous mode. The results of the ADC can be left-aligned or right-aligned in a 16-bit data register. The analogue look- door dog feature allows the application to detect whether the input voltage exceeds a user-defined high/low threshold.

The STM32 transforms the ADC into 2 channel groups: The Rule Channel Group and the Injection Channel group. The rule channel is equivalent to the program you are running, and the injection channel is equivalent to an interrupt. When your program executes normally, interrupts can interrupt your execution. Similarly, the conversion of the injected channel can interrupt the transformation of the regular channel, and after the injection channel has been converted , the rule channel will continue to convert.

An example of an image illustrates: if you put 5 temperature probes in your home yard, you put 3 inside.temperature probe; you need to keep an eye out for outdoor temperatures, but occasionally you want to see the temperature indoors; So you can use the rulesthe channel group loops through the 5 outdoor probes and displays the AD conversion results, and when you want to look at the temperature in the room, start with a buttonDynamic Injection Conversion Group (3 indoor probes) and temporarily display room temperature, when you release this button, the system will return to the rulesThe Channel Group continues to detect outdoor temperatures. From the system design, the measurement and display of indoor temperature is interrupted by the measurement and display of outdoor temperatureprocess, but the program design can be set up in the initialization phase of different conversion groups, the system does not have to change the operationThe configuration of the cyclic transformation to achieve the results of two tasks of non-interference and fast switching. It can be imagined that if there are no rulesThen the group and injection group are divided, and when you press the button, you need to configure the channel for the AD Loop scan from the new one, and then the release buttonyou need to configure the channel for the AD loop scan again.

However, this section only uses the rule channel, because it is a single-pass conversion mode: It may be understood that we are now only measuring voltage values in one place.

The steps for configuring the ADC are as follows:

1 ) Turn on the PA port and the ADC1 clock and set the PA1 as the analog input.
STM32F103RCT6 ADC Channel 1 on the PA1, so we first want to enable the PORTA clock, and then set the PA1 for the analog input. Enable the Gpioa and ADC clocks with the Rcc_apb2periphclockcmd function, set the input mode of the PA1, use the Gpio_init function. Here we list the ADC channels of the STM32 with the GPIO counterpart table:

    2 ) resets the ADC1and sets the ADC1 divide factor.
3) Initialize the ADC1 parameter, set the working mode of the ADC1 and the related information of the rule sequence.
4 ) enables the ADC to be calibrated.
5 ) to read the ADC value.

Configuring the ADC file adc.c

#include "adc.h" void Adc_init (void) {adc_inittypedef adc_ist; Gpio_inittypedef gpio_ist; Rcc_apb2periphclockcmd (rcc_apb2periph_gpioa| rcc_apb2periph_adc1,enable)//72M/6=12,ADC maximum time cannot exceed 14mrcc_adcclkconfig (RCC_PCLK2_DIV6);//Set ADC divider factor 6//PA1 Input pin Gpio_ist as analog channel. gpio_pin=gpio_pin_1; Gpio_ist. gpio_mode=gpio_mode_ain;//analog input Gpio_init (gpioa,&gpio_ist); Adc_deinit (ADC1);//Reset ADC1, set all the registers of peripheral ADC1 to the default value adc_ist. Adc_mode= ADC_MODE_INDEPENDENT;//ADC Standalone mode adc_ist. adc_scanconvmode=disable;//single channel mode adc_ist. adc_continuousconvmode=disable;//single Conversion Mode//conversion is triggered by the software rather than by the external trigger adc_ist. Adc_externaltrigconv=adc_externaltrigconv_none; Adc_ist. ADC_DATAALIGN=ADC_DATAALIGN_RIGHT;//ADC Data Right-aligns the adc_ist. adc_nbrofchannel=1;//number of ADC channels with sequential rules conversion adc_init (adc1,&adc_ist); Adc_cmd (adc1,enable);//enable the specified adc1adc_resetcalibration (ADC1);//Turn on reset calibration while (Adc_getresetcalibrationstatus (ADC1)) ;//wait for reset calibration to end adc_startcalibration (ADC1);//Turn on AD calibration while (Adc_getcalibrationstatus (ADC1));//wait for calibration to end}//get ADC value//ch: Channel value 0 ~3u16 GET_ADC (U8 ch) {//Set the rule group channel for the specified ADC to set their turnAdc_regularchannelconfig (ADC1,CH,1,ADC_SAMPLETIME_239CYCLES5) of the sequencing and sampling time; Adc_softwarestartconvcmd (ADC1, enable);//enable the ADC1 software conversion function to be specified while (! Adc_getflagstatus (ADC1,ADC_FLAG_EOC));//wait for the conversion to end return Adc_getconversionvalue (ADC1);//Returns the result of the most recent ADC1 rule group}u16 get_ Adc_average (U8 ch,u8 times) {u32 tem_val=0;u8 i;for (i=0;i<times;i++) {TEM_VAL+=GET_ADC (ch);d Elay_ms (5);} return tem_val/times;}

#ifndef _adc_h#define _adc_h#include "sys.h" #include "delay.h" void adc_init (void); U16 GET_ADC (U8 ch); U16 GET_ADC_ Average (U8 ch,u8 times); #endif

Main function
 #include "     Led.h "#include" delay.h "#include" sys.h "#include" usart.h "#include" lcd.h "#include" adc.h "void init () {delay_init (); The delay function initializes the uart_init (9600);  The serial port is initialized to 9600led_init (); Initialize the hardware interface with the LED connection lcd_init (); Adc_init (); point_color=red;//set the font to Red lcd_showstring (60,40,200,24,24, "ADC Test ^-^"); Lcd_showstring (60,70,200,16,16, "Medium difficulty"); Lcd_showstring (60,90,200,16,16, "2015/1/24"); Lcd_showstring (60,110,200,16,16, "By--mr yh");//Display message point_color=blue;//set font to Blue lcd_showstring (60,130,200,16,16, "Adc_ch0_val:"); Lcd_showstring (60,150,200,16,16, "adc_ch0_vol:0.000v");} int main (void) {U16 adcnum;float tem;init (); while (1) {adcnum=get_adc_average (adc_channel_1,10); Lcd_showxnum (156,130,adcnum,4,16,0);//Displays the value of the ADC tem= (float) adcnum* (3.3/4096); adcnum=tem; Lcd_showxnum (156,150,adcnum,1,16,0);//Display the whole digit tem-=adcnum;tem*=1000 of voltage value; Lcd_showxnum (172,150,tem,3,16,0x80);//Displays the decimal place of the value of the ADC led0=! Led0;delay_ms ()}} 

after you have obtained the value of the ADC: The formula for converting to a voltage value is not understood at all. Orz

But there is one place to be aware of the use of Lcd_showxnum ()

Re-turn out its source code

Displays the number, the high point is 0, or the//x,y: start coordinate//num: value (0~999999999); Len: Length (that is, number of digits to display)//size: Font size//mode://[7]:0, not filled, 1, fill 0.//[6:1]: Reserved//[0]:0, non-overlay display, 1, overlay display. Void Lcd_showxnum (U16 x,u16 y , u32 num,u8 len,u8 size,u8 mode) {  U8 t,temp;u8 enshow=0;   for (t=0;t<len;t++) {temp= (Num/lcd_pow (10,len-t-1))%10;if (enshow==0&&t< (len-1)) {if (temp==0) {if ( mode&0x80) Lcd_showchar (x+ (SIZE/2) *t,y, ' 0 ', size,mode&0x01);  Else Lcd_showchar (x+ (SIZE/2) *t,y, ", size,mode&0x01);   Continue;} else enshow=1;   
See the description of the last parameter, mode is a 8-bit variable, the 7th bit is 0 for not filled, and 1 for the fill.

At first, there was little understanding of the concept of filling, so the two results (fill and no fill) were burned in and looked at, and when the display was 0.001, it would show 0 if it was not populated. 1 (There are 2 spaces between the point and 1), and if it is filled it will display 0.001 (normal display) so my understanding of padding is: If a number 6, you want to display 006, then you need to set the number of the length of 3, fill mode

Cortex_m3_stm32 Embedded Learning Note (16): ADC Experiment (Analog-to-digital conversion)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.