this is BOSCH oil temp. measurement program write for STM32 (Nucleo- F746ZG)

Committer:
rahulp5
Date:
Wed Jan 13 08:20:53 2021 +0000
Revision:
1:e79bc88f73d6
Parent:
0:a1e3728036c5
change temp to 25

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rahulp5 0:a1e3728036c5 1 #include "mbed.h"
rahulp5 0:a1e3728036c5 2
rahulp5 0:a1e3728036c5 3 Serial pc(USBTX, USBRX);
rahulp5 0:a1e3728036c5 4
rahulp5 0:a1e3728036c5 5 AnalogIn vrefint1(PA_3);
rahulp5 0:a1e3728036c5 6 AnalogIn vrefint2(PC_0);
rahulp5 0:a1e3728036c5 7 AnalogIn vrefint3(PC_3);
rahulp5 0:a1e3728036c5 8 AnalogIn vrefint4(PF_3);
rahulp5 0:a1e3728036c5 9 AnalogIn vrefint5(PF_5);
rahulp5 0:a1e3728036c5 10 AnalogIn vrefint6(PF_10);
rahulp5 0:a1e3728036c5 11 AnalogIn vrefint7(PB_1);
rahulp5 0:a1e3728036c5 12 AnalogIn vrefint8(PA_6);
rahulp5 0:a1e3728036c5 13
rahulp5 0:a1e3728036c5 14 DigitalOut Buzzer(PA_5);
rahulp5 0:a1e3728036c5 15
rahulp5 0:a1e3728036c5 16 DigitalOut led1(PF_15);
rahulp5 0:a1e3728036c5 17 DigitalOut led2(PE_13);
rahulp5 0:a1e3728036c5 18 DigitalOut led3(PF_14);
rahulp5 0:a1e3728036c5 19 DigitalOut led4(PE_11);
rahulp5 0:a1e3728036c5 20 DigitalOut led5(PE_9);
rahulp5 0:a1e3728036c5 21 DigitalOut led6(PF_13);
rahulp5 0:a1e3728036c5 22 DigitalOut led7(PF_12);
rahulp5 0:a1e3728036c5 23 DigitalOut led8(PD_15);
rahulp5 0:a1e3728036c5 24
rahulp5 0:a1e3728036c5 25
rahulp5 0:a1e3728036c5 26
rahulp5 0:a1e3728036c5 27 int main()
rahulp5 0:a1e3728036c5 28 {
rahulp5 1:e79bc88f73d6 29 Buzzer = 0;
rahulp5 1:e79bc88f73d6 30 led1 = 0;
rahulp5 1:e79bc88f73d6 31 led2 = 0;
rahulp5 1:e79bc88f73d6 32 led3 = 0;
rahulp5 1:e79bc88f73d6 33 led4 = 0;
rahulp5 1:e79bc88f73d6 34 led5 = 0;
rahulp5 1:e79bc88f73d6 35 led6 = 0;
rahulp5 1:e79bc88f73d6 36 led7 = 0;
rahulp5 1:e79bc88f73d6 37 led8 = 0;
rahulp5 0:a1e3728036c5 38
rahulp5 0:a1e3728036c5 39 float val1, val2, val3, val4, val5, val6, val7, val8;
rahulp5 0:a1e3728036c5 40
rahulp5 0:a1e3728036c5 41 float Volts1, Volts2, Volts3, Volts4, Volts5, Volts6, Volts7, Volts8;
rahulp5 0:a1e3728036c5 42
rahulp5 0:a1e3728036c5 43 float temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8;
rahulp5 0:a1e3728036c5 44
rahulp5 0:a1e3728036c5 45 float tempC1, tempC2, tempC3, tempC4, tempC5, tempC6, tempC7, tempC8;
rahulp5 0:a1e3728036c5 46
rahulp5 0:a1e3728036c5 47 float avg1[50], avg2[50], avg3[50], avg4[50], avg5[50], avg6[50], avg7[50], avg8[50];
rahulp5 0:a1e3728036c5 48
rahulp5 0:a1e3728036c5 49 float totalavg1, totalavg2, totalavg3, totalavg4, totalavg5, totalavg6, totalavg7, totalavg8 = 0.0;
rahulp5 0:a1e3728036c5 50
rahulp5 0:a1e3728036c5 51 float Rx;
rahulp5 1:e79bc88f73d6 52
rahulp5 0:a1e3728036c5 53 //for 3v
rahulp5 0:a1e3728036c5 54 //float C = -74.26;
rahulp5 1:e79bc88f73d6 55
rahulp5 0:a1e3728036c5 56 //for 5v
rahulp5 0:a1e3728036c5 57 float C = 106.85;
rahulp5 1:e79bc88f73d6 58
rahulp5 0:a1e3728036c5 59 //for 3.3v
rahulp5 0:a1e3728036c5 60 //float slope = 167.03;
rahulp5 1:e79bc88f73d6 61
rahulp5 1:e79bc88f73d6 62
rahulp5 0:a1e3728036c5 63 //for 5v
rahulp5 0:a1e3728036c5 64 float slope = 48.31;
rahulp5 0:a1e3728036c5 65
rahulp5 1:e79bc88f73d6 66 //for sensor supply 5v adc reff 3.3v 1) 1.10 for 30 deg c 2) 0.09 fpr 30deg c
rahulp5 1:e79bc88f73d6 67 //float C = 110.42;
rahulp5 1:e79bc88f73d6 68 //float C = 88.9;
rahulp5 1:e79bc88f73d6 69 //float slope = 14.21;
rahulp5 1:e79bc88f73d6 70 //float slope = 20.73;
rahulp5 1:e79bc88f73d6 71
rahulp5 1:e79bc88f73d6 72
rahulp5 1:e79bc88f73d6 73
rahulp5 0:a1e3728036c5 74 float calibration;
rahulp5 0:a1e3728036c5 75
rahulp5 0:a1e3728036c5 76 float R0 = 100.0;
rahulp5 0:a1e3728036c5 77
rahulp5 0:a1e3728036c5 78 float alpha = 0.00385;
rahulp5 0:a1e3728036c5 79
rahulp5 1:e79bc88f73d6 80 float tempTh = 26;
rahulp5 0:a1e3728036c5 81
rahulp5 0:a1e3728036c5 82 while(true)
rahulp5 0:a1e3728036c5 83 {
rahulp5 0:a1e3728036c5 84
rahulp5 0:a1e3728036c5 85 for(int i=0; i < 50; i++)
rahulp5 0:a1e3728036c5 86 {
rahulp5 0:a1e3728036c5 87 val1 = vrefint1.read_u16();
rahulp5 1:e79bc88f73d6 88 //pc.printf("%3.2f",val1);
rahulp5 1:e79bc88f73d6 89 //Volts1 = (val1/65536)*3.3;
rahulp5 0:a1e3728036c5 90 Volts1 = (val1/65536)*3.3;
rahulp5 0:a1e3728036c5 91 Rx = Volts1*slope+C;
rahulp5 0:a1e3728036c5 92 temp1= (Rx/R0-1.0)/alpha;
rahulp5 0:a1e3728036c5 93 calibration=0.3+(0.005*temp1);
rahulp5 0:a1e3728036c5 94 tempC1=temp1-calibration;
rahulp5 0:a1e3728036c5 95 avg1[i]=tempC1;
rahulp5 0:a1e3728036c5 96
rahulp5 0:a1e3728036c5 97 val2 = vrefint2.read_u16();
rahulp5 0:a1e3728036c5 98
rahulp5 0:a1e3728036c5 99 Volts2 = (val2/65536)*3.3;
rahulp5 0:a1e3728036c5 100 Rx = Volts2*slope+C;
rahulp5 0:a1e3728036c5 101 temp2= (Rx/R0-1.0)/alpha;
rahulp5 0:a1e3728036c5 102 calibration=0.3+(0.005*temp2);
rahulp5 0:a1e3728036c5 103 tempC2=temp2-calibration;
rahulp5 0:a1e3728036c5 104 avg2[i]=tempC2;
rahulp5 0:a1e3728036c5 105
rahulp5 0:a1e3728036c5 106 val3 = vrefint3.read_u16();
rahulp5 0:a1e3728036c5 107
rahulp5 0:a1e3728036c5 108 Volts3 = (val3/65536)*3.3;
rahulp5 0:a1e3728036c5 109 Rx = Volts3*slope+C;
rahulp5 0:a1e3728036c5 110 temp3= (Rx/R0-1.0)/alpha;
rahulp5 0:a1e3728036c5 111 calibration=0.3+(0.005*temp3);
rahulp5 0:a1e3728036c5 112 tempC3=temp3-calibration;
rahulp5 0:a1e3728036c5 113 avg3[i]=tempC3;
rahulp5 0:a1e3728036c5 114
rahulp5 0:a1e3728036c5 115 val4 = vrefint4.read_u16();
rahulp5 0:a1e3728036c5 116
rahulp5 0:a1e3728036c5 117 Volts4 = (val4/65536)*3.3;
rahulp5 0:a1e3728036c5 118 Rx = Volts4*slope+C;
rahulp5 0:a1e3728036c5 119 temp4= (Rx/R0-1.0)/alpha;
rahulp5 0:a1e3728036c5 120 calibration=0.3+(0.005*temp4);
rahulp5 0:a1e3728036c5 121 tempC4=temp4-calibration;
rahulp5 0:a1e3728036c5 122 avg4[i]=tempC4;
rahulp5 0:a1e3728036c5 123
rahulp5 0:a1e3728036c5 124 val5 = vrefint5.read_u16();
rahulp5 0:a1e3728036c5 125
rahulp5 0:a1e3728036c5 126 Volts5 = (val5/65536)*3.3;
rahulp5 0:a1e3728036c5 127 Rx = Volts5*slope+C;
rahulp5 0:a1e3728036c5 128 temp5= (Rx/R0-1.0)/alpha;
rahulp5 0:a1e3728036c5 129 calibration=0.3+(0.005*temp5);
rahulp5 0:a1e3728036c5 130 tempC5=temp5-calibration;
rahulp5 0:a1e3728036c5 131 avg5[i]=tempC5;
rahulp5 0:a1e3728036c5 132
rahulp5 0:a1e3728036c5 133 val6 = vrefint6.read_u16();
rahulp5 0:a1e3728036c5 134
rahulp5 0:a1e3728036c5 135 Volts6 = (val6/65536)*3.3;
rahulp5 0:a1e3728036c5 136 Rx = Volts6*slope+C;
rahulp5 0:a1e3728036c5 137 temp6= (Rx/R0-1.0)/alpha;
rahulp5 0:a1e3728036c5 138 calibration=0.3+(0.005*temp6);
rahulp5 0:a1e3728036c5 139 tempC6=temp6-calibration;
rahulp5 0:a1e3728036c5 140 avg6[i]=tempC6;
rahulp5 0:a1e3728036c5 141
rahulp5 0:a1e3728036c5 142 val7 = vrefint7.read_u16();
rahulp5 0:a1e3728036c5 143
rahulp5 0:a1e3728036c5 144 Volts7 = (val7/65536)*3.3;
rahulp5 0:a1e3728036c5 145 Rx = Volts7*slope+C;
rahulp5 0:a1e3728036c5 146 temp7= (Rx/R0-1.0)/alpha;
rahulp5 0:a1e3728036c5 147 calibration=0.3+(0.005*temp7);
rahulp5 0:a1e3728036c5 148 tempC7=temp7-calibration;
rahulp5 0:a1e3728036c5 149 avg7[i]=tempC7;
rahulp5 0:a1e3728036c5 150
rahulp5 0:a1e3728036c5 151 val8 = vrefint8.read_u16();
rahulp5 0:a1e3728036c5 152
rahulp5 0:a1e3728036c5 153 Volts8 = (val8/65536)*3.3;
rahulp5 0:a1e3728036c5 154 Rx = Volts8*slope+C;
rahulp5 0:a1e3728036c5 155 temp8= (Rx/R0-1.0)/alpha;
rahulp5 0:a1e3728036c5 156 calibration=0.3+(0.005*temp8);
rahulp5 0:a1e3728036c5 157 tempC8=temp8-calibration;
rahulp5 0:a1e3728036c5 158 avg8[i]=tempC8;
rahulp5 0:a1e3728036c5 159
rahulp5 1:e79bc88f73d6 160 wait(0.02);
rahulp5 0:a1e3728036c5 161 }
rahulp5 0:a1e3728036c5 162
rahulp5 0:a1e3728036c5 163 for(int j=0; j < 50; j++)
rahulp5 0:a1e3728036c5 164 {
rahulp5 0:a1e3728036c5 165 totalavg1 = totalavg1 + avg1[j];
rahulp5 0:a1e3728036c5 166 totalavg2 = totalavg2 + avg2[j];
rahulp5 0:a1e3728036c5 167 totalavg3 = totalavg3 + avg3[j];
rahulp5 0:a1e3728036c5 168 totalavg4 = totalavg4 + avg4[j];
rahulp5 0:a1e3728036c5 169 totalavg5 = totalavg5 + avg5[j];
rahulp5 0:a1e3728036c5 170 totalavg6 = totalavg6 + avg6[j];
rahulp5 0:a1e3728036c5 171 totalavg7 = totalavg7 + avg7[j];
rahulp5 0:a1e3728036c5 172 totalavg8 = totalavg8 + avg8[j];
rahulp5 0:a1e3728036c5 173 wait(0.01);
rahulp5 0:a1e3728036c5 174 }
rahulp5 1:e79bc88f73d6 175 totalavg1 = (int)totalavg1/50;
rahulp5 1:e79bc88f73d6 176 totalavg2 = (int)totalavg2/50;
rahulp5 1:e79bc88f73d6 177 totalavg3 = (int)totalavg3/50;
rahulp5 1:e79bc88f73d6 178 totalavg4 = (int)totalavg4/50;
rahulp5 1:e79bc88f73d6 179 totalavg5 = (int)totalavg5/50;
rahulp5 1:e79bc88f73d6 180 totalavg6 = (int)totalavg6/50;
rahulp5 1:e79bc88f73d6 181 totalavg7 = (int)totalavg7/50;
rahulp5 1:e79bc88f73d6 182 totalavg8 = (int)totalavg8/50;
rahulp5 0:a1e3728036c5 183
rahulp5 1:e79bc88f73d6 184 totalavg1 -= 6;
rahulp5 1:e79bc88f73d6 185 totalavg2 -= 2;
rahulp5 1:e79bc88f73d6 186 totalavg3 -= 7;
rahulp5 1:e79bc88f73d6 187 totalavg4 -= 2;
rahulp5 1:e79bc88f73d6 188 totalavg5 -= 2;
rahulp5 1:e79bc88f73d6 189 totalavg6 -= 2;
rahulp5 1:e79bc88f73d6 190 totalavg7 -= 2;
rahulp5 1:e79bc88f73d6 191 totalavg8 -= 2;
rahulp5 1:e79bc88f73d6 192
rahulp5 1:e79bc88f73d6 193 pc.printf("%3.2f %3.2f %3.2f %3.2f %3.2f %3.2f %3.2f %3.2f\n", totalavg1, totalavg2, totalavg3, totalavg4, totalavg5, totalavg6, totalavg7, totalavg8);
rahulp5 1:e79bc88f73d6 194
rahulp5 1:e79bc88f73d6 195 if((totalavg1 >= tempTh && totalavg1 < 100 ) || (totalavg2 >= tempTh && totalavg2 < 100 ) || (totalavg3 >= tempTh && totalavg3 < 100 ) || (totalavg4 >= tempTh && totalavg4 < 100) || (totalavg5 >= tempTh && totalavg5 < 100 ) || (totalavg6 >= tempTh && totalavg6 < 100) || (totalavg7 >= tempTh && totalavg7 < 100 ) || (totalavg8 >= tempTh && totalavg8 < 100 ))
rahulp5 1:e79bc88f73d6 196 Buzzer = 1;
rahulp5 1:e79bc88f73d6 197
rahulp5 0:a1e3728036c5 198 else
rahulp5 1:e79bc88f73d6 199 Buzzer = 0;
rahulp5 1:e79bc88f73d6 200
rahulp5 0:a1e3728036c5 201
rahulp5 1:e79bc88f73d6 202 if((totalavg1 >= tempTh) && (totalavg1 < 100))
rahulp5 1:e79bc88f73d6 203 {
rahulp5 1:e79bc88f73d6 204 led1 = 1;
rahulp5 1:e79bc88f73d6 205 //pc.printf("Relay 1 is ON\n");
rahulp5 1:e79bc88f73d6 206 }
rahulp5 1:e79bc88f73d6 207 else
rahulp5 1:e79bc88f73d6 208 {
rahulp5 0:a1e3728036c5 209 led1 = 0;
rahulp5 1:e79bc88f73d6 210 //pc.printf("Relay 1 is OFF\n");
rahulp5 1:e79bc88f73d6 211 }
rahulp5 0:a1e3728036c5 212 totalavg1 = 0;
rahulp5 0:a1e3728036c5 213
rahulp5 1:e79bc88f73d6 214 if((totalavg2 >= tempTh) && (totalavg2 < 100))
rahulp5 1:e79bc88f73d6 215 led2 = 1;
rahulp5 1:e79bc88f73d6 216 else
rahulp5 0:a1e3728036c5 217 led2 = 0;
rahulp5 0:a1e3728036c5 218 totalavg2 = 0;
rahulp5 0:a1e3728036c5 219
rahulp5 1:e79bc88f73d6 220 if((totalavg3 >= tempTh) && (totalavg3 < 100))
rahulp5 1:e79bc88f73d6 221 led3 = 1;
rahulp5 1:e79bc88f73d6 222 else
rahulp5 0:a1e3728036c5 223 led3 = 0;
rahulp5 0:a1e3728036c5 224 totalavg3 = 0;
rahulp5 0:a1e3728036c5 225
rahulp5 1:e79bc88f73d6 226 if((totalavg4 >= tempTh) && (totalavg4 < 100))
rahulp5 1:e79bc88f73d6 227 led4 = 1;
rahulp5 1:e79bc88f73d6 228 else
rahulp5 0:a1e3728036c5 229 led4 = 0;
rahulp5 0:a1e3728036c5 230 totalavg4 = 0;
rahulp5 0:a1e3728036c5 231
rahulp5 1:e79bc88f73d6 232 if((totalavg5 >= tempTh) && (totalavg5 < 100))
rahulp5 1:e79bc88f73d6 233 led5 = 1;
rahulp5 1:e79bc88f73d6 234 else
rahulp5 0:a1e3728036c5 235 led5 = 0;
rahulp5 0:a1e3728036c5 236 totalavg5 = 0;
rahulp5 0:a1e3728036c5 237
rahulp5 1:e79bc88f73d6 238 if((totalavg6 >= tempTh) && (totalavg6 < 100))
rahulp5 1:e79bc88f73d6 239 led6 = 1;
rahulp5 1:e79bc88f73d6 240 else
rahulp5 0:a1e3728036c5 241 led6 = 0;
rahulp5 0:a1e3728036c5 242 totalavg6 = 0;
rahulp5 0:a1e3728036c5 243
rahulp5 1:e79bc88f73d6 244 if((totalavg7 >= tempTh) && (totalavg7 < 100))
rahulp5 1:e79bc88f73d6 245 led7 = 1;
rahulp5 1:e79bc88f73d6 246 else
rahulp5 0:a1e3728036c5 247 led7 = 0;
rahulp5 0:a1e3728036c5 248 totalavg7 = 0;
rahulp5 0:a1e3728036c5 249
rahulp5 1:e79bc88f73d6 250 if((totalavg8 >= tempTh) && (totalavg8 < 100))
rahulp5 1:e79bc88f73d6 251 led8 = 1;
rahulp5 1:e79bc88f73d6 252 else
rahulp5 0:a1e3728036c5 253 led8 = 0;
rahulp5 0:a1e3728036c5 254 totalavg8 = 0;
rahulp5 0:a1e3728036c5 255
rahulp5 0:a1e3728036c5 256
rahulp5 0:a1e3728036c5 257 wait(0.5f);
rahulp5 0:a1e3728036c5 258 //totalavg1, totalavg2, totalavg3, totalavg4, totalavg5, totalavg6, totalavg7, totalavg8 = 0.0;
rahulp5 0:a1e3728036c5 259 }
rahulp5 0:a1e3728036c5 260 }