TP CAN DII4A

Dependencies:   mbed-rtos mbed

Committer:
yoshiz37
Date:
Sat Jun 03 19:05:11 2017 +0000
Revision:
0:f7a3b8c3f8ce
TP CAN DII4A

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yoshiz37 0:f7a3b8c3f8ce 1 #include "mbed.h"
yoshiz37 0:f7a3b8c3f8ce 2 #include "rtos.h"
yoshiz37 0:f7a3b8c3f8ce 3 #include "tHIH6130.h"
yoshiz37 0:f7a3b8c3f8ce 4
yoshiz37 0:f7a3b8c3f8ce 5 // I2C
yoshiz37 0:f7a3b8c3f8ce 6 #define I2C_SDA p28
yoshiz37 0:f7a3b8c3f8ce 7 #define I2C_SCL p27
yoshiz37 0:f7a3b8c3f8ce 8
yoshiz37 0:f7a3b8c3f8ce 9 // CAN
yoshiz37 0:f7a3b8c3f8ce 10 #define CAN_RD p30
yoshiz37 0:f7a3b8c3f8ce 11 #define CAN_TD p29
yoshiz37 0:f7a3b8c3f8ce 12
yoshiz37 0:f7a3b8c3f8ce 13 #define SIG_RX_CAN 0x01
yoshiz37 0:f7a3b8c3f8ce 14 #define ADR_HIH 0x27 // adresse I2C réelle (sur 7 bits)
yoshiz37 0:f7a3b8c3f8ce 15 #define FREQUENCY_CAN 20000
yoshiz37 0:f7a3b8c3f8ce 16
yoshiz37 0:f7a3b8c3f8ce 17 // DATAS
yoshiz37 0:f7a3b8c3f8ce 18 #define TROP_CHAUD_TEMP 40 // Il fait "trop chaud" lorsqu'on dépasse 40°C
yoshiz37 0:f7a3b8c3f8ce 19 #define TROP_FROID_TEMP -10 // Il fait "trop froid" lorsqu'on est en dessous de 10°C
yoshiz37 0:f7a3b8c3f8ce 20 #define IL_PLEUT 80 // Forte probabilité de pleuvoir lorsqu'on est au dessus de 80% d'humidité
yoshiz37 0:f7a3b8c3f8ce 21
yoshiz37 0:f7a3b8c3f8ce 22 // TEMPORISATION
yoshiz37 0:f7a3b8c3f8ce 23 #define DELAY_ACQUISITION_MS 500
yoshiz37 0:f7a3b8c3f8ce 24
yoshiz37 0:f7a3b8c3f8ce 25 // IDENTIFIANTS TRAMES CAN
yoshiz37 0:f7a3b8c3f8ce 26 // Module HIH6130
yoshiz37 0:f7a3b8c3f8ce 27 #define ID_TEMP 0x011
yoshiz37 0:f7a3b8c3f8ce 28 #define ID_HUMI 0x021
yoshiz37 0:f7a3b8c3f8ce 29 #define ID_TEMP_HUMI 0x031
yoshiz37 0:f7a3b8c3f8ce 30 #define ID_TROP_CHAUD 0x111
yoshiz37 0:f7a3b8c3f8ce 31 #define ID_TEMP_NORMAL 0x112
yoshiz37 0:f7a3b8c3f8ce 32 #define ID_TROP_FROID 0x113
yoshiz37 0:f7a3b8c3f8ce 33 #define ID_IL_PLEUT 0x121
yoshiz37 0:f7a3b8c3f8ce 34 #define ID_IL_PLEUT_PAS 0x122
yoshiz37 0:f7a3b8c3f8ce 35 // Module LCD
yoshiz37 0:f7a3b8c3f8ce 36 #define ID_REQUETE_LCD 0x401
yoshiz37 0:f7a3b8c3f8ce 37
yoshiz37 0:f7a3b8c3f8ce 38
yoshiz37 0:f7a3b8c3f8ce 39 // LED
yoshiz37 0:f7a3b8c3f8ce 40 DigitalOut led1(LED1);
yoshiz37 0:f7a3b8c3f8ce 41
yoshiz37 0:f7a3b8c3f8ce 42 // CAPTEUR HIH6130
yoshiz37 0:f7a3b8c3f8ce 43 I2C i2c(I2C_SDA , I2C_SCL );
yoshiz37 0:f7a3b8c3f8ce 44 tHIH6130 Capteur(ADR_HIH,&i2c);
yoshiz37 0:f7a3b8c3f8ce 45 int currentTemp, currentHumi ;
yoshiz37 0:f7a3b8c3f8ce 46
yoshiz37 0:f7a3b8c3f8ce 47 // CAN
yoshiz37 0:f7a3b8c3f8ce 48 CAN CanPort(CAN_RD, CAN_TD);
yoshiz37 0:f7a3b8c3f8ce 49 CANMessage MessageRx;
yoshiz37 0:f7a3b8c3f8ce 50 CANMessage MessageTx;
yoshiz37 0:f7a3b8c3f8ce 51
yoshiz37 0:f7a3b8c3f8ce 52 // Autres
yoshiz37 0:f7a3b8c3f8ce 53 Thread threadPrincipal;
yoshiz37 0:f7a3b8c3f8ce 54 Thread threadCANSend ;
yoshiz37 0:f7a3b8c3f8ce 55
yoshiz37 0:f7a3b8c3f8ce 56 /**
yoshiz37 0:f7a3b8c3f8ce 57 Fonction d'envoi des données CAN.
yoshiz37 0:f7a3b8c3f8ce 58 **/
yoshiz37 0:f7a3b8c3f8ce 59 void envoiCAN(){
yoshiz37 0:f7a3b8c3f8ce 60 while(true){
yoshiz37 0:f7a3b8c3f8ce 61 Thread::signal_wait(SIG_RX_CAN);
yoshiz37 0:f7a3b8c3f8ce 62
yoshiz37 0:f7a3b8c3f8ce 63 // Config Tx general (à notre protocole applicatif)
yoshiz37 0:f7a3b8c3f8ce 64 MessageTx.format = CANStandard;
yoshiz37 0:f7a3b8c3f8ce 65
yoshiz37 0:f7a3b8c3f8ce 66 // Température datas
yoshiz37 0:f7a3b8c3f8ce 67 MessageTx.type = CANData ;
yoshiz37 0:f7a3b8c3f8ce 68 MessageTx.id = ID_TEMP_HUMI ;
yoshiz37 0:f7a3b8c3f8ce 69 MessageTx.len= 2 ;
yoshiz37 0:f7a3b8c3f8ce 70 MessageTx.data[0] = (char)currentTemp;
yoshiz37 0:f7a3b8c3f8ce 71 MessageTx.data[1] = (char)currentHumi;
yoshiz37 0:f7a3b8c3f8ce 72 CanPort.write(MessageTx);
yoshiz37 0:f7a3b8c3f8ce 73
yoshiz37 0:f7a3b8c3f8ce 74 // Trop chaud/Trop froid/Température normale
yoshiz37 0:f7a3b8c3f8ce 75 // Selection
yoshiz37 0:f7a3b8c3f8ce 76 if(currentTemp >= TROP_CHAUD_TEMP)
yoshiz37 0:f7a3b8c3f8ce 77 MessageTx.id = ID_TROP_CHAUD;
yoshiz37 0:f7a3b8c3f8ce 78 else if(currentTemp <= TROP_FROID_TEMP)
yoshiz37 0:f7a3b8c3f8ce 79 MessageTx.id = ID_TROP_FROID;
yoshiz37 0:f7a3b8c3f8ce 80 else
yoshiz37 0:f7a3b8c3f8ce 81 MessageTx.id = ID_TEMP_NORMAL;
yoshiz37 0:f7a3b8c3f8ce 82
yoshiz37 0:f7a3b8c3f8ce 83 // Envoi
yoshiz37 0:f7a3b8c3f8ce 84 MessageTx.type = CANRemote ;
yoshiz37 0:f7a3b8c3f8ce 85 MessageTx.len = 0 ;
yoshiz37 0:f7a3b8c3f8ce 86 CanPort.write(MessageTx);
yoshiz37 0:f7a3b8c3f8ce 87
yoshiz37 0:f7a3b8c3f8ce 88
yoshiz37 0:f7a3b8c3f8ce 89 // Il pleut/Il pleut pas
yoshiz37 0:f7a3b8c3f8ce 90 // Selection
yoshiz37 0:f7a3b8c3f8ce 91 if(currentHumi >= IL_PLEUT)
yoshiz37 0:f7a3b8c3f8ce 92 MessageTx.id = ID_IL_PLEUT ;
yoshiz37 0:f7a3b8c3f8ce 93 else
yoshiz37 0:f7a3b8c3f8ce 94 MessageTx.id = ID_IL_PLEUT_PAS ;
yoshiz37 0:f7a3b8c3f8ce 95
yoshiz37 0:f7a3b8c3f8ce 96 // Envoi
yoshiz37 0:f7a3b8c3f8ce 97 MessageTx.type = CANRemote ;
yoshiz37 0:f7a3b8c3f8ce 98 MessageTx.len = 0 ;
yoshiz37 0:f7a3b8c3f8ce 99 CanPort.write(MessageTx);
yoshiz37 0:f7a3b8c3f8ce 100 }
yoshiz37 0:f7a3b8c3f8ce 101 }
yoshiz37 0:f7a3b8c3f8ce 102
yoshiz37 0:f7a3b8c3f8ce 103 /**
yoshiz37 0:f7a3b8c3f8ce 104 Fonction de lecture de la communication CAN.
yoshiz37 0:f7a3b8c3f8ce 105 **/
yoshiz37 0:f7a3b8c3f8ce 106 void canReader(){
yoshiz37 0:f7a3b8c3f8ce 107 if (CanPort.read(MessageRx))
yoshiz37 0:f7a3b8c3f8ce 108 {
yoshiz37 0:f7a3b8c3f8ce 109 if(MessageRx.id == ID_REQUETE_LCD){ // Si le message est du LCD alors...
yoshiz37 0:f7a3b8c3f8ce 110 led1 = !led1; // On change l'état de la LED eventuellement (effet de clignotement si rapide)
yoshiz37 0:f7a3b8c3f8ce 111 threadPrincipal.signal_set(SIG_RX_CAN); // On réveille notre thread qui enverra les valeurs des capteurs
yoshiz37 0:f7a3b8c3f8ce 112 }
yoshiz37 0:f7a3b8c3f8ce 113 }
yoshiz37 0:f7a3b8c3f8ce 114 }
yoshiz37 0:f7a3b8c3f8ce 115
yoshiz37 0:f7a3b8c3f8ce 116 /**
yoshiz37 0:f7a3b8c3f8ce 117 Fonction de lecture des données des capteurs.
yoshiz37 0:f7a3b8c3f8ce 118 **/
yoshiz37 0:f7a3b8c3f8ce 119 void lireCapteurs(){
yoshiz37 0:f7a3b8c3f8ce 120 // Récupérer la valeur des capteurs
yoshiz37 0:f7a3b8c3f8ce 121 Capteur.StartMesure();
yoshiz37 0:f7a3b8c3f8ce 122 wait_ms(50);
yoshiz37 0:f7a3b8c3f8ce 123 Capteur.UpdateData();
yoshiz37 0:f7a3b8c3f8ce 124 currentTemp = (int) Capteur.getTemp();
yoshiz37 0:f7a3b8c3f8ce 125 currentHumi = (int) Capteur.getHumi();
yoshiz37 0:f7a3b8c3f8ce 126
yoshiz37 0:f7a3b8c3f8ce 127 }
yoshiz37 0:f7a3b8c3f8ce 128
yoshiz37 0:f7a3b8c3f8ce 129 /**
yoshiz37 0:f7a3b8c3f8ce 130 Fonction d'acquisition des données des capteurs.
yoshiz37 0:f7a3b8c3f8ce 131 **/
yoshiz37 0:f7a3b8c3f8ce 132 void acquisition(){
yoshiz37 0:f7a3b8c3f8ce 133 while(1){
yoshiz37 0:f7a3b8c3f8ce 134 lireCapteurs();
yoshiz37 0:f7a3b8c3f8ce 135 wait_ms(DELAY_ACQUISITION_MS);
yoshiz37 0:f7a3b8c3f8ce 136 }
yoshiz37 0:f7a3b8c3f8ce 137 }
yoshiz37 0:f7a3b8c3f8ce 138
yoshiz37 0:f7a3b8c3f8ce 139 /**
yoshiz37 0:f7a3b8c3f8ce 140 Fonction d'initialisation de la communication CAN et des threads
yoshiz37 0:f7a3b8c3f8ce 141 **/
yoshiz37 0:f7a3b8c3f8ce 142 void init(){
yoshiz37 0:f7a3b8c3f8ce 143 // Initialisation du CAN
yoshiz37 0:f7a3b8c3f8ce 144 CanPort.frequency(FREQUENCY_CAN);
yoshiz37 0:f7a3b8c3f8ce 145
yoshiz37 0:f7a3b8c3f8ce 146 // Attach
yoshiz37 0:f7a3b8c3f8ce 147 CanPort.attach(canReader,CAN::RxIrq);
yoshiz37 0:f7a3b8c3f8ce 148
yoshiz37 0:f7a3b8c3f8ce 149 // Threads
yoshiz37 0:f7a3b8c3f8ce 150 threadPrincipal.start(acquisition);
yoshiz37 0:f7a3b8c3f8ce 151 threadCANSend.start(envoiCAN);
yoshiz37 0:f7a3b8c3f8ce 152 }
yoshiz37 0:f7a3b8c3f8ce 153
yoshiz37 0:f7a3b8c3f8ce 154 int main() {
yoshiz37 0:f7a3b8c3f8ce 155
yoshiz37 0:f7a3b8c3f8ce 156 // initialisation
yoshiz37 0:f7a3b8c3f8ce 157 init();
yoshiz37 0:f7a3b8c3f8ce 158
yoshiz37 0:f7a3b8c3f8ce 159 printf("Start OK\n");
yoshiz37 0:f7a3b8c3f8ce 160
yoshiz37 0:f7a3b8c3f8ce 161 while(1)
yoshiz37 0:f7a3b8c3f8ce 162 {
yoshiz37 0:f7a3b8c3f8ce 163 }
yoshiz37 0:f7a3b8c3f8ce 164 }
yoshiz37 0:f7a3b8c3f8ce 165