![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TVZ test
main.cpp@0:5918d7a4ad04, 2014-01-07 (annotated)
- Committer:
- cvitas
- Date:
- Tue Jan 07 14:12:18 2014 +0000
- Revision:
- 0:5918d7a4ad04
- Child:
- 1:abeac322d8d8
prva runda
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 | 0:5918d7a4ad04 | 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 | 0:5918d7a4ad04 | 38 | while (async_port.writeable()==0); //is there a place to write? |
cvitas | 0:5918d7a4ad04 | 39 | async_port.putc(switch_word); //transmit switch_word |
cvitas | 0:5918d7a4ad04 | 40 | if (async_port.readable()==1){ //is there a character to be read? |
cvitas | 0:5918d7a4ad04 | 41 | Inbuff[1] = async_port.getc(); //if yes, t |
cvitas | 0:5918d7a4ad04 | 42 | } |
cvitas | 0:5918d7a4ad04 | 43 | if (async_port.readable()==1){ //is there a character to be read? |
cvitas | 0:5918d7a4ad04 | 44 | Inbuff[0] = async_port.getc(); //if yes, t |
cvitas | 0:5918d7a4ad04 | 45 | } |
cvitas | 0:5918d7a4ad04 | 46 | if (async_port.readable()==1){ //is there a character to be read? |
cvitas | 0:5918d7a4ad04 | 47 | recd_val=async_port.getc(); //if yes, t |
cvitas | 0:5918d7a4ad04 | 48 | RXDIN = 1; |
cvitas | 0:5918d7a4ad04 | 49 | } |
cvitas | 0:5918d7a4ad04 | 50 | wait(0.01); |
cvitas | 0:5918d7a4ad04 | 51 | //set leds according to incoming word from slave |
cvitas | 0:5918d7a4ad04 | 52 | red_led=0; //preset both to 0 |
cvitas | 0:5918d7a4ad04 | 53 | green_led=0; |
cvitas | 0:5918d7a4ad04 | 54 | recd_val=recd_val&0x03; //AND out unwanted bits |
cvitas | 0:5918d7a4ad04 | 55 | if (recd_val==1) |
cvitas | 0:5918d7a4ad04 | 56 | red_led=1; |
cvitas | 0:5918d7a4ad04 | 57 | if (recd_val==2) |
cvitas | 0:5918d7a4ad04 | 58 | green_led=1; |
cvitas | 0:5918d7a4ad04 | 59 | if (recd_val==3){ |
cvitas | 0:5918d7a4ad04 | 60 | red_led=1; |
cvitas | 0:5918d7a4ad04 | 61 | green_led=1; |
cvitas | 0:5918d7a4ad04 | 62 | } |
cvitas | 0:5918d7a4ad04 | 63 | wait(0.5); |
cvitas | 0:5918d7a4ad04 | 64 | RXDIN = 0; |
cvitas | 0:5918d7a4ad04 | 65 | } |
cvitas | 0:5918d7a4ad04 | 66 | } |