working code of cdms with i2c

Dependencies:   mbed-rtos mbed

Fork of rtos_basic by mbed official

master.cpp

Committer:
viswa_chaitanya
Date:
2014-09-18
Revision:
7:6b1a6941ac87

File content as of revision 7:6b1a6941ac87:

#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 FUNC_I2C_MASTER_MAIN(char command, int slave_address, int iterations)
{
    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(slave_address|0x00);           //addressing the slave to write
        if(acknowledge2)                             //proceeding further only if slave is addressed
        {
            printf("3\n");
        acknowledge1 = (bool) master.write(command);         //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(slave_address | 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  
}