/** * @brief Configure COMP1 and COMP2 with interrupt * @param None * @retval None */ static void COMP_Config(void) { /*##-1- Configure the COMPx peripheral ###################################*/ /* COMP1 Init: the higher threshold is set to VREFINT ~ 1.22V but can be changed to other available possibilities */ Comp1Handle.Instance = COMP1; Comp1Handle.Init.InvertingInput = COMP_INVERTINGINPUT_VREFINT; Comp1Handle.Init.Output = COMP_OUTPUT_NONE; Comp1Handle.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED; Comp1Handle.Init.Hysteresis = COMP_HYSTERESIS_HIGH; Comp1Handle.Init.Mode = COMP_MODE_LOWPOWER; Comp1Handle.Init.WindowMode = COMP_WINDOWMODE_DISABLE; Comp1Handle.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING_FALLING; if(HAL_COMP_Init(&Comp1Handle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* COMP2 Init: the lower threshold is set to VREFINT/4 ~ 1.22 / 4 ~ 0.305 V but can be changed to other available possibilities */ Comp2Handle.Instance = COMP2; Comp2Handle.Init.InvertingInput = COMP_INVERTINGINPUT_1_4VREFINT; Comp2Handle.Init.Output = COMP_OUTPUT_NONE; Comp2Handle.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED; Comp2Handle.Init.Hysteresis = COMP_HYSTERESIS_HIGH; Comp2Handle.Init.Mode = COMP_MODE_LOWPOWER; Comp2Handle.Init.WindowMode = COMP_WINDOWMODE_ENABLE; Comp2Handle.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING_FALLING; if(HAL_COMP_Init(&Comp2Handle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Start COMP1 */ if(HAL_COMP_Start_IT(&Comp1Handle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Start COMP2 */ if(HAL_COMP_Start_IT(&Comp2Handle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } }
static void COMP_Config(void) { /*##-1- Configure the COMP peripheral ######################################*/ hcomp1.Instance = COMP1; hcomp1.Init.InvertingInput = COMP_INVERTINGINPUT_VREFINT; hcomp1.Init.Output = COMP_OUTPUT_NONE; hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED; hcomp1.Init.Mode = COMP_MODE_LOWPOWER; hcomp1.Init.Hysteresis = COMP_HYSTERESIS_NONE; hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLED; hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_IT_RISING_FALLING; if(HAL_COMP_Init(&hcomp1) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Start teh COMP1 and enable the interrupt ###########################*/ if(HAL_COMP_Start_IT(&hcomp1) != HAL_OK) { /* Initialization Error */ Error_Handler(); } }
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_ADC_Init(); APP_ADC_Init(&hadc); #ifdef USE_COMPARATORS MX_COMP1_Init(); MX_COMP2_Init(); #endif MX_DAC_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ HAL_UART_Transmit(&huart2,str,strlen((const char *) str),1000); HAL_UART_Transmit(&huart2,heading,strlen((const char *) heading),1000); dacConfig.DAC_Trigger = DAC_TRIGGER_NONE; dacConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; HAL_DAC_ConfigChannel(&hdac,&dacConfig,DAC_CHANNEL_1); HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_12B_R,2048); HAL_DAC_Start(&hdac,DAC_CHANNEL_1); /* Initialize ADC peripheral according to the passed parameters */ if (HAL_ADC_Init(&hadc) != HAL_OK) { while(-1); } /* ### - 2 - Start calibration ############################################ */ if (HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED) != HAL_OK) { while(-1); } /* ### - 3 - Channel configuration ######################################## */ /* Select Channel 0 to be converted */ ///sConfig.Channel = ADC_CHANNEL_0; //if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) //{ // Error_Handler(); //} /* ### - 4 - Start conversion in DMA mode ################################# */ if (HAL_ADC_Start_DMA(&hadc, (uint32_t *) adcBuf,ADC_BUFSIZE) != HAL_OK) { while(-1); } compValues[0]= '-'; compValues[1]= '-'; #ifdef USE_COMPARATORS HAL_COMP_Start_IT(&hcomp1); HAL_COMP_Start_IT(&hcomp2); #endif /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ uint16_t cnt=0; uint16_t dacValue = 0; memset(dataline, ' ', sizeof(dataline)); dataline[sizeof(dataline)-1] = 0; while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_12B_R,dacValue); #ifdef WAIT_BEFORE_READING int i; for(i=0; i < 10000; i++); #endif itoa (cnt,(char *) &dataline[0],5); itoa (dacValue,(char *)&dataline[10],5); itoa (adcBuf[0],(char *)&dataline[20],4); dataline[25] = '/'; itoa (adcBuf[2],(char *)&dataline[25],4); itoa (adcBuf[1],(char *)&dataline[40],4); dataline[45] = '/'; itoa (adcBuf[3],(char *)&dataline[45],4); #ifdef USE_COMPARATORS dataline[60] = compValues[0]; dataline[65] = compValues[1]; #else // memcpy("Not Used",dataline[60],8); #endif dataline[70] = (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==GPIO_PIN_SET)?'X':'0'; dataline[75] = (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==GPIO_PIN_SET)?'X':'0'; dataline[87] = '\r'; dataline[88] = '\n'; dataline[89] = 0; HAL_UART_Transmit(&huart2,dataline,strlen((const char *) dataline),1000); cnt++; dacValue = (dacValue >= (4096-DAC_STEPSIZE))?0:dacValue+DAC_STEPSIZE; } /* USER CODE END 3 */ }