green rosh
/
RTOS_BAE_IITMSAT
slave.cpp@3:307c56629df0, 2014-07-09 (annotated)
- Committer:
- greenroshks
- Date:
- Wed Jul 09 09:03:11 2014 +0000
- Revision:
- 3:307c56629df0
- Parent:
- 2:1792c9cda669
updated with pranoy's hk
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
greenroshks | 2:1792c9cda669 | 1 | #include "slave.h" |
greenroshks | 2:1792c9cda669 | 2 | #include "HK.h" |
greenroshks | 2:1792c9cda669 | 3 | |
greenroshks | 2:1792c9cda669 | 4 | extern SensorData Sensor; |
greenroshks | 2:1792c9cda669 | 5 | I2CSlave slave(p28,p27); //configuring pins p27, p28 as I2Cslave |
greenroshks | 2:1792c9cda669 | 6 | Serial screen (USBTX,USBRX); |
greenroshks | 2:1792c9cda669 | 7 | void write_to_master(char send) //function to write data to master |
greenroshks | 2:1792c9cda669 | 8 | { |
greenroshks | 2:1792c9cda669 | 9 | int acknowledge; |
greenroshks | 2:1792c9cda669 | 10 | acknowledge = slave.write(send); //sending the byte to master |
greenroshks | 2:1792c9cda669 | 11 | if(acknowledge==1) |
greenroshks | 2:1792c9cda669 | 12 | { |
greenroshks | 2:1792c9cda669 | 13 | screen.printf(" acknowledge %d sent %x \n",acknowledge,send); |
greenroshks | 2:1792c9cda669 | 14 | } |
greenroshks | 2:1792c9cda669 | 15 | } |
greenroshks | 2:1792c9cda669 | 16 | |
greenroshks | 2:1792c9cda669 | 17 | void split(float data) //function to split data into 4 individual bytes and send to master |
greenroshks | 2:1792c9cda669 | 18 | { |
greenroshks | 2:1792c9cda669 | 19 | union convert |
greenroshks | 2:1792c9cda669 | 20 | { |
greenroshks | 2:1792c9cda669 | 21 | char byte[4]; //array containing individual bytes |
greenroshks | 2:1792c9cda669 | 22 | float number; //floating point number to be split into four bytes |
greenroshks | 2:1792c9cda669 | 23 | }v; |
greenroshks | 2:1792c9cda669 | 24 | v.number=data; |
greenroshks | 2:1792c9cda669 | 25 | write_to_master(v.byte[0]); |
greenroshks | 2:1792c9cda669 | 26 | write_to_master(v.byte[1]); |
greenroshks | 2:1792c9cda669 | 27 | write_to_master(v.byte[2]); |
greenroshks | 2:1792c9cda669 | 28 | write_to_master(v.byte[3]); |
greenroshks | 2:1792c9cda669 | 29 | screen.printf("sent %f\n",v.number); |
greenroshks | 2:1792c9cda669 | 30 | } |
greenroshks | 2:1792c9cda669 | 31 | |
greenroshks | 2:1792c9cda669 | 32 | |
greenroshks | 2:1792c9cda669 | 33 | |
greenroshks | 2:1792c9cda669 | 34 | void FUNC_I2C_SLAVE_MAIN() |
greenroshks | 2:1792c9cda669 | 35 | { |
greenroshks | 2:1792c9cda669 | 36 | wait(0.5); |
greenroshks | 2:1792c9cda669 | 37 | screen.printf("\nSlave entered\n"); |
greenroshks | 2:1792c9cda669 | 38 | slave.address(0x20); //assigning slave address |
greenroshks | 2:1792c9cda669 | 39 | int Switch_Variable; |
greenroshks | 2:1792c9cda669 | 40 | int ReadAddressed=1; |
greenroshks | 2:1792c9cda669 | 41 | int WriteGeneral=3; |
greenroshks | 2:1792c9cda669 | 42 | int loopvariable1=1; |
greenroshks | 2:1792c9cda669 | 43 | int loopvariable2=0; |
greenroshks | 2:1792c9cda669 | 44 | //initialising dummy sensor data |
greenroshks | 2:1792c9cda669 | 45 | |
greenroshks | 2:1792c9cda669 | 46 | while(loopvariable1) |
greenroshks | 2:1792c9cda669 | 47 | { |
greenroshks | 2:1792c9cda669 | 48 | //to read data from master |
greenroshks | 2:1792c9cda669 | 49 | if(slave.receive()==WriteGeneral) //checking if slave is addressed to write |
greenroshks | 2:1792c9cda669 | 50 | { |
greenroshks | 2:1792c9cda669 | 51 | Switch_Variable=slave.read(); //receiving data |
greenroshks | 2:1792c9cda669 | 52 | screen.printf("switch variable=%d\n",Switch_Variable); |
greenroshks | 2:1792c9cda669 | 53 | slave.stop(); //reset slave to default receiving state |
greenroshks | 2:1792c9cda669 | 54 | loopvariable1=0; |
greenroshks | 2:1792c9cda669 | 55 | //to interpret and write data to master |
greenroshks | 2:1792c9cda669 | 56 | switch(Switch_Variable) |
greenroshks | 2:1792c9cda669 | 57 | { |
greenroshks | 2:1792c9cda669 | 58 | |
greenroshks | 2:1792c9cda669 | 59 | case 1: while(loopvariable2<30) |
greenroshks | 2:1792c9cda669 | 60 | { |
greenroshks | 2:1792c9cda669 | 61 | if(slave.receive()==ReadAddressed) //check if slave is addressed to read |
greenroshks | 2:1792c9cda669 | 62 | { |
greenroshks | 2:1792c9cda669 | 63 | if(loopvariable2%3==0) |
greenroshks | 2:1792c9cda669 | 64 | { |
greenroshks | 2:1792c9cda669 | 65 | screen.printf("\nvoltage%d\n",loopvariable2/3); |
greenroshks | 2:1792c9cda669 | 66 | split(Sensor.Voltage[loopvariable2/3]); |
greenroshks | 2:1792c9cda669 | 67 | } |
greenroshks | 2:1792c9cda669 | 68 | else if(loopvariable2%3==1) |
greenroshks | 2:1792c9cda669 | 69 | { |
greenroshks | 2:1792c9cda669 | 70 | screen.printf("\ncurrent%d\n",loopvariable2/3); |
greenroshks | 2:1792c9cda669 | 71 | split(Sensor.Current[loopvariable2/3]); |
greenroshks | 2:1792c9cda669 | 72 | } |
greenroshks | 2:1792c9cda669 | 73 | else if(loopvariable2%3==2) |
greenroshks | 2:1792c9cda669 | 74 | { |
greenroshks | 2:1792c9cda669 | 75 | screen.printf("\ntemp%d\n",loopvariable2/3); |
greenroshks | 3:307c56629df0 | 76 | split(Sensor.Temperature[loopvariable2/3]); |
greenroshks | 2:1792c9cda669 | 77 | } |
greenroshks | 2:1792c9cda669 | 78 | loopvariable2++; |
greenroshks | 2:1792c9cda669 | 79 | slave.stop(); |
greenroshks | 2:1792c9cda669 | 80 | } |
greenroshks | 2:1792c9cda669 | 81 | } |
greenroshks | 2:1792c9cda669 | 82 | break; |
greenroshks | 2:1792c9cda669 | 83 | case 2 : screen.printf(" telecommand 2\n"); |
greenroshks | 2:1792c9cda669 | 84 | break; |
greenroshks | 2:1792c9cda669 | 85 | |
greenroshks | 2:1792c9cda669 | 86 | }//switch case ends |
greenroshks | 2:1792c9cda669 | 87 | } |
greenroshks | 2:1792c9cda669 | 88 | } |
greenroshks | 2:1792c9cda669 | 89 | screen.printf("done"); |
greenroshks | 2:1792c9cda669 | 90 | } |