一、底层初始化
#ifndef _HAL_ADC_DRV_H_
#define _HAL_ADC_DRV_H_
typedef enum
{
ADC_CH_POWER = 0,
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;
/*----------------------------- External Variables ---------------------------*/
extern const ADCx_CONFIG_TABLE c_adcxConfigTable;
extern const ADC_CHx_CONFIG_TABLE c_adcchxConfigTable[ADC_CH_NUM];
/*------------------------- Global Function Prototypes -----------------------*/
void adc_hw_init(void);
#endif //_HAL_ADC_DRV_H_
const ADC_CHx_CONFIG_TABLE c_adcchxConfigTable[ADC_CH_NUM] =
{
{GPIOA, GPIO_Pin_4, ADC_Channel_4, ADC_SampleTime_239_5Cycles},
};
const ADCx_CONFIG_TABLE c_adcxConfigTable =
{
ADC1, RCC_APB2Periph_ADC1
};
void adc_hw_init(void)
{
uint8_t i;
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOC Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
for(i=0; 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);
/* ADC Calibration */
ADC_GetCalibrationFactor(c_adcxConfigTable.ADCx);
/* Enable the ADC peripheral */
ADC_Cmd(c_adcxConfigTable.ADCx, ENABLE);
/* Wait the ADRDY flag */
while(!ADC_GetFlagStatus(c_adcxConfigTable.ADCx, ADC_FLAG_ADRDY));
/* ADC1 regular Software Start Conv */
//ADC_StartOfConversion(c_adcxConfigTable.ADCx);
}
二、中间层处理及具体实现
#ifndef _ADC_DRV_H_
#define _ADC_DRV_H_
void adc_init(void);
void adc_start_sample(ADC_CHx_INDEX channel);
uint16_t adc_get_sample_val(void);
void adc_close_sample(ADC_CHx_INDEX channel);
void adc_sample_real_time(void);
#endif //_ADC_DRV_H_
typedef struct
{
uint8_t enable_flg;
uint8_t status;
uint16_t sample_val;
ADC_CHx_INDEX channel;
}adc_drv_t;
adc_drv_t adc_drv;
void adc_init(void)
{
adc_hw_init();
memset(&adc_drv, 0, sizeof(adc_drv));
}
void adc_start_sample(ADC_CHx_INDEX channel)
{
if(adc_drv.enable_flg) return;
adc_drv.enable_flg = 1;
adc_drv.status = 0;
adc_drv.channel = channel;
adc_drv.sample_val = 0;
ADC_StopOfConversion(c_adcxConfigTable.ADCx);
ADC_ChannelConfig(c_adcxConfigTable.ADCx, c_adcchxConfigTable[channel].ADC_Channel_x, c_adcchxConfigTable[channel].ADC_SampleTime_x_5Cycles);
ADC_StartOfConversion(c_adcxConfigTable.ADCx);
}
void adc_close_sample(ADC_CHx_INDEX channel)
{
if(adc_drv.enable_flg == 0) return;
adc_drv.enable_flg = 0;
ADC_StopOfConversion(c_adcxConfigTable.ADCx);
}
uint16_t adc_set_sample_val(void)
{
return adc_drv.sample_val;
}
void adc_sample_real_time(void)
{
if(adc_drv.enable_flg == 0) return;
if(IS_TIMER_EVT(TIME_EVT_1))
{
if(adc_drv.status == 0)
{
adc_drv.status = 1;
}
else
{
if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) != RESET)
{
adc_drv.sample_val = ADC_GetConversionValue(c_adcxConfigTable.ADCx);
}
adc_drv.status = 0;
}
}
}
三、开启某通道,采集值、关闭某通道
if (IS_TIMER_EVT(TIME_EVT_1024)) {
debug_top("\r\n------------111111---------------\r\n");
adc_start_sample(ADC_CH_POWER);
hot_pair_value = adc_set_sample_val();
//adc_close_sample(ADC_CH_POWER);
debug_top("\r\nhot_v=%d\r\n",hot_pair_value);
}