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);
}
}