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.
Fork of all_combined_week6 by
Diff: slave.cpp
- Revision:
- 8:772ec80d9c29
- Parent:
- 7:b3f876f605d9
diff -r b3f876f605d9 -r 772ec80d9c29 slave.cpp
--- a/slave.cpp Fri Sep 19 05:12:40 2014 +0000
+++ b/slave.cpp Fri Sep 19 10:35:04 2014 +0000
@@ -1,10 +1,95 @@
#include "slave.h"
#include "HK.h"
+
+
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
+
+void 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()==3)//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()==1)//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;
@@ -27,10 +112,10 @@
screen.printf("\nSlave entered\n");
slave.address(slave_address); //assigning slave address
- slave.stop();
+
char Switch_Variable;
-
+ slave.stop();
int loopvariable1=1;
int loopvariable2=0;
int loopvariable3=1;
@@ -90,4 +175,4 @@
}
screen.printf("\nexited slave function\n");
}
-
\ No newline at end of file
+ */
\ No newline at end of file
