Junaid Ahmed
/
hw2
uart communication
main.cpp@0:46e2bf06ec86, 2014-03-07 (annotated)
- Committer:
- mja054
- Date:
- Fri Mar 07 23:14:41 2014 +0000
- Revision:
- 0:46e2bf06ec86
homework 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mja054 | 0:46e2bf06ec86 | 1 | #include "mbed.h" |
mja054 | 0:46e2bf06ec86 | 2 | #define BAUD_RATE 921600 |
mja054 | 0:46e2bf06ec86 | 3 | #define INITIATER 0 |
mja054 | 0:46e2bf06ec86 | 4 | |
mja054 | 0:46e2bf06ec86 | 5 | // http://mbed.org/platforms/FRDM-KL46Z/ |
mja054 | 0:46e2bf06ec86 | 6 | |
mja054 | 0:46e2bf06ec86 | 7 | DigitalOut myled(LED_GREEN); |
mja054 | 0:46e2bf06ec86 | 8 | Serial pc(USBTX, USBRX); |
mja054 | 0:46e2bf06ec86 | 9 | Serial uart(PTE0, PTE1); |
mja054 | 0:46e2bf06ec86 | 10 | |
mja054 | 0:46e2bf06ec86 | 11 | char num = 0; |
mja054 | 0:46e2bf06ec86 | 12 | |
mja054 | 0:46e2bf06ec86 | 13 | void receiver_send() |
mja054 | 0:46e2bf06ec86 | 14 | { |
mja054 | 0:46e2bf06ec86 | 15 | while(1) { |
mja054 | 0:46e2bf06ec86 | 16 | wait(1); |
mja054 | 0:46e2bf06ec86 | 17 | uart.putc(num); |
mja054 | 0:46e2bf06ec86 | 18 | num += 2; |
mja054 | 0:46e2bf06ec86 | 19 | myled = !myled; |
mja054 | 0:46e2bf06ec86 | 20 | } |
mja054 | 0:46e2bf06ec86 | 21 | } |
mja054 | 0:46e2bf06ec86 | 22 | |
mja054 | 0:46e2bf06ec86 | 23 | void send(char c) |
mja054 | 0:46e2bf06ec86 | 24 | { |
mja054 | 0:46e2bf06ec86 | 25 | uart.putc(c); |
mja054 | 0:46e2bf06ec86 | 26 | } |
mja054 | 0:46e2bf06ec86 | 27 | |
mja054 | 0:46e2bf06ec86 | 28 | void read() |
mja054 | 0:46e2bf06ec86 | 29 | { |
mja054 | 0:46e2bf06ec86 | 30 | myled = !myled; |
mja054 | 0:46e2bf06ec86 | 31 | char c = uart.getc(); |
mja054 | 0:46e2bf06ec86 | 32 | num += 2; |
mja054 | 0:46e2bf06ec86 | 33 | // send(num); |
mja054 | 0:46e2bf06ec86 | 34 | if (INITIATER) |
mja054 | 0:46e2bf06ec86 | 35 | pc.printf("%d\n", c); |
mja054 | 0:46e2bf06ec86 | 36 | } |
mja054 | 0:46e2bf06ec86 | 37 | |
mja054 | 0:46e2bf06ec86 | 38 | int main() { |
mja054 | 0:46e2bf06ec86 | 39 | pc.baud(BAUD_RATE); |
mja054 | 0:46e2bf06ec86 | 40 | uart.baud(BAUD_RATE); |
mja054 | 0:46e2bf06ec86 | 41 | |
mja054 | 0:46e2bf06ec86 | 42 | if (INITIATER) { |
mja054 | 0:46e2bf06ec86 | 43 | uart.attach(read); |
mja054 | 0:46e2bf06ec86 | 44 | num = 1; |
mja054 | 0:46e2bf06ec86 | 45 | uart.putc(num); |
mja054 | 0:46e2bf06ec86 | 46 | } else { |
mja054 | 0:46e2bf06ec86 | 47 | num = 0; |
mja054 | 0:46e2bf06ec86 | 48 | receiver_send(); |
mja054 | 0:46e2bf06ec86 | 49 | } |
mja054 | 0:46e2bf06ec86 | 50 | |
mja054 | 0:46e2bf06ec86 | 51 | pc.printf("Hello World\n"); |
mja054 | 0:46e2bf06ec86 | 52 | |
mja054 | 0:46e2bf06ec86 | 53 | while (true) { |
mja054 | 0:46e2bf06ec86 | 54 | wait(0.5); |
mja054 | 0:46e2bf06ec86 | 55 | } |
mja054 | 0:46e2bf06ec86 | 56 | } |