freemaster1.0

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
viswachaitanya
Date:
Wed Dec 03 10:36:09 2014 +0000
Commit message:
pre 12/2014 testing

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
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
diff -r 000000000000 -r a39f49a913e6 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 03 10:36:09 2014 +0000
@@ -0,0 +1,12 @@
+#include "master.h"                          //library                                              //
+int main()
+{
+    while(1)
+    {
+    master_main();
+    wait(1);
+    }
+}
+
+
+
diff -r 000000000000 -r a39f49a913e6 master.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/master.cpp	Wed Dec 03 10:36:09 2014 +0000
@@ -0,0 +1,83 @@
+#include "master.h"
+
+I2C master (p28,p27);                      //configure pins p27,p28 as I2C master
+Serial pc (USBTX,USBRX);
+
+struct SensorData                          //HK_data_structure
+{
+    char voltage[10];
+    char current[10];
+    char temp[10];
+} Sensor;
+
+void master_main()
+{
+    wait(0.5);
+    printf("1\n");
+    bool acknowledge1;
+    bool acknowledge2;
+    uint8_t loopvariable2=0;
+    bool loopvariable1 = true;
+    bool loopvariable3 = true;
+    while(loopvariable1)
+    {
+//-------------writing the command to slave--------------------------------------------------------------        
+        printf("2\n");
+        master.frequency(100000);                       //set clock frequency
+        master.start();                                 //initiating the data transfer
+        acknowledge2 = (bool) master.write(addr|0x00);           //addressing the slave to write
+        if(acknowledge2)                             //proceeding further only if slave is addressed
+        {
+            printf("3\n");
+        acknowledge1 = (bool) master.write(get_hk_data);         //sending the command to slave 
+        if(acknowledge1)                             //proceeding further only if sent data is acknowledged   
+           {
+               printf("acknowledge1=%d\n",acknowledge1);
+               loopvariable1=false;                         //if acknowledged, breaking loop in next iteration
+               
+//--------------reading data from slave---------------------------------------------------------------          
+               while(loopvariable3)
+                 {              
+                    master.frequency(100000);                    //set clock frequency 
+                    master.start();                              //initiate data transfer 
+                    acknowledge1 = (bool) master.write(addr | 0x01);    //addressing the slave to read
+                    
+                    if(acknowledge1)                          //proceedong only if slae is addressed
+                       {  
+                       loopvariable3 = false;
+                       while(loopvariable2<8)
+                          { 
+                              Sensor.voltage[loopvariable2] =  receive_byte();              //receiving data if acknowledged
+                              printf(" voltage%d = %x\n",loopvariable2,Sensor.voltage[loopvariable2]);
+                             
+                              Sensor.current[loopvariable2] =  receive_byte();             //receiving data if acknowledged
+                              printf(" current%d = %x\n",loopvariable2, Sensor.current[loopvariable2]);
+                            
+                              Sensor.temp[loopvariable2] =  receive_byte();                //receiving data if acknowledged
+                              printf(" temperature%d = %x\n",loopvariable2,Sensor.temp[loopvariable2]);
+                            
+                              loopvariable2++;
+                          
+                          } //while(loopvariable2<30) 
+                       }//if(acknowledge1==1)
+                 
+                    master.stop();
+                 }//while(loopvariable3) 
+            }//if(acknowledge1==1)   
+        }//if(acknowledge2==1) 
+     }//while(loopvariable1)
+   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  
+}
+
+
+
diff -r 000000000000 -r a39f49a913e6 master.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/master.h	Wed Dec 03 10:36:09 2014 +0000
@@ -0,0 +1,10 @@
+#include "mbed.h"                          //library                                              
+#define get_hk_data '1'                      //sample telecommands
+#define telecommand2 '2'
+#define telecommand3 '3'
+#define telecommand4 '4'
+#define telecommand5 '5'
+
+const int addr = 0x20;                     //slave address 
+char receive_byte();                      //funtion to receive float data 
+void master_main();
\ No newline at end of file
diff -r 000000000000 -r a39f49a913e6 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Dec 03 10:36:09 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1
\ No newline at end of file