嵌入式实现双路adc通道转换

Faith ·
更新时间:2024-11-10
· 601 次阅读

本程序对应的开发板为蓝桥杯嵌入式开发板stm32f103rbt6,为中型mcu。当涉及两路甚至多路adc通道时,常用的方法有两种dma方法和注入通道,本文采用后者。

adc初始化

void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOB, &GPIO_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; 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_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); RCC_ADCCLKConfig(RCC_PCLK2_Div6); /* ADC1 regular channel8 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_239Cycles5);//对应pb.0 ADC_InjectedChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_239Cycles5); //内部温度传感器 ADC_TempSensorVrefintCmd(ENABLE);//该语句启动内部温度测量 ADC_AutoInjectedConvCmd(ADC1,ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); }

adc转换函数

常规通道转换

float Read_ADC_Conv(void) { float ADC_Value; /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)); ADC_Value=ADC_GetConversionValue(ADC1)*3.3/0xfff; return ADC_Value; }

注入通道转换

float Read_ADC_InjeConv(void) { float ADC_Value; /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_JEOC)); ADC_Value=ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_1); ADC_ClearFlag(ADC1,ADC_FLAG_JEOC); return ADC_Value; }

主程序

int main(void) { float temp; u8 string[20]; STM3210B_LCD_Init(); SysTick_Config(SystemCoreClock/1000); ADC_Config(); LCD_SetBackColor(White); LCD_SetTextColor(Blue); LCD_DisplayStringLine(Line0," ADC Test "); LCD_DisplayStringLine(Line1," "); LCD_DisplayStringLine(Line2," Chanel_8-PB0 V: "); LCD_DisplayStringLine(Line3," "); LCD_DisplayStringLine(Line4," "); LCD_DisplayStringLine(Line5," Chanel_16 Temp:"); LCD_DisplayStringLine(Line6," "); LCD_DisplayStringLine(Line7," "); LCD_DisplayStringLine(Line8," "); LCD_DisplayStringLine(Line9," "); while (1) { Delay_Ms(200); temp=Read_ADC_Conv(); sprintf((char*)string,"%.3f",temp); LCD_DisplayStringLine(Line3,string); temp=Read_ADC_InjeConv(); //sprintf((char*)string,"%.3f",(1430-temp*1000)/4.3+25); sprintf((char*)string,"%.3f",(5855.85-temp*3.3)/17.6085+25); LCD_DisplayStringLine(Line6,string); } }
作者:初入江湖的郭大侠



adc 嵌入 嵌入式

需要 登录 后方可回复, 如果你还没有账号请 注册新账号