![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
freemaster1.0
master.cpp
- Committer:
- viswachaitanya
- Date:
- 2014-12-03
- Revision:
- 0:a39f49a913e6
File content as of revision 0:a39f49a913e6:
#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 master_main() { 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(addr|0x00); //addressing the slave to write if(acknowledge2) //proceeding further only if slave is addressed { printf("3\n"); acknowledge1 = (bool) master.write(get_hk_data); //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(addr | 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 }