About STM32 ADC DMA usage experience (2)

Publisher:vettykattyLatest update time:2018-05-20 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

(II) ADC cyclically collects six voltages, using DMA.
     This experiment was really depressing. My lack of understanding of DMA made me fall into misunderstandings. After understanding it, I admired the power of DMA even more.
     The misunderstanding is: from the goal of the experiment, we know that this time we use DMA to transfer the data converted by ADC to an array in the memory for storage. Because 6 channels are collected, the scanning mode of ADC is enabled here. Once the ADC is started, the channels selected in SQRX will be converted in sequence. The problem is that I thought that ADC and DMA would not work in coordination at first, that is, ADC converts its own data and DMA transfers its own data. In this case, the array in the memory is not what I want. Later, I really studied it for a long time. With the reminder of a brother in the group, I realized that I might have thought too complicated. Maybe I could just convert it once in ADC and then transfer the data once in DMA. Ok, after the experiment, I learned that this idea is correct.
    Okay, after saying so much nonsense, let's get to the point.
    The six regular channels of ADC1 used here are: CH0, CH1, CH2, CH3, CH14, CH15,
                    and the corresponding pins are PA0, PA1, PA2, PA3, PC4, PC5.
    About the configuration of ADC:
    the scanning mode of ADC1 is started, as well as the continuous conversion mode and the independent working mode (only one ADC is used). Because DMA is used, the DMA bit must be enabled, and the external trigger (SWSTART) is used. The data is right-aligned. There are also SQRX and so on. I won’t talk about them. ADC interrupt is not needed here. The interrupt is in DMA.
    About the configuration of DMA:
    because the ADC request is specified in the first channel of DMA1, DMA_CH1 is used here, the peripheral address is the only data register of ADC (u32) & ADC1->DR, and the memory address is (u32) SendBuff array, which can store 6 elements. Here, the transmission completion interrupt (TCIF) is enabled, and the selection is to read from the peripheral, the circular mode, the peripheral address non-incremental mode, the memory address incremental mode, the peripheral data width is 16 bits, the memory address is 16 bits, and the non-memory to memory mode.
    About DMA interrupt function:
   When DMA transfers data 6 times, TCIF bit is automatically set, and the program enters the interrupt service function. First, turn off the continuous conversion of ADC. We put the array processing here, and send it to the serial port after processing. Through the computer's hyperterminal, you can see the data of the 6 pin voltages that keep changing. Don't forget to clear the interrupt flag and set the continuous conversion of ADC, and then start the conversion again.
    In the main program, just initialize the system function and the serial port, then configure DMA, start the regular conversion channel, and start DMA, and then wait in an infinite loop.
    Attached below is some code
