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