i2c master receiving 4 bit data from slave

Dependencies:   mbed

master.cpp

Committer:
viswachaitanya
Date:
2014-07-05
Revision:
0:e96ddb4b964e

File content as of revision 0:e96ddb4b964e:

#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  
}