for checking i2c with integrated bae
Dependencies: SDFileSystem mbed-rtos mbed
master.cpp@0:cb112a717428, 2014-09-15 (annotated)
- Committer:
- viswa_chaitanya
- Date:
- Mon Sep 15 14:01:49 2014 +0000
- Revision:
- 0:cb112a717428
cdms master(hk runs every 20s)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
viswa_chaitanya | 0:cb112a717428 | 1 | #include "master.h" |
viswa_chaitanya | 0:cb112a717428 | 2 | |
viswa_chaitanya | 0:cb112a717428 | 3 | I2C master (A4,A5); //configure pins p27,p28 as I2C master |
viswa_chaitanya | 0:cb112a717428 | 4 | Serial pc (USBTX,USBRX); |
viswa_chaitanya | 0:cb112a717428 | 5 | SensorData Sensor; |
viswa_chaitanya | 0:cb112a717428 | 6 | |
viswa_chaitanya | 0:cb112a717428 | 7 | void FUNC_I2C_MASTER_MAIN(int command, int slave_address, int iterations) |
viswa_chaitanya | 0:cb112a717428 | 8 | { |
viswa_chaitanya | 0:cb112a717428 | 9 | //wait(0.5); |
viswa_chaitanya | 0:cb112a717428 | 10 | printf("\nmaster entered\n"); |
viswa_chaitanya | 0:cb112a717428 | 11 | int acknowledge1; |
viswa_chaitanya | 0:cb112a717428 | 12 | int acknowledge2; |
viswa_chaitanya | 0:cb112a717428 | 13 | uint8_t loopvariable1=1,loopvariable2=0; |
viswa_chaitanya | 0:cb112a717428 | 14 | int addr = slave_address; |
viswa_chaitanya | 0:cb112a717428 | 15 | while(loopvariable1) |
viswa_chaitanya | 0:cb112a717428 | 16 | { |
viswa_chaitanya | 0:cb112a717428 | 17 | printf("\nmaster entered loop \n"); |
viswa_chaitanya | 0:cb112a717428 | 18 | //-------------writing the command to slave-------------------------------------------------------------- |
viswa_chaitanya | 0:cb112a717428 | 19 | master.frequency(100000); //set clock frequency |
viswa_chaitanya | 0:cb112a717428 | 20 | master.start(); //initiating the data transfer |
viswa_chaitanya | 0:cb112a717428 | 21 | acknowledge2=master.write(addr|0x00); //addressing the slave to write |
viswa_chaitanya | 0:cb112a717428 | 22 | if(acknowledge2==1) //proceeding further only if slave is addressed |
viswa_chaitanya | 0:cb112a717428 | 23 | { |
viswa_chaitanya | 0:cb112a717428 | 24 | // printf("\nmaster entered 1 \n"); |
viswa_chaitanya | 0:cb112a717428 | 25 | acknowledge1=master.write(command); //sending the command to slave |
viswa_chaitanya | 0:cb112a717428 | 26 | if(acknowledge1==1) //proceeding further only if sent data is acknowledged |
viswa_chaitanya | 0:cb112a717428 | 27 | { |
viswa_chaitanya | 0:cb112a717428 | 28 | pc.printf("acknowledge1=%d\n",acknowledge1); |
viswa_chaitanya | 0:cb112a717428 | 29 | loopvariable1=0; //if acknowledged, breaking loop in next iteration |
viswa_chaitanya | 0:cb112a717428 | 30 | //--------------reading data from slave--------------------------------------------------------------- |
viswa_chaitanya | 0:cb112a717428 | 31 | |
viswa_chaitanya | 0:cb112a717428 | 32 | master.frequency(100000); //set clock frequency |
viswa_chaitanya | 0:cb112a717428 | 33 | master.start(); //initiate data transfer |
viswa_chaitanya | 0:cb112a717428 | 34 | acknowledge1 = master.write(addr | 0x01); //addressing the slave to read |
viswa_chaitanya | 0:cb112a717428 | 35 | |
viswa_chaitanya | 0:cb112a717428 | 36 | if(acknowledge1==1) //proceedong only if slae is addressed |
viswa_chaitanya | 0:cb112a717428 | 37 | { |
viswa_chaitanya | 0:cb112a717428 | 38 | while(loopvariable2<iterations) |
viswa_chaitanya | 0:cb112a717428 | 39 | { |
viswa_chaitanya | 0:cb112a717428 | 40 | if(loopvariable2%3==0) |
viswa_chaitanya | 0:cb112a717428 | 41 | { |
viswa_chaitanya | 0:cb112a717428 | 42 | Sensor.voltage[loopvariable2/3] = receive_byte(); //receiving data if acknowledged |
viswa_chaitanya | 0:cb112a717428 | 43 | pc.printf(" voltage%d = %u\n",loopvariable2/3,Sensor.voltage[loopvariable2/3]); |
viswa_chaitanya | 0:cb112a717428 | 44 | } |
viswa_chaitanya | 0:cb112a717428 | 45 | else if(loopvariable2%3==1) |
viswa_chaitanya | 0:cb112a717428 | 46 | { |
viswa_chaitanya | 0:cb112a717428 | 47 | Sensor.current[loopvariable2/3] = receive_byte(); //receiving data if acknowledged |
viswa_chaitanya | 0:cb112a717428 | 48 | pc.printf(" current%d = %u\n",loopvariable2/3, Sensor.current[loopvariable2/3]); |
viswa_chaitanya | 0:cb112a717428 | 49 | } |
viswa_chaitanya | 0:cb112a717428 | 50 | else if(loopvariable2%3==2) |
viswa_chaitanya | 0:cb112a717428 | 51 | { |
viswa_chaitanya | 0:cb112a717428 | 52 | Sensor.temp[loopvariable2/3] = receive_byte(); //receiving data if acknowledged |
viswa_chaitanya | 0:cb112a717428 | 53 | pc.printf(" temperature%d = %u\n",loopvariable2/3,Sensor.temp[loopvariable2/3]); |
viswa_chaitanya | 0:cb112a717428 | 54 | } |
viswa_chaitanya | 0:cb112a717428 | 55 | loopvariable2++; |
viswa_chaitanya | 0:cb112a717428 | 56 | |
viswa_chaitanya | 0:cb112a717428 | 57 | } //while(loopvariable2<30) |
viswa_chaitanya | 0:cb112a717428 | 58 | }//if(acknowledge1==1) |
viswa_chaitanya | 0:cb112a717428 | 59 | |
viswa_chaitanya | 0:cb112a717428 | 60 | master.stop(); |
viswa_chaitanya | 0:cb112a717428 | 61 | }//if(acknowledge1==1) |
viswa_chaitanya | 0:cb112a717428 | 62 | }//if(acknowledge2==1) |
viswa_chaitanya | 0:cb112a717428 | 63 | }//while(loopvariable1) |
viswa_chaitanya | 0:cb112a717428 | 64 | pc.printf("done"); |
viswa_chaitanya | 0:cb112a717428 | 65 | printf("\nMaster exited\n"); |
viswa_chaitanya | 0:cb112a717428 | 66 | }//main |
viswa_chaitanya | 0:cb112a717428 | 67 | |
viswa_chaitanya | 0:cb112a717428 | 68 | |
viswa_chaitanya | 0:cb112a717428 | 69 | //----------------function to read and return the data received----------------------------------- |
viswa_chaitanya | 0:cb112a717428 | 70 | char receive_byte() |
viswa_chaitanya | 0:cb112a717428 | 71 | { |
viswa_chaitanya | 0:cb112a717428 | 72 | |
viswa_chaitanya | 0:cb112a717428 | 73 | char value; |
viswa_chaitanya | 0:cb112a717428 | 74 | value = master.read(1); |
viswa_chaitanya | 0:cb112a717428 | 75 | return(value); //returning the 4 byte floating point number |
viswa_chaitanya | 0:cb112a717428 | 76 | } |