![](/media/cache/profiles/283c53a1a1af3cdff3387c5be4135f6f.jpg.50x50_q85.jpg)
asdf
main.cpp@2:7c83035c340e, 2015-06-10 (annotated)
- Committer:
- andreashatzl
- Date:
- Wed Jun 10 16:32:24 2015 +0000
- Revision:
- 2:7c83035c340e
- Parent:
- 1:756c9df9881f
abissiwas
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andreashatzl | 0:24f914e903b4 | 1 | /* |
andreashatzl | 0:24f914e903b4 | 2 | Serial Communication with a PC |
andreashatzl | 0:24f914e903b4 | 3 | The mbed Microcontroller can communicate with a host PC through a "USB Virtual Serial Port" |
andreashatzl | 0:24f914e903b4 | 4 | over the same USB cable that is used for programming. |
andreashatzl | 0:24f914e903b4 | 5 | This enables you to: |
andreashatzl | 0:24f914e903b4 | 6 | a) Print out messages to a host PC terminal (useful for debugging!) |
andreashatzl | 0:24f914e903b4 | 7 | b) Read input from the host PC keyboard |
andreashatzl | 0:24f914e903b4 | 8 | c) Communicate with applications and programming languages running on the host PC |
andreashatzl | 0:24f914e903b4 | 9 | that can communicate with a serial port, e.g. perl, python, java, c# and so on. |
andreashatzl | 0:24f914e903b4 | 10 | .. see http://mbed.org/handbook/SerialPC |
andreashatzl | 0:24f914e903b4 | 11 | |
andreashatzl | 0:24f914e903b4 | 12 | The C stdin, stdout and stderr file handles are also defaulted to the PC serial connection |
andreashatzl | 0:24f914e903b4 | 13 | standard calls are: printf, scanf, getc, putc, .. |
andreashatzl | 0:24f914e903b4 | 14 | */ |
andreashatzl | 0:24f914e903b4 | 15 | |
andreashatzl | 0:24f914e903b4 | 16 | #include "mbed.h" |
andreashatzl | 0:24f914e903b4 | 17 | // global vars and objects |
andreashatzl | 0:24f914e903b4 | 18 | BusOut doLeds(LED1,LED2,LED3,LED4); |
andreashatzl | 2:7c83035c340e | 19 | DigitalOut led1(LED1); |
andreashatzl | 0:24f914e903b4 | 20 | DigitalOut led2(LED2); |
andreashatzl | 2:7c83035c340e | 21 | DigitalOut led3(LED3); |
andreashatzl | 2:7c83035c340e | 22 | DigitalOut led4(LED4); |
andreashatzl | 2:7c83035c340e | 23 | InterruptIn diUp(p15); |
andreashatzl | 2:7c83035c340e | 24 | Timer timer1; |
andreashatzl | 0:24f914e903b4 | 25 | Serial pc(USBTX, USBRX); // tx, rx ; is default !!! (9600, 8N1) |
andreashatzl | 0:24f914e903b4 | 26 | char recChar=0; |
andreashatzl | 0:24f914e903b4 | 27 | bool recFlag=false; |
andreashatzl | 0:24f914e903b4 | 28 | char recArr[20]; |
andreashatzl | 0:24f914e903b4 | 29 | int index=0; |
andreashatzl | 0:24f914e903b4 | 30 | |
andreashatzl | 0:24f914e903b4 | 31 | // functions |
andreashatzl | 0:24f914e903b4 | 32 | void flushSerialBuffer() { |
andreashatzl | 0:24f914e903b4 | 33 | while (pc.readable()) { |
andreashatzl | 0:24f914e903b4 | 34 | pc.getc(); |
andreashatzl | 0:24f914e903b4 | 35 | } |
andreashatzl | 0:24f914e903b4 | 36 | } |
andreashatzl | 0:24f914e903b4 | 37 | |
andreashatzl | 0:24f914e903b4 | 38 | void readData() { |
andreashatzl | 0:24f914e903b4 | 39 | recChar = pc.getc(); |
andreashatzl | 0:24f914e903b4 | 40 | recArr[index] = recChar; |
andreashatzl | 0:24f914e903b4 | 41 | index++; |
andreashatzl | 0:24f914e903b4 | 42 | |
andreashatzl | 0:24f914e903b4 | 43 | recFlag = true; |
andreashatzl | 0:24f914e903b4 | 44 | recArr[index] = 0; |
andreashatzl | 0:24f914e903b4 | 45 | index = 0; |
andreashatzl | 0:24f914e903b4 | 46 | pc.printf(" - That's the input: %s\r\n", recArr); |
andreashatzl | 0:24f914e903b4 | 47 | |
andreashatzl | 0:24f914e903b4 | 48 | if(recArr[0]=='a') |
andreashatzl | 0:24f914e903b4 | 49 | { |
andreashatzl | 0:24f914e903b4 | 50 | doLeds = 0; |
andreashatzl | 2:7c83035c340e | 51 | } |
andreashatzl | 2:7c83035c340e | 52 | else if(recArr[0]=='e') |
andreashatzl | 2:7c83035c340e | 53 | { |
andreashatzl | 2:7c83035c340e | 54 | doLeds = 15; |
andreashatzl | 2:7c83035c340e | 55 | } |
andreashatzl | 2:7c83035c340e | 56 | else if(recArr[0]=='A') |
andreashatzl | 2:7c83035c340e | 57 | { |
andreashatzl | 2:7c83035c340e | 58 | doLeds = 0; |
andreashatzl | 2:7c83035c340e | 59 | } |
andreashatzl | 2:7c83035c340e | 60 | else if(recArr[0]=='E') |
andreashatzl | 1:756c9df9881f | 61 | { |
andreashatzl | 1:756c9df9881f | 62 | doLeds = 15; |
andreashatzl | 2:7c83035c340e | 63 | } |
andreashatzl | 2:7c83035c340e | 64 | else if(recArr[0]=='t') |
andreashatzl | 0:24f914e903b4 | 65 | { |
andreashatzl | 2:7c83035c340e | 66 | doLeds = !doLeds; |
andreashatzl | 2:7c83035c340e | 67 | } |
andreashatzl | 2:7c83035c340e | 68 | |
andreashatzl | 2:7c83035c340e | 69 | |
andreashatzl | 2:7c83035c340e | 70 | else |
andreashatzl | 2:7c83035c340e | 71 | { |
andreashatzl | 2:7c83035c340e | 72 | pc.printf("Eingabe inkorrekt!"); |
andreashatzl | 2:7c83035c340e | 73 | flushSerialBuffer(); |
andreashatzl | 2:7c83035c340e | 74 | while(1) |
andreashatzl | 2:7c83035c340e | 75 | { timer1.start(); |
andreashatzl | 2:7c83035c340e | 76 | if(timer1.read_ms()>=100) |
andreashatzl | 2:7c83035c340e | 77 | {doLeds = !doLeds; |
andreashatzl | 2:7c83035c340e | 78 | timer1.reset();} |
andreashatzl | 2:7c83035c340e | 79 | |
andreashatzl | 2:7c83035c340e | 80 | |
andreashatzl | 2:7c83035c340e | 81 | } |
andreashatzl | 2:7c83035c340e | 82 | } |
andreashatzl | 2:7c83035c340e | 83 | |
andreashatzl | 2:7c83035c340e | 84 | |
andreashatzl | 2:7c83035c340e | 85 | |
andreashatzl | 0:24f914e903b4 | 86 | |
andreashatzl | 0:24f914e903b4 | 87 | flushSerialBuffer(); |
andreashatzl | 0:24f914e903b4 | 88 | |
andreashatzl | 0:24f914e903b4 | 89 | |
andreashatzl | 0:24f914e903b4 | 90 | } |
andreashatzl | 2:7c83035c340e | 91 | |
andreashatzl | 2:7c83035c340e | 92 | void Up() |
andreashatzl | 2:7c83035c340e | 93 | { |
andreashatzl | 2:7c83035c340e | 94 | doLeds = 9; |
andreashatzl | 2:7c83035c340e | 95 | return; |
andreashatzl | 2:7c83035c340e | 96 | } |
andreashatzl | 0:24f914e903b4 | 97 | |
andreashatzl | 0:24f914e903b4 | 98 | int main() { |
andreashatzl | 2:7c83035c340e | 99 | diUp.rise(&Up); |
andreashatzl | 0:24f914e903b4 | 100 | // pc.baud(115200); |
andreashatzl | 0:24f914e903b4 | 101 | pc.baud(38400); |
andreashatzl | 0:24f914e903b4 | 102 | pc.format(8, SerialBase::Odd, 2); |
andreashatzl | 2:7c83035c340e | 103 | doLeds = 0; |
andreashatzl | 2:7c83035c340e | 104 | |
andreashatzl | 0:24f914e903b4 | 105 | |
andreashatzl | 0:24f914e903b4 | 106 | flushSerialBuffer(); |
andreashatzl | 0:24f914e903b4 | 107 | pc.printf("Hatzl Andreas: Gruppe A\r\n Bitte geben Sie einen Befehl ein:\r\n"); |
andreashatzl | 0:24f914e903b4 | 108 | pc.attach(&readData); |
andreashatzl | 0:24f914e903b4 | 109 | |
andreashatzl | 0:24f914e903b4 | 110 | while(1) { |
andreashatzl | 0:24f914e903b4 | 111 | if (recFlag) { |
andreashatzl | 0:24f914e903b4 | 112 | flushSerialBuffer(); |
andreashatzl | 0:24f914e903b4 | 113 | // pc.printf(" - That's the input: %s\r\n", recArr); // non reantrant function |
andreashatzl | 0:24f914e903b4 | 114 | recFlag = false; |
andreashatzl | 1:756c9df9881f | 115 | |
andreashatzl | 0:24f914e903b4 | 116 | } |
andreashatzl | 0:24f914e903b4 | 117 | } |
andreashatzl | 0:24f914e903b4 | 118 | } |