1、配置DAC
2、配置COMP,COMP1_INP 设置成Swtich with DAC_OUT1使两者内部相连,即外部输入引脚COMP1_INM会与DAC_OUT1引脚的电平比较,大于或者小于设定DAC电压阈值会触发上升沿和下降沿中断
3、初始化DAC与COMP
/* USER CODE BEGIN 2 */ HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_8B_R,100);//设置DAC电压阈值,大概1.28V左右 HAL_DAC_Start(&hdac,DAC_CHANNEL_1);//开启DAC HAL_COMP_Start_IT(&hcomp1);//开启比较中断 /* USER CODE END 2 */
4、比较中断回调函数
/* USER CODE BEGIN 4 */ void HAL_COMP_TriggerCallback(COMP_HandleTypeDef *hcomp) { int i = 0; if(hcomp->Instance == COMP1) { if(COMP_OUTPUTLEVEL_HIGH == HAL_COMP_GetOutputLevel(&hcomp1))//Input+大于Input- 即DAC设定的阈值大于输入的检测电压 { i = 1; } else//否则就是设定的电压阈值小于输入的检测电压 { i = 2; } } } /* USER CODE END 4 */
标签:CODE,HAL,阈值,DAC,COMP,USER,STM32CubeIDE From: https://www.cnblogs.com/lizhiqiang0204/p/16966461.html