help with analog converter on stm32 nucleo

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
dp.alex
Posts: 1
Joined: Thu Jun 25, 2015 11:43 pm

help with analog converter on stm32 nucleo

Post by dp.alex »

Hi,
I’m trying to read from adc converter of nucleo board stm32f411re a signal from a buzzer piezoelettric (the potential difference that they generate is almost 30 volts but the nucleo board works at 3.3 volts, so i used a diode to stabilize the voltage to 2.4 volts) but I’m not sure if what I read is correct. I will use six channels but for a first test i’m trying with a single channel.
Printing the values I see that if, I don’t hit the sensor, samples vector value is always 700 more or less(I think this value is my 0), because if I hit the sensor on the back samples value is almost 0(maybe negative voltage?). If I hit the front value rises at 2000(mybe the positive voltage?).
Sorry for my english and I thank you all for the answers.

This is my code:

Code: Select all

 #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "ch.h"
    #include "hal.h"
    #include "chprintf.h"
 
    /* ADC conversion group settings */
    #define ADC_GRP_NUM_CHANNELS   1
    #define ADC_GRP_BUF_DEPTH      8
 
    /* Buffer for ADC samples */
    static adcsample_t samples[ADC_GRP_NUM_CHANNELS * ADC_GRP_BUF_DEPTH];
    float volt=0;
    int i;
    /*
    * ADC conversion group.
    * Mode:        Circular buffer, 1 samples of 6 channels, SW triggered.
    * Channels:    IN10, IN11, IN14, IN15, IN8, IN9
    */
    static const ADCConversionGroup adcgrpcfg = {
      TRUE,
      ADC_GRP_NUM_CHANNELS,
      NULL,
      NULL,
      0,                        /* CR1 */
      ADC_CR2_SWSTART,          /* CR2 */
      0,
      ADC_SMPR2_SMP_AN8(ADC_SAMPLE_56),
      ADC_SQR1_NUM_CH(ADC_GRP_NUM_CHANNELS),
      0,                        /* SQR2 */
      ADC_SQR3_SQ1_N(ADC_CHANNEL_IN8)
    };
 
    /*===========================================================================*/
    /* Initialization and main thread.                                           */
    /*===========================================================================*/
 
    /*
    * Application entry point.
    */
    int main(void) {
      halInit();
      chSysInit();
      sdStart(&SD2, NULL);
 
      /* Setup pins for ADC */
      palSetGroupMode(GPIOB, PAL_PORT_BIT(0) | 0,
                     0, PAL_MODE_INPUT_ANALOG);
 
 
      adcStart(&ADCD1, NULL);
      adcStartConversion(&ADCD1, &adcgrpcfg, samples, ADC_GRP_BUF_DEPTH);
 
      while (TRUE) {
        for(i=0;i<8;i++){
        volt= (samples[i]/4096)*3.3;
 
        chprintf((BaseSequentialStream *)&SD2, "Sample: %d \t",samples[i]);
        chprintf((BaseSequentialStream *)&SD2, "Volt: %d \t",volt);
        volt=0;
        }
         chThdSleepMilliseconds(100);
      }
    }
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: help with analog converter on stm32 nucleo

Post by Giovanni »

Hi,

The code looks correct.

I am not sure to have understood correctly but if you use a diode to "stabilize" the voltage, what you measure is the diode barrier voltage. Post your input circuitry.

Just a note, if you convert a single channel in circular mode you are hammering the CPU with lots of interrupts because the ADC runs very fast. You may have to use single shot conversions or trigger the ADC using a timer to work at a lower rate.

Giovanni
Post Reply