hk data sent using string

Dependencies:   mbed-rtos mbed

Fork of BAE_FRDMTESIN2 by green rosh

Committer:
greenroshks
Date:
Wed Sep 10 06:41:21 2014 +0000
Revision:
0:8b0d43fe6c05
Child:
1:b4bb78a9b92e
BAE FRDM Final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
greenroshks 0:8b0d43fe6c05 1 #include "slave.h"
greenroshks 0:8b0d43fe6c05 2 #include "HK.h"
greenroshks 0:8b0d43fe6c05 3
greenroshks 0:8b0d43fe6c05 4 extern struct SensorData Sensor;
greenroshks 0:8b0d43fe6c05 5 I2CSlave slave(A4,A5); //configuring pins p27, p28 as I2Cslave
greenroshks 0:8b0d43fe6c05 6 Serial screen (USBTX,USBRX);
greenroshks 0:8b0d43fe6c05 7 void write_to_master(char send) //function to write data to master
greenroshks 0:8b0d43fe6c05 8 {
greenroshks 0:8b0d43fe6c05 9 int acknowledge;
greenroshks 0:8b0d43fe6c05 10 int loopvariable4=1;
greenroshks 0:8b0d43fe6c05 11 while(loopvariable4)
greenroshks 0:8b0d43fe6c05 12 {
greenroshks 0:8b0d43fe6c05 13 acknowledge = slave.write(send); //sending the byte to master
greenroshks 0:8b0d43fe6c05 14 if(acknowledge==1)
greenroshks 0:8b0d43fe6c05 15 {
greenroshks 0:8b0d43fe6c05 16 screen.printf(" acknowledge %d sent %u \n",acknowledge,send);
greenroshks 0:8b0d43fe6c05 17 loopvariable4=0;
greenroshks 0:8b0d43fe6c05 18 }
greenroshks 0:8b0d43fe6c05 19 }
greenroshks 0:8b0d43fe6c05 20 }
greenroshks 0:8b0d43fe6c05 21
greenroshks 0:8b0d43fe6c05 22
greenroshks 0:8b0d43fe6c05 23
greenroshks 0:8b0d43fe6c05 24
greenroshks 0:8b0d43fe6c05 25 void FUNC_I2C_SLAVE_MAIN(int iterations)
greenroshks 0:8b0d43fe6c05 26 {
greenroshks 0:8b0d43fe6c05 27
greenroshks 0:8b0d43fe6c05 28 screen.printf("\nSlave entered\n");
greenroshks 0:8b0d43fe6c05 29 slave.address(slave_address); //assigning slave address
greenroshks 0:8b0d43fe6c05 30 slave.stop();
greenroshks 0:8b0d43fe6c05 31 char Switch_Variable;
greenroshks 0:8b0d43fe6c05 32
greenroshks 0:8b0d43fe6c05 33
greenroshks 0:8b0d43fe6c05 34 int loopvariable1=1;
greenroshks 0:8b0d43fe6c05 35 int loopvariable2=0;
greenroshks 0:8b0d43fe6c05 36 int loopvariable3=1;
greenroshks 0:8b0d43fe6c05 37 //initialising dummy sensor data
greenroshks 0:8b0d43fe6c05 38
greenroshks 0:8b0d43fe6c05 39 while(loopvariable1)
greenroshks 0:8b0d43fe6c05 40 {
greenroshks 0:8b0d43fe6c05 41 //to read data from master
greenroshks 0:8b0d43fe6c05 42 if(slave.receive()==WriteGeneral) //checking if slave is addressed to write
greenroshks 0:8b0d43fe6c05 43 {
greenroshks 0:8b0d43fe6c05 44 Switch_Variable=slave.read(); //receiving data
greenroshks 0:8b0d43fe6c05 45 screen.printf("switch variable=%d\n",Switch_Variable);
greenroshks 0:8b0d43fe6c05 46 slave.stop(); //reset slave to default receiving state
greenroshks 0:8b0d43fe6c05 47 loopvariable1=0;
greenroshks 0:8b0d43fe6c05 48 //to interpret and write data to master
greenroshks 0:8b0d43fe6c05 49 switch(Switch_Variable)
greenroshks 0:8b0d43fe6c05 50 {
greenroshks 0:8b0d43fe6c05 51
greenroshks 0:8b0d43fe6c05 52
greenroshks 0:8b0d43fe6c05 53 case 1: printf("\nEntered switch\n");
greenroshks 0:8b0d43fe6c05 54 while(loopvariable3)
greenroshks 0:8b0d43fe6c05 55 {
greenroshks 0:8b0d43fe6c05 56 if(slave.receive()==ReadAddressed) //check if slave is addressed to read
greenroshks 0:8b0d43fe6c05 57 {
greenroshks 0:8b0d43fe6c05 58 while(loopvariable2<iterations)
greenroshks 0:8b0d43fe6c05 59 {
greenroshks 0:8b0d43fe6c05 60 if(loopvariable2%3==0)
greenroshks 0:8b0d43fe6c05 61 {
greenroshks 0:8b0d43fe6c05 62 screen.printf("\nvoltage%d\n",loopvariable2/3);
greenroshks 0:8b0d43fe6c05 63 write_to_master(Sensor.Voltage[loopvariable2/3]);
greenroshks 0:8b0d43fe6c05 64 }
greenroshks 0:8b0d43fe6c05 65 else if(loopvariable2%3==1)
greenroshks 0:8b0d43fe6c05 66 {
greenroshks 0:8b0d43fe6c05 67 screen.printf("\ncurrent%d\n",loopvariable2/3);
greenroshks 0:8b0d43fe6c05 68 write_to_master(Sensor.Current[loopvariable2/3]);
greenroshks 0:8b0d43fe6c05 69 }
greenroshks 0:8b0d43fe6c05 70 else if(loopvariable2%3==2)
greenroshks 0:8b0d43fe6c05 71 {
greenroshks 0:8b0d43fe6c05 72 screen.printf("\ntemp%d\n",loopvariable2/3);
greenroshks 0:8b0d43fe6c05 73 write_to_master(Sensor.Temperature[loopvariable2/3]);
greenroshks 0:8b0d43fe6c05 74 }
greenroshks 0:8b0d43fe6c05 75 loopvariable2++;
greenroshks 0:8b0d43fe6c05 76 }//while(loopvariable2<30)
greenroshks 0:8b0d43fe6c05 77 printf("\nExited i2c while loop\n");
greenroshks 0:8b0d43fe6c05 78 slave.stop();
greenroshks 0:8b0d43fe6c05 79 loopvariable3=0;
greenroshks 0:8b0d43fe6c05 80 }//if(read addressed)
greenroshks 0:8b0d43fe6c05 81
greenroshks 0:8b0d43fe6c05 82 }//while(loopvariable3)
greenroshks 0:8b0d43fe6c05 83
greenroshks 0:8b0d43fe6c05 84 break;
greenroshks 0:8b0d43fe6c05 85 case 2 : screen.printf(" telecommand 2\n");
greenroshks 0:8b0d43fe6c05 86 break;
greenroshks 0:8b0d43fe6c05 87
greenroshks 0:8b0d43fe6c05 88 }//switch case ends
greenroshks 0:8b0d43fe6c05 89 }
greenroshks 0:8b0d43fe6c05 90 }
greenroshks 0:8b0d43fe6c05 91 screen.printf("\nexited slave function\n");
greenroshks 0:8b0d43fe6c05 92 }
greenroshks 0:8b0d43fe6c05 93