Victor Karthik / Mbed 2 deprecated STM32Disco_Melexis

Dependencies:   mbed

Committer:
vctkarthik
Date:
Tue Feb 11 22:51:15 2020 +0000
Revision:
1:0624feb5a279
Parent:
0:7a2e98bb27f5
Child:
2:5e957dcc94bc
Temperature working;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vctkarthik 0:7a2e98bb27f5 1 #include "mbed.h"
vctkarthik 0:7a2e98bb27f5 2
vctkarthik 1:0624feb5a279 3
vctkarthik 0:7a2e98bb27f5 4 /* for the button press device should start reading, then it should stop the loop for button press */
vctkarthik 0:7a2e98bb27f5 5
vctkarthik 0:7a2e98bb27f5 6 AnalogIn analog_value(A0);
vctkarthik 0:7a2e98bb27f5 7 InterruptIn button(PA_0);
vctkarthik 0:7a2e98bb27f5 8 DigitalOut myled(LED1);
vctkarthik 0:7a2e98bb27f5 9 bool workState= false;
vctkarthik 0:7a2e98bb27f5 10
vctkarthik 1:0624feb5a279 11
vctkarthik 1:0624feb5a279 12 I2C i2c(PB_11,PB_10); //sda,scl
vctkarthik 1:0624feb5a279 13
vctkarthik 1:0624feb5a279 14
vctkarthik 1:0624feb5a279 15 const int dev_add_W = 0xB6 ; // address with write(0) bit at last////Address must be bit shifted left,
vctkarthik 1:0624feb5a279 16 const int reg_To_address = 0x27 ;//temperature of the object pointed
vctkarthik 1:0624feb5a279 17
vctkarthik 1:0624feb5a279 18
vctkarthik 1:0624feb5a279 19
vctkarthik 1:0624feb5a279 20
vctkarthik 0:7a2e98bb27f5 21 void read_ecg(bool work)
vctkarthik 0:7a2e98bb27f5 22 {
vctkarthik 0:7a2e98bb27f5 23 float meas_r;
vctkarthik 0:7a2e98bb27f5 24 float meas_v;
vctkarthik 1:0624feb5a279 25 int temp;
vctkarthik 1:0624feb5a279 26 float tempC;
vctkarthik 1:0624feb5a279 27 int ack=1;
vctkarthik 0:7a2e98bb27f5 28
vctkarthik 0:7a2e98bb27f5 29 if(work){
vctkarthik 0:7a2e98bb27f5 30 meas_r = analog_value.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
vctkarthik 0:7a2e98bb27f5 31 meas_v = meas_r * 3300; // Converts value in the 0V-3.3V range
vctkarthik 1:0624feb5a279 32
vctkarthik 1:0624feb5a279 33 char cmd[2];
vctkarthik 1:0624feb5a279 34 char reg[1];
vctkarthik 1:0624feb5a279 35 reg[0] = 0x27;
vctkarthik 1:0624feb5a279 36 // i2c.write(dev_add_W, 1, true);
vctkarthik 1:0624feb5a279 37 // i2c.read(ack);
vctkarthik 1:0624feb5a279 38
vctkarthik 1:0624feb5a279 39 i2c.write(dev_add_W);
vctkarthik 1:0624feb5a279 40 i2c.read(ack);
vctkarthik 1:0624feb5a279 41 i2c.write(reg_To_address);
vctkarthik 1:0624feb5a279 42 i2c.read(ack);
vctkarthik 1:0624feb5a279 43
vctkarthik 1:0624feb5a279 44 // write (int address, const char *data, int length, bool repeated=false)
vctkarthik 1:0624feb5a279 45 i2c.write(dev_add_W,reg,1,true );
vctkarthik 1:0624feb5a279 46 i2c.read(ack);
vctkarthik 1:0624feb5a279 47
vctkarthik 1:0624feb5a279 48 // read (int address, char *data, int length, bool repeated=false)
vctkarthik 1:0624feb5a279 49 i2c.read(dev_add_W, cmd,2,false);
vctkarthik 1:0624feb5a279 50 temp = (cmd[1]<<8)|cmd[0];
vctkarthik 1:0624feb5a279 51 tempC = (0.02*temp) -273.15;
vctkarthik 1:0624feb5a279 52
vctkarthik 1:0624feb5a279 53
vctkarthik 0:7a2e98bb27f5 54
vctkarthik 0:7a2e98bb27f5 55 // Display values
vctkarthik 0:7a2e98bb27f5 56 printf("measure = %f = %.0f mV\n", meas_r, meas_v);
vctkarthik 1:0624feb5a279 57
vctkarthik 1:0624feb5a279 58 //temp =15143;
vctkarthik 1:0624feb5a279 59 printf("Temperature : %f \r\n",tempC);
vctkarthik 1:0624feb5a279 60
vctkarthik 0:7a2e98bb27f5 61 }
vctkarthik 0:7a2e98bb27f5 62
vctkarthik 0:7a2e98bb27f5 63
vctkarthik 0:7a2e98bb27f5 64
vctkarthik 0:7a2e98bb27f5 65
vctkarthik 0:7a2e98bb27f5 66 }
vctkarthik 0:7a2e98bb27f5 67
vctkarthik 0:7a2e98bb27f5 68 void toggle()
vctkarthik 0:7a2e98bb27f5 69 {
vctkarthik 0:7a2e98bb27f5 70 myled = !myled;
vctkarthik 0:7a2e98bb27f5 71 workState = !workState;
vctkarthik 0:7a2e98bb27f5 72
vctkarthik 0:7a2e98bb27f5 73
vctkarthik 0:7a2e98bb27f5 74
vctkarthik 0:7a2e98bb27f5 75
vctkarthik 0:7a2e98bb27f5 76 }
vctkarthik 0:7a2e98bb27f5 77
vctkarthik 0:7a2e98bb27f5 78
vctkarthik 0:7a2e98bb27f5 79
vctkarthik 0:7a2e98bb27f5 80 int main() {
vctkarthik 0:7a2e98bb27f5 81
vctkarthik 0:7a2e98bb27f5 82 button.rise(&toggle) ;
vctkarthik 0:7a2e98bb27f5 83 while(1)
vctkarthik 0:7a2e98bb27f5 84 {
vctkarthik 0:7a2e98bb27f5 85
vctkarthik 0:7a2e98bb27f5 86 button.rise(&toggle) ;
vctkarthik 0:7a2e98bb27f5 87 read_ecg(workState);
vctkarthik 0:7a2e98bb27f5 88
vctkarthik 0:7a2e98bb27f5 89 wait(1);
vctkarthik 0:7a2e98bb27f5 90 }
vctkarthik 0:7a2e98bb27f5 91
vctkarthik 0:7a2e98bb27f5 92
vctkarthik 0:7a2e98bb27f5 93
vctkarthik 0:7a2e98bb27f5 94 }