Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of all_combined_week6 by
slave.cpp@4:79b97f9cab00, 2014-07-12 (annotated)
- Committer:
- greenroshks
- Date:
- Sat Jul 12 07:26:06 2014 +0000
- Revision:
- 4:79b97f9cab00
- Parent:
- 3:9b597ed04ef4
- Child:
- 6:82153349cc9b
Version (may be) 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 | 3:9b597ed04ef4 | 16 | screen.printf(" acknowledge %d sent %x \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 | { |
| harshit_felicity | 0:cbe0ea884289 | 27 | wait(0.5); |
| 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 | { |
| harshit_felicity | 0:cbe0ea884289 | 50 | |
| greenroshks | 3:9b597ed04ef4 | 51 | case '1': while(loopvariable3) |
| greenroshks | 3:9b597ed04ef4 | 52 | { |
| harshit_felicity | 0:cbe0ea884289 | 53 | if(slave.receive()==ReadAddressed) //check if slave is addressed to read |
| harshit_felicity | 0:cbe0ea884289 | 54 | { |
| greenroshks | 3:9b597ed04ef4 | 55 | while(loopvariable2<iterations) |
| greenroshks | 3:9b597ed04ef4 | 56 | { |
| harshit_felicity | 0:cbe0ea884289 | 57 | if(loopvariable2%3==0) |
| harshit_felicity | 0:cbe0ea884289 | 58 | { |
| greenroshks | 3:9b597ed04ef4 | 59 | screen.printf("\nvoltage%d\n",loopvariable2/3); |
| greenroshks | 3:9b597ed04ef4 | 60 | write_to_master(Sensor.Voltage[loopvariable2/3]); |
| harshit_felicity | 0:cbe0ea884289 | 61 | } |
| harshit_felicity | 0:cbe0ea884289 | 62 | else if(loopvariable2%3==1) |
| harshit_felicity | 0:cbe0ea884289 | 63 | { |
| greenroshks | 3:9b597ed04ef4 | 64 | screen.printf("\ncurrent%d\n",loopvariable2/3); |
| greenroshks | 3:9b597ed04ef4 | 65 | write_to_master(Sensor.Current[loopvariable2/3]); |
| harshit_felicity | 0:cbe0ea884289 | 66 | } |
| harshit_felicity | 0:cbe0ea884289 | 67 | else if(loopvariable2%3==2) |
| harshit_felicity | 0:cbe0ea884289 | 68 | { |
| greenroshks | 3:9b597ed04ef4 | 69 | screen.printf("\ntemp%d\n",loopvariable2/3); |
| greenroshks | 3:9b597ed04ef4 | 70 | write_to_master(Sensor.Temperature[loopvariable2/3]); |
| harshit_felicity | 0:cbe0ea884289 | 71 | } |
| harshit_felicity | 0:cbe0ea884289 | 72 | loopvariable2++; |
| greenroshks | 3:9b597ed04ef4 | 73 | }//while(loopvariable2<30) |
| greenroshks | 3:9b597ed04ef4 | 74 | slave.stop(); |
| greenroshks | 3:9b597ed04ef4 | 75 | loopvariable3=0; |
| greenroshks | 3:9b597ed04ef4 | 76 | }//if(read addressed) |
| greenroshks | 3:9b597ed04ef4 | 77 | |
| greenroshks | 3:9b597ed04ef4 | 78 | }//while(loopvariable3) |
| greenroshks | 3:9b597ed04ef4 | 79 | |
| harshit_felicity | 0:cbe0ea884289 | 80 | break; |
| greenroshks | 3:9b597ed04ef4 | 81 | case '2' : screen.printf(" telecommand 2\n"); |
| harshit_felicity | 0:cbe0ea884289 | 82 | break; |
| harshit_felicity | 0:cbe0ea884289 | 83 | |
| harshit_felicity | 0:cbe0ea884289 | 84 | }//switch case ends |
| harshit_felicity | 0:cbe0ea884289 | 85 | } |
| harshit_felicity | 0:cbe0ea884289 | 86 | } |
| greenroshks | 3:9b597ed04ef4 | 87 | screen.printf("done"); |
| harshit_felicity | 0:cbe0ea884289 | 88 | } |
| greenroshks | 3:9b597ed04ef4 | 89 |
