STM32 collects multi-channel AD through DMA

Publisher:乐呵的挑Latest update time:2018-05-13 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

environment:

Host: XP

Development environment: MDK4.23

MCU:STM32F103CBT6


illustrate:

Collect AD through pins PA1 and PA2. Collect AD 10 times for each channel.

#include "ad_driver.h"  

  

//Global variables  

//AD sampling storage space  

__IO uint16_t ADCConvertedValue[20];  

  

//function  

//Initialize AD  

void init_ad(void)  

{  

    ADC_InitTypeDef ADC_InitStructure;  

    DMA_InitTypeDef DMA_InitStructure;  

    GPIO_InitTypeDef GPIO_InitStructure;  

  

    //---------------------Charge AD initialization--------------------  

    //Start DMA clock  

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);  

  

    //Start ADC1 clock  

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);  

  

    //Sampling pin settings  

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);  

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;  

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;  

    GPIO_Init(GPIOA, &GPIO_InitStructure);  

  

    //DMA1 channel 1 configuration  

    DMA_DeInit(DMA1_Channel1);  

    //Peripheral address  

    DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS;  

    //Memory address  

    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADCConvertedValue;  

    //dma transmission direction is one-way  

    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;  

    //Set the length of the DMA buffer during transmission  

    DMA_InitStructure.DMA_BufferSize = 20;  

    //Set the DMA peripheral increment mode, a peripheral  

    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  

    //Set DMA memory increment mode  

    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  

    //Peripheral data word length  

    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;  

    //Memory data word length  

    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;  

    //Set DMA transfer mode: continuous loop mode  

    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;  

    //Set the DMA priority level  

    DMA_InitStructure.DMA_Priority = DMA_Priority_High;  

    //Set the variables in the two DMA memories to access each other  

    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;  

    DMA_Init(DMA1_Channel1, &DMA_InitStructure);  

      

    // Enable channel 1  

    DMA_Cmd(DMA1_Channel1, ENABLE);  

  

    //ADC1 configuration  

    //Independent working mode  

    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;  

    //scanning method  

    ADC_InitStructure.ADC_ScanConvMode = ENABLE;  

    //Continuous conversion  

    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;  

    //External trigger disabled  

    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  

    //Right align the data  

    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;  

    //Number of channels to convert  

    ADC_InitStructure.ADC_NbrOfChannel = 2;  

    ADC_Init(ADC1, &ADC_InitStructure);  

      

    //Rule mode channel configuration  

    ADC_RegularChannelConfig(ADC1, ADC_Channel_1 , 1, ADC_SampleTime_239Cycles5);  

    ADC_RegularChannelConfig(ADC1, ADC_Channel_2 , 2, ADC_SampleTime_239Cycles5);  

      

    // Enable DMA of ADC1  

    ADC_DMACmd(ADC1, ENABLE);  

      

    // Enable ADC1  

    ADC_Cmd(ADC1, ENABLE);  

      

    // Enable ADC1 reset calibration register    

    ADC_ResetCalibration(ADC1);  

    // Check if the calibration register is reset  

    while(ADC_GetResetCalibrationStatus(ADC1));  

      

    //Start calibration  

    ADC_StartCalibration(ADC1);  

    //Check if calibration is complete  

    while(ADC_GetCalibrationStatus(ADC1));  

       

    //Start software conversion of ADC1  

    ADC_SoftwareStartConvCmd(ADC1, ENABLE);  

}  

  

//Get charging voltage  

float voltage_charge(void)  

{  

    uint8_t i = 0;  

    uint16_t sum = 0;  

    float v = 0;  

  

    //Get the average value of charging voltage for 10 times  

    for (i = 0;i < 10;i++)  

    {  

        sum += ADCConvertedValue[i * 2];   

    }  

    sum /= 10;  

    //Convert to voltage value  

    v = 0.002991 * sum;  

  

    return v;  

}  

  

//Get the discharge voltage  

float voltage_discharge(void)  

{  

    uint8_t i = 0;  

    uint16_t sum = 0;  

    float v = 0;  

  

    //Get the average value of charging voltage for 10 times  

    for (i = 0;i < 10;i++)  

    {  

        sum += ADCConvertedValue[i * 2 + 1];   

    }  

    sum /= 10;  

    //Convert to voltage value  

    v = 0.002991 * sum;  

  

    return v;  

}  


Keywords:STM32 Reference address:STM32 collects multi-channel AD through DMA

Previous article:Understanding of dma in stm32
Next article:Summary of STM32 peripheral DMA usage

Recommended ReadingLatest update time:2024-11-23 10:29

STM32 program transplantation_Internal flash boot times management
1. Test environment: STM32C8T6 2. Test interface: 3. The serial port uses serial port 1, baud rate 9600 MCU pins------------CH340 pins VCC--------------------VCC GND-------------------GND PA9--------------------RXD   PA10-------------------TXD 1. Function: 1. Use STM32 internal falsh to record the number of boot times
[Microcontroller]
STM32 program transplantation_Internal flash boot times management
STM32 internal flash reading and writing program
/* Base address of the Flash sectors */   #define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */   #define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */   #define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes
[Microcontroller]
Quadcopter flight control system designed with STM32
1. Introduction Quadrotor is a vertical take-off and landing aircraft with compact structure and unique flight mode. Compared with ordinary aircraft, it has the advantages of simple structure, low failure rate and greater lift per unit volume. It has broad application prospects in many military and civilian fields and
[Microcontroller]
Quadcopter flight control system designed with STM32
Simulation IIC timing based on stm32 microcontroller
What I am going to talk about next is the analog IIC timing based on the stm32 microcontroller, as well as some things to pay attention to; combined with the MMA7455 accelerometer I made, I posted the source code of the analog IIC for your reference. 1. Because in the IIC protocol, when the bus is idle, both SDA and
[Microcontroller]
Simulation IIC timing based on stm32 microcontroller
STM32 Novice Growth Record---GPIO Usage
span style="font-size:14px;" /span   span style="font-size:14px;" #include "stm32f10x_lib.h"   #include "stm32f10x.h"      GPIO_InitTypeDef GPIO_InitStructure; //Define GPIO macro operation structure      void Delay(__IO uint32_t nCount) //__IO macro definition volatile   {     for(; nCount != 0; nCount--); 
[Microcontroller]
STM32 Novice Growth Record---GPIO Usage
Some tips for DMA programming of STM32F4/F2
    The STM32F2/F4 DMA controller is carefully designed, and the firmware program is quite flexible in selecting the appropriate 16-data stream x 16-channel combination. The dual AHB port structure and the direct path to the APB bridge avoid the CPU pause on the AHB1 access when the DMA services the low-speed APB peri
[Microcontroller]
stm32 PWM input mode measuring pulse speed and duty cycle
1. Conceptual understanding PWM input capture mode is a special case of input capture mode. 1. Each timer has four input capture channels IC1, IC2, IC3, and IC4. IC1 and IC2 form a group, and IC3 and IC4 form a group. And the corresponding relationship between the pins and registers can be set. 2. Two ICx signals are
[Microcontroller]
Solar-LED Street Light Solution Based on STM32 MCU
As fossil energy is decreasing and the problem of global warming caused by excessive greenhouse gas emissions is becoming more and more important, people are actively developing various types of renewable energy on the one hand, and advocating green environmental protection technologies for energy conservation and e
[Microcontroller]
Solar-LED Street Light Solution Based on STM32 MCU
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号