2025-04-12 3:46 AM
Hi! I have a question about the ADC WD. I am trying to use it, being triggered by a touchscreen, I have set it as follow:
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure the analog watchdog
*/
AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;
AnalogWDGConfig.HighThreshold = 0;
AnalogWDGConfig.LowThreshold = 0;
AnalogWDGConfig.Channel = ADC_CHANNEL_10;
AnalogWDGConfig.ITMode = ENABLE;
if (HAL_ADC_AnalogWDGConfig(&hadc1, &AnalogWDGConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_10;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
I did this in the while loop:
HAL_ADC_Start_IT(&hadc1);
while (1)
{ // Define the correct framebuffer address
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if (cmd == 0x01){
cmd = 0x00;
// Reconfigure ADC to read from first channel (ADC_CHANNEL_11)
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_12;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
// Start the ADC and poll for completion
//HAL_ADC_Start(&hadc1);
//HAL_ADC_PollForConversion(&hadc1, 10); // Wait for conversion to complete with a timeout
//uhADCxConvertedValue = HAL_ADC_GetValue(&hadc1); // Read the value from ADC_CHANNEL_11
// Reconfigure ADC to read from second channel (ADC_CHANNEL_9)
sConfig.Channel = ADC_CHANNEL_10;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
// Start the ADC and poll for completion
HAL_ADC_Start(&hadc1);
//HAL_ADC_PollForConversion(&hadc1, 10); // Wait for conversion to complete with a timeout
uhADCxConvertedValue = HAL_ADC_GetValue(&hadc1); // Add the value from ADC_CHANNEL_9
HAL_Delay(100);
sprintf(msg3, "Val: %d", uhADCxConvertedValue);
memset(buffer2, 255, sizeof(buffer2)); // Clear buffer
LCD_ShowString(buffer2, 100, 200, 16, msg3, 0);
HAL_Delay(100);
}
sprintf(msg3, "Val: Outside %d", uhADCxConvertedValue);
memset(buffer2, 255, sizeof(buffer2)); // Clear buffer
LCD_ShowString(buffer2, 100, 200, 16, msg3, 0);
}
And added this to set a flag when it is triggered:
void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
{
/* Set variable to report analog watchdog out of window status to main */
/* program.
* */
cmd = 0x01;
__HAL_ADC_CLEAR_FLAG(&hadc1, ADC_FLAG_AWD);
__HAL_ADC_CLEAR_FLAG(&hadc1, ADC_FLAG_EOC);
__HAL_ADC_CLEAR_FLAG(&hadc1, ADC_FLAG_OVR);
HAL_ADC_Start_IT(&hadc1);
}
Then my issue comes when I set a safeguard region for the watch dog. Once I run the code, it all works fine and I can see it toggling the flag in the oscilloscope, however, as soon as I hit the safeguard value range, it stops triggering.
If someone can help me!
Thank you!
2025-04-12 10:52 AM
Can you format the code using the </> tool? You've been here a while. It helps readability.