![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
working code of bae with i2c
Fork of all_combined_week6 by
Diff: slave.cpp
- Revision:
- 7:47e07755d9a4
- Parent:
- 6:82153349cc9b
diff -r 82153349cc9b -r 47e07755d9a4 slave.cpp --- a/slave.cpp Tue Jul 15 10:04:38 2014 +0000 +++ b/slave.cpp Thu Sep 18 10:29:33 2014 +0000 @@ -4,7 +4,109 @@ extern struct SensorData Sensor; I2CSlave slave(p28,p27); //configuring pins p27, p28 as I2Cslave Serial screen (USBTX,USBRX); -void write_to_master(char send) //function to write data to master + + +//#include "slave.h" +//I2CSlave slave(p28,p27); //configuring pins p27, p28 as I2Cslave +//Serial pc (USBTX,USBRX); +//SensorData Sensor; +/*struct SensorData //HK_data_structure +{ + char voltage[10]; + char current[10]; + char temp[10]; +}Sensor;*/ + +int FUNC_I2C_SLAVE_MAIN(int iterations) +{ + + wait(0.5); + slave.address(0x20); //assigning slave address + slave.stop(); + char Switch_Variable; + int ReadAddressed=1; + int WriteGeneral=3; + bool loopvariable1=true; + uint8_t loopvariable2=0; + bool loopvariable3=true; +//---------------initialising dummy sensor data----------------------------------------------------------- + Sensor.Voltage[0]='a';Sensor.Current[0]='1';Sensor.Temperature[0]='k'; + Sensor.Voltage[1]='b';Sensor.Current[1]='2';Sensor.Temperature[1]='l'; + Sensor.Voltage[2]='c';Sensor.Current[2]='3';Sensor.Temperature[2]='m'; + Sensor.Voltage[3]='d';Sensor.Current[3]='4';Sensor.Temperature[3]='n'; + Sensor.Voltage[4]='e';Sensor.Current[4]='5';Sensor.Temperature[4]='o'; + Sensor.Voltage[5]='f';Sensor.Current[5]='1';Sensor.Temperature[5]='p'; + Sensor.Voltage[6]='g';Sensor.Current[6]='2';Sensor.Temperature[6]='q'; + Sensor.Voltage[7]='h';Sensor.Current[7]='3';Sensor.Temperature[7]='r'; + while(loopvariable1) + { +//------------------------to read data from master--------------------------------------------------------- + if(slave.receive()==WriteGeneral) //checking if slave is addressed to write + { + Switch_Variable=slave.read(); //receiving data + printf("switch variable=%d\n",Switch_Variable); + slave.stop(); //reset slave to default receiving state + loopvariable1=false; +//----------------------to interpret and write data to master---------------------------------------------- + switch(Switch_Variable) + { + + case '1': while(loopvariable3) + { + if(slave.receive()==ReadAddressed) //check if slave is addressed to read + { + loopvariable3=false; + while(loopvariable2<8) //running loop for sending 30 sensors data + { + printf("\nvoltage%d\n",loopvariable2); + write_to_master(Sensor.Voltage[loopvariable2]); //calling function to send float data + + printf("\ncurrent%d\n",loopvariable2); + write_to_master(Sensor.Current[loopvariable2]); //calling function to send float data + + printf("\ntemp%d\n",loopvariable2); + write_to_master(Sensor.Temperature[loopvariable2]); //calling function to send float data + + loopvariable2++; + } + + } + + } + break; + case '2' : printf(" telecommand 2\n"); + break; + + } + } +} + slave.stop(); + printf("done"); +} + +//------------------function to write data to master--------------------------------------------------- +void write_to_master(char send) +{ + bool acknowledge; + bool loopvariable4=true; + while(loopvariable4) + { + acknowledge = (bool) slave.write(send); //sending the byte to master + if(acknowledge) //breaking loop if data is acknowledged + { + printf(" acknowledge %d sent %x \n",acknowledge,send); + loopvariable4 = false; + } + } +} + + + + + + + +/*void write_to_master(char send) //function to write data to master { int acknowledge; int loopvariable4=1; @@ -13,7 +115,7 @@ acknowledge = slave.write(send); //sending the byte to master if(acknowledge==1) { - screen.printf(" acknowledge %d sent %u \n",acknowledge,send); + printf(" acknowledge %d sent %u \n",acknowledge,send); loopvariable4=0; } } @@ -24,8 +126,8 @@ void FUNC_I2C_SLAVE_MAIN(int iterations) { - - screen.printf("\nSlave entered\n"); + // slave.stop(); + printf("\nSlave entered\n"); slave.address(slave_address); //assigning slave address char Switch_Variable; @@ -37,11 +139,13 @@ while(loopvariable1) { -//to read data from master +// slave.stop(); +//to read data from master + printf("not receiving\n"); if(slave.receive()==WriteGeneral) //checking if slave is addressed to write { Switch_Variable=slave.read(); //receiving data - screen.printf("switch variable=%d\n",Switch_Variable); + printf("switch variable=%d\n",Switch_Variable); slave.stop(); //reset slave to default receiving state loopvariable1=0; //to interpret and write data to master @@ -58,17 +162,17 @@ { if(loopvariable2%3==0) { - screen.printf("\nvoltage%d\n",loopvariable2/3); + printf("\nvoltage%d\n",loopvariable2/3); write_to_master(Sensor.Voltage[loopvariable2/3]); } else if(loopvariable2%3==1) { - screen.printf("\ncurrent%d\n",loopvariable2/3); + printf("\ncurrent%d\n",loopvariable2/3); write_to_master(Sensor.Current[loopvariable2/3]); } else if(loopvariable2%3==2) { - screen.printf("\ntemp%d\n",loopvariable2/3); + printf("\ntemp%d\n",loopvariable2/3); write_to_master(Sensor.Temperature[loopvariable2/3]); } loopvariable2++; @@ -81,12 +185,12 @@ }//while(loopvariable3) break; - case 2 : screen.printf(" telecommand 2\n"); + case 2 : printf(" telecommand 2\n"); break; }//switch case ends } } - screen.printf("\nexited slave function\n"); -} + printf("\nexited slave function\n"); +}*/ \ No newline at end of file