![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
freemaster1.0
master.cpp@0:a39f49a913e6, 2014-12-03 (annotated)
- Committer:
- viswachaitanya
- Date:
- Wed Dec 03 10:36:09 2014 +0000
- Revision:
- 0:a39f49a913e6
pre 12/2014 testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
viswachaitanya | 0:a39f49a913e6 | 1 | #include "master.h" |
viswachaitanya | 0:a39f49a913e6 | 2 | |
viswachaitanya | 0:a39f49a913e6 | 3 | I2C master (p28,p27); //configure pins p27,p28 as I2C master |
viswachaitanya | 0:a39f49a913e6 | 4 | Serial pc (USBTX,USBRX); |
viswachaitanya | 0:a39f49a913e6 | 5 | |
viswachaitanya | 0:a39f49a913e6 | 6 | struct SensorData //HK_data_structure |
viswachaitanya | 0:a39f49a913e6 | 7 | { |
viswachaitanya | 0:a39f49a913e6 | 8 | char voltage[10]; |
viswachaitanya | 0:a39f49a913e6 | 9 | char current[10]; |
viswachaitanya | 0:a39f49a913e6 | 10 | char temp[10]; |
viswachaitanya | 0:a39f49a913e6 | 11 | } Sensor; |
viswachaitanya | 0:a39f49a913e6 | 12 | |
viswachaitanya | 0:a39f49a913e6 | 13 | void master_main() |
viswachaitanya | 0:a39f49a913e6 | 14 | { |
viswachaitanya | 0:a39f49a913e6 | 15 | wait(0.5); |
viswachaitanya | 0:a39f49a913e6 | 16 | printf("1\n"); |
viswachaitanya | 0:a39f49a913e6 | 17 | bool acknowledge1; |
viswachaitanya | 0:a39f49a913e6 | 18 | bool acknowledge2; |
viswachaitanya | 0:a39f49a913e6 | 19 | uint8_t loopvariable2=0; |
viswachaitanya | 0:a39f49a913e6 | 20 | bool loopvariable1 = true; |
viswachaitanya | 0:a39f49a913e6 | 21 | bool loopvariable3 = true; |
viswachaitanya | 0:a39f49a913e6 | 22 | while(loopvariable1) |
viswachaitanya | 0:a39f49a913e6 | 23 | { |
viswachaitanya | 0:a39f49a913e6 | 24 | //-------------writing the command to slave-------------------------------------------------------------- |
viswachaitanya | 0:a39f49a913e6 | 25 | printf("2\n"); |
viswachaitanya | 0:a39f49a913e6 | 26 | master.frequency(100000); //set clock frequency |
viswachaitanya | 0:a39f49a913e6 | 27 | master.start(); //initiating the data transfer |
viswachaitanya | 0:a39f49a913e6 | 28 | acknowledge2 = (bool) master.write(addr|0x00); //addressing the slave to write |
viswachaitanya | 0:a39f49a913e6 | 29 | if(acknowledge2) //proceeding further only if slave is addressed |
viswachaitanya | 0:a39f49a913e6 | 30 | { |
viswachaitanya | 0:a39f49a913e6 | 31 | printf("3\n"); |
viswachaitanya | 0:a39f49a913e6 | 32 | acknowledge1 = (bool) master.write(get_hk_data); //sending the command to slave |
viswachaitanya | 0:a39f49a913e6 | 33 | if(acknowledge1) //proceeding further only if sent data is acknowledged |
viswachaitanya | 0:a39f49a913e6 | 34 | { |
viswachaitanya | 0:a39f49a913e6 | 35 | printf("acknowledge1=%d\n",acknowledge1); |
viswachaitanya | 0:a39f49a913e6 | 36 | loopvariable1=false; //if acknowledged, breaking loop in next iteration |
viswachaitanya | 0:a39f49a913e6 | 37 | |
viswachaitanya | 0:a39f49a913e6 | 38 | //--------------reading data from slave--------------------------------------------------------------- |
viswachaitanya | 0:a39f49a913e6 | 39 | while(loopvariable3) |
viswachaitanya | 0:a39f49a913e6 | 40 | { |
viswachaitanya | 0:a39f49a913e6 | 41 | master.frequency(100000); //set clock frequency |
viswachaitanya | 0:a39f49a913e6 | 42 | master.start(); //initiate data transfer |
viswachaitanya | 0:a39f49a913e6 | 43 | acknowledge1 = (bool) master.write(addr | 0x01); //addressing the slave to read |
viswachaitanya | 0:a39f49a913e6 | 44 | |
viswachaitanya | 0:a39f49a913e6 | 45 | if(acknowledge1) //proceedong only if slae is addressed |
viswachaitanya | 0:a39f49a913e6 | 46 | { |
viswachaitanya | 0:a39f49a913e6 | 47 | loopvariable3 = false; |
viswachaitanya | 0:a39f49a913e6 | 48 | while(loopvariable2<8) |
viswachaitanya | 0:a39f49a913e6 | 49 | { |
viswachaitanya | 0:a39f49a913e6 | 50 | Sensor.voltage[loopvariable2] = receive_byte(); //receiving data if acknowledged |
viswachaitanya | 0:a39f49a913e6 | 51 | printf(" voltage%d = %x\n",loopvariable2,Sensor.voltage[loopvariable2]); |
viswachaitanya | 0:a39f49a913e6 | 52 | |
viswachaitanya | 0:a39f49a913e6 | 53 | Sensor.current[loopvariable2] = receive_byte(); //receiving data if acknowledged |
viswachaitanya | 0:a39f49a913e6 | 54 | printf(" current%d = %x\n",loopvariable2, Sensor.current[loopvariable2]); |
viswachaitanya | 0:a39f49a913e6 | 55 | |
viswachaitanya | 0:a39f49a913e6 | 56 | Sensor.temp[loopvariable2] = receive_byte(); //receiving data if acknowledged |
viswachaitanya | 0:a39f49a913e6 | 57 | printf(" temperature%d = %x\n",loopvariable2,Sensor.temp[loopvariable2]); |
viswachaitanya | 0:a39f49a913e6 | 58 | |
viswachaitanya | 0:a39f49a913e6 | 59 | loopvariable2++; |
viswachaitanya | 0:a39f49a913e6 | 60 | |
viswachaitanya | 0:a39f49a913e6 | 61 | } //while(loopvariable2<30) |
viswachaitanya | 0:a39f49a913e6 | 62 | }//if(acknowledge1==1) |
viswachaitanya | 0:a39f49a913e6 | 63 | |
viswachaitanya | 0:a39f49a913e6 | 64 | master.stop(); |
viswachaitanya | 0:a39f49a913e6 | 65 | }//while(loopvariable3) |
viswachaitanya | 0:a39f49a913e6 | 66 | }//if(acknowledge1==1) |
viswachaitanya | 0:a39f49a913e6 | 67 | }//if(acknowledge2==1) |
viswachaitanya | 0:a39f49a913e6 | 68 | }//while(loopvariable1) |
viswachaitanya | 0:a39f49a913e6 | 69 | printf("done"); |
viswachaitanya | 0:a39f49a913e6 | 70 | }//main |
viswachaitanya | 0:a39f49a913e6 | 71 | |
viswachaitanya | 0:a39f49a913e6 | 72 | |
viswachaitanya | 0:a39f49a913e6 | 73 | //----------------function to read and return the data received----------------------------------- |
viswachaitanya | 0:a39f49a913e6 | 74 | char receive_byte() |
viswachaitanya | 0:a39f49a913e6 | 75 | { |
viswachaitanya | 0:a39f49a913e6 | 76 | |
viswachaitanya | 0:a39f49a913e6 | 77 | char value; |
viswachaitanya | 0:a39f49a913e6 | 78 | value = master.read(1); |
viswachaitanya | 0:a39f49a913e6 | 79 | return(value); //returning the 4 byte floating point number |
viswachaitanya | 0:a39f49a913e6 | 80 | } |
viswachaitanya | 0:a39f49a913e6 | 81 | |
viswachaitanya | 0:a39f49a913e6 | 82 | |
viswachaitanya | 0:a39f49a913e6 | 83 |