编译工具:MDK5代码生成工具:CubeMX(veision5.6.1)芯片:STM32072CBT6
直接上代码
ADC.h文件
#ifndef _adc_h
#define _adc_h
#include "sys.h"
#define LEAKAGE_0 ADC_Channel_0
#define LEAKAGE_1 ADC_Channel_1
#define LEAKAGE_2 ADC_Channel_2
#define LEAKAGE_3 ADC_Channel_3
#define LEAKAGE_4 ADC_Channel_4
#define LEAKAGE_5 ADC_Channel_5
#define LEAKAGE_6 ADC_Channel_6
void ADC1_DMA_Init(void);
#endif
ADC.c文件
#include "adc.h"
#define ADC1_DR_Address ((u32)0x40012400+0x4c)
uint16_t adcvalue
[7];
void ADC1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure
;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
, ENABLE
);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1
| RCC_AHBPeriph_GPIOA
, ENABLE
);
GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_0
| GPIO_Pin_1
|GPIO_Pin_2
|GPIO_Pin_3
| GPIO_Pin_4
|GPIO_Pin_5
| \
GPIO_Pin_6
| GPIO_Pin_7
;
GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AN
;
GPIO_Init(GPIOA
, &GPIO_InitStructure
);
GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_0
;
GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AN
;
GPIO_Init(GPIOB
, &GPIO_InitStructure
);
}
void ADC1_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure
;
ADC_InitTypeDef ADC_InitStructure
;
NVIC_InitTypeDef NVIC_InitStructure
;
DMA_DeInit(DMA1_Channel1
);
DMA_InitStructure
.DMA_PeripheralBaseAddr
= (u32
)&ADC1
->DR
;
DMA_InitStructure
.DMA_MemoryBaseAddr
= (u32
)&adcvalue
;
DMA_InitStructure
.DMA_DIR
= DMA_DIR_PeripheralSRC
;
DMA_InitStructure
.DMA_BufferSize
= 7;
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
);
ADC_DMARequestModeConfig(ADC1
, ADC_DMAMode_Circular
);
ADC_StructInit(&ADC_InitStructure
);
ADC_InitStructure
.ADC_Resolution
= ADC_Resolution_12b
;
ADC_InitStructure
.ADC_ContinuousConvMode
= ENABLE
;
ADC_InitStructure
.ADC_ExternalTrigConvEdge
= ADC_ExternalTrigConvEdge_None
;
ADC_InitStructure
.ADC_DataAlign
= ADC_DataAlign_Right
;
ADC_InitStructure
.ADC_ScanDirection
= ADC_ScanDirection_Upward
;
ADC_Init(ADC1
, &ADC_InitStructure
);
RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div2
);
ADC_ChannelConfig(ADC1
, LEAKAGE_0
, ADC_SampleTime_55_5Cycles
);
ADC_ChannelConfig(ADC1
, LEAKAGE_1
,ADC_SampleTime_55_5Cycles
);
ADC_ChannelConfig(ADC1
, LEAKAGE_2
,ADC_SampleTime_55_5Cycles
);
ADC_ChannelConfig(ADC1
, LEAKAGE_3
,ADC_SampleTime_55_5Cycles
);
ADC_ChannelConfig(ADC1
, LEAKAGE_4
,ADC_SampleTime_55_5Cycles
);
ADC_ChannelConfig(ADC1
, LEAKAGE_5
,ADC_SampleTime_55_5Cycles
);
ADC_ChannelConfig(ADC1
, LEAKAGE_6
,ADC_SampleTime_55_5Cycles
);
NVIC_InitStructure
.NVIC_IRQChannel
= DMA1_Channel1_IRQn
;
NVIC_InitStructure
.NVIC_IRQChannelCmd
= ENABLE
;
NVIC_InitStructure
.NVIC_IRQChannelPriority
= 1;
NVIC_Init(&NVIC_InitStructure
);
DMA_ITConfig(DMA1_Channel1
,DMA_IT_TC
,ENABLE
);
ADC_GetCalibrationFactor(ADC1
);
DMA_Cmd(DMA1_Channel1
, ENABLE
);
ADC_DMACmd(ADC1
, ENABLE
);
ADC_Cmd(ADC1
, ENABLE
);
while(!ADC_GetFlagStatus(ADC1
, ADC_FLAG_ADEN
));
ADC_StartOfConversion(ADC1
);
}
void DMA1_Channel1_IRQHandler(){
if(DMA_GetFlagStatus(DMA1_IT_TC1
)!=RESET
){
DMA_ClearFlag(DMA1_IT_TC1
);
ADC_StartOfConversion(ADC1
);
}
}
void ADC1_DMA_Init(void)
{
ADC1_GPIO_Config();
ADC1_Mode_Config();
}