![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
working code of bae with i2c
Fork of all_combined_week6 by
slave.cpp@7:47e07755d9a4, 2014-09-18 (annotated)
- Committer:
- viswachaitanya
- Date:
- Thu Sep 18 10:29:33 2014 +0000
- Revision:
- 7:47e07755d9a4
- Parent:
- 6:82153349cc9b
working bae code with i2c
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); |
viswachaitanya | 7:47e07755d9a4 | 7 | |
viswachaitanya | 7:47e07755d9a4 | 8 | |
viswachaitanya | 7:47e07755d9a4 | 9 | //#include "slave.h" |
viswachaitanya | 7:47e07755d9a4 | 10 | //I2CSlave slave(p28,p27); //configuring pins p27, p28 as I2Cslave |
viswachaitanya | 7:47e07755d9a4 | 11 | //Serial pc (USBTX,USBRX); |
viswachaitanya | 7:47e07755d9a4 | 12 | //SensorData Sensor; |
viswachaitanya | 7:47e07755d9a4 | 13 | /*struct SensorData //HK_data_structure |
viswachaitanya | 7:47e07755d9a4 | 14 | { |
viswachaitanya | 7:47e07755d9a4 | 15 | char voltage[10]; |
viswachaitanya | 7:47e07755d9a4 | 16 | char current[10]; |
viswachaitanya | 7:47e07755d9a4 | 17 | char temp[10]; |
viswachaitanya | 7:47e07755d9a4 | 18 | }Sensor;*/ |
viswachaitanya | 7:47e07755d9a4 | 19 | |
viswachaitanya | 7:47e07755d9a4 | 20 | int FUNC_I2C_SLAVE_MAIN(int iterations) |
viswachaitanya | 7:47e07755d9a4 | 21 | { |
viswachaitanya | 7:47e07755d9a4 | 22 | |
viswachaitanya | 7:47e07755d9a4 | 23 | wait(0.5); |
viswachaitanya | 7:47e07755d9a4 | 24 | slave.address(0x20); //assigning slave address |
viswachaitanya | 7:47e07755d9a4 | 25 | slave.stop(); |
viswachaitanya | 7:47e07755d9a4 | 26 | char Switch_Variable; |
viswachaitanya | 7:47e07755d9a4 | 27 | int ReadAddressed=1; |
viswachaitanya | 7:47e07755d9a4 | 28 | int WriteGeneral=3; |
viswachaitanya | 7:47e07755d9a4 | 29 | bool loopvariable1=true; |
viswachaitanya | 7:47e07755d9a4 | 30 | uint8_t loopvariable2=0; |
viswachaitanya | 7:47e07755d9a4 | 31 | bool loopvariable3=true; |
viswachaitanya | 7:47e07755d9a4 | 32 | //---------------initialising dummy sensor data----------------------------------------------------------- |
viswachaitanya | 7:47e07755d9a4 | 33 | Sensor.Voltage[0]='a';Sensor.Current[0]='1';Sensor.Temperature[0]='k'; |
viswachaitanya | 7:47e07755d9a4 | 34 | Sensor.Voltage[1]='b';Sensor.Current[1]='2';Sensor.Temperature[1]='l'; |
viswachaitanya | 7:47e07755d9a4 | 35 | Sensor.Voltage[2]='c';Sensor.Current[2]='3';Sensor.Temperature[2]='m'; |
viswachaitanya | 7:47e07755d9a4 | 36 | Sensor.Voltage[3]='d';Sensor.Current[3]='4';Sensor.Temperature[3]='n'; |
viswachaitanya | 7:47e07755d9a4 | 37 | Sensor.Voltage[4]='e';Sensor.Current[4]='5';Sensor.Temperature[4]='o'; |
viswachaitanya | 7:47e07755d9a4 | 38 | Sensor.Voltage[5]='f';Sensor.Current[5]='1';Sensor.Temperature[5]='p'; |
viswachaitanya | 7:47e07755d9a4 | 39 | Sensor.Voltage[6]='g';Sensor.Current[6]='2';Sensor.Temperature[6]='q'; |
viswachaitanya | 7:47e07755d9a4 | 40 | Sensor.Voltage[7]='h';Sensor.Current[7]='3';Sensor.Temperature[7]='r'; |
viswachaitanya | 7:47e07755d9a4 | 41 | while(loopvariable1) |
viswachaitanya | 7:47e07755d9a4 | 42 | { |
viswachaitanya | 7:47e07755d9a4 | 43 | //------------------------to read data from master--------------------------------------------------------- |
viswachaitanya | 7:47e07755d9a4 | 44 | if(slave.receive()==WriteGeneral) //checking if slave is addressed to write |
viswachaitanya | 7:47e07755d9a4 | 45 | { |
viswachaitanya | 7:47e07755d9a4 | 46 | Switch_Variable=slave.read(); //receiving data |
viswachaitanya | 7:47e07755d9a4 | 47 | printf("switch variable=%d\n",Switch_Variable); |
viswachaitanya | 7:47e07755d9a4 | 48 | slave.stop(); //reset slave to default receiving state |
viswachaitanya | 7:47e07755d9a4 | 49 | loopvariable1=false; |
viswachaitanya | 7:47e07755d9a4 | 50 | //----------------------to interpret and write data to master---------------------------------------------- |
viswachaitanya | 7:47e07755d9a4 | 51 | switch(Switch_Variable) |
viswachaitanya | 7:47e07755d9a4 | 52 | { |
viswachaitanya | 7:47e07755d9a4 | 53 | |
viswachaitanya | 7:47e07755d9a4 | 54 | case '1': while(loopvariable3) |
viswachaitanya | 7:47e07755d9a4 | 55 | { |
viswachaitanya | 7:47e07755d9a4 | 56 | if(slave.receive()==ReadAddressed) //check if slave is addressed to read |
viswachaitanya | 7:47e07755d9a4 | 57 | { |
viswachaitanya | 7:47e07755d9a4 | 58 | loopvariable3=false; |
viswachaitanya | 7:47e07755d9a4 | 59 | while(loopvariable2<8) //running loop for sending 30 sensors data |
viswachaitanya | 7:47e07755d9a4 | 60 | { |
viswachaitanya | 7:47e07755d9a4 | 61 | printf("\nvoltage%d\n",loopvariable2); |
viswachaitanya | 7:47e07755d9a4 | 62 | write_to_master(Sensor.Voltage[loopvariable2]); //calling function to send float data |
viswachaitanya | 7:47e07755d9a4 | 63 | |
viswachaitanya | 7:47e07755d9a4 | 64 | printf("\ncurrent%d\n",loopvariable2); |
viswachaitanya | 7:47e07755d9a4 | 65 | write_to_master(Sensor.Current[loopvariable2]); //calling function to send float data |
viswachaitanya | 7:47e07755d9a4 | 66 | |
viswachaitanya | 7:47e07755d9a4 | 67 | printf("\ntemp%d\n",loopvariable2); |
viswachaitanya | 7:47e07755d9a4 | 68 | write_to_master(Sensor.Temperature[loopvariable2]); //calling function to send float data |
viswachaitanya | 7:47e07755d9a4 | 69 | |
viswachaitanya | 7:47e07755d9a4 | 70 | loopvariable2++; |
viswachaitanya | 7:47e07755d9a4 | 71 | } |
viswachaitanya | 7:47e07755d9a4 | 72 | |
viswachaitanya | 7:47e07755d9a4 | 73 | } |
viswachaitanya | 7:47e07755d9a4 | 74 | |
viswachaitanya | 7:47e07755d9a4 | 75 | } |
viswachaitanya | 7:47e07755d9a4 | 76 | break; |
viswachaitanya | 7:47e07755d9a4 | 77 | case '2' : printf(" telecommand 2\n"); |
viswachaitanya | 7:47e07755d9a4 | 78 | break; |
viswachaitanya | 7:47e07755d9a4 | 79 | |
viswachaitanya | 7:47e07755d9a4 | 80 | } |
viswachaitanya | 7:47e07755d9a4 | 81 | } |
viswachaitanya | 7:47e07755d9a4 | 82 | } |
viswachaitanya | 7:47e07755d9a4 | 83 | slave.stop(); |
viswachaitanya | 7:47e07755d9a4 | 84 | printf("done"); |
viswachaitanya | 7:47e07755d9a4 | 85 | } |
viswachaitanya | 7:47e07755d9a4 | 86 | |
viswachaitanya | 7:47e07755d9a4 | 87 | //------------------function to write data to master--------------------------------------------------- |
viswachaitanya | 7:47e07755d9a4 | 88 | void write_to_master(char send) |
viswachaitanya | 7:47e07755d9a4 | 89 | { |
viswachaitanya | 7:47e07755d9a4 | 90 | bool acknowledge; |
viswachaitanya | 7:47e07755d9a4 | 91 | bool loopvariable4=true; |
viswachaitanya | 7:47e07755d9a4 | 92 | while(loopvariable4) |
viswachaitanya | 7:47e07755d9a4 | 93 | { |
viswachaitanya | 7:47e07755d9a4 | 94 | acknowledge = (bool) slave.write(send); //sending the byte to master |
viswachaitanya | 7:47e07755d9a4 | 95 | if(acknowledge) //breaking loop if data is acknowledged |
viswachaitanya | 7:47e07755d9a4 | 96 | { |
viswachaitanya | 7:47e07755d9a4 | 97 | printf(" acknowledge %d sent %x \n",acknowledge,send); |
viswachaitanya | 7:47e07755d9a4 | 98 | loopvariable4 = false; |
viswachaitanya | 7:47e07755d9a4 | 99 | } |
viswachaitanya | 7:47e07755d9a4 | 100 | } |
viswachaitanya | 7:47e07755d9a4 | 101 | } |
viswachaitanya | 7:47e07755d9a4 | 102 | |
viswachaitanya | 7:47e07755d9a4 | 103 | |
viswachaitanya | 7:47e07755d9a4 | 104 | |
viswachaitanya | 7:47e07755d9a4 | 105 | |
viswachaitanya | 7:47e07755d9a4 | 106 | |
viswachaitanya | 7:47e07755d9a4 | 107 | |
viswachaitanya | 7:47e07755d9a4 | 108 | |
viswachaitanya | 7:47e07755d9a4 | 109 | /*void write_to_master(char send) //function to write data to master |
harshit_felicity | 0:cbe0ea884289 | 110 | { |
harshit_felicity | 0:cbe0ea884289 | 111 | int acknowledge; |
greenroshks | 3:9b597ed04ef4 | 112 | int loopvariable4=1; |
greenroshks | 3:9b597ed04ef4 | 113 | while(loopvariable4) |
greenroshks | 3:9b597ed04ef4 | 114 | { |
harshit_felicity | 0:cbe0ea884289 | 115 | acknowledge = slave.write(send); //sending the byte to master |
harshit_felicity | 0:cbe0ea884289 | 116 | if(acknowledge==1) |
harshit_felicity | 0:cbe0ea884289 | 117 | { |
viswachaitanya | 7:47e07755d9a4 | 118 | printf(" acknowledge %d sent %u \n",acknowledge,send); |
greenroshks | 3:9b597ed04ef4 | 119 | loopvariable4=0; |
harshit_felicity | 0:cbe0ea884289 | 120 | } |
greenroshks | 3:9b597ed04ef4 | 121 | } |
harshit_felicity | 0:cbe0ea884289 | 122 | } |
harshit_felicity | 0:cbe0ea884289 | 123 | |
greenroshks | 3:9b597ed04ef4 | 124 | |
greenroshks | 3:9b597ed04ef4 | 125 | |
greenroshks | 3:9b597ed04ef4 | 126 | |
greenroshks | 3:9b597ed04ef4 | 127 | void FUNC_I2C_SLAVE_MAIN(int iterations) |
harshit_felicity | 0:cbe0ea884289 | 128 | { |
viswachaitanya | 7:47e07755d9a4 | 129 | // slave.stop(); |
viswachaitanya | 7:47e07755d9a4 | 130 | printf("\nSlave entered\n"); |
greenroshks | 3:9b597ed04ef4 | 131 | slave.address(slave_address); //assigning slave address |
greenroshks | 3:9b597ed04ef4 | 132 | char Switch_Variable; |
greenroshks | 3:9b597ed04ef4 | 133 | |
greenroshks | 3:9b597ed04ef4 | 134 | |
harshit_felicity | 0:cbe0ea884289 | 135 | int loopvariable1=1; |
harshit_felicity | 0:cbe0ea884289 | 136 | int loopvariable2=0; |
greenroshks | 3:9b597ed04ef4 | 137 | int loopvariable3=1; |
harshit_felicity | 0:cbe0ea884289 | 138 | //initialising dummy sensor data |
greenroshks | 3:9b597ed04ef4 | 139 | |
harshit_felicity | 0:cbe0ea884289 | 140 | while(loopvariable1) |
harshit_felicity | 0:cbe0ea884289 | 141 | { |
viswachaitanya | 7:47e07755d9a4 | 142 | // slave.stop(); |
viswachaitanya | 7:47e07755d9a4 | 143 | //to read data from master |
viswachaitanya | 7:47e07755d9a4 | 144 | printf("not receiving\n"); |
harshit_felicity | 0:cbe0ea884289 | 145 | if(slave.receive()==WriteGeneral) //checking if slave is addressed to write |
harshit_felicity | 0:cbe0ea884289 | 146 | { |
harshit_felicity | 0:cbe0ea884289 | 147 | Switch_Variable=slave.read(); //receiving data |
viswachaitanya | 7:47e07755d9a4 | 148 | printf("switch variable=%d\n",Switch_Variable); |
harshit_felicity | 0:cbe0ea884289 | 149 | slave.stop(); //reset slave to default receiving state |
harshit_felicity | 0:cbe0ea884289 | 150 | loopvariable1=0; |
harshit_felicity | 0:cbe0ea884289 | 151 | //to interpret and write data to master |
harshit_felicity | 0:cbe0ea884289 | 152 | switch(Switch_Variable) |
harshit_felicity | 0:cbe0ea884289 | 153 | { |
greenroshks | 6:82153349cc9b | 154 | |
greenroshks | 6:82153349cc9b | 155 | |
greenroshks | 6:82153349cc9b | 156 | case 1: printf("\nEntered switch\n"); |
greenroshks | 6:82153349cc9b | 157 | while(loopvariable3) |
greenroshks | 3:9b597ed04ef4 | 158 | { |
harshit_felicity | 0:cbe0ea884289 | 159 | if(slave.receive()==ReadAddressed) //check if slave is addressed to read |
harshit_felicity | 0:cbe0ea884289 | 160 | { |
greenroshks | 3:9b597ed04ef4 | 161 | while(loopvariable2<iterations) |
greenroshks | 3:9b597ed04ef4 | 162 | { |
harshit_felicity | 0:cbe0ea884289 | 163 | if(loopvariable2%3==0) |
harshit_felicity | 0:cbe0ea884289 | 164 | { |
viswachaitanya | 7:47e07755d9a4 | 165 | printf("\nvoltage%d\n",loopvariable2/3); |
greenroshks | 3:9b597ed04ef4 | 166 | write_to_master(Sensor.Voltage[loopvariable2/3]); |
harshit_felicity | 0:cbe0ea884289 | 167 | } |
harshit_felicity | 0:cbe0ea884289 | 168 | else if(loopvariable2%3==1) |
harshit_felicity | 0:cbe0ea884289 | 169 | { |
viswachaitanya | 7:47e07755d9a4 | 170 | printf("\ncurrent%d\n",loopvariable2/3); |
greenroshks | 3:9b597ed04ef4 | 171 | write_to_master(Sensor.Current[loopvariable2/3]); |
harshit_felicity | 0:cbe0ea884289 | 172 | } |
harshit_felicity | 0:cbe0ea884289 | 173 | else if(loopvariable2%3==2) |
harshit_felicity | 0:cbe0ea884289 | 174 | { |
viswachaitanya | 7:47e07755d9a4 | 175 | printf("\ntemp%d\n",loopvariable2/3); |
greenroshks | 3:9b597ed04ef4 | 176 | write_to_master(Sensor.Temperature[loopvariable2/3]); |
harshit_felicity | 0:cbe0ea884289 | 177 | } |
harshit_felicity | 0:cbe0ea884289 | 178 | loopvariable2++; |
greenroshks | 3:9b597ed04ef4 | 179 | }//while(loopvariable2<30) |
greenroshks | 6:82153349cc9b | 180 | printf("\nExited i2c while loop\n"); |
greenroshks | 3:9b597ed04ef4 | 181 | slave.stop(); |
greenroshks | 3:9b597ed04ef4 | 182 | loopvariable3=0; |
greenroshks | 3:9b597ed04ef4 | 183 | }//if(read addressed) |
greenroshks | 3:9b597ed04ef4 | 184 | |
greenroshks | 3:9b597ed04ef4 | 185 | }//while(loopvariable3) |
greenroshks | 3:9b597ed04ef4 | 186 | |
harshit_felicity | 0:cbe0ea884289 | 187 | break; |
viswachaitanya | 7:47e07755d9a4 | 188 | case 2 : printf(" telecommand 2\n"); |
harshit_felicity | 0:cbe0ea884289 | 189 | break; |
harshit_felicity | 0:cbe0ea884289 | 190 | |
harshit_felicity | 0:cbe0ea884289 | 191 | }//switch case ends |
harshit_felicity | 0:cbe0ea884289 | 192 | } |
harshit_felicity | 0:cbe0ea884289 | 193 | } |
viswachaitanya | 7:47e07755d9a4 | 194 | printf("\nexited slave function\n"); |
viswachaitanya | 7:47e07755d9a4 | 195 | }*/ |
greenroshks | 3:9b597ed04ef4 | 196 |