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
Revision 3:9b597ed04ef4, committed 2014-07-11
- Comitter:
- greenroshks
- Date:
- Fri Jul 11 08:54:12 2014 +0000
- Parent:
- 2:94ca2059f8b2
- Child:
- 4:79b97f9cab00
- Commit message:
- Updated with new slave
Changed in this revision
--- a/hkfinal.cpp Fri Jul 11 05:14:12 2014 +0000
+++ b/hkfinal.cpp Fri Jul 11 08:54:12 2014 +0000
@@ -59,13 +59,13 @@
for(LoopIterator=0; LoopIterator<16; LoopIterator++) {
if(LoopIterator%2==0) {
- SensorData.Current[LoopIterator]=quantiz(cstart,cstep,(CurrentInput.read()/(50*rsens)));
- SensorData.Voltage[LoopIterator]=quantiz(vstart,vstep,(VoltageInput.read()/5.37));
- SensorData.Temperature[LoopIterator]=quantiz(tstart,tstep,(-90.7*3.18*TemperatureInput.read()+190.1543));
+ SensorData.Current[LoopIterator/2]=quantiz(cstart,cstep,(CurrentInput.read()/(50*rsens)));
+ SensorData.Voltage[LoopIterator/2]=quantiz(vstart,vstep,(VoltageInput.read()/5.37));
+ SensorData.Temperature[LoopIterator/2]=quantiz(tstart,tstep,(-90.7*3.18*TemperatureInput.read()+190.1543));
} else {
- SensorData.Current[LoopIterator-1]=SensorData.Current[LoopIterator-1]<<4+quantiz(cstart,cstep,(CurrentInput.read()/(50*rsens)));
- SensorData.Voltage[LoopIterator-1]=SensorData.Voltage[LoopIterator-1]<<4+quantiz(vstart,vstep,(VoltageInput.read()/5.37));
- SensorData.Temperature[LoopIterator-1]=SensorData.Temperature[LoopIterator-1]<<4+quantiz(tstart,tstep,(-90.7*3.18*TemperatureInput.read()+190.1543));
+ SensorData.Current[(LoopIterator-1)/2]=SensorData.Current[LoopIterator-1]<<4+quantiz(cstart,cstep,(CurrentInput.read()/(50*rsens)));
+ SensorData.Voltage[(LoopIterator-1)/2]=SensorData.Voltage[LoopIterator-1]<<4+quantiz(vstart,vstep,(VoltageInput.read()/5.37));
+ SensorData.Temperature[(LoopIterator-1)/2]=SensorData.Temperature[LoopIterator-1]<<4+quantiz(tstart,tstep,(-90.7*3.18*TemperatureInput.read()+190.1543));
}
// The following lines are used to iterate the select lines from 0 to 15
SelectLine0=!(SelectLine0);
--- a/slave.cpp Fri Jul 11 05:14:12 2014 +0000
+++ b/slave.cpp Fri Jul 11 08:54:12 2014 +0000
@@ -1,98 +1,89 @@
#include "slave.h"
-SensorData Sensor;
+#include "HK.h"
+
+extern SensorData Sensor;
I2CSlave slave(p28,p27); //configuring pins p27, p28 as I2Cslave
-Serial green(USBTX, USBRX);
-
+Serial screen (USBTX,USBRX);
void write_to_master(char send) //function to write data to master
{
int acknowledge;
+ int loopvariable4=1;
+ while(loopvariable4)
+ {
acknowledge = slave.write(send); //sending the byte to master
if(acknowledge==1)
{
- green.printf(" acknowledge %d sent %x \n",acknowledge,send);
+ screen.printf(" acknowledge %d sent %x \n",acknowledge,send);
+ loopvariable4=0;
}
+ }
}
- 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]);
- green.printf("sent %f\n",v.number);
- }
-
-
-
-void FUNC_I2C_SLAVE_MAIN()
+
+
+
+void FUNC_I2C_SLAVE_MAIN(int iterations)
{
wait(0.5);
- slave.address(0x20); //assigning slave address
- int Switch_Variable;
- int ReadAddressed=1;
- int WriteGeneral=3;
+ screen.printf("\nSlave entered\n");
+ slave.address(slave_address); //assigning slave address
+ char Switch_Variable;
+
+
int loopvariable1=1;
int loopvariable2=0;
+ int loopvariable3=1;
//initialising dummy sensor data
-// SensorData Sensor;
- Sensor.voltage[0]=0.1;Sensor.current[0]=0.0;Sensor.temp[0]=1.0;
- Sensor.voltage[1]=0.1;Sensor.current[1]=1.1;Sensor.temp[1]=2.0;
- Sensor.voltage[2]=0.2;Sensor.current[2]=2.2;Sensor.temp[2]=2.0;
- Sensor.voltage[3]=0.3;Sensor.current[3]=3.3;Sensor.temp[3]=3.0;
- Sensor.voltage[4]=0.4;Sensor.current[4]=4.4;Sensor.temp[4]=4.0;
- Sensor.voltage[5]=0.5;Sensor.current[5]=5.5;Sensor.temp[5]=5.0;
- Sensor.voltage[6]=0.6;Sensor.current[6]=6.6;Sensor.temp[6]=6.0;
- Sensor.voltage[7]=0.7;Sensor.current[7]=7.7;Sensor.temp[7]=7.0;
- Sensor.voltage[8]=0.8;Sensor.current[8]=8.8;Sensor.temp[8]=8.0;
- Sensor.voltage[9]=0.9;Sensor.current[9]=9.9;Sensor.temp[9]=20.5;
+
while(loopvariable1)
{
//to read data from master
if(slave.receive()==WriteGeneral) //checking if slave is addressed to write
{
Switch_Variable=slave.read(); //receiving data
- green.printf("switch variable=%d\n",Switch_Variable);
+ 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: while(loopvariable2<30)
- {
+ case '1': while(loopvariable3)
+ {
if(slave.receive()==ReadAddressed) //check if slave is addressed to read
{
+ while(loopvariable2<iterations)
+ {
if(loopvariable2%3==0)
{
- green.printf("\nvoltage%d\n",loopvariable2/3);
- split(Sensor.voltage[loopvariable2/3]);
+ screen.printf("\nvoltage%d\n",loopvariable2/3);
+ write_to_master(Sensor.Voltage[loopvariable2/3]);
}
else if(loopvariable2%3==1)
{
- green.printf("\ncurrent%d\n",loopvariable2/3);
- split(Sensor.current[loopvariable2/3]);
+ screen.printf("\ncurrent%d\n",loopvariable2/3);
+ write_to_master(Sensor.Current[loopvariable2/3]);
}
else if(loopvariable2%3==2)
{
- green.printf("\ntemp%d\n",loopvariable2/3);
- split(Sensor.temp[loopvariable2/3]);
+ screen.printf("\ntemp%d\n",loopvariable2/3);
+ write_to_master(Sensor.Temperature[loopvariable2/3]);
}
loopvariable2++;
- slave.stop();
- }
- }
+ }//while(loopvariable2<30)
+ slave.stop();
+ loopvariable3=0;
+ }//if(read addressed)
+
+ }//while(loopvariable3)
+
break;
- case 2 : green.printf(" telecommand 2\n");
+ case '2' : screen.printf(" telecommand 2\n");
break;
}//switch case ends
}
}
- green.printf("done");
+ screen.printf("done");
}
+
\ No newline at end of file
--- a/slave.h Fri Jul 11 05:14:12 2014 +0000
+++ b/slave.h Fri Jul 11 08:54:12 2014 +0000
@@ -1,14 +1,6 @@
#include "mbed.h"
-
-
-void split(float); //function to split 4 bcurrentte data
+#define WriteGeneral 3
+#define ReadAddressed 1
+#define slave_address 0x20
void write_to_master(char); //function to write data to master
-void FUNC_I2C_SLAVE_MAIN();
-
-struct SensorData //HK_data_structure
-{
- float voltage[10];
- float current[10];
- float temp[10];
- //float Battery[2];
-};
+void FUNC_I2C_SLAVE_MAIN(int iterations);
\ No newline at end of file
