![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
working code of bae with i2c
Fork of all_combined_week6 by
slave.cpp@0:cbe0ea884289, 2014-07-10 (annotated)
- Committer:
- harshit_felicity
- Date:
- Thu Jul 10 11:35:40 2014 +0000
- Revision:
- 0:cbe0ea884289
- Child:
- 3:9b597ed04ef4
Fuck Yeahh..btich
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harshit_felicity | 0:cbe0ea884289 | 1 | #include "slave.h" |
harshit_felicity | 0:cbe0ea884289 | 2 | SensorData Sensor; |
harshit_felicity | 0:cbe0ea884289 | 3 | I2CSlave slave(p28,p27); //configuring pins p27, p28 as I2Cslave |
harshit_felicity | 0:cbe0ea884289 | 4 | Serial green(USBTX, USBRX); |
harshit_felicity | 0:cbe0ea884289 | 5 | |
harshit_felicity | 0:cbe0ea884289 | 6 | void write_to_master(char send) //function to write data to master |
harshit_felicity | 0:cbe0ea884289 | 7 | { |
harshit_felicity | 0:cbe0ea884289 | 8 | int acknowledge; |
harshit_felicity | 0:cbe0ea884289 | 9 | acknowledge = slave.write(send); //sending the byte to master |
harshit_felicity | 0:cbe0ea884289 | 10 | if(acknowledge==1) |
harshit_felicity | 0:cbe0ea884289 | 11 | { |
harshit_felicity | 0:cbe0ea884289 | 12 | green.printf(" acknowledge %d sent %x \n",acknowledge,send); |
harshit_felicity | 0:cbe0ea884289 | 13 | } |
harshit_felicity | 0:cbe0ea884289 | 14 | } |
harshit_felicity | 0:cbe0ea884289 | 15 | |
harshit_felicity | 0:cbe0ea884289 | 16 | void split(float data) //function to split data into 4 individual bytes and send to master |
harshit_felicity | 0:cbe0ea884289 | 17 | { |
harshit_felicity | 0:cbe0ea884289 | 18 | union convert |
harshit_felicity | 0:cbe0ea884289 | 19 | { |
harshit_felicity | 0:cbe0ea884289 | 20 | char byte[4]; //array containing individual bytes |
harshit_felicity | 0:cbe0ea884289 | 21 | float number; //floating point number to be split into four bytes |
harshit_felicity | 0:cbe0ea884289 | 22 | }v; |
harshit_felicity | 0:cbe0ea884289 | 23 | v.number=data; |
harshit_felicity | 0:cbe0ea884289 | 24 | write_to_master(v.byte[0]); |
harshit_felicity | 0:cbe0ea884289 | 25 | write_to_master(v.byte[1]); |
harshit_felicity | 0:cbe0ea884289 | 26 | write_to_master(v.byte[2]); |
harshit_felicity | 0:cbe0ea884289 | 27 | write_to_master(v.byte[3]); |
harshit_felicity | 0:cbe0ea884289 | 28 | green.printf("sent %f\n",v.number); |
harshit_felicity | 0:cbe0ea884289 | 29 | } |
harshit_felicity | 0:cbe0ea884289 | 30 | |
harshit_felicity | 0:cbe0ea884289 | 31 | |
harshit_felicity | 0:cbe0ea884289 | 32 | |
harshit_felicity | 0:cbe0ea884289 | 33 | void FUNC_I2C_SLAVE_MAIN() |
harshit_felicity | 0:cbe0ea884289 | 34 | { |
harshit_felicity | 0:cbe0ea884289 | 35 | wait(0.5); |
harshit_felicity | 0:cbe0ea884289 | 36 | slave.address(0x20); //assigning slave address |
harshit_felicity | 0:cbe0ea884289 | 37 | int Switch_Variable; |
harshit_felicity | 0:cbe0ea884289 | 38 | int ReadAddressed=1; |
harshit_felicity | 0:cbe0ea884289 | 39 | int WriteGeneral=3; |
harshit_felicity | 0:cbe0ea884289 | 40 | int loopvariable1=1; |
harshit_felicity | 0:cbe0ea884289 | 41 | int loopvariable2=0; |
harshit_felicity | 0:cbe0ea884289 | 42 | //initialising dummy sensor data |
harshit_felicity | 0:cbe0ea884289 | 43 | // SensorData Sensor; |
harshit_felicity | 0:cbe0ea884289 | 44 | Sensor.voltage[0]=0.1;Sensor.current[0]=0.0;Sensor.temp[0]=1.0; |
harshit_felicity | 0:cbe0ea884289 | 45 | Sensor.voltage[1]=0.1;Sensor.current[1]=1.1;Sensor.temp[1]=2.0; |
harshit_felicity | 0:cbe0ea884289 | 46 | Sensor.voltage[2]=0.2;Sensor.current[2]=2.2;Sensor.temp[2]=2.0; |
harshit_felicity | 0:cbe0ea884289 | 47 | Sensor.voltage[3]=0.3;Sensor.current[3]=3.3;Sensor.temp[3]=3.0; |
harshit_felicity | 0:cbe0ea884289 | 48 | Sensor.voltage[4]=0.4;Sensor.current[4]=4.4;Sensor.temp[4]=4.0; |
harshit_felicity | 0:cbe0ea884289 | 49 | Sensor.voltage[5]=0.5;Sensor.current[5]=5.5;Sensor.temp[5]=5.0; |
harshit_felicity | 0:cbe0ea884289 | 50 | Sensor.voltage[6]=0.6;Sensor.current[6]=6.6;Sensor.temp[6]=6.0; |
harshit_felicity | 0:cbe0ea884289 | 51 | Sensor.voltage[7]=0.7;Sensor.current[7]=7.7;Sensor.temp[7]=7.0; |
harshit_felicity | 0:cbe0ea884289 | 52 | Sensor.voltage[8]=0.8;Sensor.current[8]=8.8;Sensor.temp[8]=8.0; |
harshit_felicity | 0:cbe0ea884289 | 53 | Sensor.voltage[9]=0.9;Sensor.current[9]=9.9;Sensor.temp[9]=20.5; |
harshit_felicity | 0:cbe0ea884289 | 54 | while(loopvariable1) |
harshit_felicity | 0:cbe0ea884289 | 55 | { |
harshit_felicity | 0:cbe0ea884289 | 56 | //to read data from master |
harshit_felicity | 0:cbe0ea884289 | 57 | if(slave.receive()==WriteGeneral) //checking if slave is addressed to write |
harshit_felicity | 0:cbe0ea884289 | 58 | { |
harshit_felicity | 0:cbe0ea884289 | 59 | Switch_Variable=slave.read(); //receiving data |
harshit_felicity | 0:cbe0ea884289 | 60 | green.printf("switch variable=%d\n",Switch_Variable); |
harshit_felicity | 0:cbe0ea884289 | 61 | slave.stop(); //reset slave to default receiving state |
harshit_felicity | 0:cbe0ea884289 | 62 | loopvariable1=0; |
harshit_felicity | 0:cbe0ea884289 | 63 | //to interpret and write data to master |
harshit_felicity | 0:cbe0ea884289 | 64 | switch(Switch_Variable) |
harshit_felicity | 0:cbe0ea884289 | 65 | { |
harshit_felicity | 0:cbe0ea884289 | 66 | |
harshit_felicity | 0:cbe0ea884289 | 67 | case 1: while(loopvariable2<30) |
harshit_felicity | 0:cbe0ea884289 | 68 | { |
harshit_felicity | 0:cbe0ea884289 | 69 | if(slave.receive()==ReadAddressed) //check if slave is addressed to read |
harshit_felicity | 0:cbe0ea884289 | 70 | { |
harshit_felicity | 0:cbe0ea884289 | 71 | if(loopvariable2%3==0) |
harshit_felicity | 0:cbe0ea884289 | 72 | { |
harshit_felicity | 0:cbe0ea884289 | 73 | green.printf("\nvoltage%d\n",loopvariable2/3); |
harshit_felicity | 0:cbe0ea884289 | 74 | split(Sensor.voltage[loopvariable2/3]); |
harshit_felicity | 0:cbe0ea884289 | 75 | } |
harshit_felicity | 0:cbe0ea884289 | 76 | else if(loopvariable2%3==1) |
harshit_felicity | 0:cbe0ea884289 | 77 | { |
harshit_felicity | 0:cbe0ea884289 | 78 | green.printf("\ncurrent%d\n",loopvariable2/3); |
harshit_felicity | 0:cbe0ea884289 | 79 | split(Sensor.current[loopvariable2/3]); |
harshit_felicity | 0:cbe0ea884289 | 80 | } |
harshit_felicity | 0:cbe0ea884289 | 81 | else if(loopvariable2%3==2) |
harshit_felicity | 0:cbe0ea884289 | 82 | { |
harshit_felicity | 0:cbe0ea884289 | 83 | green.printf("\ntemp%d\n",loopvariable2/3); |
harshit_felicity | 0:cbe0ea884289 | 84 | split(Sensor.temp[loopvariable2/3]); |
harshit_felicity | 0:cbe0ea884289 | 85 | } |
harshit_felicity | 0:cbe0ea884289 | 86 | loopvariable2++; |
harshit_felicity | 0:cbe0ea884289 | 87 | slave.stop(); |
harshit_felicity | 0:cbe0ea884289 | 88 | } |
harshit_felicity | 0:cbe0ea884289 | 89 | } |
harshit_felicity | 0:cbe0ea884289 | 90 | break; |
harshit_felicity | 0:cbe0ea884289 | 91 | case 2 : green.printf(" telecommand 2\n"); |
harshit_felicity | 0:cbe0ea884289 | 92 | break; |
harshit_felicity | 0:cbe0ea884289 | 93 | |
harshit_felicity | 0:cbe0ea884289 | 94 | }//switch case ends |
harshit_felicity | 0:cbe0ea884289 | 95 | } |
harshit_felicity | 0:cbe0ea884289 | 96 | } |
harshit_felicity | 0:cbe0ea884289 | 97 | green.printf("done"); |
harshit_felicity | 0:cbe0ea884289 | 98 | } |