How read channels 1-8 on ADC1
Posted: Mon Jun 21, 2021 9:20 pm
I am using a nucleo board with a ST32 103RB micro and the Arduino IDE to upload my sketch.
I have 8 ADC lines I want to read from PA0 - PA7. When I read from PA2 it will show the same reading as PA1 and when I read from PA3 it will show the same ADC reading as PA0. I am not sure why that is or I don't understand the pin mapping correlation between Arduino and ST to make this work. Any help as to what is wrong and/or guidance?
I have 8 ADC lines I want to read from PA0 - PA7. When I read from PA2 it will show the same reading as PA1 and when I read from PA3 it will show the same ADC reading as PA0. I am not sure why that is or I don't understand the pin mapping correlation between Arduino and ST to make this work. Any help as to what is wrong and/or guidance?
Code: Select all
#include <stm32yyxx_ll_adc.h>
#include <stdio.h>
#include <math.h>
const int totalSensingLines = 8;
const int currentLines[totalSensingLines] = { PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7 };
void setup()
{
analogReadResolution(12); // set resolution to 12-bit
for (int i = 0; i < totalSensingLines; i++) {
pinMode(currentLines[i], INPUT);
}
}
void loop()
{
read_lines(currentReadings, totalSensingLines);
}
void read_lines(double *buf, int buffSize) {
double samples[buffSize] = {0.0};
double vout[buffSize] = {0.0};
int totalSamples = 10;
double thisPeak = 0.0;
// Take Samples
for (int i = 0; i < totalSamples; i++) {
for (int k = 0; k < buffSize; k++) {
thisPeak = analogRead(currentLines[k]);
if (thisPeak > samples[k]) {
samples[k] = thisPeak;
}
}
}
// Calculate Reading
for (int i = 0; i < buffSize; i++) {
vout[i] = samples[i] * (3.3 / 4096.0);
buf[i] = (abs((1.65 - vout[i]) / 0.044)) / sqrt(2);
}
}