STM32 rookie diary 5---AD conversion, stm32 dish diary 5 --- ad

Source: Internet
Author: User

STM32 rookie diary 5---AD conversion, stm32 dish diary 5 --- ad

First, click the link below to download the code we need. Link

1. Add necessary files:


As we have said before, three files must be added: startup_stm32f10x_xd.s, stm32f10x_rcc.c, and system_stm32f10x.c. Among them, the xd is selected based on the capacity of your chip. These three files can be found in the Code provided by qianfan. File Path: Core.rar \ Core \ STM32 \ Source \ Must.

In addition, to operate the I/O port, you must add a library file DeviceBase. cpp of qianfan. File Path: Core.rar \ Core \ system.

2. Set Include path


This will help the compiler find the header file of Qifan library. The specific setting method has been mentioned earlier. below is the link: I am the link, click me


3. Add the AD and serial code to the project.

Add the code of the active part in the qianguan database file to the project (File: ADConvert. cpp, path: Core.rar \ Core \ system ).

Add the serial code in the qianguan database file to the project (File: SerialPort. cpp, path: Core.rar \ Core \ system ).

Shows the Project after adding:


4. Write the Code:

Our experiment is to send the voltage value read from the built-in AD to the PC through the serial port. If you do not have a host computer on your computer (Click here to download the serial port debugging Assistant ).

Create a new main. cpp (write. cpp here, instead of. c) and add the following code to the main function:

#include"System.h"ADConvert convert(0);int main(){convert.Start();while(1){serial.Print("AD channel 0 :%f \n",convert.ReadVoltage());Delay(1000);}}

First, explain the Active Directory of the hardware of STM32F103C8. Qianfan uses only the first eight channels (Channel 0 ~ Channel 7 ). Corresponding to PA0 ~ PA7.

First, define an ADConvert variable. The variable name starts at will, and qianfan starts a convert. The following parameters are the channels you need to use. Here, Qifan uses channel 0, which corresponds to the PA0 on the hardware.

After selecting the channel, you need to start the ad ing. The Qifan code is a continuous conversion, that is, the second conversion is performed after the conversion, until you stop (convert. end () function ). How do I obtain the required conversion voltage value after channel conversion is enabled? Qianguan sets a function convert. ReadVoltage (). This function reads the voltage value converted through the specified channel. The return value is the voltage value, which is a float value. After reading the voltage value, you can send this voltage value to our computer for display. The following is on the Qifan computer:


The adconverter of STM32 is a 12-bit, and the conversion speed is very accurate. At the same time, the conversion speed is within us, which is enough in our daily learning. At the beginning, the voltage value of VCC (3.3 V) was measured. In the box, Qifan pulls the interface from the VCC and finds the measurement data above the GND. Why is the value 0.710596 displayed in the box? Because the voltage measurement line is flat at the time of just unplugging, there is no fixed level at this time, so this measurement value must be meaningless. The following 0 is the voltage value of the measured GND. These results are quite satisfactory.

If you are not familiar with serial, read this article:

5. How to measure the voltage values of multiple channels

Some people may think, what should I do if my system may require measurement of multiple voltage sources? The hardware of STM32 provides a solution, rule group and injection group. This implementation is troublesome and qianfan has not implemented it. However, Qifan proposed a solution in terms of code. The software switching method can be used to measure the voltage values of multiple channels. This is also a time-sharing method. First, measure the voltage value of the 0 channel, then switch the channel of the voltage (for example, Channel 1), and then measure the voltage of Channel 1. The measurement can be completed through multiple channels. The following is the code:

#include"System.h"ADConvert convert(0);int main(){convert.Start();while(1){convert.ChangeCh(0);serial.Print("AD channel 0 :%f \n",convert.ReadVoltage());convert.ChangeCh(1);serial.Print("AD channel 1 :%f \n",convert.ReadVoltage());Delay(1000);}}

The convert. ChangeCh (int) function can switch the channel. Its parameter is the new channel to be switched. In the code above, we used channel 0 and Channel 1, and measured the two channels through software switching. Of course, you can also measure the voltage of the eight channels here, just add a few lines of code. Below is:

Qianfan's channel 0 is connected to GND, and channel 1 is connected to VCC.


Is it easy to measure the voltage in this way.





How to Use stm32 adc Conversion

AD calibration is performed first, and only one calibration is required after power-on. Then, the corresponding IO is configured as the analog input mode, and the sampling frequency, sampling mode, and sampling channel of the ADC are configured, you have to configure DMA if you need to use DMA.

How to calculate STM32 ADC Conversion

The result is that the voltage value on your current AD pin is converted to a number relative to 3.3V and 4096.

If the AD result you get is the ADC_DR variable, they have the following relationships:

ADC_DR/current voltage = 4096/ 3300 mV

If you want to obtain the current voltage value during the reverse process, you can calculate it as follows:
Unsigned long Voltage;
Voltage = ADC_DR; // assume that the AD result is stored in the ADC_DR variable;

Voltage = (Voltage * 3300)/4096; // you get the correct Voltage result, unit: mV

Generally, for efficiency, we will share the following points:
Voltage = (Voltage * 825)/1024; // you get the correct Voltage result, unit: mV

Further, the efficiency will be higher:
Voltage = (Voltage * 825)> 10; // you get the correct Voltage result, unit: mV

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: info-contact@alibabacloud.com 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.