STM32F103 ADC+DMA 双通道(GD32F103也通用)

GD32F103也通用此代码
直接上代码

#include "adc.h"
#include "delay.h"

#define ADC1_DR_Address    ((uint32_t)0x4001244C) 

#define ADC1_SampleTimes  2  //采样次数
#define ADC1_ChannelCnt   2  //采样通道数

 __IO uint16_t ADCConvertedValue[ADC1_SampleTimes][ADC1_ChannelCnt];


void ADC_IO_Init(void)  
{
  GPIO_InitTypeDef GPIO_InitStructure;
	
  RCC_APB2PeriphClockCmd(  ADC_GPIO_CLK1|ADC_GPIO_CLK2, ENABLE);

  GPIO_InitStructure.GPIO_Pin = ADC_PIN1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(ADC_GPIO_PORT1, &GPIO_InitStructure);
	
  GPIO_InitStructure.GPIO_Pin = ADC_PIN2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(ADC_GPIO_PORT2, &GPIO_InitStructure);
}

void   ADC_DMAConfig(void)  
{
	 DMA_InitTypeDef    DMA_InitStructure;
	
	 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr =  (uint32_t)&(ADC1->DR);
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = ADC1_ChannelCnt * ADC1_SampleTimes;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  
  DMA_Cmd(DMA1_Channel1, ENABLE);
}


void   ADC_Config(void)
{
	ADC_InitTypeDef     ADC_InitStructure;
	
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
	
	ADC_DeInit(ADC1);
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = ADC1_ChannelCnt;
  ADC_Init(ADC1, &ADC_InitStructure);
 
  RCC_ADCCLKConfig(RCC_PCLK2_Div6);  

  ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 2, ADC_SampleTime_55Cycles5);
 
  ADC_DMACmd(ADC1, ENABLE);
  ADC_Cmd(ADC1, ENABLE);
 
  ADC_ResetCalibration(ADC1);
  delay_ms(100);
  while(ADC_GetResetCalibrationStatus(ADC1));
  ADC_StartCalibration(ADC1);
  while(ADC_GetCalibrationStatus(ADC1));
  
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
 
}
//初始化外设时调用
void   UserADC_init(void)
{
	ADC_IO_Init();
	ADC_DMAConfig();
	ADC_Config();
}
//在任意要取ADC值的地方调用
uint16_t   GetADCVal(uint8_t pinIndex)
{
	return  ((ADCConvertedValue[0][pinIndex]+ADCConvertedValue[1][pinIndex])>>1);
}
#ifndef __ADC_H
#define __ADC_H

#include "stm32f10x.h"

 
#define ADC_PIN1               GPIO_Pin_0       /* PB0 */
#define ADC_GPIO_PORT1         GPIOB                    /* GPIOB */
#define ADC_GPIO_CLK1          RCC_APB2Periph_GPIOB

#define ADC_PIN2               GPIO_Pin_1       /* PA1 */
#define ADC_GPIO_PORT2         GPIOA                    /* GPIOA */
#define ADC_GPIO_CLK2          RCC_APB2Periph_GPIOA

void       UserADC_init(void) ;
uint16_t   GetADCVal(uint8_t pinIndex);

#endif //__ADC_H

你可能感兴趣的:(STM32,stm32,单片机,arm)