RT-Thread ADC device learning and use
Table of contents
1. ADC Sampling Basics
- 1.1 ADC channels
- 1.2 ADC Resolution
- 1.3 ADC sampling calculation
2. ADC device operation function
- 2.1 Find ADC device
- 2.2 Enable/disable ADC channel
- 2.3 Read sampled value
3. Example of ADC device
- 3.1 ADC device usage steps
- 3.2 Testing
I have thought about writing all the commonly used devices of RT-Thread. In fact, through the previous article “Comprehensive Understanding of RT-Thread I/O Device Model”, as well as the study and analysis of UART and PIN devices, for some other RT-Thread The equipment can basically be used by looking at the data by itself, and it is the same idea. It can be used by writing programs and testing by yourself.
In addition, RT-Thread has a lot of software packages, and we really don’t need to write too much in our actual ordinary applications (unless the device sensor you use is a niche brand or a new product, then according to the previous article, you must also deliberately write it yourself. Try to write the driver), so I will simply test a few typical devices, and the device driver will be updated.
This article is relatively light, testing a simple ADC device.
The development environment recorded in this RT-Thread column:
RT-Thread record (1. RT-Thread version, RT-Thread Studio development environment and quick start with CubeMX development)
RT-Thread record (2. RT-Thread kernel startup process – startup file and source code analysis)
RT-Thread Equipment Series blog post link:
RT-Thread records (10. Comprehensive understanding of RT-Thread I/O device model)
RT-Thread record (11. UART device of I/O device model – source code analysis)
RT-Thread record (12. UART device of I/O device model – use test)
RT-Thread record (thirteen, PIN device of I/O device model)
1. ADC Sampling Basics
ADC (Analog-to-Digital Converter) is an analog-to-digital converter that converts analog signals into digital signals, which is relatively simple. Before we test, briefly explain a few basic points of ADC use:
1.1 ADC channels
For the current MCU, there are generally ADC pins. Connect the analog quantity to be detected to the corresponding IO port, and it can be used after configuration. It has been recorded in the previous STM32 notes article, as shown in the following table:
1.2 ADC Resolution
The resolution is represented by the number of bits in binary (or decimal), generally 8 bits, 10 bits, 12 bits, 16 bits, etc.
For the STM32F1xx series chips we used for testing, their ADC supports a maximum of 12 bits, which can be seen in the STM32 chip manual:
The screenshot is to illustrate that a lot of information is actually explained in the official documents. Of course, the official documents are in English. Let me declare again that the best information for learning a chip is the official document.
You may be able to find many common problems by searching online, but if there are few online reference materials for a new chip, you have to think of the official documents!
What is the concept of resolution?
For example, if an ADC is set to 12-bit resolution, 2 to the 12th power = 4096, simply speaking, this ADC device divides its range into 4096 parts: 0 ~ 4095, the maximum value of 4095 is equal to the maximum value of its range.
Generally speaking, we use the STM32 VDDA pin to be directly connected to 3.3V, so the range of the STM32 ADC is 0~3.3V. If you read the ADC value of 4095, it means that the voltage you read is 3.3V.
As for whether the 12-bit resolution is 4095 or 4096, there are actually different opinions on the Internet. Most of them use 4095. In fact, the results are not much different.
1.3 ADC sampling calculation
What our ADC reads is a digital quantity, and the representation of the digital quantity is not intuitive, so generally we still have to convert it into an analog quantity. The conversion formula has also been mentioned in the previous STM32 usage record article, as shown in the figure below:
According to the above description, as long as the VDDA pin of STM32 is directly connected to 3.3V during circuit design, it can be calculated by the following formula:
The final read voltage value = VDDA (generally 3.3V) * read ADC value (understood as how many copies are actually read) / 4095 (12-bit resolution).
2. ADC device operation function
From the beginning of this article, we will not analyze the device driver implementation principle. The previous UART and PIN devices are already very thorough. If you are interested, you can check the source code analysis by yourself.
The ADC device operation function is relatively simple, only 4:
2.1 Find ADC device
rt_device_findAs I have seen before, the general lookup function of the I/O device model:
/*parameter description name ADC device name return - if the device handle finds the corresponding device, it will return the corresponding device handle RT_NULL No device found */rt_device_t rt_device_find(const char* name);
But what needs to be added here is that using this function to find the ADC device needs to be converted, and the device handle we define for receiving is not used
rt_device_t, instead you should use
2.2 Enable/disable ADC channel
Enable and disable the ADC channel according to the GPIO pin you use (corresponding to the ADC channel table above).
Enable ADC channel:
/*Parameter description dev ADC device handle channel ADC channel return - RT_EOK success - RT_ENOSYS failure, device operation method is empty Other error codes fail */rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_uint32_t channel)
Close the ADC channel:
/*parameter description dev ADC device handle channel ADC channel return——RT_EOK succeeds—RT_ENOSYS fails, device operation method is empty, other error codes fail*/rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel)
2.3 Read sampled value
/*parameter description dev ADC device handle channel ADC channel return - read value*/rt_uint32_t rt_adc_read(rt_adc_device_t dev, rt_uint32_t channel)
3. Example of ADC device
The principle and operation functions of ADC are relatively simple. We use ADC equipment in RT-Thread Studio, but its use steps need to be paid attention to:
3.1 ADC device usage steps
As we said before, in the RT-Thread project, all peripherals are configured in
board.hIn the file, we can view the comments on the ADC usage steps:
1. First, in the RT-Thread Studio project, open RT-Thread Settings and enable the ADC driver, as shown in the figure below:
Following the operation in the above figure, open the ADC driver through the component column, exit and save:
2. Macro definition
#define BSP_USING_ADC1(Here you need to determine which ADC you are going to use):
3. Find an ADC initialization code and configure it through STM32CubeMX:
We use STM32CubeMX to configure according to the first article in the series: RT-Thread records (1. RT-Thread version, RT-Thread Studio development environment and quick start with CubeMX development):
Remember to look at the ADC clock section after setting:
Generate the code directly after completion (don’t open it, go back to RT-Thread Studio and click OK if the interface pops up) and you can find that the project has been updated
At this point, we have been able to find what we need
adc.hheader file found
adc.cThis function in the file:
HAL_ADC_MspInitfunction copied to
board.cAt the end of the file, as shown below:
stm32xxxx_hal_config.hfile, enable the ADC module:
Here we should pay attention to the next point. We have used STM32CubeMX, so the location of this file has changed. This has been mentioned before. See the picture for details:
In fact, after we use STM32CubeMX to set up the ADC, we don’t need to modify this last step, because CubeMX has been modified in the file:
At this point, even if the configuration of the ADC is complete, we can directly use the ADC device operation function to realize the reading of the ADC in the application program.
Based on the above steps, we directly call the interface at the application layer, and directly put a picture to illustrate:
The power supply of the development board is not the standard 3.3V, but 3.28V, so the calculation of the final result is a bit wrong = =!
The operation and use of the ADC device learned in this article is relatively simple, but its configuration is relatively complicated.
After writing the ADC device, and plan to test another SPI device, it is time to make a summary of the device driver. The I/O device model part is actually learned in the previous blog posts, and it will be done. Others just need to take time to look at it. .