----------------------------------------------------------------------------------------------------------- 
 void Adc_Init(void)
{    
 //Initialize IO port first
  RCC->APB2ENR|=1<<2; //Enable PORTA port clock 
  RCC->APB2ENR|=1<<4; //Enable PORTC port clock 
 GPIOA->CRL&=0XFFFF0000; //PA0 1 2 3 anolog input
 GPIOC->CRL&=0XFF00FFFF; //PC4,5 anolog input

 //Channel 10/11 settings    
 RCC->APB2ENR|=1<<9; //ADC1 clock enable   
 RCC->APB2RSTR|=1<<9; //ADC1 reset
 RCC->APB2RSTR&=~(1<<9); //Reset end     
 RCC->CFGR&=~(3<<14); //Division factor cleared 
 //SYSCLK/DIV2=12M ADC clock is set to 12M, ADC maximum clock cannot exceed 14M!
 //Otherwise, the ADC accuracy will decrease! 
 RCC->CFGR|=2<<14;        
 ADC1->CR1&=0XF0FFFF; //Working mode cleared
 ADC1->CR1|=0<<16; //Independent working mode  
 ADC1->CR1|=1<<8; //Scanning mode
 ADC1->CR2|=1<<1; //Enable continuous conversion            
 ADC1->CR2|=1<<8; //Enable DMA      
// ADC1->CR2&=~(1<<1); //Single conversion mode
 ADC1->CR2&=~(7<<17);    
 ADC1->CR2|=7<<17; //Software control conversion  
 ADC1->CR2|=1<<20; //Use external trigger (SWSTART)!!! An event must be used to trigger
 ADC1->CR2&=~(1<<11); //Right alignment

 ADC1->SQR1&=~(0XF<<20);
 ADC1->SQR1|=5<<20; //6 conversions in regular sequence
 ADC1->SQR3 = 0X00000000;
 ADC1->SQR3|= 0X1EE18820;       
 //Set sampling time of channels 0~3,14,15
 ADC1->SMPR1&=0XFFFC0FFF; //Clear sampling time of channels 14,15
 ADC1->SMPR2&=0XFFFFF000; //Clear sampling time of channels 0,1,2,3  

 ADC1->SMPR1|=7<<15; //Channel 15 239.5 cycles, increasing the sampling time can improve the accuracyADC1-  
 >SMPR1|=7<<12; //Channel 14 239.5 cycles, increasing the sampling time can improve the accuracyADC1-   
 >SMPR2|=7<<9; //Channel 3 239.5 cycles, increasing the sampling time can improve the accuracyADC1-  
 >SMPR2|=7<<6; //Channel 2 239.5 cycles, increasing the sampling time can improve the accuracyADC1-  
 >SMPR2|=7<<3; //Channel 1 239.5 cycles, increasing the sampling time can improve the accuracyADC1-  
 >SMPR2|=7<<0; //Channel 0 239.5 cycles, increasing the sampling time can improve the accuracy 

 ADC1->CR2|=1<<0; //Turn on AD converter  
 ADC1->CR2|=1<<3; //Enable reset calibration  
 while(ADC1->CR2&1<<3); //Wait for calibration to end     
    //This bit is set by software and cleared by hardware. This bit will be cleared after the calibration register is initialized.    
 ADC1->CR2|=1<<2; //Turn on AD calibration    
 while(ADC1->CR2&1<<2); //Wait for calibration to end
 //This bit is set by software to start calibration and cleared by hardware when calibration ends  
}     

 void MYDMA_Config(DMA_Channel_TypeDef*DMA_CHx,u32 cpar,u32 cmar,u16 cndtr)
{
 u32 DR_Base; //Used as a buffer, I don't know why. It is necessary
 RCC->AHBENR|=1<<0; //Turn on DMA1 clock
 DR_Base=cpar;
 DMA_CHx->CPAR=DR_Base; //DMA1 peripheral address 
 DMA_CHx->CMAR=(u32)cmar; //DMA1, memory address
 DMA1_MEM_LEN=cndtr; //Save DMA transfer data amount
 DMA_CHx->CNDTR=cndtr; //DMA1, transfer data amount
 DMA_CHx->CCR=0X00000000; //Reset
 DMA_CHx->CCR|=1<<1; //Allow interrupt after transfer
 DMA_CHx->CCR|=0<<4; //Read from peripheral
 DMA_CHx->CCR|=1<<5; //Cyclic mode
 DMA_CHx->CCR|=0<<6; //Peripheral address non-incremental mode
 DMA_CHx->CCR|=1<<7; //Memory incremental mode
 DMA_CHx->CCR|=1<<8; //Peripheral data width is 16 bits
 DMA_CHx->CCR|=1<<10; //Memory data width is 16 bits
 DMA_CHx->CCR|=1<<12; //Medium priority
 DMA_CHx->CCR|=0<<14; //Non-memory to memory mode
 MY_NVIC_Init(1,3,DMA1_Channel1_IRQChannel ,2);     

//Enable a DMA transfer
void MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx)
{
 DMA_CHx->CCR&=~(1<<0); //Disable DMA transfer 
 DMA_CHx->CNDTR=DMA1_MEM_LEN; //DMA1, amount of data transferred 
 DMA_CHx->CCR|=1<<0; //Start DMA transfer
}
u16 ADC1_DR, adcx;

