cancel
Showing results for 
Search instead for 
Did you mean: 

How to read two adc channels in stm32 g071?

RShre.2
Associate III

I saw a similar post here that is related to my problem. But unlike in this post, I want to read to adc channel inside a timer interrupt which has a sampling rate of 50KHz. How do I do it? I tried following this post and but adding start and stop adc insider the timer didn't work. The timer interrupt stopped working. So, is there any other way around or what is it that I am doing wrong? I don't have a code sample for this particular problem at the moment but if anyone could help me get started, I'd appreciate your help. 

12 REPLIES 12
RhSilicon
Lead
RhSilicon
Lead

Note that the STM32 has to have multiple ADCs, this model you mentioned seems to only have 1 ADC (1x 12bits ADC). It would have to read one channel (ANALOG PIN) at a time.

en.BD_STM32G071

https://www.st.com/en/microcontrollers-microprocessors/stm32g071rb.html

But it says, 16-channels like in DAC where it has 2 channels. I used two DAC channels successfully. 
It should be able to read sequentially inside the timer, I assume? Maybe my question was a bit misleading. But thanks for the video, i did check it out and will try now. Hopefully, will work. 


@RShre.2 wrote:

But it says, 16-channels like in DAC where it has 2 channels. I used two DAC channels successfully. 
It should be able to read sequentially inside the timer, I assume? Maybe my question was a bit misleading. But thanks for the video, i did check it out and will try now. Hopefully, will work. 


I believe there was a mistake, DAC is used to input a Digital value and output an Analog value. When the input is Analog and the read value is Digital, the ADC is used.

This channel part is really confusing, but it's like this: there are 16 input channels, but only 1 ADC. So this STM32 model has to read one channel at a time. For example this image of 1 ADC and 8 input channels:

For example the STM32F405OE has 3 ADC and 24 channels. In this way it is possible to read 3 channels at the same time:

en.bd-stm32f405xx

 

 

GwenoleB
ST Employee

Hello @RShre.2,

First of all, I'm not in favor to start the ADC conversion inside a Timer interrupt. An interrupt must be as short as possible!
As you want to read the ADC->DR register inside the timer interrupt, I understand the conversion is already done, doesn't it?
The easiest way is to trigger a DMA transfer from ADC->DR to memory based on timer. In case of multiple channels, the best way will remain also to transfer all data by DMA configured in circular mode.

Best Regards,

Gwénolé

Hi Gwénolé,
This is what I am doing right now. As I learnt that getvalue doesn't actually do anything more than accessing the registers, I am not sure where the conversion is taking place. 
 
void TIM2_IRQHandler(void)
{
if (TIM2->SR & TIM_SR_UIF)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_6);
__HAL_ADC_CLEAR_FLAG(&hadc1, ADC_FLAG_EOS);
adc1 = HAL_ADC_GetValue(&hadc1);
DAC1 -> DHR12R1 = adc1;
__HAL_ADC_CLEAR_FLAG(&hadc1, ADC_FLAG_EOS);
adc2 = HAL_ADC_GetValue(&hadc1);
DAC1 -> DHR12R2 = adc2;
TIM2->SR = ~TIM_SR_UIF;  /* Clear all interrupts */
}
 
}
 
with this I get the following output, 
where blue should be around 3.3.V and red should be 1V but i think the adc is confused between two different channels. 
 
what is the pro of using a DMA instead of saving the value into a variable directly in the interrupt? I need to read adc values in real time to do further calculation so I am reading the adc inside the timer interrupt.
 
 
Btw, I have put the external trigger conversion source in adc (CubeMX) as tim2 which is my timer interrupt here. 

 

Hmm okay. 

As i understand these three adcs can read values simulatenously.

In my case, it's fine for me if the same adc with two different channels read values sequentially because I assume the HAL get value is fast enough that i can read two values within 50KHz. If that's possible, but when I tried that it's giving me weird value as I posted in another comment. 

If this is unavoidable with my stm then I have no other choice than drop this idea. 

GwenoleB
ST Employee

Hi,
I'll try on my side and propose you a solution that can fit with your need.

I keep in you in touch,

Best Regards,

Gwénolé