Display a message on PC using UART.

Dependencies:   mbed-src mbed

Committer:
anthonymcatear
Date:
Thu Mar 12 15:06:29 2015 +0000
Revision:
2:4bc4ca1d146c
Parent:
1:e9d1c42a73ae
voor nathan

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:028fac66239d 1 #include "mbed.h"
bcostm 1:e9d1c42a73ae 2
anthonymcatear 2:4bc4ca1d146c 3 Serial device(SERIAL_TX, SERIAL_RX);
anthonymcatear 2:4bc4ca1d146c 4
anthonymcatear 2:4bc4ca1d146c 5 DigitalOut out1(D2);
anthonymcatear 2:4bc4ca1d146c 6 DigitalOut out2(D3);
anthonymcatear 2:4bc4ca1d146c 7 DigitalOut out3(D4);
anthonymcatear 2:4bc4ca1d146c 8 DigitalOut out4(D7);
anthonymcatear 2:4bc4ca1d146c 9 DigitalOut out5(D8);
anthonymcatear 2:4bc4ca1d146c 10
anthonymcatear 2:4bc4ca1d146c 11
anthonymcatear 2:4bc4ca1d146c 12 void Rx_interrupt();
anthonymcatear 2:4bc4ca1d146c 13 void SetOutput();
anthonymcatear 2:4bc4ca1d146c 14 void SetTime();
anthonymcatear 2:4bc4ca1d146c 15
anthonymcatear 2:4bc4ca1d146c 16 char c = 0;
anthonymcatear 2:4bc4ca1d146c 17 char time_enable = 0;
anthonymcatear 2:4bc4ca1d146c 18 int delay_ms = 75;
anthonymcatear 2:4bc4ca1d146c 19
anthonymcatear 2:4bc4ca1d146c 20 int main()
anthonymcatear 2:4bc4ca1d146c 21 {
anthonymcatear 2:4bc4ca1d146c 22 device.attach(&Rx_interrupt, Serial::RxIrq);
anthonymcatear 2:4bc4ca1d146c 23 while(1) {
anthonymcatear 2:4bc4ca1d146c 24 if(time_enable == 1 && c != 255) {
anthonymcatear 2:4bc4ca1d146c 25 SetTime();
anthonymcatear 2:4bc4ca1d146c 26 } else if(c != 0) {
anthonymcatear 2:4bc4ca1d146c 27 SetOutput();
anthonymcatear 2:4bc4ca1d146c 28 }
anthonymcatear 2:4bc4ca1d146c 29 }
anthonymcatear 2:4bc4ca1d146c 30 }
anthonymcatear 2:4bc4ca1d146c 31
anthonymcatear 2:4bc4ca1d146c 32 void Rx_interrupt()
anthonymcatear 2:4bc4ca1d146c 33 {
anthonymcatear 2:4bc4ca1d146c 34 c = device.getc();
anthonymcatear 2:4bc4ca1d146c 35 if (c == 255)
anthonymcatear 2:4bc4ca1d146c 36 {
anthonymcatear 2:4bc4ca1d146c 37 time_enable = 1;
anthonymcatear 2:4bc4ca1d146c 38 }
anthonymcatear 2:4bc4ca1d146c 39 //device.putc('x'); // Ter controle
anthonymcatear 2:4bc4ca1d146c 40 }
bcostm 1:e9d1c42a73ae 41
anthonymcatear 2:4bc4ca1d146c 42 void SetOutput()
anthonymcatear 2:4bc4ca1d146c 43 {
anthonymcatear 2:4bc4ca1d146c 44 __disable_irq(); // disables interrupts
anthonymcatear 2:4bc4ca1d146c 45 if(c > 47 && c < 54) {
anthonymcatear 2:4bc4ca1d146c 46 if(c == 49) {
anthonymcatear 2:4bc4ca1d146c 47 out1 = 1;
anthonymcatear 2:4bc4ca1d146c 48 out2 = 0;
anthonymcatear 2:4bc4ca1d146c 49 out3 = 0;
anthonymcatear 2:4bc4ca1d146c 50 out4 = 0;
anthonymcatear 2:4bc4ca1d146c 51 out5 = 0;
anthonymcatear 2:4bc4ca1d146c 52 //device.putc('1');
anthonymcatear 2:4bc4ca1d146c 53 } else if(c == 50) {
anthonymcatear 2:4bc4ca1d146c 54 out1 = 0;
anthonymcatear 2:4bc4ca1d146c 55 out2 = 1;
anthonymcatear 2:4bc4ca1d146c 56 out3 = 0;
anthonymcatear 2:4bc4ca1d146c 57 out4 = 0;
anthonymcatear 2:4bc4ca1d146c 58 out5 = 0;
anthonymcatear 2:4bc4ca1d146c 59 //device.putc('2');
anthonymcatear 2:4bc4ca1d146c 60 } else if(c == 51) {
anthonymcatear 2:4bc4ca1d146c 61 out1 = 0;
anthonymcatear 2:4bc4ca1d146c 62 out2 = 0;
anthonymcatear 2:4bc4ca1d146c 63 out3 = 1;
anthonymcatear 2:4bc4ca1d146c 64 out4 = 0;
anthonymcatear 2:4bc4ca1d146c 65 out5 = 0;
anthonymcatear 2:4bc4ca1d146c 66 //device.putc('3');
anthonymcatear 2:4bc4ca1d146c 67 } else if(c == 52) {
anthonymcatear 2:4bc4ca1d146c 68 out1 = 0;
anthonymcatear 2:4bc4ca1d146c 69 out2 = 0;
anthonymcatear 2:4bc4ca1d146c 70 out3 = 0;
anthonymcatear 2:4bc4ca1d146c 71 out4 = 1;
anthonymcatear 2:4bc4ca1d146c 72 out5 = 0;
anthonymcatear 2:4bc4ca1d146c 73 //device.putc('4');
anthonymcatear 2:4bc4ca1d146c 74 } else if(c == 53) {
anthonymcatear 2:4bc4ca1d146c 75 out1 = 0;
anthonymcatear 2:4bc4ca1d146c 76 out2 = 0;
anthonymcatear 2:4bc4ca1d146c 77 out3 = 0;
anthonymcatear 2:4bc4ca1d146c 78 out4 = 0;
anthonymcatear 2:4bc4ca1d146c 79 out5 = 1;
anthonymcatear 2:4bc4ca1d146c 80 //device.putc('5');
anthonymcatear 2:4bc4ca1d146c 81 }
anthonymcatear 2:4bc4ca1d146c 82 } else {
anthonymcatear 2:4bc4ca1d146c 83 if((c & 1) == 1) { //Selecteert bit0 en kijkt als die 1 is
anthonymcatear 2:4bc4ca1d146c 84 out1 = 1;
anthonymcatear 2:4bc4ca1d146c 85 } else {
anthonymcatear 2:4bc4ca1d146c 86 out1 = 0;
anthonymcatear 2:4bc4ca1d146c 87 }
anthonymcatear 2:4bc4ca1d146c 88 if((c & 2) == 2) { //Selecteert bit1 en kijkt als die 1 is
anthonymcatear 2:4bc4ca1d146c 89 out2 = 1;
anthonymcatear 2:4bc4ca1d146c 90 } else {
anthonymcatear 2:4bc4ca1d146c 91 out2 = 0;
anthonymcatear 2:4bc4ca1d146c 92 }
anthonymcatear 2:4bc4ca1d146c 93 if((c & 4) == 4) { //Selecteert bit2 en kijkt als die 1 is
anthonymcatear 2:4bc4ca1d146c 94 out3 = 1;
anthonymcatear 2:4bc4ca1d146c 95 } else {
anthonymcatear 2:4bc4ca1d146c 96 out3 = 0;
anthonymcatear 2:4bc4ca1d146c 97 }
anthonymcatear 2:4bc4ca1d146c 98 if((c & 8) == 8) { //Selecteert bit3 en kijkt als die 1 is
anthonymcatear 2:4bc4ca1d146c 99 out4 = 1;
anthonymcatear 2:4bc4ca1d146c 100 } else {
anthonymcatear 2:4bc4ca1d146c 101 out4 = 0;
anthonymcatear 2:4bc4ca1d146c 102 }
anthonymcatear 2:4bc4ca1d146c 103 if((c & 16) == 16) { //Selecteert bit4 en kijkt als die 1 is
anthonymcatear 2:4bc4ca1d146c 104 out5 = 1;
anthonymcatear 2:4bc4ca1d146c 105 } else {
anthonymcatear 2:4bc4ca1d146c 106 out5 = 0;
anthonymcatear 2:4bc4ca1d146c 107 }
anthonymcatear 2:4bc4ca1d146c 108 }
anthonymcatear 2:4bc4ca1d146c 109 c = '0';
anthonymcatear 2:4bc4ca1d146c 110 __enable_irq(); //enables interrupts
anthonymcatear 2:4bc4ca1d146c 111 device.putc('r'); //sends a byte for confirmation
anthonymcatear 2:4bc4ca1d146c 112 wait_ms(delay_ms);
anthonymcatear 2:4bc4ca1d146c 113
anthonymcatear 2:4bc4ca1d146c 114 out1 = 0;
anthonymcatear 2:4bc4ca1d146c 115 out2 = 0;
anthonymcatear 2:4bc4ca1d146c 116 out3 = 0;
anthonymcatear 2:4bc4ca1d146c 117 out4 = 0;
anthonymcatear 2:4bc4ca1d146c 118 out5 = 0;
bcostm 0:028fac66239d 119 }
anthonymcatear 2:4bc4ca1d146c 120
anthonymcatear 2:4bc4ca1d146c 121 void SetTime() {
anthonymcatear 2:4bc4ca1d146c 122 delay_ms = (int)c;
anthonymcatear 2:4bc4ca1d146c 123 time_enable = '0';
anthonymcatear 2:4bc4ca1d146c 124 }