STM32F103C8T6 DMA ADC not working

Post here all questions related to LibMaple core if you can't find a relevant section!
Post Reply
nikjayswal
Posts: 6
Joined: Tue Feb 25, 2020 9:16 am

STM32F103C8T6 DMA ADC not working

Post by nikjayswal »

i am using stm32f103c8t6. i am working on osciloscope project the input frequency is 2KHz Square wave. the problem i getting is not showing proper square wave on arduino Serial plotter but when i change 2KHz into 99Khz the Serial Plotter showing Square Wave. is Arduino Code is Wrong ?


Code: Select all


#include <STM32ADC.h>
#include <HardwareTimer.h>
STM32ADC myADC(ADC1); 

uint8 pin = PA7; 

volatile static bool dma1_ch1_Active; //flag for interrupt

float usPeriod,timer_freq,ary[320];
uint16_t timPeriod_c;

#define maxSamples 1000
uint16_t buffer[maxSamples];

void setup() {
  Serial.begin(115200); 
  pinMode(pin, INPUT_ANALOG);//AD pin.
  usPeriod = 250;
  timer_freq = 1000000/usPeriod;
  timPeriod_c = 72000000/timer_freq;
  
}



void loop() {  
      // Take our samples
      DMA();
      if(dma1_ch1_Active == 0){
        for(int x=0; x<maxSamples; x++){
          Serial.println(buffer[x]);
        }
      }
}

void DMA(){
  
        Timer3.setPrescaleFactor(1);
        Timer3.setOverflow(timPeriod_c);
        
//  timer_set_period(Timer3, usPeriod);
  Timer3.setMasterModeTrGo(TIMER_CR2_MMS_UPDATE);
//  myADC.calibrate();
  myADC.setSampleRate(ADC_SMPR_1_5); //default - myADC.setSampleRate(ADC_SMPR_1_5); 
  myADC.setPins(&pin, 1);
  dma1_ch1_Active = 1;
  myADC.setTrigger(ADC_EXT_EV_TIM3_TRGO);
  myADC.setDMA(buffer, maxSamples, (DMA_MINC_MODE | DMA_TRNS_CMPLT), DMA1_CH1_Event);  
//  myADC.setDMA(buffer, maxSamples, (DMA_MINC_MODE | DMA_TRNS_CMPLT), DmaIRQ);
    while (dma1_ch1_Active >= 1){}; //wait for DMA to complete.  
//  dma1_ch1_Active = 0;
//  myADC.startConversion();
//  myADC.setTrigger(ADC_EXT_EV_SWSTART);
    dma_disable(DMA1, DMA_CH1); //End of trasfer, disable DMA and Continuous mode.
        
  }

 
static void DMA1_CH1_Event() {
  dma1_ch1_Active = 0;
}
Attachments
99KHz Square Wave
99KHz Square Wave
99Khz.png (81.89 KiB) Viewed 6611 times
2KHz Square Wave
2KHz Square Wave
ADC.png (83.9 KiB) Viewed 6611 times
by stevestrong » Thu Apr 09, 2020 7:41 pm
Have you ever heard about aliasing? And Nyquist theorem?
If not, I strongly advise to get information about it.

Short answer: the sampling frequency should be at least double of the minimum sampled frequency.
In practice, factor 10 is actually recommended.
With 4 kHz sampling freq you cannot get useful data for input signals above 400Hz.
Everything you measure now makes no sense.
Increase the sampling frequency to at least 100kHz, even better 1MHz.
Of course, your data buffer should be then increased respectively.
Then you will see better results.
Go to full post
Last edited by nikjayswal on Thu Apr 09, 2020 7:10 pm, edited 2 times in total.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: STM32F103C8T6 DMA ADC not working

Post by stevestrong »

Please post a simple test sketch without any tft functions or other libs, which can be tested by any others.
Please use the code tag for posting code (see the 5th icon from left when editing the post).

How do you generate and measure the input frequency?
nikjayswal
Posts: 6
Joined: Tue Feb 25, 2020 9:16 am

Re: STM32F103C8T6 DMA ADC not working

Post by nikjayswal »

Edited and removed tft functions. The square wave frequency generated from another stm32f103 MCU and frequency measured with Digital Frequency Meter.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: STM32F103C8T6 DMA ADC not working

Post by stevestrong »

Have you ever heard about aliasing? And Nyquist theorem?
If not, I strongly advise to get information about it.

Short answer: the sampling frequency should be at least double of the minimum sampled frequency.
In practice, factor 10 is actually recommended.
With 4 kHz sampling freq you cannot get useful data for input signals above 400Hz.
Everything you measure now makes no sense.
Increase the sampling frequency to at least 100kHz, even better 1MHz.
Of course, your data buffer should be then increased respectively.
Then you will see better results.
nikjayswal
Posts: 6
Joined: Tue Feb 25, 2020 9:16 am

Re: STM32F103C8T6 DMA ADC not working

Post by nikjayswal »

as you said to increase Sampling frequency factor of 10. i had increased the frequency and it's working good but when i had increased the input frequency above 10KHz it's not working properly i also tried
Timer3.setPrescaleFactor(1);
Timer3.setOverflow(1);
but same problem. as other tool as in the Link it has same processor but it is working above 50Khz
https://jyetech.com/dso-138-oscilloscope-diy-kit/

video : https://www.youtube.com/watch?v=x19kwG-wJRI
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: STM32F103C8T6 DMA ADC not working

Post by stevestrong »

You cannot use any frequency to drive the ADC, because it has its internal limitations.
So even if you drive the trigger to 18MHz, it will still not work.
As homework: calculate the maximum acquisition rate of the ADC.

You can find some more info here: https://web.archive.org/web/20190316214 ... =19&t=1847
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: STM32F103C8T6 DMA ADC not working

Post by ag123 »

if you are using stm32f103, and a timer, the max sample rates that i can get using the timer to drive the adc is about 500 khz (ksps)
https://github.com/ag88/GirinoSTM32F103duino
Post Reply

Return to “General discussion”