Salve 12c
Dependencies: KL25Z_I2C_busreset mbed
main.cpp@0:72b6af395c85, 2014-09-23 (annotated)
- Committer:
- Sarahi
- Date:
- Tue Sep 23 20:49:02 2014 +0000
- Revision:
- 0:72b6af395c85
Slave I2c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sarahi | 0:72b6af395c85 | 1 | #include "mbed.h" |
Sarahi | 0:72b6af395c85 | 2 | |
Sarahi | 0:72b6af395c85 | 3 | |
Sarahi | 0:72b6af395c85 | 4 | I2CSlave slave(A5,A4); //Configure I2C slave |
Sarahi | 0:72b6af395c85 | 5 | |
Sarahi | 0:72b6af395c85 | 6 | DigitalOut red_led(LED_RED); //red led |
Sarahi | 0:72b6af395c85 | 7 | DigitalOut green_led(LED_GREEN); //green led |
Sarahi | 0:72b6af395c85 | 8 | DigitalIn switch_ip1(SW2); |
Sarahi | 0:72b6af395c85 | 9 | DigitalIn switch_ip2(SW3); |
Sarahi | 0:72b6af395c85 | 10 | |
Sarahi | 0:72b6af395c85 | 11 | char switch_word ; //word we will send |
Sarahi | 0:72b6af395c85 | 12 | char recd_val; //value received from master |
Sarahi | 0:72b6af395c85 | 13 | |
Sarahi | 0:72b6af395c85 | 14 | int main() { |
Sarahi | 0:72b6af395c85 | 15 | |
Sarahi | 0:72b6af395c85 | 16 | slave.address(0x52); |
Sarahi | 0:72b6af395c85 | 17 | while (1) { |
Sarahi | 0:72b6af395c85 | 18 | //set up switch_word from switches that are pressed |
Sarahi | 0:72b6af395c85 | 19 | switch_word=0xa0; //set up a recognisable output pattern |
Sarahi | 0:72b6af395c85 | 20 | if (switch_ip1==0) |
Sarahi | 0:72b6af395c85 | 21 | switch_word=switch_word|0x01; |
Sarahi | 0:72b6af395c85 | 22 | if (switch_ip2==0) |
Sarahi | 0:72b6af395c85 | 23 | switch_word=switch_word|0x02; |
Sarahi | 0:72b6af395c85 | 24 | slave.write(switch_word); //load up word to send |
Sarahi | 0:72b6af395c85 | 25 | |
Sarahi | 0:72b6af395c85 | 26 | //test for I2C, and act accordingly |
Sarahi | 0:72b6af395c85 | 27 | int i = slave.receive(); |
Sarahi | 0:72b6af395c85 | 28 | printf ("%d\n\r" ,i); |
Sarahi | 0:72b6af395c85 | 29 | if (i == 3) //slave is addressed, Master will write |
Sarahi | 0:72b6af395c85 | 30 | recd_val= slave.read(); |
Sarahi | 0:72b6af395c85 | 31 | //now set leds according to received word |
Sarahi | 0:72b6af395c85 | 32 | red_led=1; |
Sarahi | 0:72b6af395c85 | 33 | green_led=1; |
Sarahi | 0:72b6af395c85 | 34 | recd_val=recd_val&0x03; |
Sarahi | 0:72b6af395c85 | 35 | if (recd_val==1) |
Sarahi | 0:72b6af395c85 | 36 | red_led=0; |
Sarahi | 0:72b6af395c85 | 37 | if (recd_val==2) |
Sarahi | 0:72b6af395c85 | 38 | green_led=0; |
Sarahi | 0:72b6af395c85 | 39 | if (recd_val==3){ |
Sarahi | 0:72b6af395c85 | 40 | red_led=0; |
Sarahi | 0:72b6af395c85 | 41 | green_led=0; |
Sarahi | 0:72b6af395c85 | 42 | } |
Sarahi | 0:72b6af395c85 | 43 | } //end of while |
Sarahi | 0:72b6af395c85 | 44 | } //end of main |