Demo: ST Nucleo ADC with UART output. Works on all Nucleos.
Dependencies: mbed
Fork of Nucleo-ADC_with_UART_output by
main.cpp@1:59ea769dee88, 2015-03-19 (annotated)
- Committer:
- Foxnec
- Date:
- Thu Mar 19 17:45:37 2015 +0000
- Revision:
- 1:59ea769dee88
- Parent:
- 0:dade2710823b
Final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Foxnec | 1:59ea769dee88 | 1 | /********************************************************************************** |
Foxnec | 1:59ea769dee88 | 2 | * @file main.cpp |
Foxnec | 1:59ea769dee88 | 3 | * @author Daniel Toms |
Foxnec | 1:59ea769dee88 | 4 | * @version V0.1 |
Foxnec | 1:59ea769dee88 | 5 | * @date 09-March-2015 |
Foxnec | 1:59ea769dee88 | 6 | * @brief Simple ADC convertor example for Nucleo without DMA. |
Foxnec | 1:59ea769dee88 | 7 | * The value is written to the serial port every second at 115200baud. |
Foxnec | 1:59ea769dee88 | 8 | * Compatible with all the NUCLEOs. |
Foxnec | 1:59ea769dee88 | 9 | ***********************************************************************************/ |
Foxnec | 0:dade2710823b | 10 | |
Foxnec | 1:59ea769dee88 | 11 | /**********************************************************************************/ |
Foxnec | 1:59ea769dee88 | 12 | /* Table of A/D - D/A pins on Nucleo F303 (LQFP64) */ |
Foxnec | 1:59ea769dee88 | 13 | /**********************************************************************************/ |
Foxnec | 1:59ea769dee88 | 14 | /* LQFP64 pin | Nucleo pin | ST Pin | AD/DA Number | Channel */ |
Foxnec | 1:59ea769dee88 | 15 | /* 8 | A5 | PC_0 | ADC12 | 6 */ |
Foxnec | 1:59ea769dee88 | 16 | /* 9 | A4 | PC_1 | ADC12 | 7 */ |
Foxnec | 1:59ea769dee88 | 17 | /* 14 | A0 | PA_0 | ADC1 | 1 */ |
Foxnec | 1:59ea769dee88 | 18 | /* 15 | A1 | PA_1 | ADC1 | 2 */ |
Foxnec | 1:59ea769dee88 | 19 | /* 16 | D1 | PA_2 | ADC1 | 3 */ |
Foxnec | 1:59ea769dee88 | 20 | /* 17 | D0 | PA_3 | ADC1 | 4 */ |
Foxnec | 1:59ea769dee88 | 21 | /* 20 | A2 | PA_4 | ADC2 | 1 */ |
Foxnec | 1:59ea769dee88 | 22 | /* 21 | D13 | PA_5 | ADC2 | 2 */ |
Foxnec | 1:59ea769dee88 | 23 | /* 22 | D12 | PA_6 | ADC2 | 3 */ |
Foxnec | 1:59ea769dee88 | 24 | /* 23 | D11 | PA_7 | ADC2 | 4 */ |
Foxnec | 1:59ea769dee88 | 25 | /* 26 | A3 | PB_0 | ADC3 | 12 */ |
Foxnec | 1:59ea769dee88 | 26 | /**********************************************************************************/ |
Foxnec | 1:59ea769dee88 | 27 | /* 20 | A2 | PA_4 | DAC1 | 1 */ |
Foxnec | 1:59ea769dee88 | 28 | /* 21 | D13 | PA_5 | DAC1 | 2 */ |
Foxnec | 1:59ea769dee88 | 29 | /**********************************************************************************/ |
Foxnec | 0:dade2710823b | 30 | |
Foxnec | 1:59ea769dee88 | 31 | /* Includes ----------------------------------------------------------------------*/ |
Foxnec | 1:59ea769dee88 | 32 | #include "mbed.h" |
Foxnec | 1:59ea769dee88 | 33 | |
Foxnec | 1:59ea769dee88 | 34 | /* Defines -----------------------------------------------------------------------*/ |
Foxnec | 0:dade2710823b | 35 | // Calculate the corresponding acquisition measure for a given value in mV |
Foxnec | 0:dade2710823b | 36 | #define rawmeas(x) ((0xFFFF*x)/3300) |
Foxnec | 0:dade2710823b | 37 | // Calculate the corresponding mV value from measured voltage |
Foxnec | 0:dade2710823b | 38 | #define mv(x) ((x*3300)/0xFFFF) |
Foxnec | 1:59ea769dee88 | 39 | |
Foxnec | 1:59ea769dee88 | 40 | /* Function prototypes -----------------------------------------------------------*/ |
Foxnec | 0:dade2710823b | 41 | |
Foxnec | 1:59ea769dee88 | 42 | /* Variables ---------------------------------------------------------------------*/ |
Foxnec | 1:59ea769dee88 | 43 | |
Foxnec | 1:59ea769dee88 | 44 | //mbed - initialization of peripherals |
Foxnec | 1:59ea769dee88 | 45 | AnalogIn analog_value(A0); |
Foxnec | 1:59ea769dee88 | 46 | Serial pc(SERIAL_TX, SERIAL_RX); |
Foxnec | 1:59ea769dee88 | 47 | DigitalOut myled(LED1); |
Foxnec | 1:59ea769dee88 | 48 | |
Foxnec | 1:59ea769dee88 | 49 | /* Functions----------------------------------------------------------------------*/ |
Foxnec | 1:59ea769dee88 | 50 | |
Foxnec | 1:59ea769dee88 | 51 | |
Foxnec | 1:59ea769dee88 | 52 | /*********************************************************************************** |
Foxnec | 1:59ea769dee88 | 53 | * Function Name : main. |
Foxnec | 1:59ea769dee88 | 54 | * Description : Main routine. |
Foxnec | 1:59ea769dee88 | 55 | * Input : None. |
Foxnec | 1:59ea769dee88 | 56 | * Output : None. |
Foxnec | 1:59ea769dee88 | 57 | * Return : None. |
Foxnec | 1:59ea769dee88 | 58 | ***********************************************************************************/ |
Foxnec | 0:dade2710823b | 59 | int main() { |
Foxnec | 0:dade2710823b | 60 | |
Foxnec | 1:59ea769dee88 | 61 | pc.baud(115200); //Sets the serial port baud rate to 115200baud. |
Foxnec | 0:dade2710823b | 62 | |
Foxnec | 1:59ea769dee88 | 63 | while(1) { // infinite loop |
Foxnec | 1:59ea769dee88 | 64 | uint16_t meas = analog_value.read_u16(); // Converts and read the analog input value |
Foxnec | 1:59ea769dee88 | 65 | if (meas > rawmeas(1000)) { // If the value is greater than 1000 mV toggle the LED |
Foxnec | 1:59ea769dee88 | 66 | myled = !myled; // toggles the LED |
Foxnec | 0:dade2710823b | 67 | } |
Foxnec | 0:dade2710823b | 68 | |
Foxnec | 1:59ea769dee88 | 69 | pc.printf("Measured value: %d\n", meas); // Prints the measured value in millivolts |
Foxnec | 0:dade2710823b | 70 | pc.printf("%d mV\n", mv(meas)); |
Foxnec | 0:dade2710823b | 71 | |
Foxnec | 1:59ea769dee88 | 72 | wait(1); // Waits for one second between measures. |
Foxnec | 1:59ea769dee88 | 73 | // You can use wait_ms(x) and wait_us(x) to wait for shorter periods. |
Foxnec | 0:dade2710823b | 74 | |
Foxnec | 0:dade2710823b | 75 | } |
Foxnec | 0:dade2710823b | 76 | } |