
A trivial example of a UART comm. Works on all Nucleos.
Dependencies: mbed
main.cpp@2:bdc32e32f6f1, 2015-03-05 (annotated)
- Committer:
- Foxnec
- Date:
- Thu Mar 05 22:09:54 2015 +0000
- Revision:
- 2:bdc32e32f6f1
- Parent:
- 1:a149bb0c3d05
- Child:
- 3:d470d455b369
Minor changes, but most importantly a versioning test :-)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Foxnec | 0:389db7a2ed7f | 1 | #include "mbed.h" |
Foxnec | 1:a149bb0c3d05 | 2 | #include <string> |
Foxnec | 0:389db7a2ed7f | 3 | |
Foxnec | 0:389db7a2ed7f | 4 | //------------------------------------ |
Foxnec | 0:389db7a2ed7f | 5 | // Hyperterminal configuration |
Foxnec | 1:a149bb0c3d05 | 6 | // 115200 bauds, 8-bit data, no parity |
Foxnec | 0:389db7a2ed7f | 7 | //------------------------------------ |
Foxnec | 2:bdc32e32f6f1 | 8 | void flushSerialPort(); |
Foxnec | 0:389db7a2ed7f | 9 | |
Foxnec | 0:389db7a2ed7f | 10 | Serial pc(SERIAL_TX, SERIAL_RX); |
Foxnec | 0:389db7a2ed7f | 11 | |
Foxnec | 0:389db7a2ed7f | 12 | DigitalOut myled(LED1); |
Foxnec | 0:389db7a2ed7f | 13 | |
Foxnec | 0:389db7a2ed7f | 14 | int main() |
Foxnec | 0:389db7a2ed7f | 15 | { |
Foxnec | 1:a149bb0c3d05 | 16 | int i; |
Foxnec | 2:bdc32e32f6f1 | 17 | char text[0xFF]; |
Foxnec | 1:a149bb0c3d05 | 18 | |
Foxnec | 1:a149bb0c3d05 | 19 | |
Foxnec | 0:389db7a2ed7f | 20 | pc.baud(115200); |
Foxnec | 1:a149bb0c3d05 | 21 | i=1; |
Foxnec | 2:bdc32e32f6f1 | 22 | pc.printf("Hello World !\n-----------------\nYou can tell me anything and I will repeat it...\n"); |
Foxnec | 1:a149bb0c3d05 | 23 | |
Foxnec | 0:389db7a2ed7f | 24 | while(1) { |
Foxnec | 0:389db7a2ed7f | 25 | |
Foxnec | 1:a149bb0c3d05 | 26 | wait(1); |
Foxnec | 2:bdc32e32f6f1 | 27 | |
Foxnec | 0:389db7a2ed7f | 28 | myled = !myled; |
Foxnec | 2:bdc32e32f6f1 | 29 | |
Foxnec | 1:a149bb0c3d05 | 30 | //NOTE: the data must be terminated with CR(Carriage return) |
Foxnec | 1:a149bb0c3d05 | 31 | //NOTE: I had no luck using scanf() - it just scans an empty string. |
Foxnec | 2:bdc32e32f6f1 | 32 | |
Foxnec | 1:a149bb0c3d05 | 33 | i=0; |
Foxnec | 2:bdc32e32f6f1 | 34 | while ((text[i-1] != '\r') && (i<0xFF)) |
Foxnec | 2:bdc32e32f6f1 | 35 | { |
Foxnec | 2:bdc32e32f6f1 | 36 | if (pc.readable()) |
Foxnec | 1:a149bb0c3d05 | 37 | text[i++] = getc(pc); |
Foxnec | 2:bdc32e32f6f1 | 38 | |
Foxnec | 1:a149bb0c3d05 | 39 | } |
Foxnec | 1:a149bb0c3d05 | 40 | |
Foxnec | 1:a149bb0c3d05 | 41 | pc.printf("Received data: %s", text); |
Foxnec | 1:a149bb0c3d05 | 42 | memset(&text, 0, i); |
Foxnec | 2:bdc32e32f6f1 | 43 | //flushSerialPort(pc); |
Foxnec | 1:a149bb0c3d05 | 44 | |
Foxnec | 0:389db7a2ed7f | 45 | } |
Foxnec | 0:389db7a2ed7f | 46 | } |
Foxnec | 0:389db7a2ed7f | 47 | |
Foxnec | 2:bdc32e32f6f1 | 48 | void flushSerialPort() |
Foxnec | 0:389db7a2ed7f | 49 | { |
Foxnec | 0:389db7a2ed7f | 50 | |
Foxnec | 2:bdc32e32f6f1 | 51 | while(pc.readable()) |
Foxnec | 2:bdc32e32f6f1 | 52 | pc.getc(); |
Foxnec | 2:bdc32e32f6f1 | 53 | |
Foxnec | 0:389db7a2ed7f | 54 | return; |
Foxnec | 0:389db7a2ed7f | 55 | } |