Victor Karthik / Mbed 2 deprecated STM32Disco_Melexis

Dependencies:   mbed

Committer:
vctkarthik
Date:
Thu Dec 03 16:59:50 2020 +0000
Revision:
3:3023306be214
Parent:
2:5e957dcc94bc
stm32f413-Discovery board

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vctkarthik 0:7a2e98bb27f5 1 #include "mbed.h"
vctkarthik 2:5e957dcc94bc 2 //#include "Mlx90615.h"
vctkarthik 3:3023306be214 3 // this is working one for Disco-F413Zh board as of Dec 3, 2020
vctkarthik 0:7a2e98bb27f5 4
vctkarthik 1:0624feb5a279 5
vctkarthik 0:7a2e98bb27f5 6 /* for the button press device should start reading, then it should stop the loop for button press */
vctkarthik 0:7a2e98bb27f5 7
vctkarthik 0:7a2e98bb27f5 8 AnalogIn analog_value(A0);
vctkarthik 0:7a2e98bb27f5 9 InterruptIn button(PA_0);
vctkarthik 0:7a2e98bb27f5 10 DigitalOut myled(LED1);
vctkarthik 0:7a2e98bb27f5 11 bool workState= false;
vctkarthik 3:3023306be214 12
vctkarthik 1:0624feb5a279 13
vctkarthik 1:0624feb5a279 14 I2C i2c(PB_11,PB_10); //sda,scl
vctkarthik 2:5e957dcc94bc 15 //SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd"); //// mosi, miso, sclk, cs, the pinout on the mbed Cool Components workshop board stm32F4
vctkarthik 2:5e957dcc94bc 16
vctkarthik 2:5e957dcc94bc 17 //////////////MLX90615////////////////////////////////////////////////////
vctkarthik 2:5e957dcc94bc 18
vctkarthik 3:3023306be214 19 const int dev_add_W = 0xB6 ; // address with write(0) bit at last////Address must be bit shifted left already shifted,
vctkarthik 3:3023306be214 20 const int reg_To_address = 0x27 ;//address of temperature of the object pointed
vctkarthik 3:3023306be214 21 const int reg_Ta_address = 0x26 ;//address of temperature of the object pointed
vctkarthik 2:5e957dcc94bc 22 //const int reg_emis_address = 0x13 ;//address of the emissivity register
vctkarthik 3:3023306be214 23 //const int emis_human = 16384 ;/// emis_val=0.98*16384+.49;
vctkarthik 2:5e957dcc94bc 24 /*
vctkarthik 2:5e957dcc94bc 25
vctkarthik 2:5e957dcc94bc 26 uint8_t crc8(uint8_t *addr, uint8_t len)
vctkarthik 2:5e957dcc94bc 27 {
vctkarthik 2:5e957dcc94bc 28 uint8_t crc = 0;
vctkarthik 2:5e957dcc94bc 29 while (len--) {
vctkarthik 2:5e957dcc94bc 30 uint8_t inbyte = *addr++;
vctkarthik 2:5e957dcc94bc 31 int i;
vctkarthik 2:5e957dcc94bc 32 for (i = 8; i; i--)
vctkarthik 2:5e957dcc94bc 33 {
vctkarthik 2:5e957dcc94bc 34 uint8_t carry = (crc ^ inbyte) & 0x80;
vctkarthik 2:5e957dcc94bc 35 crc <<= 1;
vctkarthik 2:5e957dcc94bc 36 if (carry)
vctkarthik 2:5e957dcc94bc 37 crc ^= 0x7;
vctkarthik 2:5e957dcc94bc 38 inbyte <<= 1;
vctkarthik 2:5e957dcc94bc 39 }
vctkarthik 2:5e957dcc94bc 40 }
vctkarthik 2:5e957dcc94bc 41 return crc;
vctkarthik 2:5e957dcc94bc 42 } */
vctkarthik 1:0624feb5a279 43
vctkarthik 2:5e957dcc94bc 44 class Mlx90615 {
vctkarthik 2:5e957dcc94bc 45 public:
vctkarthik 2:5e957dcc94bc 46 /* Mlx90615(): { // _pin(pin) means pass pin to the DigitalOut constructor
vctkarthik 2:5e957dcc94bc 47 // default the output to 0
vctkarthik 2:5e957dcc94bc 48 }*/
vctkarthik 3:3023306be214 49
vctkarthik 2:5e957dcc94bc 50 float readTemp(int regAddress)
vctkarthik 2:5e957dcc94bc 51 {
vctkarthik 2:5e957dcc94bc 52 char cmd[2];
vctkarthik 2:5e957dcc94bc 53 char reg[1];
vctkarthik 2:5e957dcc94bc 54 int ack=1;
vctkarthik 2:5e957dcc94bc 55 reg[0] = regAddress;
vctkarthik 3:3023306be214 56
vctkarthik 2:5e957dcc94bc 57 /// write to the address of the device
vctkarthik 2:5e957dcc94bc 58 i2c.write(dev_add_W);
vctkarthik 2:5e957dcc94bc 59 i2c.read(ack);
vctkarthik 2:5e957dcc94bc 60 // write to the reg address
vctkarthik 2:5e957dcc94bc 61 i2c.write(reg_To_address);
vctkarthik 2:5e957dcc94bc 62 i2c.read(ack);
vctkarthik 3:3023306be214 63
vctkarthik 2:5e957dcc94bc 64 // write (int address, const char *data, int length, bool repeated=false)
vctkarthik 2:5e957dcc94bc 65 i2c.write(dev_add_W,reg,1,true ); // write with repeated start
vctkarthik 2:5e957dcc94bc 66 i2c.read(ack);
vctkarthik 3:3023306be214 67
vctkarthik 2:5e957dcc94bc 68 // read (int address, char *data, int length, bool repeated=false)
vctkarthik 2:5e957dcc94bc 69 i2c.read(dev_add_W, cmd,2,false);
vctkarthik 2:5e957dcc94bc 70 int temp = (cmd[1]<<8)|cmd[0]; // raw temp value
vctkarthik 3:3023306be214 71 float tempC = (0.02*temp) -273.15; // temp in Celcius
vctkarthik 2:5e957dcc94bc 72 float tempF= (tempC*9/5) +31;
vctkarthik 3:3023306be214 73
vctkarthik 2:5e957dcc94bc 74 return tempF;
vctkarthik 2:5e957dcc94bc 75
vctkarthik 3:3023306be214 76
vctkarthik 2:5e957dcc94bc 77 }
vctkarthik 3:3023306be214 78
vctkarthik 3:3023306be214 79
vctkarthik 2:5e957dcc94bc 80 //private:;
vctkarthik 3:3023306be214 81
vctkarthik 2:5e957dcc94bc 82 };
vctkarthik 3:3023306be214 83
vctkarthik 2:5e957dcc94bc 84
vctkarthik 2:5e957dcc94bc 85 /*float mlx90615_readTemp(int regAddress)
vctkarthik 2:5e957dcc94bc 86 {
vctkarthik 2:5e957dcc94bc 87 char cmd[2];
vctkarthik 2:5e957dcc94bc 88 char reg[1];
vctkarthik 2:5e957dcc94bc 89 int ack=1;
vctkarthik 2:5e957dcc94bc 90 reg[0] = regAddress;
vctkarthik 3:3023306be214 91
vctkarthik 2:5e957dcc94bc 92 /// write to the address of the device
vctkarthik 2:5e957dcc94bc 93 i2c.write(dev_add_W);
vctkarthik 2:5e957dcc94bc 94 i2c.read(ack);
vctkarthik 2:5e957dcc94bc 95 // write to the reg address
vctkarthik 2:5e957dcc94bc 96 i2c.write(reg_To_address);
vctkarthik 2:5e957dcc94bc 97 i2c.read(ack);
vctkarthik 3:3023306be214 98
vctkarthik 2:5e957dcc94bc 99 // write (int address, const char *data, int length, bool repeated=false)
vctkarthik 2:5e957dcc94bc 100 i2c.write(dev_add_W,reg,1,true ); // write with repeated start
vctkarthik 2:5e957dcc94bc 101 i2c.read(ack);
vctkarthik 3:3023306be214 102
vctkarthik 2:5e957dcc94bc 103 // read (int address, char *data, int length, bool repeated=false)
vctkarthik 2:5e957dcc94bc 104 i2c.read(dev_add_W, cmd,2,false);
vctkarthik 2:5e957dcc94bc 105 int temp = (cmd[1]<<8)|cmd[0]; // raw temp value
vctkarthik 3:3023306be214 106 float tempC = (0.02*temp) -273.15; // temp in Celcius
vctkarthik 2:5e957dcc94bc 107 float tempF= temp;//(tempC*9/5) +31;
vctkarthik 3:3023306be214 108
vctkarthik 2:5e957dcc94bc 109 return tempF;
vctkarthik 2:5e957dcc94bc 110
vctkarthik 3:3023306be214 111
vctkarthik 2:5e957dcc94bc 112 }*/
vctkarthik 2:5e957dcc94bc 113 /*
vctkarthik 2:5e957dcc94bc 114
vctkarthik 2:5e957dcc94bc 115 void mlx90615_emisWrite(int emis_val)
vctkarthik 2:5e957dcc94bc 116 {
vctkarthik 3:3023306be214 117
vctkarthik 3:3023306be214 118
vctkarthik 2:5e957dcc94bc 119 //uint8_t data_low = 0;
vctkarthik 2:5e957dcc94bc 120 // uint8_t data_high = 0;
vctkarthik 2:5e957dcc94bc 121 // int ack=1;
vctkarthik 3:3023306be214 122 //
vctkarthik 3:3023306be214 123 //
vctkarthik 3:3023306be214 124 //
vctkarthik 3:3023306be214 125 // data_low=(uint8_t)(emis_val & 0x00ff);;
vctkarthik 2:5e957dcc94bc 126 // data_high= (uint8_t)((emis_val & 0xff00) >> 8);
vctkarthik 3:3023306be214 127 //
vctkarthik 2:5e957dcc94bc 128 // uint8_t *emisData=0x00;
vctkarthik 2:5e957dcc94bc 129 // uint8_t emisData_A[4];// arrary to keep it in pointer
vctkarthik 2:5e957dcc94bc 130 // emisData_A[0]=dev_add_W;
vctkarthik 2:5e957dcc94bc 131 // emisData_A[1]=reg_emis_address;
vctkarthik 2:5e957dcc94bc 132 // emisData_A[2]=data_low;
vctkarthik 2:5e957dcc94bc 133 // emisData_A[3]=data_high;
vctkarthik 2:5e957dcc94bc 134 // emisData=emisData_A;
vctkarthik 3:3023306be214 135
vctkarthik 3:3023306be214 136
vctkarthik 2:5e957dcc94bc 137 char cmd[4] = { 0 };
vctkarthik 2:5e957dcc94bc 138 uint8_t crcbuf[4] = { 0 };
vctkarthik 3:3023306be214 139
vctkarthik 3:3023306be214 140
vctkarthik 3:3023306be214 141
vctkarthik 2:5e957dcc94bc 142 crcbuf[0] = dev_add_W; //write Address
vctkarthik 2:5e957dcc94bc 143 crcbuf[1] = cmd[0] = reg_emis_address;
vctkarthik 2:5e957dcc94bc 144 crcbuf[2] = cmd[1] = (uint8_t)(emis_val & 0xFF);
vctkarthik 2:5e957dcc94bc 145 crcbuf[3] = cmd[2] = (uint8_t)((emis_val) >> 8);
vctkarthik 2:5e957dcc94bc 146 cmd[3] = crc8(crcbuf, 4);
vctkarthik 3:3023306be214 147
vctkarthik 2:5e957dcc94bc 148 i2c.write(dev_add_W, cmd, 4, true);
vctkarthik 2:5e957dcc94bc 149 wait(0.1);
vctkarthik 3:3023306be214 150
vctkarthik 3:3023306be214 151
vctkarthik 3:3023306be214 152
vctkarthik 3:3023306be214 153
vctkarthik 3:3023306be214 154
vctkarthik 2:5e957dcc94bc 155
vctkarthik 2:5e957dcc94bc 156 // /// write to the address of the device
vctkarthik 2:5e957dcc94bc 157 // i2c.write(dev_add_W);
vctkarthik 2:5e957dcc94bc 158 // i2c.read(ack);
vctkarthik 2:5e957dcc94bc 159 // // write to the reg address
vctkarthik 2:5e957dcc94bc 160 // i2c.write(reg_To_address);
vctkarthik 2:5e957dcc94bc 161 // i2c.read(ack);
vctkarthik 3:3023306be214 162 //
vctkarthik 2:5e957dcc94bc 163 // //write the low 8 bits
vctkarthik 2:5e957dcc94bc 164 // i2c.write(data_low);
vctkarthik 2:5e957dcc94bc 165 // i2c.read(ack);
vctkarthik 3:3023306be214 166 //
vctkarthik 3:3023306be214 167 // //write to the high bits
vctkarthik 2:5e957dcc94bc 168 // i2c.write(data_high);
vctkarthik 2:5e957dcc94bc 169 // i2c.read(ack);
vctkarthik 3:3023306be214 170 //
vctkarthik 2:5e957dcc94bc 171 // //write pec
vctkarthik 2:5e957dcc94bc 172 // i2c.write(crc8(emisData,4));
vctkarthik 2:5e957dcc94bc 173 // i2c.read(ack);
vctkarthik 3:3023306be214 174
vctkarthik 1:0624feb5a279 175
vctkarthik 1:0624feb5a279 176
vctkarthik 1:0624feb5a279 177
vctkarthik 2:5e957dcc94bc 178 }*/
vctkarthik 2:5e957dcc94bc 179
vctkarthik 2:5e957dcc94bc 180 //////////////MLX90615///////////////////////////////////////////////////////////////////////
vctkarthik 1:0624feb5a279 181
vctkarthik 0:7a2e98bb27f5 182 void read_ecg(bool work)
vctkarthik 0:7a2e98bb27f5 183 {
vctkarthik 0:7a2e98bb27f5 184 float meas_r;
vctkarthik 0:7a2e98bb27f5 185 float meas_v;
vctkarthik 3:3023306be214 186 int temp;
vctkarthik 3:3023306be214 187 float tempC;
vctkarthik 3:3023306be214 188 int ack=1;
vctkarthik 2:5e957dcc94bc 189 float bodyTempF;
vctkarthik 3:3023306be214 190 float envTempF;
vctkarthik 2:5e957dcc94bc 191 Mlx90615 mlx;
vctkarthik 2:5e957dcc94bc 192
vctkarthik 3:3023306be214 193
vctkarthik 3:3023306be214 194
vctkarthik 0:7a2e98bb27f5 195 if(work){
vctkarthik 0:7a2e98bb27f5 196 meas_r = analog_value.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
vctkarthik 0:7a2e98bb27f5 197 meas_v = meas_r * 3300; // Converts value in the 0V-3.3V range
vctkarthik 3:3023306be214 198
vctkarthik 3:3023306be214 199 char cmd[2];
vctkarthik 3:3023306be214 200 char reg[1];
vctkarthik 3:3023306be214 201 reg[0] = 0x27;
vctkarthik 3:3023306be214 202 // i2c.write(dev_add_W, 1, true);
vctkarthik 3:3023306be214 203 // i2c.read(ack);
vctkarthik 3:3023306be214 204
vctkarthik 2:5e957dcc94bc 205 bodyTempF = mlx.readTemp(reg_To_address);
vctkarthik 2:5e957dcc94bc 206 envTempF = mlx.readTemp(reg_Ta_address);
vctkarthik 3:3023306be214 207
vctkarthik 3:3023306be214 208 i2c.write(dev_add_W);
vctkarthik 3:3023306be214 209 i2c.read(ack);
vctkarthik 3:3023306be214 210 i2c.write(reg_To_address);
vctkarthik 3:3023306be214 211 i2c.read(ack);
vctkarthik 3:3023306be214 212
vctkarthik 3:3023306be214 213 // write (int address, const char *data, int length, bool repeated=false)
vctkarthik 3:3023306be214 214 i2c.write(dev_add_W,reg,1,true );
vctkarthik 3:3023306be214 215 i2c.read(ack);
vctkarthik 3:3023306be214 216
vctkarthik 3:3023306be214 217 // read (int address, char *data, int length, bool repeated=false)
vctkarthik 3:3023306be214 218 i2c.read(dev_add_W, cmd,2,false);
vctkarthik 3:3023306be214 219 temp = (cmd[1]<<8)|cmd[0];
vctkarthik 3:3023306be214 220 tempC = (0.02*temp) -273.15;
vctkarthik 3:3023306be214 221
vctkarthik 3:3023306be214 222
vctkarthik 3:3023306be214 223
vctkarthik 3:3023306be214 224
vctkarthik 0:7a2e98bb27f5 225 // Display values
vctkarthik 0:7a2e98bb27f5 226 printf("measure = %f = %.0f mV\n", meas_r, meas_v);
vctkarthik 3:3023306be214 227
vctkarthik 3:3023306be214 228 //temp =15143;
vctkarthik 3:3023306be214 229 printf("Temperature : %f \r\n",tempC);
vctkarthik 2:5e957dcc94bc 230 printf("Body Temperature : %f \r\n",bodyTempF);
vctkarthik 3:3023306be214 231
vctkarthik 3:3023306be214 232
vctkarthik 2:5e957dcc94bc 233 printf("Env Temperature : %f \r\n",envTempF);
vctkarthik 3:3023306be214 234
vctkarthik 0:7a2e98bb27f5 235 }
vctkarthik 3:3023306be214 236
vctkarthik 3:3023306be214 237
vctkarthik 3:3023306be214 238
vctkarthik 3:3023306be214 239
vctkarthik 0:7a2e98bb27f5 240 }
vctkarthik 3:3023306be214 241
vctkarthik 3:3023306be214 242
vctkarthik 2:5e957dcc94bc 243 // toggleing the button ////////////// +
vctkarthik 3:3023306be214 244
vctkarthik 3:3023306be214 245
vctkarthik 0:7a2e98bb27f5 246 void toggle()
vctkarthik 0:7a2e98bb27f5 247 {
vctkarthik 3:3023306be214 248 myled = !myled;
vctkarthik 0:7a2e98bb27f5 249 workState = !workState;
vctkarthik 3:3023306be214 250
vctkarthik 3:3023306be214 251
vctkarthik 3:3023306be214 252
vctkarthik 3:3023306be214 253
vctkarthik 0:7a2e98bb27f5 254 }
vctkarthik 3:3023306be214 255
vctkarthik 0:7a2e98bb27f5 256
vctkarthik 0:7a2e98bb27f5 257
vctkarthik 0:7a2e98bb27f5 258 int main() {
vctkarthik 3:3023306be214 259
vctkarthik 0:7a2e98bb27f5 260 button.rise(&toggle) ;
vctkarthik 3:3023306be214 261
vctkarthik 2:5e957dcc94bc 262 //mlx90615_emisWrite(emis_human);
vctkarthik 2:5e957dcc94bc 263 //mlx90615_emisWrite(emis_human);
vctkarthik 2:5e957dcc94bc 264 wait(0.5);
vctkarthik 0:7a2e98bb27f5 265 while(1)
vctkarthik 0:7a2e98bb27f5 266 {
vctkarthik 3:3023306be214 267
vctkarthik 0:7a2e98bb27f5 268 button.rise(&toggle) ;
vctkarthik 0:7a2e98bb27f5 269 read_ecg(workState);
vctkarthik 3:3023306be214 270
vctkarthik 0:7a2e98bb27f5 271 wait(1);
vctkarthik 3:3023306be214 272
vctkarthik 2:5e957dcc94bc 273 }
vctkarthik 3:3023306be214 274
vctkarthik 3:3023306be214 275
vctkarthik 3:3023306be214 276
vctkarthik 0:7a2e98bb27f5 277 }
vctkarthik 2:5e957dcc94bc 278
vctkarthik 2:5e957dcc94bc 279
vctkarthik 2:5e957dcc94bc 280
vctkarthik 2:5e957dcc94bc 281
vctkarthik 2:5e957dcc94bc 282