green rosh
/
i2c_master
I2C master
Fork of i2c_master by
master.cpp
- Committer:
- greenroshks
- Date:
- 2014-12-04
- Revision:
- 2:899f7e7c61ed
- Parent:
- 1:14b369c0f314
File content as of revision 2:899f7e7c61ed:
#include "master.h" I2C master (p28,p27); //configure pins p27,p28 as I2C master Serial pc (USBTX,USBRX); SensorData Sensor; void FUNC_I2C_MASTER_MAIN(int command, int slave_address, int iterations) { printf("\nmaster entered\n"); int acknowledge1; int acknowledge2; uint8_t 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 = %u\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 = %u\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 = %u\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"); printf("\nMaster exited\n"); }//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 }