MM32F003具有8路12位ADC,最高可以达到1Msps的转换速度。对于一般的电压检测是基本够用的。
这个例程通过while(1)
循环的方式,持续将ADC采样值转换成电压值后,通过串口输出。
ADC的初始化分为两个步骤:GPIO配置和ADC参数的配置。
1 ADC管脚的GPIO相关配置
ADCConfig(ADCch ADC_Channel)
函数实现ADC通道的设置,不同的ADC通道,通过GPIO_Config_AIN()
函数调用不同的GPIO配置参数。
void ADCConfig(ADCch ADC_Channel)
{
if( ADCch0 == ADC_Channel ) {
GPIO_Config_AIN(GPIOA, GPIO_Pin_0);
} else if( ADCch4 == ADC_Channel ) {
GPIO_Config_AIN(GPIOA, GPIO_Pin_4);
} else if( ADCch5 == ADC_Channel ) {
GPIO_Config_AIN(GPIOA, GPIO_Pin_5);
} else if( ADCch6 == ADC_Channel ) {
GPIO_Config_AIN(GPIOA, GPIO_Pin_6);
} else if( ADCch8 == ADC_Channel ) {
GPIO_Config_AIN(GPIOB, GPIO_Pin_0);
} else if( ADCch9 == ADC_Channel ) {
GPIO_Config_AIN(GPIOB, GPIO_Pin_1);
} else if( ADCch10 == ADC_Channel ) {
GPIO_Config_AIN(GPIOB, GPIO_Pin_3);
} else if( ADCch11 == ADC_Channel ) {
GPIO_Config_AIN(GPIOB, GPIO_Pin_4);
} else if( ADCch12 == ADC_Channel ) {
GPIO_Config_AIN(GPIOB, GPIO_Pin_7);
} else {
}
ADCSingleChannelInit(ADC_Channel);
}
GPIO_Config_AIN()
函数将对应的GPIO配置为模拟输入模式。
void GPIO_Config_AIN(GPIO_TypeDef* GPIOx, u16 GPIO_Pin_n)
{
GPIO_InitTypeDef GPIO_InitStructure;
if(GPIOx == GPIOA) {
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //GPIO clock starts
}
if(GPIOx == GPIOB) {
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); //GPIO clock starts
}
if(GPIOx == GPIOC) {
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); //GPIO clock starts
}
if(GPIOx == GPIOD) {
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE); //GPIO clock starts
}
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_n; //pin
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Output speed
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //GPIO mode
GPIO_Init(GPIOx, &GPIO_InitStructure);
}
2 配置ADC参数
void ADCSingleChannelInit(ADCch ADC_Channel_x)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_StructInit(&ADC_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Enable ADC clock
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_PRESCARE = ADC_PCLK2_PRESCARE_16; //ADC prescale factor
ADC_InitStructure.ADC_Mode = ADC_Mode_Continuous_Scan; //Set ADC mode to continuous conversion mode
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //AD data right-justified
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_x, 0, ADC_SampleTime_239_5Cycles);//Enable the channel
if(ADC_Channel_x == ADC_Channel_TempSensor) {
ADC_TempSensorCmd(ENABLE); //Enable internal temperature sensor
}
if(ADC_Channel_x == ADC_Channel_Vrefint) {
ADC_VrefintCmd(ENABLE); //Enable internal temperature sensor
}
//Enable ADCDMA
ADC_Cmd(ADC1, ENABLE); //Enable AD conversion
}
3 读取ADC值
u16 ADC1_SingleChannel_Get(void)
{
u16 puiADData;
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0);
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
puiADData = ADC_GetConversionValue(ADC1);
return puiADData;
}
4 将ADC值转为电压值,循环读取
多次读取ADC,取平均值。
u16 Get_Adc_Average(uint8_t times)
{
u32 temp_val = 0;
u8 t;
u8 delay;
for(t = 0; t < times; t++) {
temp_val += ADC1_SingleChannel_Get();
for(delay = 0; delay < 100; delay++);
}
return temp_val / times;
}
main()
函数实现:
int main(void)
{
delay_init();
//init uart
Uart_ConfigInit(9600);
ADCConfig(ADCch10); //use PB3
while(1) {
ADCVAL = Get_Adc_Average(5);
//fValue = ((float)ADCVAL / 4095) * 3.3; //use 3.3V as VDD
fValue = (((float)ADCVAL)/4095)*5; //use 5V as VDD
delay_ms(1000);
UartSendGroup((u8*)printBuf, sprintf(printBuf, "adc value = 0x%4X \r\n",ADCVAL));
UartSendGroup((u8*)printBuf, sprintf(printBuf, "voltage value = %f \r\n",fValue));
}
}
官方例程的修正已基本完成,需要程序的同学可以到百度网盘下载。
提取码:0o5i
另外做个小广告,我自己打样的mm32f003最小系统板已经焊接调试好,如果大家想要玩玩这块芯片,可以直接到我的淘宝小店---EE百宝箱---购买,拿到后可以直接烧写程序,验证官方每一个官方例程,快速上手。
你的支持和点赞,是让我坚持下去的最大动力!