/*############################################################# Program Name : ex1_analog_single Author : Grant Phillips Date Modified : 13/01/2016 Compiler : ARMmbed Tested On : STM32F4-Discovery Description : Example that measures an analog voltage on PC1 and displays the result as a 16-bit value (0-16535), a percentage, and as a voltage (0-3.3V). The output is displayed on a PC using the Serial Wire Viewer (SWV). See the SWV examples for an explanation of the use of this feature. Requirements : * STM32F4-Discovery Board Circuit : * a potentiometer (POT) connected to PC1 * A wire link between PB3 and pin6 of the SWD connector (CN2) as explained in the SWV examples section ##############################################################*/ #include "mbed.h" #include "SWO.h" SWO_Channel SWO; AnalogIn ain(PC_1); int main() { uint16_t analogval; double analogvolt; while(1) { //read the 16-bit value from the analog pin analogval = ain.read_u16(); //the STM32F4-Trainer only supports 12-bit A/D (0 - 4095), but the read_u16 function converts it to a 16-bit value (0 - 65535) //calculate the voltage from the 16-bit value analogvolt = (double)analogval / 65535.0 * 3.3; //cast analogval to a double data type for formula to store result as double SWO.printf("Analog value: %5d (16-bit) %4.2f%% %.2f Volts\n", analogval, ain.read(), analogvolt); wait(0.01); } }