i2c master receiving 4 bit data from slave

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
viswachaitanya
Date:
Sat Jul 05 06:40:06 2014 +0000
Commit message:
i2cmaster receiving 4 bit data from slave

Changed in this revision

master.cpp Show annotated file Show diff for this revision Revisions of this file
master.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/master.cpp	Sat Jul 05 06:40:06 2014 +0000
@@ -0,0 +1,67 @@
+#include "master.h"
+
+void FUNC_I2C_MASTER_MAIN(int command, int slave_address, int iterations)
+{
+    int acknowledge1;
+    int acknowledge2;
+    int 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
+        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("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 = %x\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 = %x\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 = %x\n",loopvariable2/3,Sensor.temp[loopvariable2/3]);
+                            }
+                          loopvariable2++;
+                          
+                         } //while(loopvariable2<30) 
+                       }//if(acknowledge1==1)
+                 
+              master.stop();
+           }//if(acknowledge1==1)   
+        }//if(acknowledge2==1) 
+     }//while(loopvariable1)
+   pc.printf("done");
+}//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  
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/master.h	Sat Jul 05 06:40:06 2014 +0000
@@ -0,0 +1,25 @@
+#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 I2C master
+Serial pc (USBTX,USBRX);
+
+//const int addr = 0x20;                     //slave address 
+char receive_byte();                      //funtion to receive float data 
+
+
+struct SensorData                          //HK_data_structure
+{
+    char voltage[5];
+    char current[5];
+    char temp[5];
+} Sensor;
+void FUNC_I2C_MASTER_MAIN(int command, int slave_address, int iterations);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Jul 05 06:40:06 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/04dd9b1680ae
\ No newline at end of file