STM32 MCU ADC learning experience summary

Publisher:xrmilkLatest update time:2018-11-19 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 Because the company's products need to use AD to detect battery voltage, the requirements are not very high, and I suddenly want to use DMA+ADC+TIM. I thought it was very simple before, but the actual use made me feel ashamed. The problems I encountered made me confused. I kept checking information and testing, and finally solved the problems one by one. At the same time, I have a new understanding of STM32 ADC, and I plan to practice STM32 resources as much as possible in my spare time.


I used STM32F4 to debug ADC3+DMA+TIM1 (single channel). First, I checked the DMA information, and then I could read the data normally quickly by referring to the official ADC3+DMA. Then I directly added a timer to trigger the AD conversion, but it failed. I started to check the information and read the manual, and gradually understood the relationship between the three.


First, the timer generates a trigger signal, and the AD starts converting after detecting the conversion signal. Each time the data is converted, it is placed in the specified memory address through DMA until the DMA_BufferSize setting value set by the DMA is reached, at which time the DMA sets the corresponding flag bit, thus completing a DMA transfer.


From the above relationship, we can know that ADC conversion is one-time, that is, single-shot non-scanning mode (I tested AD single channel), because once the continuous mode is triggered, it will convert continuously, so the timer trigger conversion loses its meaning. Then DMA is set to normal mode, that is, after completing a DMA transfer, stop the transmission, and subsequent DMA requests will not be responded to, because after the DMA transfer is completed, it means that data processing can be performed. At this time, in order to prevent the data from being overwritten (there are other ways to prevent data from being overwritten on the Internet).


1> About the PWM output of the timer


At first, I used CH1 of timer 1 as the trigger signal of AD. The corresponding pin was PA8. When configuring the pin, I configured it in multiplexing mode without calling GPIO_PinAFConfig. PA8 was multiplexed into the output pin of TIM1. I overlooked an important factor about the timer clock, so the frequency I set was always wrong.


Check the reference manual of stmf4. If APBx_PRESC is 1, the timer clock is the PCLKx clock, otherwise it is 2 times PCLKx.


- If it is timer 1 and timer 8, you need to call TIM_CtrlPWMOutputs to turn on the pwm output, and then you can correctly view the waveform output of PA8 through the oscilloscope.


2>AD conversion

-ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;


I don't understand this sentence at all. After looking up the information, I found that the DMA of the ADC of stm32F4 has 4 modes, which is mainly used to increase the sampling speed by combining the ADC module. The default mode is similar to mode 1.


DMA mode 1 enabled (2 / 3 half-words one by one - 1 then 2 then 3)


//Get the ADC value in sequence, the resolution is 12 bits,


DMA mode 2 enabled (2 / 3 half-words by pairs - 2&1 then 1&3 then 3&2)


//You can use these three ADC modules together for sampling. The sampling speed is also three times that of a single one (2.4*3Msps). The resolution is 12 bits. After completing two conversions, the value should be


//ADC2+ADC1,ADC1+ADC3,ADC3+ADC2


DMA mode 3 enabled (2 / 3 bytes by pairs - 2&1 then 1&3 then 3&2)


//Mode 3 is similar to Mode 2, but the resolution requirement is 8 or 6 bits. Although the resolution is reduced, the conversion time is shorter than that of 12 bits.


-ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;


//Continuous mode must be disabled, otherwise the timer trigger will lose its meaning


ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;


//Check the register and find that you need to enable external trigger. The above is to turn on and set the polarity of the trigger signal


ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;


//Select the trigger time


-Once the external trigger is used, the software trigger does not need to be called again.


3> DMA transmission


- Each time the ADC converts, the DMA moves the data once. After reaching the specified number of times, the transfer is completed.


-DMA restart. I have seen many people on the Internet saying that DMA transfer cannot be achieved after DMA is turned off and then turned on again. The solution to DMA restart is mentioned in the presentation of the STM32 seminar.


I tested it according to the second method and found that if the data processing time is long, there will be problems. Then I turned off the timer and ADC together, processed the data, configured DMA, and turned on AD and timer, and it was normal. I am not sure where the problem is.


-The DMA of stm32f4 is divided into data stream and channel, where the channel is similar to the trigger source of stm32f1, and the data stream of F4 is similar to the channel of F1. 21ic basic knowledge


In this way, ADC+DMA+TIM works normally.


