working code of bae with i2c

Dependencies:   mbed-rtos mbed

Fork of all_combined_week6 by green rosh

Files at this revision

API Documentation at this revision

Comitter:
viswachaitanya
Date:
Thu Sep 18 10:29:33 2014 +0000
Parent:
6:82153349cc9b
Commit message:
working bae code with i2c

Changed in this revision

ACS.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
slave.cpp Show annotated file Show diff for this revision Revisions of this file
slave.h Show annotated file Show diff for this revision Revisions of this file
--- a/ACS.cpp	Tue Jul 15 10:04:38 2014 +0000
+++ b/ACS.cpp	Thu Sep 18 10:29:33 2014 +0000
@@ -1,8 +1,8 @@
 #include "ACS.h"
 
 
-PwmOut PWM1(p6);        //Functions used to generate PWM signal 
-                        //PWM output comes from pins p6
+PwmOut PWM1(p5);        //Functions used to generate PWM signal 
+                   //PWM output comes from pins p6
 Serial pc1(USBTX, USBRX);
 
 
--- a/main.cpp	Tue Jul 15 10:04:38 2014 +0000
+++ b/main.cpp	Thu Sep 18 10:29:33 2014 +0000
@@ -58,7 +58,7 @@
         printf("The state of acs_write2flash thread is %d\n",ptr_t_acs_write2flash->get_state());
         printf("The state of beacon thread is %d\n",ptr_t_bea->get_state());*/
              
-        //FUNC_I2C_SLAVE_MAIN(24);
+        FUNC_I2C_SLAVE_MAIN(24);
         printf("The time to execute send2cdms is %f\n",t.read());
         t.reset();
     }
@@ -266,7 +266,7 @@
     
     if(schedcount%1==0)
     {
-        ptr_t_acs -> signal_set(0x1);
+        //ptr_t_acs -> signal_set(0x1);
     }
     if(schedcount%2==0)
     {
@@ -274,7 +274,7 @@
     }
     if(schedcount%3==0)
     {
-        ptr_t_bea -> signal_set(0x3);
+        //ptr_t_bea -> signal_set(0x3);
     }
     schedcount++;
 }
--- a/slave.cpp	Tue Jul 15 10:04:38 2014 +0000
+++ b/slave.cpp	Thu Sep 18 10:29:33 2014 +0000
@@ -4,7 +4,109 @@
 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
+
+
+//#include "slave.h"
+//I2CSlave slave(p28,p27);                       //configuring pins p27, p28 as I2Cslave
+//Serial pc (USBTX,USBRX);
+//SensorData Sensor;
+/*struct SensorData                              //HK_data_structure
+{
+    char voltage[10];
+    char current[10];
+    char temp[10];
+}Sensor;*/
+
+int 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()==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()==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;
@@ -13,7 +115,7 @@
            acknowledge = slave.write(send);    //sending the byte to master
            if(acknowledge==1)
             {
-              screen.printf(" acknowledge %d sent %u \n",acknowledge,send);
+              printf(" acknowledge %d sent %u \n",acknowledge,send);
               loopvariable4=0;
             }
            } 
@@ -24,8 +126,8 @@
  
 void FUNC_I2C_SLAVE_MAIN(int iterations)
 {
-    
-    screen.printf("\nSlave entered\n");
+ //   slave.stop();
+    printf("\nSlave entered\n");
     slave.address(slave_address);                           //assigning slave address
     char Switch_Variable;
     
@@ -37,11 +139,13 @@
     
     while(loopvariable1)
     { 
-//to read data from master       
+//    slave.stop();
+//to read data from master    
+     printf("not receiving\n");   
     if(slave.receive()==WriteGeneral)                   //checking if slave is addressed to write
     {
       Switch_Variable=slave.read();                   //receiving data
-      screen.printf("switch variable=%d\n",Switch_Variable);
+      printf("switch variable=%d\n",Switch_Variable);
       slave.stop();                          //reset slave to default receiving state
       loopvariable1=0;
 //to interpret and write data to master       
@@ -58,17 +162,17 @@
                      {
                       if(loopvariable2%3==0)
                           {
-                          screen.printf("\nvoltage%d\n",loopvariable2/3);
+                          printf("\nvoltage%d\n",loopvariable2/3);
                           write_to_master(Sensor.Voltage[loopvariable2/3]);
                           }
                       else if(loopvariable2%3==1)
                           {   
-                          screen.printf("\ncurrent%d\n",loopvariable2/3);
+                          printf("\ncurrent%d\n",loopvariable2/3);
                           write_to_master(Sensor.Current[loopvariable2/3]);
                           }
                       else if(loopvariable2%3==2)
                           {   
-                          screen.printf("\ntemp%d\n",loopvariable2/3);
+                          printf("\ntemp%d\n",loopvariable2/3);
                           write_to_master(Sensor.Temperature[loopvariable2/3]);
                           }         
                       loopvariable2++;
@@ -81,12 +185,12 @@
                     }//while(loopvariable3)
                   
                   break;
-         case 2 : screen.printf(" telecommand 2\n");
+         case 2 : printf(" telecommand 2\n");
                   break;         
                  
         }//switch case ends
    }   
 }
-   screen.printf("\nexited slave function\n");
-}
+   printf("\nexited slave function\n");
+}*/
  
\ No newline at end of file
--- a/slave.h	Tue Jul 15 10:04:38 2014 +0000
+++ b/slave.h	Thu Sep 18 10:29:33 2014 +0000
@@ -1,6 +1,23 @@
 #include "mbed.h"
+
+int FUNC_I2C_SLAVE_MAIN(int iterations);
+void write_to_master(char);                    //function to write data to master
+
+
+
+
+
+
+
+
+
+
+
+
+
+/*#include "mbed.h"
 #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(int iterations);
\ No newline at end of file
+void FUNC_I2C_SLAVE_MAIN(int iterations);*/
\ No newline at end of file