Skip to content

Instantly share code, notes, and snippets.

@dsiganos
Created March 24, 2018 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsiganos/14c1034fae9c2ca527409ca607f27173 to your computer and use it in GitHub Desktop.
Save dsiganos/14c1034fae9c2ca527409ca607f27173 to your computer and use it in GitHub Desktop.
#include "mbed.h"
I2CSlave slave(PA_9,PA_10); //configuring pins p27, p28 as I2Cslave
Serial pc (USBTX,USBRX);
void split(float); //function to split 4 bcurrentte data
void write_to_master(char); //function to write data to master
void FUNC_I2C_SLAVE_MAIN();
DigitalOut myled(LED1);
void write_to_master(char send) //function to write data to master
{
int acknowledge;
acknowledge = slave.write(send); //sending the byte to master
if(acknowledge==1)
{
pc.printf(" acknowledge %d sent %x \n",acknowledge,send);
}
}
void split(float data) //function to split data into 4 individual bytes and send to master
{
union convert
{
char byte[4]; //array containing individual bytes
float number; //floating point number to be split into four bytes
}v;
v.number=data;
write_to_master(v.byte[0]);
write_to_master(v.byte[1]);
write_to_master(v.byte[2]);
write_to_master(v.byte[3]);
pc.printf("sent %f\n",v.number);
}
int main()
{
wait(0.5);
slave.address(0x20); //assigning slave address
int Switch_Variable;
//int ReadAddressed=1;
int WriteGeneral=3;
while(1)
{
pc.printf("switch variable=%d\n",slave.receive());
//to read data from master
if(slave.receive()==WriteGeneral) //checking if slave is addressed to write
{
Switch_Variable=slave.read(); //receiving data
pc.printf("switch variable=%d\n",Switch_Variable);
slave.stop(); //reset slave to default receiving state
//to interpret and write data to master
switch(Switch_Variable)
{
case 1:
myled = 1; // LED is ON
pc.printf("ON\n");
break;
case 2:
myled = 0; // LED is OFF
pc.printf("OFF\n");
break;
}//switch case ends
}
}
pc.printf("done");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment