![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
jkjkjkjk
Fork of all_combined_week6 by
slave.cpp
- Committer:
- viswachaitanya
- Date:
- 2014-07-17
- Revision:
- 7:5cf19bfdbacd
- Parent:
- 6:82153349cc9b
File content as of revision 7:5cf19bfdbacd:
#include "slave.h" int acknowledge; int loopvariable4; 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 { loopvariable4=1; while(loopvariable4) { acknowledge = slave.write(send); //sending the byte to master if(acknowledge==1) { screen.printf(" acknowledge %d sent %u \n",acknowledge,send); loopvariable4=0; } } } void FUNC_I2C_SLAVE_MAIN(int iterations) { slave.address(slave_address); //assigning slave address char Switch_Variable; slave.stop(); Sensor.voltage[0]='a';Sensor.current[0]='1';Sensor.temp[0]='k'; Sensor.voltage[1]='b';Sensor.current[1]='2';Sensor.temp[1]='l'; Sensor.voltage[2]='c';Sensor.current[2]='3';Sensor.temp[2]='m'; Sensor.voltage[3]='d';Sensor.current[3]='4';Sensor.temp[3]='n'; Sensor.voltage[4]='e';Sensor.current[4]='5';Sensor.temp[4]='o'; Sensor.voltage[5]='f';Sensor.current[5]='1';Sensor.temp[5]='p'; Sensor.voltage[6]='g';Sensor.current[6]='2';Sensor.temp[6]='q'; Sensor.voltage[7]='h';Sensor.current[7]='3';Sensor.temp[7]='r'; screen.printf("\nSlave entered\n"); int loopvariable1=1; int loopvariable2=0; int loopvariable3=1; //initialising dummy sensor data while(loopvariable1) { //to read data from master 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); slave.stop(); //reset slave to default receiving state loopvariable1=0; //to interpret and write data to master switch(Switch_Variable) { case 1: printf("\nEntered switch\n"); while(loopvariable3) { if(slave.receive()==ReadAddressed) //check if slave is addressed to read { while(loopvariable2<iterations) { if(loopvariable2%3==0) { screen.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); write_to_master(Sensor.current[loopvariable2/3]); } else if(loopvariable2%3==2) { screen.printf("\ntemp%d\n",loopvariable2/3); write_to_master(Sensor.temp[loopvariable2/3]); } loopvariable2++; }//while(loopvariable2<30) printf("\nExited i2c while loop\n"); slave.stop(); loopvariable3=0; }//if(read addressed) }//while(loopvariable3) break; case 2 : screen.printf(" telecommand 2\n"); break; }//switch case ends } } screen.printf("\nexited slave function\n"); }