I have seen an earlier post about this, and tried reading voltage using several pins, but am unable to figure out the problem.
The board I am using is STM32F103C8T6 and the core is the STM32 official core. I want to measure an analog voltage with a 12 bit resolution. I am using the following code to write voltage to a breadboard and measuring the same voltage:
Code: Select all
void setup() {
Serial.begin(115200);
pinMode(PB0, OUTPUT);
pinMode(PA7,INPUT_ANALOG);
analogReadResolution(12);
analogWriteResolution(12);
}
void loop() {
analogWrite(PB0,2100); //for all values above 2100, the voltage is is 3.3, and for all values below this, voltage is 0
uint16_t val = analogRead(PA7);
Serial.print(val);
Serial.print(" ");
float voltage = 3.3 * (float(val)/4096);
Serial.println(voltage);
delay(1000);
}
Can anyone please tell me how to resolve this issue?