2021-12-12 04:17 AM
void MX_ADC_Init(void)
{
/* USER CODE BEGIN ADC_Init 0 */
/* USER CODE END ADC_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC_Init 1 */
/* USER CODE END ADC_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc.Instance = ADC1;
hadc.Init.OversamplingMode = DISABLE;
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerFrequencyMode = DISABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC_Init 2 */
/* USER CODE END ADC_Init 2 */
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(adcHandle->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspInit 0 */
/* USER CODE END ADC1_MspInit 0 */
/* ADC1 clock enable */
__HAL_RCC_ADC1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**ADC GPIO Configuration
PA0 ------> ADC_IN0
*/
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN ADC1_MspInit 1 */
/* USER CODE END ADC1_MspInit 1 */
}
}
main()
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_ADC_Init();
while(1)
{
AD_RES = 0;
HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_RESET);
//HAL_UART_Transmit(&huart1, "HI\r\n", sizeof("HI\r\n"), 100);
// HAL_UART_Transmit(&huart2, "BYE\r\n", sizeof("HI\r\n"), 100);
HAL_ADC_Start(&hadc1);
// Poll ADC1 Perihperal & TimeOut = 1mSec
HAL_ADC_PollForConversion(&hadc1, 1);
AD_RES = HAL_ADC_GetValue(&hadc1);
sprintf(Buf,"raw value adc1 %u\n\r", AD_RES);
HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
HAL_Delay(100);
}
}
2021-12-12 07:03 AM
What value are you getting out?
Monitor the HAL_* functions to verify they are returning HAL_OK. If they are not, start your troubleshooting there.
2021-12-12 07:34 AM
Set this aka input signal impedance
hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
2021-12-12 07:26 PM
Thankyou for the reply im getting 2801 constantly
"Monitor the HAL_* functions to verify they are returning HAL_OK. If they are not, start your troubleshooting there"
Ok i will check it out
2021-12-12 07:32 PM
Yes i have added this line in the code..
2021-12-13 01:55 AM
2021-12-13 06:52 AM
thankyou i have added the maximum of "hadc.Init.SamplingTime = ADC_SAMPLETIME_160CYCLES_5;" The code got hanged., And i have now got a code which is working with the single adc but for polling adc0,adc1,adc5,adc6 all together the channel is not changing im getting only one value for all the adc i have attached my new code below any thoughts on that please let me know
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 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 = ENABLE;
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;
//naga
hadc.Init.SamplingTime = ADC_SAMPLETIME_160CYCLES_5;
if (HAL_ADC_Init(&hadc1) != 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_0;
// sConfig.Rank = 1;
// sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES;
// if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != 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_1;
// sConfig.Rank = 2;
// sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
// if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != 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_TEMPSENSOR;
// sConfig.Rank = 3;
// sConfig.SamplingTime = ADC_SAMPLETIME_112CYCLES;
// if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
// {
// Error_Handler();
// }
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
void ADC_Select_CH0 (void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 1;
//sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void ADC_Select_CH1 (void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 1;
//sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void ADC_Select_CH5 (void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = 1;
//sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
while (1)
{
AD_RES0 = 0;
AD_RES1 = 0;
HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_RESET);
ADC_Select_CH0();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
AD_RES0 = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
sprintf(Buf,"adc0 %u \r\n", AD_RES0);
HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
HAL_Delay(100);
ADC_Select_CH1();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
AD_RES1 = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
sprintf(Buf," adc1 %u \r\n", AD_RES1);
HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
HAL_Delay(100);
ADC_Select_CH5();
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
AD_RES5 = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
sprintf(Buf,"adc5 %u\n\r", AD_RES5);
HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);
HAL_Delay(100);
}