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

Committer:
adrevong
Date:
Tue Jan 28 11:19:57 2020 +0000
Revision:
9:0b803d9b4af4
Parent:
7:f5e10b18984d

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adrevong 9:0b803d9b4af4 1 /* MEMOS
adrevong 9:0b803d9b4af4 2
adrevong 9:0b803d9b4af4 3 Addresses of the different sensors TMP117 :
adrevong 9:0b803d9b4af4 4 112 (Decimel) = 70 (Hexadecimal)
adrevong 9:0b803d9b4af4 5 116 (Decimel) = 74 (Hexadecimal)
jimbaud 6:1670244c4eb4 6
adrevong 9:0b803d9b4af4 7 Addresses of the different sensors TMP117 :
adrevong 9:0b803d9b4af4 8 Ground 1001000x (0x90/0x91) Address 1
adrevong 9:0b803d9b4af4 9 V+ 1001001x (0x92/0x93) Address 2
adrevong 9:0b803d9b4af4 10 SDA 1001010x (0x94/0x95) Address 3
adrevong 9:0b803d9b4af4 11 SCL 1001011x (0x96/0x97) Address 4
adrevong 9:0b803d9b4af4 12 */
adrevong 9:0b803d9b4af4 13
adrevong 9:0b803d9b4af4 14
adrevong 9:0b803d9b4af4 15 int Intervalle_Mesures = 300; //Choix de l'intervalle de mesure en sec
adrevong 9:0b803d9b4af4 16
adrevong 9:0b803d9b4af4 17
adrevong 9:0b803d9b4af4 18
adrevong 9:0b803d9b4af4 19 //Includes
janjongboom 0:ba1c49874d3c 20 #include "mbed.h"
janjongboom 0:ba1c49874d3c 21 #include "SimpleBLE.h"
adrevong 9:0b803d9b4af4 22 #include "tca9548a.h" // tca954a libary
adrevong 9:0b803d9b4af4 23 #include "SDFileSystem.h" // Sd-Card Libray
adrevong 9:0b803d9b4af4 24 Timer timer;
adrevong 9:0b803d9b4af4 25 #include "USBSerial.h"
janjongboom 0:ba1c49874d3c 26
adrevong 9:0b803d9b4af4 27 #define I2C_SDA PB_9 // The µcontrolleur SDA
adrevong 9:0b803d9b4af4 28 #define I2C_SCL PB_8 // The µcontrolleur SCL
jimbaud 6:1670244c4eb4 29
adrevong 9:0b803d9b4af4 30 //SD Card
adrevong 9:0b803d9b4af4 31 #define MOSI PA_7
adrevong 9:0b803d9b4af4 32 #define MISO PA_6
adrevong 9:0b803d9b4af4 33 #define sclk PA_5
adrevong 9:0b803d9b4af4 34 #define cs PB_6
adrevong 9:0b803d9b4af4 35
adrevong 9:0b803d9b4af4 36
adrevong 9:0b803d9b4af4 37 //USART
adrevong 9:0b803d9b4af4 38 USBSerial pc(0x1f00, 0x2012, 0x0001, false);
jimbaud 6:1670244c4eb4 39
jimbaud 6:1670244c4eb4 40 //Init simpleBLE
adrevong 9:0b803d9b4af4 41 SimpleBLE ble("SBRA");
jimbaud 6:1670244c4eb4 42
adrevong 9:0b803d9b4af4 43 // Multiplexeurs
adrevong 9:0b803d9b4af4 44 TCA9548A i2c_sw1(I2C_SDA, I2C_SCL, 0x70);
adrevong 9:0b803d9b4af4 45 TCA9548A i2c_sw2(I2C_SDA, I2C_SCL, 0x74);
jimbaud 6:1670244c4eb4 46
adrevong 9:0b803d9b4af4 47 //Voie I2C du F411
adrevong 9:0b803d9b4af4 48 I2C i2c(I2C_SDA, I2C_SCL);
jimbaud 6:1670244c4eb4 49
adrevong 9:0b803d9b4af4 50 //SD Pinout
adrevong 9:0b803d9b4af4 51 SDFileSystem sd(MOSI, MISO, sclk, cs, "sd");
jimbaud 6:1670244c4eb4 52
jimbaud 6:1670244c4eb4 53 //Interrupt input
adrevong 9:0b803d9b4af4 54 InterruptIn button(PC_13); //User1
jimbaud 6:1670244c4eb4 55
jimbaud 6:1670244c4eb4 56
jimbaud 6:1670244c4eb4 57
adrevong 9:0b803d9b4af4 58 //Variables
adrevong 9:0b803d9b4af4 59 FILE *SDsave;
adrevong 9:0b803d9b4af4 60 float tmp[64];
adrevong 9:0b803d9b4af4 61 char cmd[2];
adrevong 9:0b803d9b4af4 62 //Sensors Address
adrevong 9:0b803d9b4af4 63 char ADD[8];
adrevong 9:0b803d9b4af4 64
adrevong 9:0b803d9b4af4 65
adrevong 9:0b803d9b4af4 66 // Characteristics BLE_Sensors input
adrevong 9:0b803d9b4af4 67 SimpleChar<float> T11 = ble.readOnly_float(0xA000, 0xA002);
adrevong 9:0b803d9b4af4 68 SimpleChar<float> T21 = ble.readOnly_float(0xA000, 0xA006);
adrevong 9:0b803d9b4af4 69 SimpleChar<float> T31 = ble.readOnly_float(0xA000, 0xA010);
adrevong 9:0b803d9b4af4 70
adrevong 9:0b803d9b4af4 71
adrevong 9:0b803d9b4af4 72
adrevong 9:0b803d9b4af4 73 void SD(int Save){
jimbaud 6:1670244c4eb4 74
adrevong 9:0b803d9b4af4 75 if (Save == 2){
adrevong 9:0b803d9b4af4 76 SDsave= fopen("/sd/SBraTests.txt", "w"); // Open the .txt file
adrevong 9:0b803d9b4af4 77 pc.printf(" initialisation SD OK\t");
adrevong 9:0b803d9b4af4 78 }
adrevong 9:0b803d9b4af4 79
adrevong 9:0b803d9b4af4 80 if (Save == 1){
adrevong 9:0b803d9b4af4 81 //SD Save
adrevong 9:0b803d9b4af4 82 for (int z=0; z<=63; z++){
adrevong 9:0b803d9b4af4 83 pc.printf(" %0.2f", tmp[z]); //Affichage pour validation
adrevong 9:0b803d9b4af4 84 fprintf(SDsave, "%0.3f\t", tmp[z]); // Savegarde sur carte SD
adrevong 9:0b803d9b4af4 85
adrevong 9:0b803d9b4af4 86 if(SDsave == NULL) { error("Could not open file for write\n");}
adrevong 9:0b803d9b4af4 87 }
adrevong 9:0b803d9b4af4 88 fprintf(SDsave, "\n");
adrevong 9:0b803d9b4af4 89 pc.printf(" \n\r");
adrevong 9:0b803d9b4af4 90 }
adrevong 9:0b803d9b4af4 91
adrevong 9:0b803d9b4af4 92 if (Save == 3){
adrevong 9:0b803d9b4af4 93 fclose(SDsave);
adrevong 9:0b803d9b4af4 94 pc.printf("Finish\n");
adrevong 9:0b803d9b4af4 95 }
adrevong 9:0b803d9b4af4 96 }
jimbaud 6:1670244c4eb4 97
jimbaud 6:1670244c4eb4 98
adrevong 9:0b803d9b4af4 99 void Sensors(int Lecture){
adrevong 9:0b803d9b4af4 100 if (Lecture == 0){
adrevong 9:0b803d9b4af4 101 //Initialisation des capteurs
adrevong 9:0b803d9b4af4 102 for (int channel=0; channel<=7; channel++){
adrevong 9:0b803d9b4af4 103 i2c_sw1.select(channel);
adrevong 9:0b803d9b4af4 104 for (int i=0; i<=6;){
adrevong 9:0b803d9b4af4 105 cmd[0] = 0x01;
adrevong 9:0b803d9b4af4 106 cmd[1] = 0x00;
adrevong 9:0b803d9b4af4 107
adrevong 9:0b803d9b4af4 108 i2c.write(ADD[i+1], cmd, 2);
adrevong 9:0b803d9b4af4 109 cmd[0] = 0x00;
adrevong 9:0b803d9b4af4 110 i2c.write(ADD[i+1], cmd, 1);
adrevong 9:0b803d9b4af4 111
adrevong 9:0b803d9b4af4 112 wait(0.3);
adrevong 9:0b803d9b4af4 113 i = i + 2;
adrevong 9:0b803d9b4af4 114 }
adrevong 9:0b803d9b4af4 115 wait(0.3);
adrevong 9:0b803d9b4af4 116
adrevong 9:0b803d9b4af4 117 i2c_sw2.select(channel);
adrevong 9:0b803d9b4af4 118 wait(0.1);
adrevong 9:0b803d9b4af4 119 for (int i=0; i<=6;){
adrevong 9:0b803d9b4af4 120 cmd[0] = 0x01;
adrevong 9:0b803d9b4af4 121 cmd[1] = 0x00;
adrevong 9:0b803d9b4af4 122
adrevong 9:0b803d9b4af4 123 i2c.write(ADD[i+1], cmd, 2);
adrevong 9:0b803d9b4af4 124 cmd[0] = 0x00;
adrevong 9:0b803d9b4af4 125 i2c.write(ADD[i+1], cmd, 1);
adrevong 9:0b803d9b4af4 126
adrevong 9:0b803d9b4af4 127 wait(0.3);
adrevong 9:0b803d9b4af4 128 i = i + 2;
adrevong 9:0b803d9b4af4 129 }
adrevong 9:0b803d9b4af4 130 }
adrevong 9:0b803d9b4af4 131 pc.printf(" initialisation Sensors OK\t");
adrevong 9:0b803d9b4af4 132 }
adrevong 9:0b803d9b4af4 133
adrevong 9:0b803d9b4af4 134 if (Lecture == 1){
adrevong 9:0b803d9b4af4 135 int num_capteur = 0;
adrevong 9:0b803d9b4af4 136 //Valeurs du multiplexeur n°1
adrevong 9:0b803d9b4af4 137 for (int channel=0; channel<=7; channel++){
adrevong 9:0b803d9b4af4 138 i2c_sw1.select(channel);
adrevong 9:0b803d9b4af4 139 //Lecture des sensors de chaine selectionne
adrevong 9:0b803d9b4af4 140 for (int i=0; i<=6;){
adrevong 9:0b803d9b4af4 141 cmd[0] = 0;
adrevong 9:0b803d9b4af4 142 cmd[1] = 0;
adrevong 9:0b803d9b4af4 143
adrevong 9:0b803d9b4af4 144 i2c.read(ADD[i], cmd, 2);
adrevong 9:0b803d9b4af4 145 wait(0.1);
adrevong 9:0b803d9b4af4 146
adrevong 9:0b803d9b4af4 147 float x = float(cmd[0])*2;
adrevong 9:0b803d9b4af4 148 float y = float(cmd[1])*0.0078125;
adrevong 9:0b803d9b4af4 149 tmp[num_capteur] = x + y;
adrevong 9:0b803d9b4af4 150 //printf(" %0.2f", x+y);//*********
adrevong 9:0b803d9b4af4 151 i = i+2;
adrevong 9:0b803d9b4af4 152 num_capteur++;
adrevong 9:0b803d9b4af4 153 }
adrevong 9:0b803d9b4af4 154 }
adrevong 9:0b803d9b4af4 155 wait(0.1);
adrevong 9:0b803d9b4af4 156 //Valeurs du multiplexeur n°2
adrevong 9:0b803d9b4af4 157 for (int channel=0; channel<=7; channel++){
adrevong 9:0b803d9b4af4 158 i2c_sw2.select(channel);
adrevong 9:0b803d9b4af4 159 //Lecture des sensors de chaine selectionne
adrevong 9:0b803d9b4af4 160 for (int i=0; i<=6;){
adrevong 9:0b803d9b4af4 161 cmd[0] = 0;
adrevong 9:0b803d9b4af4 162 cmd[1] = 0;
adrevong 9:0b803d9b4af4 163
adrevong 9:0b803d9b4af4 164 i2c.read(ADD[i], cmd, 2);
adrevong 9:0b803d9b4af4 165 wait(0.1);
adrevong 9:0b803d9b4af4 166
adrevong 9:0b803d9b4af4 167 float x = float(cmd[0])*2;
adrevong 9:0b803d9b4af4 168 float y = float(cmd[1])*0.0078125;
adrevong 9:0b803d9b4af4 169 tmp[num_capteur] = x + y;
adrevong 9:0b803d9b4af4 170 //printf(" %0.2f", x+y);//*****
adrevong 9:0b803d9b4af4 171 i = i+2;
adrevong 9:0b803d9b4af4 172 num_capteur++;
adrevong 9:0b803d9b4af4 173 }
adrevong 9:0b803d9b4af4 174 }
adrevong 9:0b803d9b4af4 175 SD(1);
adrevong 9:0b803d9b4af4 176 }
jimbaud 6:1670244c4eb4 177 }
jimbaud 6:1670244c4eb4 178
jimbaud 6:1670244c4eb4 179 // When characteristic input changing
jimbaud 6:1670244c4eb4 180 void Accupdate()
jimbaud 6:1670244c4eb4 181 {
adrevong 9:0b803d9b4af4 182 T11 = tmp[0];
adrevong 9:0b803d9b4af4 183 T21 = tmp[4];
adrevong 9:0b803d9b4af4 184 T31 = tmp[8];
jimbaud 6:1670244c4eb4 185 }
jimbaud 6:1670244c4eb4 186
jimbaud 6:1670244c4eb4 187 //Main program
adrevong 9:0b803d9b4af4 188 int main(){
adrevong 9:0b803d9b4af4 189
adrevong 9:0b803d9b4af4 190 timer.start();
adrevong 9:0b803d9b4af4 191 float time, time1;
adrevong 9:0b803d9b4af4 192
adrevong 9:0b803d9b4af4 193 time = timer.read_ms()/1000;
adrevong 9:0b803d9b4af4 194 //Sensors Address 1
adrevong 9:0b803d9b4af4 195 ADD[0]=0x90;
adrevong 9:0b803d9b4af4 196 ADD[1]=0x91;
adrevong 9:0b803d9b4af4 197 //Sensors Address 2
adrevong 9:0b803d9b4af4 198 ADD[2]=0x92;
adrevong 9:0b803d9b4af4 199 ADD[3]=0x93;
adrevong 9:0b803d9b4af4 200 //Sensors Address 3
adrevong 9:0b803d9b4af4 201 ADD[4]=0x94;
adrevong 9:0b803d9b4af4 202 ADD[5]=0x95;
adrevong 9:0b803d9b4af4 203 //Sensors Address 4
adrevong 9:0b803d9b4af4 204 ADD[6]=0x96;
adrevong 9:0b803d9b4af4 205 ADD[7]=0x97;
adrevong 9:0b803d9b4af4 206
adrevong 9:0b803d9b4af4 207 pc.printf("Initialiation...\t");
adrevong 9:0b803d9b4af4 208
adrevong 9:0b803d9b4af4 209 Sensors(0);
adrevong 9:0b803d9b4af4 210 SD(2);
adrevong 9:0b803d9b4af4 211 wait(2);
jimbaud 6:1670244c4eb4 212 ble.start();
jimbaud 6:1670244c4eb4 213 Ticker t;
jimbaud 6:1670244c4eb4 214 t.attach(&Accupdate, 5.0f);
adrevong 9:0b803d9b4af4 215
adrevong 9:0b803d9b4af4 216 pc.printf("Initialisation OK\t");
adrevong 9:0b803d9b4af4 217 pc.printf(" \n\r");
jimbaud 6:1670244c4eb4 218
adrevong 9:0b803d9b4af4 219 while (button == 1) {
adrevong 9:0b803d9b4af4 220 time1 = timer.read_ms()/1000;
jimbaud 6:1670244c4eb4 221 ble.waitForEvent();
adrevong 9:0b803d9b4af4 222
adrevong 9:0b803d9b4af4 223 if (time1-time>Intervalle_Mesures){
adrevong 9:0b803d9b4af4 224 Sensors(1);
adrevong 9:0b803d9b4af4 225 time=timer.read_ms()/1000;
adrevong 9:0b803d9b4af4 226 time1=timer.read_ms()/1000;
adrevong 9:0b803d9b4af4 227 }
adrevong 9:0b803d9b4af4 228 }SD(3);
janjongboom 0:ba1c49874d3c 229 }
adrevong 9:0b803d9b4af4 230
adrevong 9:0b803d9b4af4 231
adrevong 9:0b803d9b4af4 232