AT32F415单片机例程
/****************************************************************************************
File name:
Description:
将GPIO PC0作为AD输入口,PC0是ADC1的通道10
****************************************************************************************/
//------------------------------------- include files -----------------------------------#include <at32f4xx.h>
//------------------------------------- variable define ---------------------------------
unsigned int adc_val;
/*******************************************************************************
Function:
Descripton:
ADC input channel set
*******************************************************************************/
void adc_port_init(void)
{
GPIO_InitType GPIO_InitStructure;
/*Enable the GPIO Clock*/
#if defined (AT32F421xx)
RCC_AHBPeriphClockCmd(RCC_APB2PERIPH_GPIOC, ENABLE);
#else
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOC, ENABLE);
#endif
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0;
#if !defined (AT32F421xx)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_ANALOG;
#else
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_OutType = GPIO_OutType_PP;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_NOPULL;
#endif
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/*******************************************************************************
Function:
Descripton:
enable ADC clock
*******************************************************************************/
void adc_clk_config(void)
{
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_APB2CLK_Div4); // ê±?ó·??μéè??
/* Enable ADC1 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_ADC1, ENABLE); // ?a??ADCê±?ó
}
/*******************************************************************************
Function:
Descripton:
*******************************************************************************/
void adc_config(void)
{
ADC_InitType ADC_InitStructure;
__IO uint16_t ADCConvertedValue;
int calTimeOut = 1000;
/* ADC1 configuration ------------------------------------------------------*/
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanMode = DISABLE;
ADC_InitStructure.ADC_ContinuousMode = DISABLE; // μ¥′?×a??
ADC_InitStructure.ADC_ExternalTrig = ADC_ExternalTrig_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // bit0 = ADC D0
ADC_InitStructure.ADC_NumOfChannel = 10; // channel 10
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channels configuration */
ADC_RegularChannelConfig(ADC1, 10, 1, ADC_SampleTime_239_5);
/* Enable ADC1 */
ADC_Ctrl(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_RstCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
calTimeOut = 1000;
while(calTimeOut)
{
if(ADC_GetResetCalibrationStatus(ADC1))
calTimeOut--;
else
break;
}
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
calTimeOut = 1000;
while(calTimeOut)
{
if(ADC_GetCalibrationStatus(ADC1))
calTimeOut--;
else
break;
}
/* Start ADC1 Software Conversion */
//ADC_SoftwareStartConvCtrl(MY_ADC_SET, ENABLE);
}
/*************************************************************
Function:
Description:
*************************************************************/
void adc_init(void)
{
adc_clk_config();
adc_port_init();
adc_config();
}
/*************************************************************
Function:
Description:
*************************************************************/
void ad_convert(void)
{
// 开启ADC
ADC_ClearFlag(ADC1, ADC_FLAG_AWD | ADC_FLAG_EC | ADC_FLAG_JEC | ADC_FLAG_JSTR | ADC_FLAG_RSTR );
ADC_SoftwareStartConvCtrl(ADC1, ENABLE);
// 等待转换完成
while(RESET==ADC_GetFlagStatus(ADC1, ADC_FLAG_EC));
adc_val = ADC_GetConversionValue(ADC1);
}