STM32-单通道采集实例
实验要求:
利用ADC采集FSM4中电位器的数据
原理图分析
cubemx配置
1.轮询采集ADC的数值
uint32_t value;
while (1)
{
/*轮询采集ADC的数值*/
HAL_ADC_Start(&hadc1);//开启ADC,ADC进行采集
if(HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK)//判断标志位EOC是否置一,转换是完成
{
value = HAL_ADC_GetValue(&hadc1);
printf("采样值 = %d ",value);
printf("电压值 = %.2fV\n", value*3.3/4096);
}
HAL_Delay(1000);
2.中断采集ADC数值
主函数循环中
uint32_t value = 0;//声明一个全局变量
while (1)
{
HAL_ADC_Start_IT(&hadc1);
HAL_Delay(1000);
}
回调函数中
extern uint32_t value;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
if(hadc->Instance == ADC1){
value = HAL_ADC_GetValue(&hadc1);
printf("采样值 = %d ",value);
printf("电压值 = %.2fV\n", value*3.3/4096);
}
}
STM32-多通道采集实例
实验要求:
利用ADC采集电位器及STM32内部温度传感器的数据
原理图分析
cubemx配置
函数中使用轮询实现
uint32_t value = 0;
while (1)
{
HAL_ADC_Start(&hadc1);
if(HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK){
value = HAL_ADC_GetValue(&hadc1);
printf("value:%d ", value);
printf("ADC:%.2fV\n", value*3.3/4096);
}
if(HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK){
value = HAL_ADC_GetValue(&hadc1);
printf("value:%d ", value);
printf("ADC:%.2fV ", value*3.3/4096);
printf("temp:%.2f℃\n", (1.43 - (value*3.3/4096))/0.043 + 25);
}
HAL_Delay(1000);