Bus Can

Dependencies:   mbed ssd1306_library

Files at this revision

API Documentation at this revision

Comitter:
emmanueldavid
Date:
Wed Mar 10 18:24:03 2021 +0000
Parent:
0:793b62d1f1ec
Commit message:
version 10/03

Changed in this revision

main_Carte2.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main_Carte2.cpp	Thu Feb 11 17:05:35 2021 +0000
+++ b/main_Carte2.cpp	Wed Mar 10 18:24:03 2021 +0000
@@ -9,7 +9,6 @@
 
 
 #include "mbed.h"
-//#include "can.h"
 #include "ssd1306.h"
 
 SSD1306 OLED (I2C_SDA, I2C_SCL); // assumes default I2C address of 0x78
@@ -18,15 +17,18 @@
 //Ticker ticker;
 
 DigitalOut ledD9(PA_4);
-//DigitalOut ledD8(PA_5);   // NON, désouder SB16 si utilisation I2c
-//DigitalOut ledD7(PA_6);   // NON, désouder SB18 si utilisation I2c
+DigitalOut ledD8(PA_5);   // NON, désouder SB16 si utilisation I2c
+DigitalOut ledD7(PA_6);   // NON, désouder SB18 si utilisation I2c
 DigitalOut ledD6(PA_7);
 
 DigitalIn   SW4_1(PA_1);
 DigitalIn   SW4_0(PA_3);
+DigitalIn   SW1(PB_4, PullUp); 
+DigitalIn   SW2(PB_5, PullUp); 
+//DigitalIn   SW3(PA_8, PullUp); 
 
-InterruptIn SW1(PB_4, PullUp); 
-InterruptIn SW2(PB_5, PullUp); 
+//InterruptIn SW1(PB_4, PullUp); 
+//InterruptIn SW2(PB_5, PullUp); 
 InterruptIn SW3(PA_8, PullUp); 
 
 /** The constructor takes in RX, and TX pin respectively.
@@ -35,48 +37,62 @@
 //CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
 
 CAN can(PA_11, PA_12);
-Serial pc(USBTX, USBRX);
+//Serial pc(USBTX, USBRX);
 
 char counter = 123;
-char Carte2[] = "K2";
+char Carte2[] = "K2  ";
 char Carte2bb[] = "K2bb";
-static int Nb_messT = 0;
+int Nb_messT = 0;
 int value_SW4=0;
 
+int detectionFrontSW1(void) {
+    int frontDescendant = 0;
+    static int etatPrecedent1=1;
+    int bp = SW1.read();
+    if(bp!=etatPrecedent1 && !bp)
+        frontDescendant = 1;
+    etatPrecedent1= bp;
+    return frontDescendant;
+}
+
+int detectionFrontSW2(void) {
+    int frontDescendant = 0;
+    static int etatPrecedent1=1;
+    int bp = SW2.read();
+    if(bp!=etatPrecedent1 && !bp)
+        frontDescendant = 1;
+    etatPrecedent1= bp;
+    return frontDescendant;
+}
+
 /*
-void send()
-{
-    //pc.printf("Reception \n");
-    if (can.write(CANMessage(1337, &counter, 1))) {
-        //pc.printf("wloop()\n");
-        counter++;
-        //pc.printf("Message sent: %d\n", counter);
-        ledD7 = !ledD7;
-    } 
+int detectionFrontSW3(void) {
+    int frontDescendant = 0;
+    static int etatPrecedent1=1;
+    int bp = SW3.read();
+    if(bp!=etatPrecedent1 && !bp)
+        frontDescendant = 1;
+    etatPrecedent1= bp;
+    return frontDescendant;
 }
 */
 
