Programme complet du projet SBra. Lecture de 64 capteur thermique I2C avec 2 multiplexeur TCA9548A, enregistrement sur carte microSD et communication BLE.

Dependencies:   TCA9548A mbed SimpleBLE X_NUCLEO_IDB0XA1 SDFileSystem3 USBDevice

Files at this revision

API Documentation at this revision

Comitter:
adrevong
Date:
Tue Jan 28 11:19:57 2020 +0000
Parent:
8:f9cd29526673
Commit message:

Changed in this revision

LIS3DH_spi.lib Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
TCA9548A.lib Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/LIS3DH_spi.lib	Tue Dec 17 14:25:24 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/users/franzle/code/LIS3DH_spi/#ce2396b1c9a1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Tue Jan 28 11:19:57 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ENSMM/code/SDFileSystem3/#8b2a15992487
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TCA9548A.lib	Tue Jan 28 11:19:57 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/akashvibhute/code/TCA9548A/#768491b5e55d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Tue Jan 28 11:19:57 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/USBDevice/#53949e6131f6
--- a/main.cpp	Tue Dec 17 14:25:24 2019 +0000
+++ b/main.cpp	Tue Jan 28 11:19:57 2020 +0000
@@ -1,95 +1,232 @@
-//Includes
+/*      MEMOS 
+
+Addresses of the different sensors TMP117 :
+            112 (Decimel) = 70 (Hexadecimal)
+            116 (Decimel) = 74 (Hexadecimal)
 
+Addresses of the different sensors TMP117 :
+              Ground  1001000x (0x90/0x91)                      Address 1 
+              V+      1001001x (0x92/0x93)                      Address 2
+              SDA     1001010x (0x94/0x95)                      Address 3
+              SCL     1001011x (0x96/0x97)                      Address 4
+*/
+
+
+int Intervalle_Mesures = 300;      //Choix de l'intervalle de mesure en sec
+
+
+
+ //Includes
 #include "mbed.h"
 #include "SimpleBLE.h"
-#include "LIS3DH.h"
+#include "tca9548a.h"                           // tca954a  libary
+#include "SDFileSystem.h"                       // Sd-Card Libray
+Timer timer;
+#include "USBSerial.h"
 
-//Accelerometer
+#define I2C_SDA                  PB_9           // The µcontrolleur SDA 
+#define I2C_SCL                  PB_8           // The µcontrolleur SCL
 
-#define MOSI PC_12
-#define MISO PC_11
-#define CS PC_5
-#define SCLK PC_10
+//SD Card
+#define MOSI        PA_7
+#define MISO        PA_6
+#define sclk        PA_5
+#define cs          PB_6
+
+
+//USART
+USBSerial pc(0x1f00, 0x2012, 0x0001, false);
 
 //Init simpleBLE
+SimpleBLE ble("SBRA");
 
-SimpleBLE ble("ObCP_CROC_ENSMM");
+// Multiplexeurs
+TCA9548A i2c_sw1(I2C_SDA, I2C_SCL, 0x70);                          
+TCA9548A i2c_sw2(I2C_SDA, I2C_SCL, 0x74);
 
+//Voie I2C du F411
+I2C i2c(I2C_SDA, I2C_SCL);
 
-// GPIO set
+//SD Pinout
+SDFileSystem sd(MOSI, MISO, sclk, cs, "sd");
 
 //Interrupt input
+InterruptIn button(PC_13); //User1
 
-InterruptIn user1(PC_13);  //User1
 
-//PWM output
 
