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