working code of bae with i2c

Dependencies:   mbed-rtos mbed

Fork of all_combined_week6 by green rosh

slave.cpp

Committer:
viswachaitanya
Date:
2014-09-18
Revision:
7:47e07755d9a4
Parent:
6:82153349cc9b

File content as of revision 7:47e07755d9a4:

#include "slave.h"
#include "HK.h"
 
extern struct SensorData Sensor;
I2CSlave slave(p28,p27);                       //configuring pins p27, p28 as I2Cslave
Serial screen (USBTX,USBRX);


//#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;
           while(loopvariable4)
           {
           acknowledge = slave.write(send);    //sending the byte to master
           if(acknowledge==1)
            {
              printf(" acknowledge %d sent %u \n",acknowledge,send);
              loopvariable4=0;
            }
           } 
        }
        
 
 
 
void FUNC_I2C_SLAVE_MAIN(int iterations)
{
 //   slave.stop();
    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                         
    
    while(loopvariable1)
    { 
//    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
      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:   printf("\nEntered switch\n"); 
                   while(loopvariable3)
                  {
                     if(slave.receive()==ReadAddressed)             //check if slave is addressed to read 
                   {
                      while(loopvariable2<iterations)
                     {
                      if(loopvariable2%3==0)
                          {
                          printf("\nvoltage%d\n",loopvariable2/3);
                          write_to_master(Sensor.Voltage[loopvariable2/3]);
                          }
                      else if(loopvariable2%3==1)
                          {   
                          printf("\ncurrent%d\n",loopvariable2/3);
                          write_to_master(Sensor.Current[loopvariable2/3]);
                          }
                      else if(loopvariable2%3==2)
                          {   
                          printf("\ntemp%d\n",loopvariable2/3);
                          write_to_master(Sensor.Temperature[loopvariable2/3]);
                          }         
                      loopvariable2++;
                       }//while(loopvariable2<30)
                       printf("\nExited i2c while loop\n");
                      slave.stop();    
                      loopvariable3=0;  
                    }//if(read addressed)
                    
                    }//while(loopvariable3)
                  
                  break;
         case 2 : printf(" telecommand 2\n");
                  break;         
                 
        }//switch case ends
   }   
}
   printf("\nexited slave function\n");
}*/