Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of i2c_master by
master.cpp
00001 #include "master.h" 00002 00003 I2C master (p28,p27); //configure pins p27,p28 as I2C master 00004 Serial pc (USBTX,USBRX); 00005 SensorData Sensor; 00006 00007 void FUNC_I2C_MASTER_MAIN(int command, int slave_address, int iterations) 00008 { 00009 printf("\nmaster entered\n"); 00010 int acknowledge1; 00011 int acknowledge2; 00012 uint8_t loopvariable1=1,loopvariable2=0; 00013 int addr = slave_address; 00014 while(loopvariable1) 00015 { 00016 //-------------writing the command to slave-------------------------------------------------------------- 00017 master.frequency(100000); //set clock frequency 00018 master.start(); //initiating the data transfer 00019 acknowledge2=master.write(addr|0x00); //addressing the slave to write 00020 if(acknowledge2==1) //proceeding further only if slave is addressed 00021 { 00022 acknowledge1=master.write(command); //sending the command to slave 00023 if(acknowledge1==1) //proceeding further only if sent data is acknowledged 00024 { 00025 pc.printf("acknowledge1=%d\n",acknowledge1); 00026 loopvariable1=0; //if acknowledged, breaking loop in next iteration 00027 //--------------reading data from slave--------------------------------------------------------------- 00028 00029 master.frequency(100000); //set clock frequency 00030 master.start(); //initiate data transfer 00031 acknowledge1 = master.write(addr | 0x01); //addressing the slave to read 00032 00033 if(acknowledge1==1) //proceedong only if slae is addressed 00034 { 00035 while(loopvariable2<iterations) 00036 { 00037 if(loopvariable2%3==0) 00038 { 00039 Sensor.voltage[loopvariable2/3] = receive_byte(); //receiving data if acknowledged 00040 pc.printf(" voltage%d = %u\n",loopvariable2/3,Sensor.voltage[loopvariable2/3]); 00041 } 00042 else if(loopvariable2%3==1) 00043 { 00044 Sensor.current[loopvariable2/3] = receive_byte(); //receiving data if acknowledged 00045 pc.printf(" current%d = %u\n",loopvariable2/3, Sensor.current[loopvariable2/3]); 00046 } 00047 else if(loopvariable2%3==2) 00048 { 00049 Sensor.temp[loopvariable2/3] = receive_byte(); //receiving data if acknowledged 00050 pc.printf(" temperature%d = %u\n",loopvariable2/3,Sensor.temp[loopvariable2/3]); 00051 } 00052 loopvariable2++; 00053 00054 } //while(loopvariable2<30) 00055 }//if(acknowledge1==1) 00056 00057 master.stop(); 00058 }//if(acknowledge1==1) 00059 }//if(acknowledge2==1) 00060 }//while(loopvariable1) 00061 pc.printf("done"); 00062 printf("\nMaster exited\n"); 00063 }//main 00064 00065 00066 //----------------function to read and return the data received----------------------------------- 00067 char receive_byte() 00068 { 00069 00070 char value; 00071 value = master.read(1); 00072 return(value); //returning the 4 byte floating point number 00073 }
Generated on Fri Jul 15 2022 17:57:58 by
1.7.2