void DMAChannel1_IRQHandler(void)
{       
 u16 i;
 u32 sum[6]={0},val=0;  
 LED0 =!LED0;
 ADC1->CR2&=~(1<<1); //Turn off continuous conversion            
 for( i = 0; i<768 ;i+= 6)
 {
  sum[0] += SendBuff[i];
  sum[1] += SendBuff[i+1];
  sum[2] += SendBuff[i+2];
  sum[ 3] += SendBuff[i+3];
  sum[4] += SendBuff[i+4];
  sum[5] += SendBuff[i+5];
 }
 for(i = 0;i <6;i++)
 {
  val = sum[i]/DMA_COUNT;
  ADC1_DR = sum[i]/DMA_COUNT;
  TEMP=(float)ADC1_DR*(3.3/4096); 
  adcx=TEMP;
  LCD_ShowNum(149,70+i*20,adcx,1,16);//display voltage value
  TEMP-=adcx;
  TEMP*=1000;
  LCD_ShowNum(165,70+i*20,TEMP,3, 16);
 }
 delay_ms(200); 
   DMA1->IFCR |= 1<<1; //Clear channel completion interrupt flag
 ADC1->CR2|=1<<1; //Enable continuous conversion            
 ADC1->CR2| =1<<22; //Start rule conversion channel          

--------------------------------------------------------------------------------------


Keywords:STM32 Reference address:About STM32 ADC DMA usage experience (2)

Previous article:ARM processor startup process——S3C2440, S3C6410, S5PV210
Next article:STM32 uses SWV to output debug information under KEIL

Recommended ReadingLatest update time:2024-11-16 13:33

STM32 study notes on low power mode mechanism
This article mainly explains the mechanism of STM32 low power mode, and does not focus on the program implementation of STM32 low power consumption. In addition, it becomes very simple to implement STM32 low power consumption with the help of STM32 firmware library. 1. STM32 chip performance Chip model used: stm32,
[Microcontroller]
STM32 study notes on low power mode mechanism
STM32 USB learning notes 2
Host environment: Windows 7 SP1 Development environment: MDK5.14 Target board: STM32F103C8T6 Development library: STM32F1Cube library and STM32_USB_Device_Library The STM32Cube library provides some USB-related routines. In the Applications directory under its project directory, open the STM3210E_EVAL directory and
[Microcontroller]
Sharing of experience in STM32 CAN fieldbus experiment
Recently, I have been doing CAN field bus experiments on the STM32 experimental board. I have only done serial communication on STC51 before. In comparison, I found that CAN bus is quite complicated. At the beginning, I knew that I was a novice. I only knew that CAN bus, like serial communication, 485 communication, an
[Microcontroller]
Sharing of experience in STM32 CAN fieldbus experiment
Design of high-speed and high-precision pipeline analog-to-digital converter
   introduction   With the advent of a new generation of wireless mobile communications, digital intermediate frequency receivers in communication systems have increasingly higher requirements for ADC speed and accuracy, and pipeline ADCs that take both speed and accuracy into account are a better choice to meet this
[Microcontroller]
Tips for STM32 MCUs: How to use IAR to develop STM32
BKP is not finished yet, why switch to RTC? Because RTC and BKP are somewhat related, it is impossible to separate them. The following is the introduction of RTC in the data sheet: Introduction to RTC The real-time clock is an independent timer. The RTC module has a set of counters that count continuously, and ca
[Power Management]
Tips for STM32 MCUs: How to use IAR to develop STM32
External interrupt related analysis
 //======================================================// **Detailed explanation of key control and external interrupt examples based on STM32 **In order to reduce duplication, the external interrupt routine and key control example are combined   Study them together. I first uploaded this article on the 51 Black Fo
[Microcontroller]
External interrupt related analysis
STM32 hardware SPI drives 0.96-inch OLED
1.OLED related See —- 51 Software simulation SPI drive OLED 2. Hardware SPI See - SPI Topic (II) - STM32 driver FLASH (W25Q64) 3. Drivers The driver is transplanted with reference to the 51 MCU, except that the simulated SPI is replaced with the STM32 hardware SPI, and there is no need to write the timing co
[Microcontroller]
Design of robot servo controller based on STM32
Introduction     At present, the research focus of robot control system is on open and modular control systems. The standardization and networking of robot controllers have become a research hotspot. At the same time, the research of robot servo controllers also has great application value. In terms of servo communica
[Microcontroller]
Design of robot servo controller based on STM32
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号