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.
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
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Is it necessary to add a π-type filter circuit for serial communication? If the board is small and the space is limited, this must be considered.
- Application of Switching Power Supply ASIC in Synchronous Generator Excitation Control
- MSP430F5529 Power Management Module
- Hiring embedded software development engineers
- The conducted spurious signals of the RF with shielding cover are 5-6dB worse than those without shielding cover.
- What should we pay attention to when launching new products? In particular, is there any good advice on how to prevent plagiarism?
- China's mobile phone production accounts for half of the world's electronic product output in the first half of the year
- Design of Chinese Human-Machine Interface Based on MSP430 Microcontroller with Low Power Consumption
- Introduction to Driver Assistance System DAS
- Goodbye 2019, hello 2020. Some of the goals set in 2019 have not been achieved. It’s a pity. Let’s continue to work hard in 2020.