一、驱动实现
typedef enum
{
ADC_CH_KEY = 0,
ADC_CH_POWER,
ADC_CH_OH,
ADC_CH_NUM,
}ADC_CHx_INDEX;
typedef struct {
ADC_TypeDef * ADCx;
uint32_t RCC_APBxPeriph_ADCx;
}ADCx_CONFIG_TABLE;
typedef struct {
GPIO_TypeDef* GPIOx;
uint16_t GPIO_Pin;
uint32_t ADC_Channel_x;
uint32_t ADC_SampleTime_x_5Cycles;
}ADC_CHx_CONFIG_TABLE;
const ADC_CHx_CONFIG_TABLE c_adcchxConfigTable[ADC_CH_NUM] =
{
{GPIOA, GPIO_Pin_0, ADC_Channel_0, ADC_SampleTime_239_5Cycles},//PA0按键
{GPIOA, GPIO_Pin_1, ADC_Channel_1, ADC_SampleTime_239_5Cycles},//PA1软启动
{GPIOA, GPIO_Pin_4, ADC_Channel_4, ADC_SampleTime_239_5Cycles},//PA4热电偶
};
const ADCx_CONFIG_TABLE c_adcxConfigTable =
{
ADC1, RCC_APB2Periph_ADC1
};
volatile uint16_t AD_value[ADC_CH_NUM] = {0}; //实际AD值
void adc_dma_init(void)
{
uint8_t i = 0;
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
for (i = 0; i < ADC_CH_NUM; i++) {
/* Configure ADC Channel11 as analog input */
GPIO_InitStructure.GPIO_Pin = c_adcchxConfigTable[i].GPIO_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(c_adcchxConfigTable[i].GPIOx, &GPIO_InitStructure);
}
/* ADCs DeInit */
ADC_DeInit(c_adcxConfigTable.ADCx);
/* Initialize ADC structure */
ADC_StructInit(&ADC_InitStructure);
/* ADC1 Periph clock enable */
RCC_APB2PeriphClockCmd(c_adcxConfigTable.RCC_APBxPeriph_ADCx, ENABLE);
/* Configure the ADC1 in continuous mode with a resolution equal to 12 bits */
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(c_adcxConfigTable.ADCx, &ADC_InitStructure);
/* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
// ADC_ChannelConfig(c_adcxConfigTable.ADCx, c_adcchxConfigTable[i].ADC_Channel_x, c_adcchxConfigTable[i].ADC_SampleTime_x_5Cycles);
for(i = 0; i < ADC_CH_NUM; i++) {
ADC_ChannelConfig(c_adcxConfigTable.ADCx, c_adcchxConfigTable[i].ADC_Channel_x, c_adcchxConfigTable[i].ADC_SampleTime_x_5Cycles);
}
/* ADC Calibration */
ADC_GetCalibrationFactor(c_adcxConfigTable.ADCx);
/* Enable the ADC peripheral */
ADC_Cmd(c_adcxConfigTable.ADCx, ENABLE);
//ADC_DMACmd(ADC1, ENABLE);
/* Wait the ADRDY flag */
while(!ADC_GetFlagStatus(c_adcxConfigTable.ADCx, ADC_FLAG_ADRDY));
/* ADC1 regular Software Start Conv */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA时钟
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) &(ADC1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &AD_value;//采集数据数组地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize =ADC_CH_NUM;//缓冲为DC_CH_NUM
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;//TODO DMA_PeripheralDataSize_HalfWord
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;//TODO 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);
ADC_DMACmd(ADC1, ENABLE);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */
ADC_StartOfConversion(c_adcxConfigTable.ADCx);
}
uint16_t adc_get_sample_val(uint8_t channel)
{
if (ADC_CH_KEY == channel) {
return AD_value[0];
}else if (ADC_CH_POWER == channel) {
return AD_value[1];
} else if (ADC_CH_OH == channel) {
return AD_value[2];
}
}
二、应用层调用
uint16_t key_adc_value = 0;
uint16_t soft_power_value = 0;
uint16_t hot_pair_value = 0;
if (IS_TIMER_EVT(TIME_EVT_1024)) {
key_adc_value = adc_get_sample_val(ADC_CH_KEY);
soft_power_value = adc_get_sample_val(ADC_CH_POWER);
hot_pair_value = adc_get_sample_val(ADC_CH_OH);
debug_top("\r\nkey_v=%d soft_v=%d hot_v=%d\r\n", key_adc_value, soft_power_value, hot_pair_value);
}