![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
freeslave1.0
slave.cpp@0:10d14005f03a, 2014-12-03 (annotated)
- Committer:
- viswachaitanya
- Date:
- Wed Dec 03 10:37:04 2014 +0000
- Revision:
- 0:10d14005f03a
pre 12/14 testing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
viswachaitanya | 0:10d14005f03a | 1 | #include "slave.h" |
viswachaitanya | 0:10d14005f03a | 2 | I2CSlave slave(p28,p27); //configuring pins p27, p28 as I2Cslave |
viswachaitanya | 0:10d14005f03a | 3 | Serial pc (USBTX,USBRX); |
viswachaitanya | 0:10d14005f03a | 4 | //SensorData Sensor; |
viswachaitanya | 0:10d14005f03a | 5 | struct SensorData //HK_data_structure |
viswachaitanya | 0:10d14005f03a | 6 | { |
viswachaitanya | 0:10d14005f03a | 7 | char voltage[10]; |
viswachaitanya | 0:10d14005f03a | 8 | char current[10]; |
viswachaitanya | 0:10d14005f03a | 9 | char temp[10]; |
viswachaitanya | 0:10d14005f03a | 10 | }Sensor; |
viswachaitanya | 0:10d14005f03a | 11 | |
viswachaitanya | 0:10d14005f03a | 12 | int FUNC_I2C_SLAVE_MAIN(int iterations) |
viswachaitanya | 0:10d14005f03a | 13 | { |
viswachaitanya | 0:10d14005f03a | 14 | |
viswachaitanya | 0:10d14005f03a | 15 | wait(0.5); |
viswachaitanya | 0:10d14005f03a | 16 | slave.address(0x20); //assigning slave address |
viswachaitanya | 0:10d14005f03a | 17 | char Switch_Variable; |
viswachaitanya | 0:10d14005f03a | 18 | int ReadAddressed=1; |
viswachaitanya | 0:10d14005f03a | 19 | int WriteGeneral=3; |
viswachaitanya | 0:10d14005f03a | 20 | bool loopvariable1=true; |
viswachaitanya | 0:10d14005f03a | 21 | uint8_t loopvariable2=0; |
viswachaitanya | 0:10d14005f03a | 22 | bool loopvariable3=true; |
viswachaitanya | 0:10d14005f03a | 23 | //---------------initialising dummy sensor data----------------------------------------------------------- |
viswachaitanya | 0:10d14005f03a | 24 | Sensor.voltage[0]='a';Sensor.current[0]='1';Sensor.temp[0]='k'; |
viswachaitanya | 0:10d14005f03a | 25 | Sensor.voltage[1]='b';Sensor.current[1]='2';Sensor.temp[1]='l'; |
viswachaitanya | 0:10d14005f03a | 26 | Sensor.voltage[2]='c';Sensor.current[2]='3';Sensor.temp[2]='m'; |
viswachaitanya | 0:10d14005f03a | 27 | Sensor.voltage[3]='d';Sensor.current[3]='4';Sensor.temp[3]='n'; |
viswachaitanya | 0:10d14005f03a | 28 | Sensor.voltage[4]='e';Sensor.current[4]='5';Sensor.temp[4]='o'; |
viswachaitanya | 0:10d14005f03a | 29 | Sensor.voltage[5]='f';Sensor.current[5]='1';Sensor.temp[5]='p'; |
viswachaitanya | 0:10d14005f03a | 30 | Sensor.voltage[6]='g';Sensor.current[6]='2';Sensor.temp[6]='q'; |
viswachaitanya | 0:10d14005f03a | 31 | Sensor.voltage[7]='h';Sensor.current[7]='3';Sensor.temp[7]='r'; |
viswachaitanya | 0:10d14005f03a | 32 | while(loopvariable1) |
viswachaitanya | 0:10d14005f03a | 33 | { |
viswachaitanya | 0:10d14005f03a | 34 | //------------------------to read data from master--------------------------------------------------------- |
viswachaitanya | 0:10d14005f03a | 35 | if(slave.receive()==WriteGeneral) //checking if slave is addressed to write |
viswachaitanya | 0:10d14005f03a | 36 | { |
viswachaitanya | 0:10d14005f03a | 37 | Switch_Variable=slave.read(); //receiving data |
viswachaitanya | 0:10d14005f03a | 38 | printf("switch variable=%d\n",Switch_Variable); |
viswachaitanya | 0:10d14005f03a | 39 | slave.stop(); //reset slave to default receiving state |
viswachaitanya | 0:10d14005f03a | 40 | loopvariable1=false; |
viswachaitanya | 0:10d14005f03a | 41 | //----------------------to interpret and write data to master---------------------------------------------- |
viswachaitanya | 0:10d14005f03a | 42 | switch(Switch_Variable) |
viswachaitanya | 0:10d14005f03a | 43 | { |
viswachaitanya | 0:10d14005f03a | 44 | |
viswachaitanya | 0:10d14005f03a | 45 | case '1': while(loopvariable3) |
viswachaitanya | 0:10d14005f03a | 46 | { |
viswachaitanya | 0:10d14005f03a | 47 | if(slave.receive()==ReadAddressed) //check if slave is addressed to read |
viswachaitanya | 0:10d14005f03a | 48 | { |
viswachaitanya | 0:10d14005f03a | 49 | loopvariable3=false; |
viswachaitanya | 0:10d14005f03a | 50 | while(loopvariable2<8) //running loop for sending 30 sensors data |
viswachaitanya | 0:10d14005f03a | 51 | { |
viswachaitanya | 0:10d14005f03a | 52 | printf("\nvoltage%d\n",loopvariable2); |
viswachaitanya | 0:10d14005f03a | 53 | write_to_master(Sensor.voltage[loopvariable2]); //calling function to send float data |
viswachaitanya | 0:10d14005f03a | 54 | |
viswachaitanya | 0:10d14005f03a | 55 | printf("\ncurrent%d\n",loopvariable2); |
viswachaitanya | 0:10d14005f03a | 56 | write_to_master(Sensor.current[loopvariable2]); //calling function to send float data |
viswachaitanya | 0:10d14005f03a | 57 | |
viswachaitanya | 0:10d14005f03a | 58 | printf("\ntemp%d\n",loopvariable2); |
viswachaitanya | 0:10d14005f03a | 59 | write_to_master(Sensor.temp[loopvariable2]); //calling function to send float data |
viswachaitanya | 0:10d14005f03a | 60 | |
viswachaitanya | 0:10d14005f03a | 61 | loopvariable2++; |
viswachaitanya | 0:10d14005f03a | 62 | } |
viswachaitanya | 0:10d14005f03a | 63 | |
viswachaitanya | 0:10d14005f03a | 64 | } |
viswachaitanya | 0:10d14005f03a | 65 | |
viswachaitanya | 0:10d14005f03a | 66 | } |
viswachaitanya | 0:10d14005f03a | 67 | break; |
viswachaitanya | 0:10d14005f03a | 68 | case '2' : printf(" telecommand 2\n"); |
viswachaitanya | 0:10d14005f03a | 69 | break; |
viswachaitanya | 0:10d14005f03a | 70 | |
viswachaitanya | 0:10d14005f03a | 71 | } |
viswachaitanya | 0:10d14005f03a | 72 | } |
viswachaitanya | 0:10d14005f03a | 73 | } |
viswachaitanya | 0:10d14005f03a | 74 | slave.stop(); |
viswachaitanya | 0:10d14005f03a | 75 | printf("done"); |
viswachaitanya | 0:10d14005f03a | 76 | } |
viswachaitanya | 0:10d14005f03a | 77 | |
viswachaitanya | 0:10d14005f03a | 78 | //------------------function to write data to master--------------------------------------------------- |
viswachaitanya | 0:10d14005f03a | 79 | void write_to_master(char send) |
viswachaitanya | 0:10d14005f03a | 80 | { |
viswachaitanya | 0:10d14005f03a | 81 | bool acknowledge; |
viswachaitanya | 0:10d14005f03a | 82 | bool loopvariable4=true; |
viswachaitanya | 0:10d14005f03a | 83 | while(loopvariable4) |
viswachaitanya | 0:10d14005f03a | 84 | { |
viswachaitanya | 0:10d14005f03a | 85 | acknowledge = (bool) slave.write(send); //sending the byte to master |
viswachaitanya | 0:10d14005f03a | 86 | if(acknowledge) //breaking loop if data is acknowledged |
viswachaitanya | 0:10d14005f03a | 87 | { |
viswachaitanya | 0:10d14005f03a | 88 | printf(" acknowledge %d sent %x \n",acknowledge,send); |
viswachaitanya | 0:10d14005f03a | 89 | loopvariable4 = false; |
viswachaitanya | 0:10d14005f03a | 90 | } |
viswachaitanya | 0:10d14005f03a | 91 | } |
viswachaitanya | 0:10d14005f03a | 92 | } |