-PwmOut PWMoutput(PB_1);          //Main PWM output
-PwmOut Green(PC_8);              //PWM Red LED
-PwmOut Red(PC_6);                //PWM Green LED
-PwmOut Blue(PC_9);               //PWM Blue LED
+//Variables
+FILE *SDsave;
+float tmp[64];
+char cmd[2];
+//Sensors Address
+char ADD[8];
+    
+    
+// Characteristics BLE_Sensors input
+SimpleChar<float> T11 = ble.readOnly_float(0xA000, 0xA002);
+SimpleChar<float> T21 = ble.readOnly_float(0xA000, 0xA006);
+SimpleChar<float> T31 = ble.readOnly_float(0xA000, 0xA010);
+
+
+
+void SD(int Save){
 
-//Init accelerometer
-
-LIS3DH      acc(MOSI, MISO, SCLK, CS, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_2G);
-
-// Characteristics Accelerometer input
-
-SimpleChar<float> accX = ble.readOnly_float(0xA000, 0xA002);
-SimpleChar<float> accY = ble.readOnly_float(0xA000, 0xA003);
-SimpleChar<float> accZ = ble.readOnly_float(0xA000, 0xA004);
+    if (Save == 2){ 
+    SDsave= fopen("/sd/SBraTests.txt", "w");        // Open the .txt file
+    pc.printf(" initialisation SD OK\t");
+    }
+    
+    if (Save == 1){
+        //SD Save
+        for (int z=0; z<=63; z++){
+            pc.printf(" %0.2f", tmp[z]);            //Affichage pour validation
+            fprintf(SDsave, "%0.3f\t", tmp[z]);     // Savegarde sur carte SD
+            
+            if(SDsave == NULL) { error("Could not open file for write\n");}     
+        }
+        fprintf(SDsave, "\n");
+        pc.printf(" \n\r");
+    }
+    
+    if (Save == 3){
+        fclose(SDsave);
+        pc.printf("Finish\n");
+    }
+}
 
 
-// When characteristic LED RGB changing
-
-void LEDupdate(uint32_t newColor)
-{
-    // read individual bytes
-    uint8_t* channels = (uint8_t*)&newColor;
-
-    // cast to float, as PwmOut expects a value between 0.0f and 1.0f
-    Red   = static_cast<float>(channels[0]) / 255.0f;
-    Green = static_cast<float>(channels[1]) / 255.0f;
-    Blue  = static_cast<float>(channels[2]) / 255.0f;
-}
-
-// When characteristic PWM output changing
-
-void PWMupdate(uint8_t pwmvalue)
-{
-
-    // cast to float, as PwmOut expects a value between 0.0f and 1.0f
-    PWMoutput   = static_cast<float>(pwmvalue) / 255.0f;
+void Sensors(int Lecture){
+    if (Lecture == 0){
+        //Initialisation des capteurs
+        for (int channel=0; channel<=7; channel++){
+            i2c_sw1.select(channel);
+            for (int i=0; i<=6;){
+                cmd[0] = 0x01;
+                cmd[1] = 0x00;
+        
+                i2c.write(ADD[i+1], cmd, 2);
+                cmd[0] = 0x00;
+                i2c.write(ADD[i+1], cmd, 1);
+        
+                wait(0.3);
+                i = i + 2;
+            }
+            wait(0.3);
+        
+            i2c_sw2.select(channel);
+            wait(0.1);
+            for (int i=0; i<=6;){
+                cmd[0] = 0x01;
+                cmd[1] = 0x00;
+        
+                i2c.write(ADD[i+1], cmd, 2);
+                cmd[0] = 0x00;
+                i2c.write(ADD[i+1], cmd, 1);
+        
+                wait(0.3);
+                i = i + 2;
+            }
+        }
+        pc.printf(" initialisation Sensors OK\t");
+    }
+    
+    if (Lecture == 1){
+        int num_capteur = 0;
+        //Valeurs du multiplexeur n°1
+        for (int channel=0; channel<=7; channel++){
+            i2c_sw1.select(channel);
+            //Lecture des sensors de chaine selectionne
+            for (int i=0; i<=6;){
+                cmd[0] = 0;
+                cmd[1] = 0;
+            
+                i2c.read(ADD[i], cmd, 2);
+                wait(0.1);
+        
+                float x = float(cmd[0])*2;
+                float y = float(cmd[1])*0.0078125;
+                tmp[num_capteur] = x + y;
+                //printf(" %0.2f", x+y);//*********
+                i = i+2;
+                num_capteur++;
+            }
+        }
+        wait(0.1);
+        //Valeurs du multiplexeur n°2
+        for (int channel=0; channel<=7; channel++){
+            i2c_sw2.select(channel);
+            //Lecture des sensors de chaine selectionne
+            for (int i=0; i<=6;){
+                cmd[0] = 0;
+                cmd[1] = 0;
+            
+                i2c.read(ADD[i], cmd, 2);
+                wait(0.1);
+        
+                float x = float(cmd[0])*2;
+                float y = float(cmd[1])*0.0078125;
+                tmp[num_capteur] = x + y;
+                //printf(" %0.2f", x+y);//*****
+                i = i+2;
+                num_capteur++;
+            }
+        }
+        SD(1);
+    }
 }
 
 // When characteristic input changing
 void Accupdate()
 {
-
-    accX = float(short((acc.read_reg(LIS3DH_OUT_X_H) << 8) | acc.read_reg(LIS3DH_OUT_X_L))) * 0.001F / 15;
-    accY = float(short((acc.read_reg(LIS3DH_OUT_Y_H) << 8) | acc.read_reg(LIS3DH_OUT_Y_L))) * 0.001F / 15;
-    accZ = float(short((acc.read_reg(LIS3DH_OUT_Z_H) << 8) | acc.read_reg(LIS3DH_OUT_Z_L))) * 0.001F / 15;
-
+    T11 = tmp[0];
+    T21 = tmp[4];
+    T31 = tmp[8];
 }
 
-// Characteritic PWM LED RGB
-SimpleChar<uint32_t> color = ble.writeOnly_u32(0x6200, 0x6201, &LEDupdate);
-
-// Characteristic PWM output
-SimpleChar<uint8_t> pwmout = ble.writeOnly_u8(0xA000, 0xA001, &PWMupdate);
-
-
 //Main program
-
-int main(int, char**)
-{
-
+int main(){
+    
+    timer.start();
+    float time, time1;
+    
+    time = timer.read_ms()/1000;
+//Sensors Address 1
+    ADD[0]=0x90;
+    ADD[1]=0x91;
+//Sensors Address 2
+    ADD[2]=0x92;
+    ADD[3]=0x93;
+//Sensors Address 3
+    ADD[4]=0x94;
+    ADD[5]=0x95;
+//Sensors Address 4
+    ADD[6]=0x96;
+    ADD[7]=0x97;
+    
+    pc.printf("Initialiation...\t");
+    
+    Sensors(0);
+    SD(2);
+    wait(2);
     ble.start();
     Ticker t;
     t.attach(&Accupdate, 5.0f);
+    
+    pc.printf("Initialisation OK\t");
+    pc.printf(" \n\r");
 
-    while (1) {
+    while (button == 1) {
+        time1 = timer.read_ms()/1000;
         ble.waitForEvent();
-
-    }
+        
+        if (time1-time>Intervalle_Mesures){
+            Sensors(1);
+            time=timer.read_ms()/1000;
+            time1=timer.read_ms()/1000;
+        }
+    }SD(3);
 }
+        
+        
+    
--- a/mbed.bld	Tue Dec 17 14:25:24 2019 +0000
+++ b/mbed.bld	Tue Jan 28 11:19:57 2020 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file