Nicolas Manceau Polytech 4A Weather Click

Dependencies:   BME280 mbed

Committer:
nicolasmanceau
Date:
Tue Jun 05 16:22:47 2018 +0000
Revision:
0:fc703b278891
Nicolas Manceau; Polytech 4A; Weather Click

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nicolasmanceau 0:fc703b278891 1 /*
nicolasmanceau 0:fc703b278891 2 Nicolas Manceau
nicolasmanceau 0:fc703b278891 3 Polytech - DII4A
nicolasmanceau 0:fc703b278891 4 05/06/2018
nicolasmanceau 0:fc703b278891 5 */
nicolasmanceau 0:fc703b278891 6
nicolasmanceau 0:fc703b278891 7 #include "mbed.h"
nicolasmanceau 0:fc703b278891 8 #include "BME280.h"
nicolasmanceau 0:fc703b278891 9
nicolasmanceau 0:fc703b278891 10 //Serial pc(USBTX, USBRX);
nicolasmanceau 0:fc703b278891 11 #if defined(TARGET_LPC1768)
nicolasmanceau 0:fc703b278891 12 BME280 sensor(p28, p27);
nicolasmanceau 0:fc703b278891 13 #else
nicolasmanceau 0:fc703b278891 14 BME280 sensor(I2C_SDA, I2C_SCL);
nicolasmanceau 0:fc703b278891 15 #endif
nicolasmanceau 0:fc703b278891 16
nicolasmanceau 0:fc703b278891 17 #define SIG_RX_CAN 0x01
nicolasmanceau 0:fc703b278891 18
nicolasmanceau 0:fc703b278891 19 DigitalOut led1(LED1);
nicolasmanceau 0:fc703b278891 20 DigitalOut led2(LED2);
nicolasmanceau 0:fc703b278891 21
nicolasmanceau 0:fc703b278891 22 Thread threadA;
nicolasmanceau 0:fc703b278891 23 Thread threadB;
nicolasmanceau 0:fc703b278891 24 Thread threadC;
nicolasmanceau 0:fc703b278891 25
nicolasmanceau 0:fc703b278891 26 CAN CanPort(p30, p29);
nicolasmanceau 0:fc703b278891 27 CANMessage MessageRx;
nicolasmanceau 0:fc703b278891 28 CANMessage MessageTx;
nicolasmanceau 0:fc703b278891 29
nicolasmanceau 0:fc703b278891 30 unsigned int Id;
nicolasmanceau 0:fc703b278891 31 signed char temperature;
nicolasmanceau 0:fc703b278891 32 unsigned char humidite;
nicolasmanceau 0:fc703b278891 33 unsigned short pression;
nicolasmanceau 0:fc703b278891 34 unsigned char pression_1;
nicolasmanceau 0:fc703b278891 35 unsigned char pression_2;
nicolasmanceau 0:fc703b278891 36
nicolasmanceau 0:fc703b278891 37 void canReader(void)
nicolasmanceau 0:fc703b278891 38 {
nicolasmanceau 0:fc703b278891 39 if (CanPort.read(MessageRx))
nicolasmanceau 0:fc703b278891 40 {
nicolasmanceau 0:fc703b278891 41 led1 = !led1;
nicolasmanceau 0:fc703b278891 42 threadA.signal_set(SIG_RX_CAN);
nicolasmanceau 0:fc703b278891 43 }
nicolasmanceau 0:fc703b278891 44 }
nicolasmanceau 0:fc703b278891 45
nicolasmanceau 0:fc703b278891 46
nicolasmanceau 0:fc703b278891 47 void thA()
nicolasmanceau 0:fc703b278891 48 {
nicolasmanceau 0:fc703b278891 49 while(true)
nicolasmanceau 0:fc703b278891 50 {
nicolasmanceau 0:fc703b278891 51 Thread::signal_wait(SIG_RX_CAN);
nicolasmanceau 0:fc703b278891 52 led2 = !led2;
nicolasmanceau 0:fc703b278891 53 printf("RX FRAME ID = %X\n",MessageRx.id);
nicolasmanceau 0:fc703b278891 54 }
nicolasmanceau 0:fc703b278891 55 }
nicolasmanceau 0:fc703b278891 56
nicolasmanceau 0:fc703b278891 57 void thB()
nicolasmanceau 0:fc703b278891 58 {
nicolasmanceau 0:fc703b278891 59 while (true)
nicolasmanceau 0:fc703b278891 60 {
nicolasmanceau 0:fc703b278891 61 led1 = !led1;
nicolasmanceau 0:fc703b278891 62 printf("CAPTEUR TEMPERATURE\n");
nicolasmanceau 0:fc703b278891 63 MessageTx.id=Id;
nicolasmanceau 0:fc703b278891 64 CanPort.write(MessageTx);
nicolasmanceau 0:fc703b278891 65 wait(1);
nicolasmanceau 0:fc703b278891 66 }
nicolasmanceau 0:fc703b278891 67 }
nicolasmanceau 0:fc703b278891 68
nicolasmanceau 0:fc703b278891 69 void thC()
nicolasmanceau 0:fc703b278891 70 {
nicolasmanceau 0:fc703b278891 71 while (true)
nicolasmanceau 0:fc703b278891 72 {
nicolasmanceau 0:fc703b278891 73 //Récupération de la température dans une variable signée char (1 octet)
nicolasmanceau 0:fc703b278891 74 temperature = sensor.getTemperature();
nicolasmanceau 0:fc703b278891 75 //Récupération de la presion atmosphérique dans une variable non signée short (2 octets)
nicolasmanceau 0:fc703b278891 76 pression = sensor.getPressure();
nicolasmanceau 0:fc703b278891 77 //Récupération de l'humidité dans une variable signée char (1 octet)
nicolasmanceau 0:fc703b278891 78 humidite = sensor.getHumidity();
nicolasmanceau 0:fc703b278891 79
nicolasmanceau 0:fc703b278891 80 //Affectation de la tempèrature dans le premier octet de donnée
nicolasmanceau 0:fc703b278891 81 MessageTx.data[0] = temperature;
nicolasmanceau 0:fc703b278891 82
nicolasmanceau 0:fc703b278891 83 //Séparation de la pression atmosphérique en 2 octets
nicolasmanceau 0:fc703b278891 84 pression_1 = pression & 0b11111111;
nicolasmanceau 0:fc703b278891 85 pression_2 = pression >> 8;
nicolasmanceau 0:fc703b278891 86
nicolasmanceau 0:fc703b278891 87 //Affectation de la pression dans le deuxième et troisème octets de donnée
nicolasmanceau 0:fc703b278891 88 MessageTx.data[1] = pression_2;
nicolasmanceau 0:fc703b278891 89 MessageTx.data[2] = pression_1;
nicolasmanceau 0:fc703b278891 90
nicolasmanceau 0:fc703b278891 91 //Affectation de l'humidité dans le quatrième octet de donnée
nicolasmanceau 0:fc703b278891 92 MessageTx.data[3] = humidite;
nicolasmanceau 0:fc703b278891 93
nicolasmanceau 0:fc703b278891 94 wait(1);
nicolasmanceau 0:fc703b278891 95 }
nicolasmanceau 0:fc703b278891 96 }
nicolasmanceau 0:fc703b278891 97
nicolasmanceau 0:fc703b278891 98 int main()
nicolasmanceau 0:fc703b278891 99 {
nicolasmanceau 0:fc703b278891 100 CanPort.frequency(20000);
nicolasmanceau 0:fc703b278891 101
nicolasmanceau 0:fc703b278891 102 //Définition de l'ID conformément au protocole applicatif définit
nicolasmanceau 0:fc703b278891 103 Id = 0x002;
nicolasmanceau 0:fc703b278891 104 //Définition du nombre d'octet de donnée
nicolasmanceau 0:fc703b278891 105 MessageTx.len=4;
nicolasmanceau 0:fc703b278891 106
nicolasmanceau 0:fc703b278891 107 //CAN Standard
nicolasmanceau 0:fc703b278891 108 MessageTx.format = CANStandard;
nicolasmanceau 0:fc703b278891 109 //MessageTx.format = CANExtended;
nicolasmanceau 0:fc703b278891 110
nicolasmanceau 0:fc703b278891 111 MessageTx.type = CANData;
nicolasmanceau 0:fc703b278891 112 //MessageTx.type = CANRemote;
nicolasmanceau 0:fc703b278891 113
nicolasmanceau 0:fc703b278891 114 CanPort.attach(canReader,CAN::RxIrq);
nicolasmanceau 0:fc703b278891 115
nicolasmanceau 0:fc703b278891 116 //Création des threads
nicolasmanceau 0:fc703b278891 117 threadA.start(thA);
nicolasmanceau 0:fc703b278891 118 threadB.start(thB);
nicolasmanceau 0:fc703b278891 119 threadC.start(thC);
nicolasmanceau 0:fc703b278891 120
nicolasmanceau 0:fc703b278891 121 led1 = 0;
nicolasmanceau 0:fc703b278891 122 printf("Start OK\n");
nicolasmanceau 0:fc703b278891 123
nicolasmanceau 0:fc703b278891 124 while (true)
nicolasmanceau 0:fc703b278891 125 {
nicolasmanceau 0:fc703b278891 126 }
nicolasmanceau 0:fc703b278891 127 }
nicolasmanceau 0:fc703b278891 128