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.
main.cpp
00001 /*Program Example 7.6: I2C Slave, when called transfers switch state to mbed acting as Master, and displays state of Master's switches on its leds. 00002 */ 00003 00004 #include <mbed.h> 00005 I2CSlave slave(p9, p10); //Configure I2C slave 00006 DigitalOut red_led(p25); //red led 00007 DigitalOut green_led(p26); //green led 00008 DigitalIn switch_ip1(p5); 00009 DigitalIn switch_ip2(p6); 00010 char switch_word ; //word we will send 00011 char recd_val; //value received from master 00012 00013 int main() 00014 { 00015 slave.address(0x52); 00016 while (1) { 00017 //set up switch_word from switches that are pressed 00018 switch_word=0xa0; //set up a recognisable output pattern 00019 if (switch_ip1==1) 00020 switch_word=switch_word|0x01; 00021 if (switch_ip2==1) 00022 switch_word=switch_word|0x02; 00023 slave.write(switch_word); //load up word to send 00024 //test for I2C, and act accordingly 00025 int i = slave.receive(); 00026 if (i == 3) { //slave is addressed, Master will write 00027 recd_val= slave.read(); 00028 } 00029 //set leds according to incoming word from slave 00030 red_led=0; //preset both to 0 00031 green_led=0; 00032 recd_val=recd_val&0x03; //AND out unwanted bits 00033 if (recd_val==1) 00034 red_led=1; 00035 if (recd_val==2) 00036 green_led=1; 00037 if (recd_val==3) { 00038 red_led=1; 00039 green_led=1; 00040 } 00041 } 00042 } 00043
Generated on Thu Jul 21 2022 06:35:41 by
1.7.2