green rosh
/
BAE_vr2_1_1
The one with the new HK
Fork of BAE_vr2_1_1 by
slave.cpp@1:b4bb78a9b92e, 2014-12-02 (annotated)
- Committer:
- greenroshks
- Date:
- Tue Dec 02 04:38:19 2014 +0000
- Revision:
- 1:b4bb78a9b92e
- Parent:
- 0:8b0d43fe6c05
- Child:
- 3:02b45de29c0f
BAE FRDM final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
greenroshks | 0:8b0d43fe6c05 | 1 | #include "slave.h" |
greenroshks | 0:8b0d43fe6c05 | 2 | #include "HK.h" |
greenroshks | 0:8b0d43fe6c05 | 3 | |
greenroshks | 0:8b0d43fe6c05 | 4 | extern struct SensorData Sensor; |
greenroshks | 1:b4bb78a9b92e | 5 | I2CSlave slave(PTC9,PTC8); //configuring pins p27, p28 as I2Cslave |
greenroshks | 0:8b0d43fe6c05 | 6 | Serial screen (USBTX,USBRX); |
greenroshks | 0:8b0d43fe6c05 | 7 | void write_to_master(char send) //function to write data to master |
greenroshks | 0:8b0d43fe6c05 | 8 | { |
greenroshks | 0:8b0d43fe6c05 | 9 | int acknowledge; |
greenroshks | 0:8b0d43fe6c05 | 10 | int loopvariable4=1; |
greenroshks | 0:8b0d43fe6c05 | 11 | while(loopvariable4) |
greenroshks | 0:8b0d43fe6c05 | 12 | { |
greenroshks | 0:8b0d43fe6c05 | 13 | acknowledge = slave.write(send); //sending the byte to master |
greenroshks | 0:8b0d43fe6c05 | 14 | if(acknowledge==1) |
greenroshks | 0:8b0d43fe6c05 | 15 | { |
greenroshks | 0:8b0d43fe6c05 | 16 | screen.printf(" acknowledge %d sent %u \n",acknowledge,send); |
greenroshks | 0:8b0d43fe6c05 | 17 | loopvariable4=0; |
greenroshks | 0:8b0d43fe6c05 | 18 | } |
greenroshks | 0:8b0d43fe6c05 | 19 | } |
greenroshks | 0:8b0d43fe6c05 | 20 | } |
greenroshks | 0:8b0d43fe6c05 | 21 | |
greenroshks | 0:8b0d43fe6c05 | 22 | |
greenroshks | 0:8b0d43fe6c05 | 23 | |
greenroshks | 0:8b0d43fe6c05 | 24 | |
greenroshks | 0:8b0d43fe6c05 | 25 | void FUNC_I2C_SLAVE_MAIN(int iterations) |
greenroshks | 0:8b0d43fe6c05 | 26 | { |
greenroshks | 0:8b0d43fe6c05 | 27 | |
greenroshks | 0:8b0d43fe6c05 | 28 | screen.printf("\nSlave entered\n"); |
greenroshks | 0:8b0d43fe6c05 | 29 | slave.address(slave_address); //assigning slave address |
greenroshks | 0:8b0d43fe6c05 | 30 | slave.stop(); |
greenroshks | 0:8b0d43fe6c05 | 31 | char Switch_Variable; |
greenroshks | 0:8b0d43fe6c05 | 32 | |
greenroshks | 0:8b0d43fe6c05 | 33 | |
greenroshks | 0:8b0d43fe6c05 | 34 | int loopvariable1=1; |
greenroshks | 0:8b0d43fe6c05 | 35 | int loopvariable2=0; |
greenroshks | 0:8b0d43fe6c05 | 36 | int loopvariable3=1; |
greenroshks | 0:8b0d43fe6c05 | 37 | //initialising dummy sensor data |
greenroshks | 0:8b0d43fe6c05 | 38 | |
greenroshks | 0:8b0d43fe6c05 | 39 | while(loopvariable1) |
greenroshks | 0:8b0d43fe6c05 | 40 | { |
greenroshks | 0:8b0d43fe6c05 | 41 | //to read data from master |
greenroshks | 0:8b0d43fe6c05 | 42 | if(slave.receive()==WriteGeneral) //checking if slave is addressed to write |
greenroshks | 0:8b0d43fe6c05 | 43 | { |
greenroshks | 1:b4bb78a9b92e | 44 | slave.stop(); |
greenroshks | 0:8b0d43fe6c05 | 45 | Switch_Variable=slave.read(); //receiving data |
greenroshks | 0:8b0d43fe6c05 | 46 | screen.printf("switch variable=%d\n",Switch_Variable); |
greenroshks | 0:8b0d43fe6c05 | 47 | slave.stop(); //reset slave to default receiving state |
greenroshks | 0:8b0d43fe6c05 | 48 | loopvariable1=0; |
greenroshks | 0:8b0d43fe6c05 | 49 | //to interpret and write data to master |
greenroshks | 0:8b0d43fe6c05 | 50 | switch(Switch_Variable) |
greenroshks | 0:8b0d43fe6c05 | 51 | { |
greenroshks | 0:8b0d43fe6c05 | 52 | |
greenroshks | 0:8b0d43fe6c05 | 53 | |
greenroshks | 0:8b0d43fe6c05 | 54 | case 1: printf("\nEntered switch\n"); |
greenroshks | 0:8b0d43fe6c05 | 55 | while(loopvariable3) |
greenroshks | 0:8b0d43fe6c05 | 56 | { |
greenroshks | 0:8b0d43fe6c05 | 57 | if(slave.receive()==ReadAddressed) //check if slave is addressed to read |
greenroshks | 0:8b0d43fe6c05 | 58 | { |
greenroshks | 0:8b0d43fe6c05 | 59 | while(loopvariable2<iterations) |
greenroshks | 0:8b0d43fe6c05 | 60 | { |
greenroshks | 0:8b0d43fe6c05 | 61 | if(loopvariable2%3==0) |
greenroshks | 0:8b0d43fe6c05 | 62 | { |
greenroshks | 0:8b0d43fe6c05 | 63 | screen.printf("\nvoltage%d\n",loopvariable2/3); |
greenroshks | 0:8b0d43fe6c05 | 64 | write_to_master(Sensor.Voltage[loopvariable2/3]); |
greenroshks | 0:8b0d43fe6c05 | 65 | } |
greenroshks | 0:8b0d43fe6c05 | 66 | else if(loopvariable2%3==1) |
greenroshks | 0:8b0d43fe6c05 | 67 | { |
greenroshks | 0:8b0d43fe6c05 | 68 | screen.printf("\ncurrent%d\n",loopvariable2/3); |
greenroshks | 0:8b0d43fe6c05 | 69 | write_to_master(Sensor.Current[loopvariable2/3]); |
greenroshks | 0:8b0d43fe6c05 | 70 | } |
greenroshks | 0:8b0d43fe6c05 | 71 | else if(loopvariable2%3==2) |
greenroshks | 0:8b0d43fe6c05 | 72 | { |
greenroshks | 0:8b0d43fe6c05 | 73 | screen.printf("\ntemp%d\n",loopvariable2/3); |
greenroshks | 0:8b0d43fe6c05 | 74 | write_to_master(Sensor.Temperature[loopvariable2/3]); |
greenroshks | 0:8b0d43fe6c05 | 75 | } |
greenroshks | 0:8b0d43fe6c05 | 76 | loopvariable2++; |
greenroshks | 0:8b0d43fe6c05 | 77 | }//while(loopvariable2<30) |
greenroshks | 0:8b0d43fe6c05 | 78 | printf("\nExited i2c while loop\n"); |
greenroshks | 0:8b0d43fe6c05 | 79 | slave.stop(); |
greenroshks | 0:8b0d43fe6c05 | 80 | loopvariable3=0; |
greenroshks | 0:8b0d43fe6c05 | 81 | }//if(read addressed) |
greenroshks | 0:8b0d43fe6c05 | 82 | |
greenroshks | 0:8b0d43fe6c05 | 83 | }//while(loopvariable3) |
greenroshks | 0:8b0d43fe6c05 | 84 | |
greenroshks | 0:8b0d43fe6c05 | 85 | break; |
greenroshks | 0:8b0d43fe6c05 | 86 | case 2 : screen.printf(" telecommand 2\n"); |
greenroshks | 0:8b0d43fe6c05 | 87 | break; |
greenroshks | 0:8b0d43fe6c05 | 88 | |
greenroshks | 0:8b0d43fe6c05 | 89 | }//switch case ends |
greenroshks | 0:8b0d43fe6c05 | 90 | } |
greenroshks | 0:8b0d43fe6c05 | 91 | } |
greenroshks | 0:8b0d43fe6c05 | 92 | screen.printf("\nexited slave function\n"); |
greenroshks | 0:8b0d43fe6c05 | 93 | } |
greenroshks | 0:8b0d43fe6c05 | 94 |