Skip to main content
RShre.2
Associate III
July 30, 2023
Question

How to read two adc channels in stm32 g071?

  • July 30, 2023
  • 4 replies
  • 6206 views

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. 

This topic has been closed for replies.

4 replies

RhSilicon
Lead
July 31, 2023
RhSilicon
Lead
July 31, 2023

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

RShre.2
RShre.2Author
Associate III
July 31, 2023

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. 

RhSilicon
Lead
July 31, 2023

@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:

ST Employee
July 31, 2023

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é

RShre.2
RShre.2Author
Associate III
July 31, 2023
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. 

 

ST Employee
July 31, 2023

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é