RT-Thread ADC equipment learning to use

Table of contents

foreword

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 sampling value

3. Example of ADC equipment

  • 3.1 Steps for using ADC equipment
  • 3.2 Testing

Epilogue

foreword

I have considered writing all the devices commonly used by RT-Thread. In fact, I learned from 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 reading the data by itself. It is the same idea, and it can be used by writing programs and tests by yourself.

In addition, RT-Thread has many software packages, it is true that we do not need to write too much in our actual ordinary applications (unless the device sensor you are using is a niche brand or a new product, then according to the previous article, you also deliberately yourself Try to write the driver), so I simply test a few typical devices, and the device driver will be updated.

This article is relatively easy to test a simple ADC device.

❤️
The development environment recorded in this RT-Thread column:
RT-Thread recording (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)
❤️
Links to the series of blog posts on RT-Thread devices:
RT-Thread record (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 (13. PIN device of I/O device model)

1. ADC Sampling Basics

ADC (Analog-to-Digital Converter) The analog-to-digital converter converts analog signals into digital signals, which is relatively simple. Before we test a few basic points of ADC use:

1.1 ADC Channels

For the current MCU, there are generally ADC pins, and the analog quantity to be detected is connected to the corresponding IO port, and the configuration can be used. For the STM32 we tested, the corresponding relationship of its ADC channels is in Previous STM32 notes have been recorded in the following table:

1.2 ADC Resolution

Resolution is expressed in binary (or decimal) digits, generally 8, 10, 12, 16 and so on.

For the STM32F1xx series chips we tested, their ADC supports a maximum of 12 bits, which can be seen in the STM32 chip manual:

The screenshot is to show that many materials are actually explained in the official documents. Of course, the official documents are in English. Once again, the best information for learning a chip is the official documents.

You may be able to find many common problems by searching online, but if there are few online references for a new chip, you have to think of the official documentation!

What is the concept of resolution?

For example, if an ADC is set to a resolution of 12 bits, 2 to the 12th power = 4096. Simply put, this ADC device divides its range into 4096 parts: 0 ~ 4095 The maximum value of 4095 is equal to the maximum value of his range.
Generally speaking, we use STM32 VDDA pin to connect 3.3V directly, so the range of STM32 ADC is 0~3.3V. If the ADC value of 4095 is read, it means that the voltage read is 3.3V.

As for whether the 12-bit resolution is in addition to 4095 or 4096, there are actually different opinions on the Internet, and 4095 may account for the majority, but the results are not much different.

1.3 ADC sampling calculation

What our ADC reads is digital, and the representation of digital is not intuitive, so generally we still have to convert it into analog. The conversion formula has also been used in previous STM32 usage records, as shown 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) * the read ADC value (understood as how many copies were actually read) / 4095 (12-bit resolution).

2. ADC device operation function

From the beginning of this article, we will not analyze the driver implementation principle of the device. The previous UART and PIN devices are already very thorough. If you are interested, you can view the source code analysis by yourself.

The ADC device operation function is relatively simple, only 4:

2.1 Find ADC device

rt_device_findSeen before, the general lookup function for 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 is found*/rt_device_t rt_device_find(const char* name);

But what needs to be explained here is that using this function to find the ADC device requires a forced conversion. The device handle we define to receive is not usedrt_device_t, instead you should usert_adc_device_t

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).

To 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)

Turn off the 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_disable(rt_adc_device_t dev, rt_uint32_t channel)

2.3 Read sampling 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 equipment

The principle and operation function of ADC are relatively simple. We use the ADC device in RT-Thread Studio, but his usage steps need a little attention:

3.1 Steps for using ADC equipment

As we said before, in the RT-Thread project, all peripherals are configured inboard.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 following figure:

Following the operation in the above figure, open the ADC driver through the component column, and exit the save:

2. Macro definition#define BSP_USING_ADC1(You need to determine which ADC you are going to use here):

3. Find an ADC initialization code and configure it through STM32CubeMX:

We follow the first article in the series: RT-Thread Recording (1, RT-Thread version, RT-Thread Studio development environment and quick start with CubeMX development) to use STM32CubeMX for configuration:

Remember to take a look at the ADC clock section after setup:

After completion, generate the code directly (don’t open it, go back to RT-Thread Studio if the interface pops up and click OK) and then you can find that the project has been updatedadc.h

At this point, we have been able to find what we needHAL_ADC_MspInitfile, viaadc.hheader file foundadc.cThis function in the file:

4. putHAL_ADC_MspInitfunction copied toboard.cAt the end of the file, as shown below:

5. Modificationstm32xxxx_hal_config.hfile to enable the ADC module:

Here we have to pay attention, 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 set up the ADC using STM32CubeMX, we don’t need to modify this last step, because CubeMX has been modified in the file:

At this point, even if the ADC configuration is all completed, we can use the ADC device operation function to read the ADC directly in the application.

3.2 Testing

Based on the above steps, we directly call the interface at the application layer and put a picture directly to illustrate:

Test Results:

The power supply of the development board is not the standard 3.3V, but 3.28V, so the final result is calculated with a little error = =!

Epilogue

The operation and use of the ADC equipment studied in this article is relatively simple, but its configuration is relatively complicated.

After writing the ADC device and planning to test another SPI device, I will make a summary of the device driver. In fact, the I/O device model part has been discussed in the previous blog articles, and I will know it. The rest just take time to take a look. .

Reviewing Editor: Tang Zihong

Leave a Reply

Your email address will not be published.