I2C master

Dependencies:   mbed mbed-rtos

Fork of i2c_master by Chaitanya Viswa

Committer:
viswachaitanya
Date:
Mon Jun 30 11:55:18 2014 +0000
Revision:
0:7811fb22b3de
Child:
1:14b369c0f314
m.h version1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
viswachaitanya 0:7811fb22b3de 1 #include "master.h"
viswachaitanya 0:7811fb22b3de 2
viswachaitanya 0:7811fb22b3de 3 float receive_byte() //function to read and print the data received
viswachaitanya 0:7811fb22b3de 4 {
viswachaitanya 0:7811fb22b3de 5 union join
viswachaitanya 0:7811fb22b3de 6 {
viswachaitanya 0:7811fb22b3de 7 char byte[4]; //array containing individual bytes
viswachaitanya 0:7811fb22b3de 8 float number; //floating point number to be appended
viswachaitanya 0:7811fb22b3de 9 }append;
viswachaitanya 0:7811fb22b3de 10 float value;
viswachaitanya 0:7811fb22b3de 11 append.byte[0]=master.read(1); //reading individual bytes and storing it in array
viswachaitanya 0:7811fb22b3de 12 pc.printf("\n(1) %x\n",append.byte[0]);
viswachaitanya 0:7811fb22b3de 13 append.byte[1]=master.read(1);
viswachaitanya 0:7811fb22b3de 14 pc.printf("(2) %x\n",append.byte[1]);
viswachaitanya 0:7811fb22b3de 15 append.byte[2]=master.read(1);
viswachaitanya 0:7811fb22b3de 16 pc.printf("(3) %x\n",append.byte[2]);
viswachaitanya 0:7811fb22b3de 17 append.byte[3]=master.read(1);
viswachaitanya 0:7811fb22b3de 18 pc.printf("(4) %x\n",append.byte[3]);
viswachaitanya 0:7811fb22b3de 19 value = append.number;
viswachaitanya 0:7811fb22b3de 20 return(value); //returning the 4 byte floating point number
viswachaitanya 0:7811fb22b3de 21 }
viswachaitanya 0:7811fb22b3de 22
viswachaitanya 0:7811fb22b3de 23 void FUNC_I2C_MASTER_MAIN()
viswachaitanya 0:7811fb22b3de 24 {
viswachaitanya 0:7811fb22b3de 25 int acknowledge;
viswachaitanya 0:7811fb22b3de 26 int loopvariable1=1,loopvariable2=0;
viswachaitanya 0:7811fb22b3de 27 while(loopvariable1)
viswachaitanya 0:7811fb22b3de 28 {
viswachaitanya 0:7811fb22b3de 29 //writing the command to slave
viswachaitanya 0:7811fb22b3de 30 master.frequency(100000); //set clock frequency
viswachaitanya 0:7811fb22b3de 31 master.start(); //initiating the data transfer
viswachaitanya 0:7811fb22b3de 32 master.write(addr|0x00); //addressing the slave to write
viswachaitanya 0:7811fb22b3de 33 acknowledge=master.write(get_hk_data); //sending the command to slave
viswachaitanya 0:7811fb22b3de 34 if(acknowledge==1)
viswachaitanya 0:7811fb22b3de 35 {
viswachaitanya 0:7811fb22b3de 36 pc.printf("acknowledge=%d\n",acknowledge);
viswachaitanya 0:7811fb22b3de 37 loopvariable1=0; //if acknowledged, breaking loop in next iteration
viswachaitanya 0:7811fb22b3de 38 //reading 4 byte data from slave
viswachaitanya 0:7811fb22b3de 39 while(loopvariable2<30)
viswachaitanya 0:7811fb22b3de 40 {
viswachaitanya 0:7811fb22b3de 41 master.frequency(100000); //set clock frequency
viswachaitanya 0:7811fb22b3de 42 master.start(); //initiate data transfer
viswachaitanya 0:7811fb22b3de 43 acknowledge = master.write(addr | 0x01); //addressing the slave to read
viswachaitanya 0:7811fb22b3de 44 if(acknowledge==1)
viswachaitanya 0:7811fb22b3de 45 {
viswachaitanya 0:7811fb22b3de 46 //pc.printf("enter\n");
viswachaitanya 0:7811fb22b3de 47 if(loopvariable2%3==0)
viswachaitanya 0:7811fb22b3de 48 {
viswachaitanya 0:7811fb22b3de 49 Sensor.voltage[loopvariable2/3] = receive_byte(); //receiving data if acknowledged
viswachaitanya 0:7811fb22b3de 50 pc.printf(" voltage%d = %f\n",loopvariable2/3,Sensor.voltage[loopvariable2/3]);
viswachaitanya 0:7811fb22b3de 51 }
viswachaitanya 0:7811fb22b3de 52 else if(loopvariable2%3==1)
viswachaitanya 0:7811fb22b3de 53 {
viswachaitanya 0:7811fb22b3de 54 Sensor.current[loopvariable2/3] = receive_byte(); //receiving data if acknowledged
viswachaitanya 0:7811fb22b3de 55 pc.printf(" current%d = %f\n",loopvariable2/3, Sensor.current[loopvariable2/3]);
viswachaitanya 0:7811fb22b3de 56 }
viswachaitanya 0:7811fb22b3de 57 else if(loopvariable2%3==2)
viswachaitanya 0:7811fb22b3de 58 {
viswachaitanya 0:7811fb22b3de 59 Sensor.temp[loopvariable2/3] = receive_byte(); //receiving data if acknowledged
viswachaitanya 0:7811fb22b3de 60 pc.printf(" temperature%d = %f\n",loopvariable2/3,Sensor.temp[loopvariable2/3]);
viswachaitanya 0:7811fb22b3de 61 }
viswachaitanya 0:7811fb22b3de 62
viswachaitanya 0:7811fb22b3de 63 loopvariable2++;
viswachaitanya 0:7811fb22b3de 64 }//if(acknowledge==1)
viswachaitanya 0:7811fb22b3de 65 } //while(loopvariable2<30)
viswachaitanya 0:7811fb22b3de 66 master.stop();
viswachaitanya 0:7811fb22b3de 67 }//if(acknowledge==1)
viswachaitanya 0:7811fb22b3de 68 }//while(loopvariable1)
viswachaitanya 0:7811fb22b3de 69 pc.printf("done");
viswachaitanya 0:7811fb22b3de 70 }//main