i2c slave integrated

Dependencies:   mbed-rtos mbed

Fork of BAE_FRDM_INTEGRATION by green rosh

Committer:
greenroshks
Date:
Mon Dec 15 05:58:23 2014 +0000
Revision:
8:667fbc82d634
Parent:
0:8b0d43fe6c05
BAE FINAL INTEGRATED (may be)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
greenroshks 0:8b0d43fe6c05 1 #include "HK.h"
greenroshks 8:667fbc82d634 2 MAX17048 master(A4,A5,100000);//CHECK SDA,SCL LINES,FREQUENCY
greenroshks 8:667fbc82d634 3 int quantiz(float start,float step,float x);
greenroshks 8:667fbc82d634 4 //GPIO pins used=> D2-D13, A0-A1
greenroshks 8:667fbc82d634 5 DigitalOut SelectLinesA[]={D2,D3,D4,D5};//to mux1
greenroshks 8:667fbc82d634 6 DigitalOut SelectLinesB[]={D6,D7,D8,D9};//to mux2
greenroshks 8:667fbc82d634 7 DigitalOut SelectLinesC[]={D10,D11,D12,D13};//to mux3
greenroshks 8:667fbc82d634 8
greenroshks 8:667fbc82d634 9 //--------------------------------------------MSB is SelectLines[0],LSB is SelectLines[3]--------------------------------
greenroshks 8:667fbc82d634 10
greenroshks 8:667fbc82d634 11 AnalogIn CurrentInput(A0); // Input from Current
greenroshks 8:667fbc82d634 12 AnalogIn VoltageInput(A1); // Input from Voltage Multiplexer
greenroshks 8:667fbc82d634 13 AnalogIn TemperatureInput(A2); /*Input from Temperature Multiplexer,thermistor Multiplexer- same multiplexer for both(0-3 for thermistor,4-15 for temperature sensor)*/
greenroshks 8:667fbc82d634 14
greenroshks 8:667fbc82d634 15 SensorData Sensor;//struct variable
greenroshks 0:8b0d43fe6c05 16
greenroshks 8:667fbc82d634 17 int quantiz(float start,float step,float x) // accepts min and measured values and step->quantises on a scale 0-15
greenroshks 0:8b0d43fe6c05 18 {
greenroshks 0:8b0d43fe6c05 19 int y=(x-start)/step;
greenroshks 0:8b0d43fe6c05 20 if(y<=0)y=0;
greenroshks 0:8b0d43fe6c05 21 if(y>=15)y=15;
greenroshks 0:8b0d43fe6c05 22 return y;
greenroshks 0:8b0d43fe6c05 23 }
greenroshks 8:667fbc82d634 24
greenroshks 8:667fbc82d634 25 void init_beacon(ShortBeacy x) //dummy values----------to be changed-------------------
greenroshks 8:667fbc82d634 26 {
greenroshks 8:667fbc82d634 27 x.Voltage[0]=1;
greenroshks 8:667fbc82d634 28 x.AngularSpeed[0]=2;
greenroshks 8:667fbc82d634 29 x.AngularSpeed[1]=3;
greenroshks 8:667fbc82d634 30 x.SubsystemStatus[0]=145;
greenroshks 8:667fbc82d634 31 x.Temp[0]=1;
greenroshks 8:667fbc82d634 32 x.Temp[1]=2;
greenroshks 8:667fbc82d634 33 x.Temp[2]=3;
greenroshks 8:667fbc82d634 34 x.ErrorFlag[0]=3;
greenroshks 0:8b0d43fe6c05 35 }
greenroshks 8:667fbc82d634 36 ShortBeacy Shortbeacon;//struct variable
greenroshks 8:667fbc82d634 37 void FUNC_HK_MAIN()
greenroshks 0:8b0d43fe6c05 38 {
greenroshks 0:8b0d43fe6c05 39
greenroshks 0:8b0d43fe6c05 40
greenroshks 8:667fbc82d634 41 init_beacon(Shortbeacon);
greenroshks 8:667fbc82d634 42 //initialise all selectlines to zeroes->1st line of mux selected
greenroshks 8:667fbc82d634 43 SelectLinesA[0]=SelectLinesA[1]=SelectLinesA[2]=SelectLinesA[3]=0;
greenroshks 8:667fbc82d634 44 SelectLinesB[0]=SelectLinesB[1]=SelectLinesB[2]=SelectLinesB[3]=0;
greenroshks 8:667fbc82d634 45 SelectLinesC[0]=SelectLinesC[1]=SelectLinesC[2]=SelectLinesC[3]=0;
greenroshks 0:8b0d43fe6c05 46 int LoopIterator;
greenroshks 8:667fbc82d634 47 float resistance_thermistor,voltage_thermistor;//for thermistor
greenroshks 8:667fbc82d634 48
greenroshks 8:667fbc82d634 49 for(LoopIterator=0; LoopIterator<16; LoopIterator++)
greenroshks 8:667fbc82d634 50 {
greenroshks 0:8b0d43fe6c05 51
greenroshks 8:667fbc82d634 52 if(LoopIterator%2==0)
greenroshks 8:667fbc82d634 53 {
greenroshks 8:667fbc82d634 54
greenroshks 0:8b0d43fe6c05 55 Sensor.Voltage[LoopIterator/2]=quantiz(vstart,vstep,(VoltageInput.read()*3.18*5.37));
greenroshks 8:667fbc82d634 56 Sensor.Current[LoopIterator/2]=quantiz(cstart,cstep,(CurrentInput.read()*3.18/(50*rsens)));
greenroshks 8:667fbc82d634 57 if(LoopIterator>3)
greenroshks 8:667fbc82d634 58 Sensor.Temperature[(LoopIterator-4)/2]=quantiz(tstart,tstep,(-90.7*3.18*TemperatureInput.read()+190.1543));
greenroshks 8:667fbc82d634 59 else
greenroshks 8:667fbc82d634 60 { voltage_thermistor=TemperatureInput.read()*3.18;
greenroshks 8:667fbc82d634 61 resistance_thermistor=24000*voltage_thermistor/(3.3-voltage_thermistor);
greenroshks 8:667fbc82d634 62 if(voltage_thermistor<1.378) //Temperature>12 degC
greenroshks 8:667fbc82d634 63 Sensor.PanelTemperature[(LoopIterator)/2]=quantiz(tstart_thermistor,tstep_thermistor,(3694/log(24.032242*resistance_thermistor)));
greenroshks 8:667fbc82d634 64
greenroshks 8:667fbc82d634 65 else {
greenroshks 8:667fbc82d634 66 Sensor.PanelTemperature[(LoopIterator)/2]=quantiz(tstart_thermistor,tstep_thermistor,(3365.4792/log(7.60404*resistance_thermistor)));
greenroshks 8:667fbc82d634 67 }
greenroshks 0:8b0d43fe6c05 68 }
greenroshks 8:667fbc82d634 69 }
greenroshks 8:667fbc82d634 70
greenroshks 8:667fbc82d634 71 else
greenroshks 8:667fbc82d634 72 {
greenroshks 8:667fbc82d634 73
greenroshks 8:667fbc82d634 74 Sensor.Voltage[(LoopIterator-1)/2]=Sensor.Voltage[(LoopIterator-1)/2]<<4+quantiz(vstart,vstep,(VoltageInput.read()*3.18*5.37));
greenroshks 8:667fbc82d634 75 Sensor.Current[(LoopIterator-1)/2]=Sensor.Current[(LoopIterator-1)/2]<<4+quantiz(cstart,cstep,(CurrentInput.read()*3.18/(50*rsens)));
greenroshks 8:667fbc82d634 76 if(LoopIterator>3)
greenroshks 8:667fbc82d634 77 Sensor.Temperature[(LoopIterator-5)/2]=Sensor.Temperature[(LoopIterator-5)/2]<<4+quantiz(tstart,tstep,(-90.7*3.18*TemperatureInput.read()+190.1543));
greenroshks 8:667fbc82d634 78 else
greenroshks 8:667fbc82d634 79 { voltage_thermistor=TemperatureInput.read()*3.18;
greenroshks 8:667fbc82d634 80 resistance_thermistor=24000*voltage_thermistor/(3.3-voltage_thermistor);
greenroshks 8:667fbc82d634 81 if(voltage_thermistor<1.378) //Temperature>12 degC
greenroshks 8:667fbc82d634 82 Sensor.PanelTemperature[(LoopIterator-1)/2]=Sensor.PanelTemperature[(LoopIterator-1)/2]<<4+quantiz(tstart_thermistor,tstep_thermistor,(3694/log(24.032242*resistance_thermistor)));
greenroshks 8:667fbc82d634 83 else
greenroshks 8:667fbc82d634 84 Sensor.PanelTemperature[(LoopIterator-1)/2]=Sensor.PanelTemperature[(LoopIterator-1)/2]<<4+quantiz(tstart_thermistor,tstep_thermistor,(3364.4792/log(7.60404*resistance_thermistor)));
greenroshks 8:667fbc82d634 85
greenroshks 8:667fbc82d634 86 }
greenroshks 8:667fbc82d634 87 }
greenroshks 8:667fbc82d634 88 float batteryparameters[4];//to populate battery parameters of struct variable Sensor
greenroshks 8:667fbc82d634 89 FUNC_BATTERYGAUGE_MAIN(batteryparameters);//passing array to function
greenroshks 8:667fbc82d634 90
greenroshks 8:667fbc82d634 91 Sensor.Vcell=batteryparameters[0];
greenroshks 8:667fbc82d634 92 Sensor.soc=batteryparameters[1];
greenroshks 8:667fbc82d634 93 Sensor.crate=batteryparameters[2];
greenroshks 8:667fbc82d634 94 Sensor.alerts=batteryparameters[3];
greenroshks 8:667fbc82d634 95
greenroshks 0:8b0d43fe6c05 96 // The following lines are used to iterate the select lines from 0 to 15
greenroshks 8:667fbc82d634 97 int SelectLineIterator;
greenroshks 8:667fbc82d634 98 //following is an algorithm similar to counting binary numbers of 4 bit
greenroshks 8:667fbc82d634 99 for(SelectLineIterator=3;SelectLineIterator>=0;SelectLineIterator--)
greenroshks 8:667fbc82d634 100 {
greenroshks 8:667fbc82d634 101 if(SelectLinesA[SelectLineIterator]==0)
greenroshks 8:667fbc82d634 102 {
greenroshks 8:667fbc82d634 103 SelectLinesA[SelectLineIterator]=1;
greenroshks 8:667fbc82d634 104 break;
greenroshks 8:667fbc82d634 105 }
greenroshks 8:667fbc82d634 106 else SelectLinesA[SelectLineIterator]=0;
greenroshks 8:667fbc82d634 107
greenroshks 8:667fbc82d634 108 SelectLinesB[SelectLineIterator]=SelectLinesA[SelectLineIterator];
greenroshks 8:667fbc82d634 109 SelectLinesC[SelectLineIterator]=SelectLinesA[SelectLineIterator];
greenroshks 8:667fbc82d634 110 }
greenroshks 8:667fbc82d634 111
greenroshks 8:667fbc82d634 112
greenroshks 0:8b0d43fe6c05 113 wait_us(10.0); // A delay of 10 microseconds between each sensor output. Can be changed.
greenroshks 0:8b0d43fe6c05 114
greenroshks 0:8b0d43fe6c05 115 }
greenroshks 8:667fbc82d634 116
greenroshks 8:667fbc82d634 117 }
greenroshks 8:667fbc82d634 118 void FUNC_BATTERYGAUGE_MAIN(float array[])
greenroshks 8:667fbc82d634 119 {
greenroshks 8:667fbc82d634 120 float vcell=master.vcell();
greenroshks 8:667fbc82d634 121 float soc=master.soc();
greenroshks 8:667fbc82d634 122 float crate=master.crate();
greenroshks 8:667fbc82d634 123
greenroshks 8:667fbc82d634 124 printf("\nVcell=%f",vcell);
greenroshks 8:667fbc82d634 125 printf("\nSOC=%f",soc);
greenroshks 8:667fbc82d634 126 printf("\nC_rate=%f",crate);
greenroshks 0:8b0d43fe6c05 127
greenroshks 8:667fbc82d634 128 array[0]=vcell;
greenroshks 8:667fbc82d634 129 array[1]=soc;
greenroshks 8:667fbc82d634 130 array[2]=crate;
greenroshks 8:667fbc82d634 131 if (master.alerting()== true) //alert is on
greenroshks 8:667fbc82d634 132 {
greenroshks 8:667fbc82d634 133 array[3]=master.alertFlags();
greenroshks 8:667fbc82d634 134 master.clearAlert();//clear alert
greenroshks 8:667fbc82d634 135 master.clearAlertFlags();//clear all alert flags
greenroshks 8:667fbc82d634 136 }
greenroshks 8:667fbc82d634 137
greenroshks 8:667fbc82d634 138
greenroshks 8:667fbc82d634 139 }