Reputation: 35
I'm using cubemx to initiate the project, the mcu I'm using is stm32L053C6. To that I have 2 sensors and 2 transistors connected to adc inputs. The code below shows how I get the input value, voltage, from the first temperature sensor. Then I use the same code for each value I want to read. The problem here is that the first value I read is always wrong, it gives me 2048, and it's 12 bits resolution, so...
I wonder how I can fix this?
//Start ADC reading
if(HAL_ADC_Start(&hadc) != HAL_OK){
while(1) {}
}
//Wait for EOC (end of conversion)
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
//Read ADC value
//Repeat for all channels.
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
experiments[0+index].temperature += hadc.Instance->DR;
printf("\n ex0 i rolling %d\n", experiments[0+index].temperature);
The code for reading the voltage values for the 2 temperature sensors and transistors is as below. I read the values 16 times and calculate an average. But as I said the first value I read is 2048.
void readRollingADC(int index){
HAL_Delay(1);
//Start ADC reading
if(HAL_ADC_Start(&hadc) != HAL_OK){
while(1) {}
}
//Wait for EOC (end of conversion)
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
//Read ADC value
*
> ****//Repeat for all channels. while(!(hadc.Instance->ISR & ADC_ISR_EOC)){} experiments[0+index].temperature +=
> hadc.Instance->DR; printf("\n ex0 i rolling %d\n",
> experiments[0+index].temperature);****
*
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
experiments[0+index].ube += hadc.Instance->DR;
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
experiments[0+index].vrb += hadc.Instance->DR;
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
experiments[0+index].vrc += hadc.Instance->DR;
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
experiments[1+index].temperature += hadc.Instance->DR;
printf("\n ex1 i rolling %d\n", experiments[1+index].temperature);
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
experiments[1+index].ube += hadc.Instance->DR;
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
experiments[1+index].vrb += hadc.Instance->DR;
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
experiments[1+index].vrc += hadc.Instance->DR;
}
How do I fix this? Does it have something to do with the code or the initiation of the adc? What I can do is to read the first value and not save it, but I don't think that's the proper way :)
void MX_ADC_Init(void)
{
/**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_DIV1;
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.DMAContinuousRequests = DISABLE;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc.Init.LowPowerAutoWait = ENABLE;
hadc.Init.LowPowerFrequencyMode = ENABLE;
hadc.Init.LowPowerAutoPowerOff = ENABLE;
HAL_ADC_Init(&hadc);
sConfigAdc.Channel = ADC_CHANNEL_0;
if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
while(1){}
}
sConfigAdc.Channel = ADC_CHANNEL_1;
if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
while(1){}
}
sConfigAdc.Channel = ADC_CHANNEL_2;
if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
while(1){}
}
sConfigAdc.Channel = ADC_CHANNEL_3;
if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
while(1){}
}
sConfigAdc.Channel = ADC_CHANNEL_5;
if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
while(1){}
}
sConfigAdc.Channel = ADC_CHANNEL_6;
if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
while(1){}
}
sConfigAdc.Channel = ADC_CHANNEL_7;
if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
while(1){}
}
sConfigAdc.Channel = ADC_CHANNEL_8;
if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
while(1){}
}
/*
sConfigAdc.Channel = ADC_CHANNEL_9;
if(HAL_ADC_ConfigChannel(&hadc, &sConfigAdc) != HAL_OK){
while(1){}
} */
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hadc->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspInit 0 */
/* USER CODE END ADC1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_ADC1_CLK_ENABLE();
/**ADC GPIO Configuration
PA0 ------> ADC_IN0
PA1 ------> ADC_IN1
PA2 ------> ADC_IN2
PA3 ------> ADC_IN3
PA5 ------> ADC_IN5
PA6 ------> ADC_IN6
PA7 ------> ADC_IN7
PB0 ------> ADC_IN8
PB1 ------> ADC_IN9
*/
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN ADC1_MspInit 1 */
/* USER CODE END ADC1_MspInit 1 */
}
}
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
{
if(hadc->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspDeInit 0 */
/* USER CODE END ADC1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_ADC1_CLK_DISABLE();
/**ADC GPIO Configuration
PA0 ------> ADC_IN0
PA1 ------> ADC_IN1
PA2 ------> ADC_IN2
PA3 ------> ADC_IN3
PA5 ------> ADC_IN5
PA6 ------> ADC_IN6
PA7 ------> ADC_IN7
PB0 ------> ADC_IN8
PB1 ------> ADC_IN9
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1);
}
/* USER CODE BEGIN ADC1_MspDeInit 1 */
/* USER CODE END ADC1_MspDeInit 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
The thing is, I want to read the values temp1, transistor_1 voltage (ube1, vrb1, vrc1), temp2 transistor_2 voltage (ube2, vrb2, vrc2)
16 times to get an average the code:
HAL_Delay(2);
/* Set DAC at voltage level 1 (3.1v 0xF07)*/
setDAC(0xF07); //
printf("\n***************************\n");
HAL_Delay(2);
for(int i = 0; i < 16; i++){
readRollingADC(0); // read All inputs 16 times.
void shiftAverages(){
for(int i = 0; i < 8; i++){
experiments[i].temperature = (experiments[i].temperature >> 4);
experiments[i].ube = (experiments[i].ube >> 4);
experiments[i].vrb = (experiments[i].vrb >> 4);
experiments[i].vrc = (experiments[i].vrc >> 4);
}
}
I do these things for different setDAC values, and I enter the the function readRollingADC() ut every time I enter the function the first read are always 2048 then it works fine.
it seems that somehow it gives me high when I read the first value, is that somthing that are automatically stored in the ADC register?
//Start ADC reading
if(HAL_ADC_Start(&hadc) != HAL_OK){
while(1) {}
}
//Wait for EOC (end of conversion)
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
experiments[0+index].temperature += hadc.Instance->DR;
printf("\n ex0 i rolling %d\n", experiments[0+index].temperature);
HAL_ADC_Stop(&hadc);
HAL_Delay(20);
HAL_ADC_Start(&hadc);
HAL_Delay(20);
while(!(hadc.Instance->ISR & ADC_ISR_EOC)){}
experiments[0+index].ube += hadc.Instance->DR;
printf("\n ube si %d\n", experiments[0+index].ube);
HAL_ADC_Stop(&hadc);
HAL_Delay(20);
HAL_ADC_Start(&hadc);
HAL_Delay(20);
I did like this, bu this gives me tha same value for all channels, whas this what you ment or should I write somthing else?
Upvotes: 1
Views: 3609
Reputation: 141145
You have hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV
and hadc.Init.ContinuousConvMode = DISABLE
. I guess you need to restart ADC after each channel is converted. So between each conversion you need to call HAL_ADC_Stop
and then HAL_ADC_Start
. You don't even clean ISR_EOC flag between conversions, you have to do that manually, that's why it's better to call HAL_ADC_PollForConversions
to have it cleared. And you need to call HAL_ADC_Start
exactly the same count as channels configured, ie. 9 in your setup, otherwise the ADC channel selection will miss the channel you expect to read.
With more then 5 channels I would recommend using DMA with a buffer of exactly the amount of words as channels configured and set hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV
. That way with a single HAL_ADC_Start_DMA
you get all the channels bytes. With less then 5 channels, one regular channel + the rest injected channels with auto-injection do the job.
Remember to convert read ADC values with __LL_ADC_CALC_TEMPERATURE
to temperature and with __LL_ADC_CALC_DATA_TO_VOLTAGE
to voltages, from stm32l0_ll_adc.h.
You can read about stm32l0 hal drivers here.
Upvotes: 1