![](/media/cache/profiles/8631e5b91354a846f00dbbf16ad94259.jpg.50x50_q85.jpg)
Test program for serial interface
This program turns the red LED on and then waits for a character to be entered at the terminal. The LED then goes off for 500ms before the cycle repeats.
The purpose of the program is to demonstrate the use of the serial link
main.cpp@4:a2d1329d042c, 2020-01-22 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Wed Jan 22 12:34:48 2020 +0000
- Revision:
- 4:a2d1329d042c
- Parent:
- 3:5b27b27e68ab
Echo twice to show clearly what's happening
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 2:e92d97ac59e4 | 1 | // Serial demo using mbed 5 |
WilliamMarshQMUL | 2:e92d97ac59e4 | 2 | |
WilliamMarshQMUL | 0:75835b5692b9 | 3 | #include "mbed.h" |
WilliamMarshQMUL | 0:75835b5692b9 | 4 | |
WilliamMarshQMUL | 0:75835b5692b9 | 5 | DigitalOut myled(LED1); |
WilliamMarshQMUL | 0:75835b5692b9 | 6 | Serial pc(USBTX, USBRX); // tx, rx |
WilliamMarshQMUL | 0:75835b5692b9 | 7 | |
WilliamMarshQMUL | 0:75835b5692b9 | 8 | int main() { |
WilliamMarshQMUL | 0:75835b5692b9 | 9 | char c ; |
WilliamMarshQMUL | 0:75835b5692b9 | 10 | pc.printf("Enter a character>"); |
WilliamMarshQMUL | 0:75835b5692b9 | 11 | while(1) { |
WilliamMarshQMUL | 0:75835b5692b9 | 12 | myled = 0; // turn on |
WilliamMarshQMUL | 0:75835b5692b9 | 13 | c = pc.getc(); |
WilliamMarshQMUL | 0:75835b5692b9 | 14 | myled = 1; // turn off |
WilliamMarshQMUL | 0:75835b5692b9 | 15 | pc.putc(c); |
WilliamMarshQMUL | 4:a2d1329d042c | 16 | pc.putc(c); // echo twice |
WilliamMarshQMUL | 1:961580c296da | 17 | pc.putc('\r'); |
WilliamMarshQMUL | 0:75835b5692b9 | 18 | pc.putc('\n'); |
WilliamMarshQMUL | 3:5b27b27e68ab | 19 | wait(0.5); |
WilliamMarshQMUL | 0:75835b5692b9 | 20 | pc.printf("Enter a character>"); |
WilliamMarshQMUL | 0:75835b5692b9 | 21 | } |
WilliamMarshQMUL | 0:75835b5692b9 | 22 | } |