2019-06-10 09:40 AM
I am using the board STM32F030R8, and I am trying to measure the voltage of two different ADC channels, one at a time. The fuction SendByte is for a multiplexer, then I have to measure some voltage, the two channels are measuring different parts of the circuit. Sometimes I get a value and sometimes it is just 0.
I was succesfull using the ADC to measure different channels before. But I was not using this function SendByte. I suppose the ADC is not synchronized with the function SendByte, because when the correspondent port of the multiplexer is not set the value in the circuit is 0.
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_ADC_Init();
while (1)
{
HAL_ADC_Start(&hadc); /* Start the ADC */
SendByte(0x01);
if(HAL_ADC_PollForConversion(&hadc, 100) == HAL_OK){ /* Channel 0 */
media_0 = HAL_ADC_GetValue(&hadc);
v_0 = 0.0008*media_0;
}
SendByte(0x02);
if(HAL_ADC_PollForConversion(&hadc, 100) == HAL_OK){ /* Channel 1 */
media_1 = HAL_ADC_GetValue(&hadc);
v_1 = 0.0008*media_1;
}
HAL_ADC_Stop(&hadc); /* stop the ADC */
HAL_Delay(100); /* 100 ms delay before starting ADC again */
}
}
void SendByte(int channel){
Output_Linha = channel;
for (i = 0; i < 8; i++) {
if (Output_Linha & (1 << i)) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_RESET);
} else {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_SET);
}
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_SET);
HAL_Delay(50);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_RESET);
}
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11, GPIO_PIN_SET);
HAL_Delay(50);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11, GPIO_PIN_RESET);
}
The configuration of the ADC is as it follows:
static void MX_ADC_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
hadc.Init.ContinuousConvMode = ENABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_1;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
I used CubeMX to generate the code. If you think you need something else to help me, please ask.
2019-06-10 12:20 PM
I don't use nor understand Cube, but if you poll for the ADC result, don't use Continuous mode, and convert only one channel at a time, reconfigure ADC for the other channel, and start it again.
JW