/**********************************************************
* º¯Êý¹¦ÄÜ:void Adc_Init(void)
* Èë¿Ú²ÎÊý:
* ·µ»ØÊýÖµ:
* ¹¦ÄÜ˵Ã÷:
**********************************************************/
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1 , ENABLE ); //ʹÄÜADC1ͨµÀʱÖÓ
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ÉèÖÃADC·ÖƵÒò×Ó6 72M/6=12,ADC×î´óʱ¼ä²»Äܳ¬¹ý14M
//PA1 ×÷ΪģÄâͨµÀÊäÈëÒý½Å
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Ä£ÄâÊäÈëÒý½Å
GPIO_Init(GPIOB, &GPIO_InitStructure);
ADC_DeInit(ADC1); //¸´Î»ADC1
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC¹¤×÷ģʽ:ADC1ºÍADC2¹¤×÷ÔÚ¶ÀÁ¢Ä£Ê½
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Ä£Êýת»»¹¤×÷ÔÚµ¥Í¨µÀģʽ
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Ä£Êýת»»¹¤×÷ÔÚµ¥´Îת»»Ä£Ê½
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //ת»»ÓÉÈí¼þ¶ø²»ÊÇÍⲿ´¥·¢Æô¶¯
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADCÊý¾ÝÓÒ¶ÔÆë
ADC_InitStructure.ADC_NbrOfChannel = 1; //˳Ðò½øÐйæÔòת»»µÄADCͨµÀµÄÊýÄ¿
ADC_Init(ADC1, &ADC_InitStructure); //¸ù¾ÝADC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèADCxµÄ¼Ä´æÆ÷
ADC_Cmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1
ADC_ResetCalibration(ADC1); //ʹÄܸ´Î»Ð£×¼
while(ADC_GetResetCalibrationStatus(ADC1)); //µÈ´ý¸´Î»Ð£×¼½áÊø
ADC_StartCalibration(ADC1); //¿ªÆôADУ׼
while(ADC_GetCalibrationStatus(ADC1)); //µÈ´ýУ׼½áÊø
}
/**********************************************************
* º¯Êý¹¦ÄÜ:u16 Get_Adc(void)
* Èë¿Ú²ÎÊý:
* ·µ»ØÊýÖµ:
* ¹¦ÄÜ˵Ã÷:
**********************************************************/
u16 Get_Adc(void)
{
//ÉèÖÃÖ¸¶¨ADCµÄ¹æÔò×éͨµÀ£¬Ò»¸öÐòÁУ¬²ÉÑùʱ¼ä
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5 ); //ADC1,ADCͨµÀ,²ÉÑùʱ¼äΪ55.5ÖÜÆÚ
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//µÈ´ýת»»½áÊø
return ADC_GetConversionValue(ADC1); //·µ»Ø×î½üÒ»´ÎADC1¹æÔò×éµÄת»»½á¹û
}
/**********************************************************
* º¯Êý¹¦ÄÜ: u16 Get_Adc_Average(u8 times)
* Èë¿Ú²ÎÊý:
* ·µ»ØÊýÖµ:
* ¹¦ÄÜ˵Ã÷:
**********************************************************/
u16 Get_Adc_Average(void)
{
u32 temp_val=0,temp1=0,ResultVolt=0;
u8 t;
for(t=0;t<50;t++)
{
temp1=Get_Adc();
temp_val+=temp1;
}
ResultVolt=temp_val/50;
ResultVolt=(ResultVolt*3200) >>12;
if(ResultVolt>=3190)
{
ResultVolt=3190;
over_range_flag=TRUE;
}
return ResultVolt ;
}
基于各种原因,我只给楼主提供ADC部分以供参考 |