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 i2c_master by
Revision 1:14b369c0f314, committed 2014-07-15
- Comitter:
- greenroshks
- Date:
- Tue Jul 15 10:06:29 2014 +0000
- Parent:
- 0:7811fb22b3de
- Child:
- 2:c1387cf55c7d
- Commit message:
- Master final
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Jul 15 10:06:29 2014 +0000
@@ -0,0 +1,10 @@
+#include "master.h"
+
+int main()
+{
+ while(true)
+ {
+ FUNC_I2C_MASTER_MAIN(1,0x20,24);
+ wait(23);
+ }
+}
\ No newline at end of file
--- a/master.cpp Mon Jun 30 11:55:18 2014 +0000
+++ b/master.cpp Tue Jul 15 10:06:29 2014 +0000
@@ -1,70 +1,73 @@
#include "master.h"
-
-float receive_byte() //function to read and print the data received
+
+I2C master (p28,p27); //configure pins p27,p28 as I2C master
+Serial pc (USBTX,USBRX);
+SensorData Sensor;
+
+void FUNC_I2C_MASTER_MAIN(int command, int slave_address, int iterations)
{
- union join
- {
- char byte[4]; //array containing individual bytes
- float number; //floating point number to be appended
- }append;
- float value;
- append.byte[0]=master.read(1); //reading individual bytes and storing it in array
- pc.printf("\n(1) %x\n",append.byte[0]);
- append.byte[1]=master.read(1);
- pc.printf("(2) %x\n",append.byte[1]);
- append.byte[2]=master.read(1);
- pc.printf("(3) %x\n",append.byte[2]);
- append.byte[3]=master.read(1);
- pc.printf("(4) %x\n",append.byte[3]);
- value = append.number;
- return(value); //returning the 4 byte floating point number
-}
-
-void FUNC_I2C_MASTER_MAIN()
-{
- int acknowledge;
- int loopvariable1=1,loopvariable2=0;
+ printf("\nmaster entered\n");
+ int acknowledge1;
+ int acknowledge2;
+ uint8_t loopvariable1=1,loopvariable2=0;
+ int addr = slave_address;
while(loopvariable1)
{
- //writing the command to slave
- master.frequency(100000); //set clock frequency
- master.start(); //initiating the data transfer
- master.write(addr|0x00); //addressing the slave to write
- acknowledge=master.write(get_hk_data); //sending the command to slave
- if(acknowledge==1)
+//-------------writing the command to slave--------------------------------------------------------------
+ master.frequency(100000); //set clock frequency
+ master.start(); //initiating the data transfer
+ acknowledge2=master.write(addr|0x00); //addressing the slave to write
+ if(acknowledge2==1) //proceeding further only if slave is addressed
+ {
+ acknowledge1=master.write(command); //sending the command to slave
+ if(acknowledge1==1) //proceeding further only if sent data is acknowledged
{
- pc.printf("acknowledge=%d\n",acknowledge);
- loopvariable1=0; //if acknowledged, breaking loop in next iteration
- //reading 4 byte data from slave
- while(loopvariable2<30)
- {
- master.frequency(100000); //set clock frequency
- master.start(); //initiate data transfer
- acknowledge = master.write(addr | 0x01); //addressing the slave to read
- if(acknowledge==1)
- {
- //pc.printf("enter\n");
+ pc.printf("acknowledge1=%d\n",acknowledge1);
+ loopvariable1=0; //if acknowledged, breaking loop in next iteration
+//--------------reading data from slave---------------------------------------------------------------
+
+ master.frequency(100000); //set clock frequency
+ master.start(); //initiate data transfer
+ acknowledge1 = master.write(addr | 0x01); //addressing the slave to read
+
+ if(acknowledge1==1) //proceedong only if slae is addressed
+ {
+ while(loopvariable2<iterations)
+ {
if(loopvariable2%3==0)
{
- Sensor.voltage[loopvariable2/3] = receive_byte(); //receiving data if acknowledged
- pc.printf(" voltage%d = %f\n",loopvariable2/3,Sensor.voltage[loopvariable2/3]);
+ Sensor.voltage[loopvariable2/3] = receive_byte(); //receiving data if acknowledged
+ pc.printf(" voltage%d = %u\n",loopvariable2/3,Sensor.voltage[loopvariable2/3]);
}
else if(loopvariable2%3==1)
{
- Sensor.current[loopvariable2/3] = receive_byte(); //receiving data if acknowledged
- pc.printf(" current%d = %f\n",loopvariable2/3, Sensor.current[loopvariable2/3]);
+ Sensor.current[loopvariable2/3] = receive_byte(); //receiving data if acknowledged
+ pc.printf(" current%d = %u\n",loopvariable2/3, Sensor.current[loopvariable2/3]);
}
else if(loopvariable2%3==2)
{
- Sensor.temp[loopvariable2/3] = receive_byte(); //receiving data if acknowledged
- pc.printf(" temperature%d = %f\n",loopvariable2/3,Sensor.temp[loopvariable2/3]);
+ Sensor.temp[loopvariable2/3] = receive_byte(); //receiving data if acknowledged
+ pc.printf(" temperature%d = %u\n",loopvariable2/3,Sensor.temp[loopvariable2/3]);
}
-
loopvariable2++;
- }//if(acknowledge==1)
- } //while(loopvariable2<30)
+
+ } //while(loopvariable2<30)
+ }//if(acknowledge1==1)
+
master.stop();
- }//if(acknowledge==1)
+ }//if(acknowledge1==1)
+ }//if(acknowledge2==1)
}//while(loopvariable1)
pc.printf("done");
-}//main
\ No newline at end of file
+printf("\nMaster exited\n");
+}//main
+
+
+//----------------function to read and return the data received-----------------------------------
+char receive_byte()
+{
+
+ char value;
+ value = master.read(1);
+ return(value); //returning the 4 byte floating point number
+}
\ No newline at end of file
--- a/master.h Mon Jun 30 11:55:18 2014 +0000
+++ b/master.h Tue Jul 15 10:06:29 2014 +0000
@@ -1,25 +1,22 @@
#include "mbed.h" //library //
-#define get_hk_data 1 //sample telecommands
-#define telecommand2 2
-#define telecommand3 3
-#define telecommand4 4
-#define telecommand5 5
-#define telecommand6 6
-#define telecommand7 7
-#define telecommand8 8
-#define telecommand9 9
-#define telecommand10 10
-I2C master (p28,p27); //configure pins p27,p28 as master
-Serial pc (USBTX,USBRX);
+#define get_hk_data '1' //sample telecommands
+#define telecommand2 '2'
+#define telecommand3 '3'
+#define telecommand4 '4'
+#define telecommand5 '5'
+#define telecommand6 '6'
+#define telecommand7 '7'
+#define telecommand8 '8'
+#define telecommand9 '9'
+#define telecommand10 '10'
-const int addr = 0x20; //slave address
-float receive_byte();
-void FUNC_I2C_MASTER_MAIN();
-
-struct SensorData
+char receive_byte(); //funtion to receive float data
+
+
+struct SensorData //HK_data_structure
{
- float voltage[10];
- float current[10];
- float temp[10];
- //float Battery[2];
-} Sensor;
\ No newline at end of file
+ char voltage[10];
+ char current[10];
+ char temp[10];
+} ;
+void FUNC_I2C_MASTER_MAIN(int command, int slave_address, int iterations);
\ No newline at end of file