-void AppuiSW1() {
-    if (can.write(CANMessage(2021, Carte2, 3 , CANData, CANStandard))) {// Rmq : tableau = pointeur
-        ledD6 = !ledD6;
-        Nb_messT++;
+/* Test Arbitrage */
+void AppuiSW3() {
+    if (value_SW4 == 3) {
+        if (can.write(CANMessage(60, Carte2bb, 5 , CANData, CANStandard)))  { // Rmq : tableau = pointeur
+            ledD8 = !ledD8;
+            Nb_messT++; 
+        }
     }
+    if (value_SW4 == 2) {
+        if (can.write(CANMessage(48, Carte2bb, 5 , CANData, CANStandard)))  { // Rmq : tableau = pointeur
+            ledD8 = !ledD8;
+            Nb_messT++; 
+        }
+    }     
 }
 
-void AppuiSW3() {
-    if (value_SW4 == 2) {
-        if (can.write(CANMessage(1111, Carte2bb, 3 , CANData, CANStandard))) {// Rmq : tableau = pointeur
-            ledD6 = !ledD6;
-            Nb_messT++;
-        }
-    }
-    if (value_SW4 == 3) {
-        if (can.write(CANMessage(2222, Carte2bb, 3 , CANData, CANStandard))) {// Rmq : tableau = pointeur
-            ledD6 = !ledD6;
-            Nb_messT++;
-        }
-    }    
-}
 
 int LectureSW4(){
     int ETAT;
@@ -98,6 +114,121 @@
     return ETAT;
 }
 
+
+int main()
+{
+    //pc.baud(115200);
+    //pc.printf("main()\n");
+    
+    can.frequency(500000); 
+    CANMessage msg;
+    
+    char Donnees[8]="";
+    static int Nb_messR = 0;
+    
+    ledD6 = 1; ledD7 = 1; ledD8 = 1; ledD9 = 1;
+    
+    OLED.speed(SSD1306::Medium);  // set working frequency
+    OLED.init();                   // initialize SSD1306
+    OLED.cls();                    // clear frame buffer
+
+    OLED.locate (0,0);
+    OLED.printf ("NbRx = 0");
+    OLED.locate (1,0);
+    OLED.printf ("NbTx = 0");  
+
+    SW3.fall(&AppuiSW3);
+
+    while (1) {
+        //pc.printf("Reception message \n");
+        value_SW4 = LectureSW4();
+        
+        if (detectionFrontSW1())
+        {
+            if (value_SW4 == 0) {
+                if (can.write(CANMessage(2021, Carte2, 5 , CANData, CANStandard))) { // Rmq : tableau = pointeur
+                    ledD6 = !ledD6;
+                    Nb_messT++;
+                }
+            }          
+        } 
+        /*
+        if (detectionFrontSW2())
+        {
+            if (value_SW4 == 0) {
+                if (can.write(CANMessage(2021, CANStandard))) { // Remote frame
+                    ledD7 = !ledD7;
+                    Nb_messT++;
+                }
+            }          
+        }     
+        */   
+        /*
+        if (detectionFrontSW3())
+        {
+            if (value_SW4 == 3) {
+                if (can.write(CANMessage(60, Carte2bb, 5 , CANData, CANStandard)))  { // Rmq : tableau = pointeur
+                    ledD8 = !ledD8;
+                    Nb_messT++; 
+                }
+            }
+            if (value_SW4 == 2) {
+                if (can.write(CANMessage(48, Carte2bb, 5 , CANData, CANStandard)))  { // Rmq : tableau = pointeur
+                    ledD8 = !ledD8;
+                    Nb_messT++; 
+                }
+            }                
+        }          
+        */
+      
+        if (can.read(msg)) {
+            Nb_messR++;
+            ledD9 = !ledD9;
+            
+            for (int i =0; i < msg.len; i++)
+                Donnees[i] = msg.data[i];
+
+            //pc.printf("ID = 0x%.3x\r\n", msg.id); //ID sous forme 0x suivi de l'id
+            //pc.printf("Length = %d\r\n", msg.len);
+            //pc.printf("CAN rderrors : %d, CAN tderrors : %d\n", can.rderror(), can.tderror());
+            
+        }
+        OLED.locate (0,0);
+        OLED.printf ("NbRx = %d", Nb_messR);  
+        OLED.locate (1,0);
+        OLED.printf ("NbTx = %d", Nb_messT); 
+        OLED.locate (3,0);
+        OLED.printf ("Message recu ");
+        OLED.locate (4,0);
+        OLED.printf ("=> ");
+        OLED.puts(Donnees);
+        OLED.locate (6,0);          
+        OLED.printf("Valeur REC=%d", can.rderror());
+        OLED.locate (7,0);          
+        OLED.printf("Valeur TEC=%d", can.tderror());        
+        OLED.redraw();  
+        wait(0.2);
+    }
+}
+
+
+/* Envoi d'une trame de requete */
+/*
+void AppuiSW2() {
+  if (can.write(CANMessage(1620, CANStandard ))) {
+    ledD7 = !ledD7;
+  } 
+}
+*/
+/* Reset controleur après court-circuit */
+/*
+void AppuiSW3() {
+  if (can.write(CANMessage(2021, Reset, 6 , CANData, CANStandard))) // Rmq : tableau = pointeur
+    ledD8 = !ledD8;
+  else
+    can.reset(); 
+}
+*/
 /* Reset controleur après court-circuit */
 /*
 void AppuiSW3() {
@@ -124,20 +255,7 @@
     return (ChaineAffichage);
 }
 */
-int main()
-{
-    pc.baud(115200);
-    can.frequency(500000); 
-    pc.printf("main()\n");
-    //ticker.attach(&send, 2.0);
-    CANMessage msg;
-    
-    char Donnees[8]="";
-    static int Nb_messR = 0;
-    
-    OLED.speed(SSD1306::Medium);  // set working frequency
-    OLED.init();                   // initialize SSD1306
-    OLED.cls();                    // clear frame buffer
+
     /*
     OLED.locate (0,0);             // set text cursor to line 3, column 1
     OLED.printf ("GEII - Bus CAN"); // print to frame buffer
@@ -147,59 +265,6 @@
     OLED.printf ("Envoi trame"); // print to frame buffer    
     OLED.redraw();                 
     */
-    
-    SW1.fall(&AppuiSW1);
-    SW3.fall(&AppuiSW3);
-
-    OLED.locate (0,0);
-    OLED.printf ("Nb Rx = 0");
-    OLED.locate (1,0);
-    OLED.printf ("Nb Tx = 0");  
-
-
-    while (1) {
-        //pc.printf("Reception message \n");
-        value_SW4 = LectureSW4();
-          
-        if (can.read(msg)) {
-            Nb_messR++;
-            ledD9 = !ledD9;
-            
-            for (int i =0; i < msg.len; i++)
-                Donnees[i] = msg.data[i];
-
-            pc.printf("ID = 0x%.3x\r\n", msg.id); //ID sous forme 0x suivi de l'id
-            pc.printf("Length = %d\r\n", msg.len);
-            pc.printf("CAN rderrors : %d, CAN tderrors : %d\n", can.rderror(), can.tderror());
-            
-        }
-        OLED.locate (0,0);
-        OLED.printf ("Nb Rx = %d", Nb_messR);  
-        OLED.locate (1,0);
-        OLED.printf ("Nb Tx = %d", Nb_messT); 
-        OLED.locate (3,0);
-        OLED.printf ("Message recu ");
-        OLED.locate (4,0);
-        OLED.printf ("=> ");
-        OLED.puts(Donnees);
-        OLED.locate (6,0);          
-        OLED.printf("Valeur REC=%d", can.rderror());
-        OLED.locate (7,0);          
-        OLED.printf("Valeur TEC=%d", can.tderror());        
-        OLED.redraw();  
-        wait(0.2);
-        
-        /*
-        if(!SW2)       //Detect Switch Press
-        {
-            ledD7 = !ledD7;
-            while(!SW2);   //Wait for Key release                              
-        }
-        */
-    }
-}
-
-
 
 /*
 char *