DMA data transfer of STM32F4 ADC collected data

Publisher:幸福家园Latest update time:2016-12-19 Source: eefocusKeywords:STM32F4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

      Continuing from the previous article, let's start to play with ADC's DMA transmission. Because everyone is talking about DMA, even ST's examples use DMA.

        The data collected by the ADC is stored in a fixed register. When the conventional sampling method samples multiple channels, using DMA can better avoid the loss of collected data. When the DMA function of the ADC is enabled, a DMA request will be issued when each channel is converted. The DMA method cannot completely avoid the problem of data loss. To achieve data non-loss, it is necessary to enable the OVERRUN mode at the same time as the DMA, and stop data conversion when data is lost. We only need to detect whether the OVR time occurs to solve the problem caused by the loss of sampled data. For example, channel misalignment or something.

In the Reference manual of STM32F4, you can find that the DMA of ADC1 is mapped on DMA1, CH0, Stream0.

[Experiment 1, DMA method to collect single channel data]

       Configure the DMA initialization settings for ADC1 as follows:

//DMA initialization
DMA_InitStructure.DMA_BufferSize = 4;
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32 _t)&adcvalue1; //Target data bit
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure. DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_BASE+0x4C; //ADC->DR address
DMA_InitStructure.DMA_PeripheralBurst =DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_PeripheralInc = DMA _PeripheralInc_Disable;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_Init(DMA2_Stream0,&DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);

Enable DMA transfer in the ADC register. Use two functions. One is to set the DDS bit of CR2 so that DMA transfer is enabled every time the ADC data is updated.

The other is to set the DMA bit of ADC CR2 to enable DMA transfer of ADC.

Use the following two functions respectively:

ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE); //Start DMA transfer when source data changes
ADC_DMACmd(ADC1,ENABLE); //Enable DMA transfer of ADC

Finally, the ADC sampling value is read in adcvalue. It can be seen that the ADC sampling value can still be output without using the function ADC_GetConversionValue to read the ADC DR register:

  while(1)
  {
    for(i = 0;i<10000;i++)
    {
        sum += adcvalue1;
      if(i ==9999)
      {
         avgvota = sum/10000;
         sum = 0;
        printf("avg vota is: %d \r\n",avgvota*3300/0xfff);
      }
    }
  }

[Experiment 2, DMA method to collect 4 channel data]

To sample two channels of data at the same time, first change ADC_NbrOfConversion in ADC_InitStructyre. Then use ADC_RegularChannelConfig to add channel 0 to the scan channel sequence.

From one path to four paths, a total of one line of code was changed and three lines of code were added:

ADC_InitStructyre.ADC_NbrOfConversion = 2;

ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_144Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_144Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3,ADC_SampleTime_144Cycles);
RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_144Cycles);

During the experiment, connect the inputs of PA0, PA1, PA2, and PA3 to ground or a 3.3V power supply. You can see two data jumping on the computer: 0 and 3300, indicating that the data has been sampled.

【Notes】

While conducting this experiment, a small incident occurred.

When initializing the PA port, I wrote this:

GPIO_InitStructure.GPIO_Pin = GPIO_PinSource0 | GPIO_PinSource1 | GPIO_PinSource2 | GPIO_PinSource3;

This problem caused the failure of GPIO initialization, and the ADC could not sample the value of the corresponding pin. I have been looking for the configuration problems of DMA and ADC, and accidentally found that it is not possible to do so.

GPIO_PinSource0 and GPIO_Pin_0 are different. GPIO_Pin_0 should be used when initializing the pin. Check the macro definition in the library, the two values ​​are different.

GPIO_PinSource0 refers to the pin number, while GPIO_Pin_0 refers to the corresponding bit in the GPIo register.

After changing this, everything works fine and the data from the four inputs can be sampled perfectly.

In the next article, we will experiment with other working modes of ADC.


Keywords:STM32F4 Reference address:DMA data transfer of STM32F4 ADC collected data

Previous article:STM32F4 ADC internal temperature sensor
Next article:STM32F4 ADC1

Recommended ReadingLatest update time:2024-11-15 20:23

16.6410DMA Brief Introduction
1. Why do we need DMA? First, let's look at the serial port to transmit information: sending strings. Send function: Add the following send function in uart.c: Next call in main.c: Compile make, Burn to the development board: Make an SD card, set the de
[Microcontroller]
16.6410DMA Brief Introduction
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号