
A trivial example of a UART comm. Works on all Nucleos.
Dependencies: mbed
main.cpp@0:389db7a2ed7f, 2015-03-05 (annotated)
- Committer:
- Foxnec
- Date:
- Thu Mar 05 15:31:52 2015 +0000
- Revision:
- 0:389db7a2ed7f
- Child:
- 1:a149bb0c3d05
printf_scanf_flushinbuffer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Foxnec | 0:389db7a2ed7f | 1 | #include "mbed.h" |
Foxnec | 0:389db7a2ed7f | 2 | |
Foxnec | 0:389db7a2ed7f | 3 | //------------------------------------ |
Foxnec | 0:389db7a2ed7f | 4 | // Hyperterminal configuration |
Foxnec | 0:389db7a2ed7f | 5 | // 9600 bauds, 8-bit data, no parity |
Foxnec | 0:389db7a2ed7f | 6 | //------------------------------------ |
Foxnec | 0:389db7a2ed7f | 7 | void flushSerialPort(Serial* port); |
Foxnec | 0:389db7a2ed7f | 8 | |
Foxnec | 0:389db7a2ed7f | 9 | Serial pc(SERIAL_TX, SERIAL_RX); |
Foxnec | 0:389db7a2ed7f | 10 | |
Foxnec | 0:389db7a2ed7f | 11 | DigitalOut myled(LED1); |
Foxnec | 0:389db7a2ed7f | 12 | |
Foxnec | 0:389db7a2ed7f | 13 | int main() |
Foxnec | 0:389db7a2ed7f | 14 | { |
Foxnec | 0:389db7a2ed7f | 15 | |
Foxnec | 0:389db7a2ed7f | 16 | pc.baud(115200); |
Foxnec | 0:389db7a2ed7f | 17 | int i = 1; |
Foxnec | 0:389db7a2ed7f | 18 | int q; |
Foxnec | 0:389db7a2ed7f | 19 | pc.printf("Hello World !\n"); |
Foxnec | 0:389db7a2ed7f | 20 | while(1) { |
Foxnec | 0:389db7a2ed7f | 21 | |
Foxnec | 0:389db7a2ed7f | 22 | wait(5); |
Foxnec | 0:389db7a2ed7f | 23 | pc.printf("This program runs since %d seconds.\n", i++); |
Foxnec | 0:389db7a2ed7f | 24 | myled = !myled; |
Foxnec | 0:389db7a2ed7f | 25 | flushSerialPort(&pc); |
Foxnec | 0:389db7a2ed7f | 26 | |
Foxnec | 0:389db7a2ed7f | 27 | if (pc.scanf("%d", &q)) |
Foxnec | 0:389db7a2ed7f | 28 | pc.printf("%d\n", q); |
Foxnec | 0:389db7a2ed7f | 29 | } |
Foxnec | 0:389db7a2ed7f | 30 | } |
Foxnec | 0:389db7a2ed7f | 31 | |
Foxnec | 0:389db7a2ed7f | 32 | void flushSerialPort(Serial* port) |
Foxnec | 0:389db7a2ed7f | 33 | { |
Foxnec | 0:389db7a2ed7f | 34 | |
Foxnec | 0:389db7a2ed7f | 35 | while((*port).readable()) { |
Foxnec | 0:389db7a2ed7f | 36 | (*port).getc(); |
Foxnec | 0:389db7a2ed7f | 37 | } |
Foxnec | 0:389db7a2ed7f | 38 | |
Foxnec | 0:389db7a2ed7f | 39 | return; |
Foxnec | 0:389db7a2ed7f | 40 | } |