Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 8 months ago.
AnalogIn() for STM32F401RE is performing unexpectedly
I have the following program running to test the ADC on STM32F401RE. However, when I connect the A0 to 5V, it's showing 74% and if I leaving it off, it's showing 35%. When I connect it to GND, it's showing close to 0%. I'm not sure why 5V is mapped to only 74% of the analogy input. Does anyone know why?
#include "mbed.h"
// Initialize a pins to perform analog input and digital output fucntions
AnalogIn ain(A0);
DigitalOut dout(LED1);
int main(void)
{
while (1) {
// test the voltage on the initialized analog pin
// and if greater than 0.3 * VCC set the digital pin
// to a logic 1 otherwise a logic 0
if(ain > 0.3f) {
dout = 1;
} else {
dout = 0;
}
// print the percentage and 16 bit normalized values
printf("percentage: %3.3f%%\n\r", ain.read()*100.0f);
printf("normalized: 0x%04X \n\r", ain.read_u16());
wait(3);
}
}