I want to use the internal ADC to display the collected waveform through ucgui, so as to enhance the application and understanding of AD. I use STM32 to collect the wave signal of the signal generator, collecting 300 points at a time, and then display it on the TFT screen through ucgui. In order to make the waveform look better, I checked some routines and oscilloscope information on the Internet, which said that the waveform can be reproduced and played back through digital interpolation. There are two commonly used methods of digital interpolation, one is linear interpolation and the other is sinx/x interpolation. Linear interpolation is easier to understand, and sinx/x interpolation is much more complicated. It is very troublesome to understand it. The tragedy of serious lack of mathematical skills is that I don’t even understand the principle and want to describe it in C language, so I can only use linear interpolation. However, there are C language examples of sinx/x interpolation on the Internet. After using linear interpolation, the waveform is much better than before. By adjusting the frequency of the trigger signal of TIM1, t/div is reached. How to calculate the frequency? At first, I planned to make a difference between the subscripts of the maximum and minimum values ​​of the AD acquisition results, and then multiply the absolute value by the period of tim1. Later, I decisively gave up for obvious reasons. Later, I queried the maximum and minimum values ​​and calculated the average value, and then queried once (the previous AD value was smaller than the average and the next value was larger than the average) to record the subscripts, and then queried the previous AD value was larger than the average and the next value was smaller than the average to record the subscripts. After making a difference between the two subscripts to calculate the absolute value and then calculating it with the frequency of the trigger signal, the frequency of the acquired waveform can be calculated. At present, I have only tested the square wave signal with a duty cycle of 50%, and the effect is good, but it needs to be improved, such as the case where the duty cycle is not 50%.


After a few nights of tinkering, I found that STM32 has a lot of resources, but I have only mastered a few basic things. I will continue to improve and practice in the future. I will write down the problems and understandings I encountered during the tinkering and share them with you. If there are any mistakes, I hope you can raise them and communicate with me.


Keywords:STM32 Reference address:STM32 MCU ADC learning experience summary

Previous article:Summary of 8 working modes of GPIO in STM32
Next article:STM32 learning notes: using library functions to drive LED lights

Recommended ReadingLatest update time:2024-11-23 08:31

【PIC Microcontroller】-- Introduction and Basic I/O – Buttons and LEDs
00 Write in front This series of articles is from a teacher who has served as a teaching assistant for the PIC microcontroller course. I mainly explained the contents of several experimental classes to my junior students. Here I record some of the knowledge learned in class. This series of articles mainly introduc
[Microcontroller]
【PIC Microcontroller】-- Introduction and Basic I/O – Buttons and LEDs
Power input and power supply method of analog-to-digital converter
    In this article, we will explore the issues of interfacing to an ADC through its various ports: power, ground, analog inputs, clock inputs, and digital I/O. We will begin by discussing the power input to the ADC, the common methods for powering the ADC, and some of the trade-offs of the different methods.     Be
[Power Management]
Power input and power supply method of analog-to-digital converter
Using a single chip microcomputer to make a multi-input voltage meter
In industrial control and intelligent instruments, single-chip microcomputers are often used for real-time control and real-time data processing. The information processed by the single-chip microcomputer is digital, while the relevant parameters of the controlled or measured object are often continuously changing anal
[Microcontroller]
Using a single chip microcomputer to make a multi-input voltage meter
42.485 Communication Experiment
1. 485 interface principle 2. 485 circuit chip SP3485 Generally, pins 2 and 3 are connected together. When pins 2 and 3 are at a low level, the RO terminal is enabled, and when pins 2 and 3 are at a high level, the DI terminal is enabled. 3. Circuit Using UART2 interface, PA2 is USART2_TX, PA3 is USART2_
[Microcontroller]
42.485 Communication Experiment
Timing calculation method of single chip microcomputer timer
For 12MHz, 1 machine cycle is 1us, 12/fosc = 1us. Mode 0: 13-bit timer maximum time interval = 2^13 = 8.192ms. Mode 1: 16-bit timer maximum time interval = 2^16 = 65.536ms. Mode 2: 8-bit timer maximum time interval = 2^8 = 0.256ms. =256 us. Timing is 5ms. Calculate the initial value of the timer M = 2^KX*Fosc/1
[Microcontroller]
Design of video intercom system based on 51 single chip microcomputer and Lonworks
With the emergence of intelligent residential areas, traditional doorbells are no longer suitable for modern families. People hope to understand the situation of visitors in a new way, while ensuring their own safety and reducing unnecessary troubles. Therefore, various doorbells have quietly entered thousands of ho
[Microcontroller]
Design of video intercom system based on 51 single chip microcomputer and Lonworks
STM32 MCU (1) Summary of learning materials + reference manual + LED light
Study Materials Video Tutorial  Li Xiang stm32 video tutorial 49 episodes  http://pan.baidu.com/s/1kTyt03P Atom teaches you how to use STM32  http://pan.baidu.com/s/1gd25r6F   Liu Yang STM32 (good)    http://pan.baidu.com/s/1mgkeNsG   http://www.iqiyi.com/u/1005856393    http://yun.baidu.com/share/home?uk=285396779
[Microcontroller]
STM32 memory distribution learning
1. STM32 memory distribution The figure below is a memory map of stm32, where the code area starts at 0x0800 0000, and its end address is 0x0800 0000 plus the actual chip's flash size. Its ram start address is 0x2000 0000, and the end address is still the actual chip's ram size.  2. The difference between RAM\ROM\FL
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号