赞
踩
这是根据373标准库中的案例写的但是有一点很坑的就是得先把SDADC使能再去设置分频,PWR这些,否则会一直卡在校验的地方
u8 adc_init(void)
{
SDADC_InitTypeDef SDADC_InitStruct;//定义SDADC_Inint()的结构体
SDADC_AINStructTypeDef SDADC_AINStructure;
GPIO_InitTypeDef GPIO_InitStructure;
uint32_t SDADCTimeout = 0;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDADC1, ENABLE);//SDADC1时钟开启
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);//开启PWR时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);//使能GPIOE时钟
SDADC_DeInit(SDADC1);//重新定义SDADC1
SDADC_Cmd(SDADC1, ENABLE);//使能SDADC1
PWR_SDADCAnalogCmd(PWR_SDADCAnalog_1, ENABLE);//开启SDADC1的pwr
RCC_SDADCCLKConfig(RCC_SDADCCLK_SYSCLK_Div12);//12分频,得到6Mhz信号
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//配置8,9为复用
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_Init(GPIOE, &GPIO_InitStructure);
SDADC_VREFSelect(SDADC_VREF_VDDA);//选择外部参考电压
delay_ms(5);
SDADC_Cmd(SDADC1, ENABLE);//使能SDADC1
SDADC_InitModeCmd(SDADC1, ENABLE);//进入初始状态
SDADCTimeout = 30;
while((SDADC_GetFlagStatus(SDADC1, SDADC_FLAG_INITRDY) == RESET) && (--SDADCTimeout != 0));//等待进入初始状态
if(SDADCTimeout == 0)//失败返回1
{
/* INITRDY flag is not set */
return 1;
}
SDADC_AINStructure.SDADC_InputMode = SDADC_InputMode_SEZeroReference;//差分,4分,参考0,偏移0
SDADC_AINStructure.SDADC_Gain = SDADC_Gain_1;
SDADC_AINStructure.SDADC_CommonMode = SDADC_CommonMode_VSSA;
SDADC_AINStructure.SDADC_Offset = 0;
SDADC_AINInit(SDADC1, SDADC_Conf_0, &SDADC_AINStructure);
SDADC_ChannelConfig(SDADC1, SDADC_Channel_8, SDADC_Conf_0);//配置SDADC1通道8为配置0
SDADC_InjectedChannelSelect(SDADC1, SDADC_Channel_8);//配置SDADC1通道8为输入通道
SDADC_InitStruct.SDADC_Channel=SDADC_Channel_8; //9.配置SDADC1通道8,单次快速模式
SDADC_InitStruct.SDADC_ContinuousConvMode=DISABLE;
SDADC_InitStruct.SDADC_FastConversionMode=DISABLE;
SDADC_Init(SDADC1,&SDADC_InitStruct);//sdadc1常规寄存器初始化没有写
SDADC_InitModeCmd(SDADC1, DISABLE);//退出初始状态
SDADC_CalibrationSequenceConfig(SDADC1, SDADC_CalibrationSequence_1);//配置SDADC1校准模式位0
SDADC_StartCalibration(SDADC1);//开始校准
SDADCTimeout = 30;
while((SDADC_GetFlagStatus(SDADC1, SDADC_FLAG_EOCAL) == RESET) && (--SDADCTimeout != 0));//等待校验完成
if(SDADCTimeout == 0)
{
/* EOCAL flag can not set */
return 2;
}
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。