Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SerialMBEDtoMSP430 by
main.cpp@1:abeac322d8d8, 2014-01-07 (annotated)
- Committer:
- cvitas
- Date:
- Tue Jan 07 16:44:56 2014 +0000
- Revision:
- 1:abeac322d8d8
- Parent:
- 0:5918d7a4ad04
- Child:
- 2:2e261aa02cd0
runda 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cvitas | 0:5918d7a4ad04 | 1 | #include "mbed.h" |
cvitas | 0:5918d7a4ad04 | 2 | |
cvitas | 0:5918d7a4ad04 | 3 | Serial async_port(p9, p10); //set up TX and RX on pins 9 and 10 |
cvitas | 0:5918d7a4ad04 | 4 | DigitalOut red_led(LED4); //red led |
cvitas | 0:5918d7a4ad04 | 5 | DigitalOut green_led(LED3); //green led |
cvitas | 0:5918d7a4ad04 | 6 | DigitalOut RXDIN (LED1); |
cvitas | 0:5918d7a4ad04 | 7 | DigitalOut strobe(p7); //a strobe to trigger the scope |
cvitas | 0:5918d7a4ad04 | 8 | DigitalIn switch_ip1(p5); |
cvitas | 0:5918d7a4ad04 | 9 | DigitalIn switch_ip2(p6); |
cvitas | 0:5918d7a4ad04 | 10 | |
cvitas | 0:5918d7a4ad04 | 11 | char switch_word ; //the word we will send |
cvitas | 0:5918d7a4ad04 | 12 | char recd_val; //the received value |
cvitas | 0:5918d7a4ad04 | 13 | int valueADC; |
cvitas | 0:5918d7a4ad04 | 14 | char Outbuff[2]; |
cvitas | 0:5918d7a4ad04 | 15 | char Inbuff[2]; |
cvitas | 0:5918d7a4ad04 | 16 | |
cvitas | 0:5918d7a4ad04 | 17 | int main() { |
cvitas | 0:5918d7a4ad04 | 18 | async_port.baud(9600); //set baud rate to 9600 (ie default) |
cvitas | 0:5918d7a4ad04 | 19 | //accept default format, of 8 bits, no parity |
cvitas | 0:5918d7a4ad04 | 20 | while (1){ |
cvitas | 0:5918d7a4ad04 | 21 | //Set up the word to be sent, by testing switch inputs |
cvitas | 0:5918d7a4ad04 | 22 | switch_word=0xa0; //set up a recognisable output pattern |
cvitas | 0:5918d7a4ad04 | 23 | if (switch_ip1==1) |
cvitas | 0:5918d7a4ad04 | 24 | switch_word=switch_word|0x01; //OR in lsb |
cvitas | 0:5918d7a4ad04 | 25 | if (switch_ip2==1) |
cvitas | 0:5918d7a4ad04 | 26 | switch_word=switch_word|0x02; //OR in next lsb |
cvitas | 0:5918d7a4ad04 | 27 | strobe =1; //short strobe pulse |
cvitas | 0:5918d7a4ad04 | 28 | wait_us(10); |
cvitas | 0:5918d7a4ad04 | 29 | strobe=0; |
cvitas | 0:5918d7a4ad04 | 30 | valueADC = 0x0302; |
cvitas | 0:5918d7a4ad04 | 31 | Outbuff[0] = valueADC; |
cvitas | 0:5918d7a4ad04 | 32 | Outbuff[1] = valueADC >> 8; |
cvitas | 1:abeac322d8d8 | 33 | /* |
cvitas | 0:5918d7a4ad04 | 34 | while (async_port.writeable()==0); //is there a place to write? |
cvitas | 0:5918d7a4ad04 | 35 | async_port.putc(Outbuff[1]); //transmit switch_word |
cvitas | 0:5918d7a4ad04 | 36 | while (async_port.writeable()==0); //is there a place to write? |
cvitas | 0:5918d7a4ad04 | 37 | async_port.putc(Outbuff[0]); //transmit switch_word |
cvitas | 1:abeac322d8d8 | 38 | */ |
cvitas | 0:5918d7a4ad04 | 39 | while (async_port.writeable()==0); //is there a place to write? |
cvitas | 0:5918d7a4ad04 | 40 | async_port.putc(switch_word); //transmit switch_word |
cvitas | 0:5918d7a4ad04 | 41 | if (async_port.readable()==1){ //is there a character to be read? |
cvitas | 0:5918d7a4ad04 | 42 | Inbuff[1] = async_port.getc(); //if yes, t |
cvitas | 0:5918d7a4ad04 | 43 | } |
cvitas | 0:5918d7a4ad04 | 44 | if (async_port.readable()==1){ //is there a character to be read? |
cvitas | 0:5918d7a4ad04 | 45 | Inbuff[0] = async_port.getc(); //if yes, t |
cvitas | 1:abeac322d8d8 | 46 | RXDIN = 1; |
cvitas | 0:5918d7a4ad04 | 47 | } |
cvitas | 1:abeac322d8d8 | 48 | /*if (async_port.readable()==1){ //is there a character to be read? |
cvitas | 0:5918d7a4ad04 | 49 | recd_val=async_port.getc(); //if yes, t |
cvitas | 0:5918d7a4ad04 | 50 | RXDIN = 1; |
cvitas | 1:abeac322d8d8 | 51 | }*/ |
cvitas | 0:5918d7a4ad04 | 52 | wait(0.01); |
cvitas | 0:5918d7a4ad04 | 53 | //set leds according to incoming word from slave |
cvitas | 0:5918d7a4ad04 | 54 | red_led=0; //preset both to 0 |
cvitas | 0:5918d7a4ad04 | 55 | green_led=0; |
cvitas | 0:5918d7a4ad04 | 56 | recd_val=recd_val&0x03; //AND out unwanted bits |
cvitas | 0:5918d7a4ad04 | 57 | if (recd_val==1) |
cvitas | 0:5918d7a4ad04 | 58 | red_led=1; |
cvitas | 0:5918d7a4ad04 | 59 | if (recd_val==2) |
cvitas | 0:5918d7a4ad04 | 60 | green_led=1; |
cvitas | 0:5918d7a4ad04 | 61 | if (recd_val==3){ |
cvitas | 0:5918d7a4ad04 | 62 | red_led=1; |
cvitas | 0:5918d7a4ad04 | 63 | green_led=1; |
cvitas | 0:5918d7a4ad04 | 64 | } |
cvitas | 0:5918d7a4ad04 | 65 | wait(0.5); |
cvitas | 0:5918d7a4ad04 | 66 | RXDIN = 0; |
cvitas | 0:5918d7a4ad04 | 67 | } |
cvitas | 0:5918d7a4ad04 | 68 | } |