ok
Dependencies: mbed ssd1306_library
Diff: main_Carte1.cpp
- Revision:
- 5:9c975d6bd219
- Parent:
- 4:4f681e272268
diff -r 4f681e272268 -r 9c975d6bd219 main_Carte1.cpp --- a/main_Carte1.cpp Wed Feb 10 14:50:41 2021 +0000 +++ b/main_Carte1.cpp Wed Mar 10 18:20:16 2021 +0000 @@ -9,6 +9,9 @@ #include "mbed.h" #include "ssd1306.h" +#include "stm32f3xx_hal_can.h" + +CAN_HandleTypeDef hcan1; typedef enum{ DataFrame, @@ -18,7 +21,8 @@ } TypeTrame; TypeTrame Trame; -SSD1306 OLED (I2C_SDA, I2C_SCL); // assumes default I2C address of 0x78 +SSD1306 OLED (I2C_SDA, I2C_SCL, 0x78); // assumes default I2C address of 0x78 +//SSD1306 OLED (I2C_SDA, I2C_SCL, 0x3C); // assumes default I2C address of 0x78 /* typedef struct{ @@ -28,42 +32,80 @@ } can_msg_t; */ -DigitalOut ledD9(PA_4); -DigitalOut ledD8(PA_5); // Attention, désouder SB16&SB18 si utilisation I2c -DigitalOut ledD7(PA_6); -DigitalOut ledD6(PA_7); +DigitalOut ledD9(PA_4); +DigitalOut ledD8(PA_5); // Attention, désouder SB16&SB18 si utilisation I2c +DigitalOut ledD7(PA_6); +DigitalOut ledD6(PA_7); -DigitalIn SW4_1(PA_1); -DigitalIn SW4_0(PA_3); +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); AnalogIn adc_RV1(PA_0); +//Timer antirebond; CAN can(PA_11, PA_12); -//Serial pc(USBTX, USBRX); +Serial pc(USBTX, USBRX); int value_SW4; // valeur 0,1,2,3 + +char Reset[6] = "Reset"; +char Donnees[12]=""; char Data[5] = "GEII"; +char DataArbitrage[5] = "K1bb"; +int Nb_messT = 0; +int Nb_messR = 0; + -/* Envoi d'une trame de donnees */ -void AppuiSW1() { - if (value_SW4 == 1) - { - if (can.write(CANMessage(2021, Data, 5 , CANData, CANStandard))) // Rmq : tableau = pointeur - ledD6 = !ledD6; - } +int detectionFrontSW1(void) { + int frontDescendant = 0; + static int etatPrecedent1=1; + int bp1 = SW1.read(); + if(bp1!=etatPrecedent1 && !bp1) + frontDescendant = 1; + etatPrecedent1= bp1; + return frontDescendant; } -/* Envoi d'une trame de requete */ -void AppuiSW2() { - if (can.write(CANMessage(1620, CANStandard ))) { - ledD7 = !ledD7; - } +int detectionFrontSW2(void) { + int frontDescendant = 0; + static int etatPrecedent2=1; + int bp2 = SW2.read(); + if(bp2!=etatPrecedent2 && !bp2) + frontDescendant = 1; + etatPrecedent2= bp2; + return frontDescendant; } +/* +int detectionFrontSW3(void) { + int frontDescendant = 0; + static int etatPrecedent1=1; + int bp = SW3.read(); + if(bp!=etatPrecedent1 && !bp) + frontDescendant = 1; + etatPrecedent1= bp; + return frontDescendant; +} +*/ + +/* Test Arbitrage */ +void AppuiSW3() { + if (value_SW4 == 3) { + if (can.write(CANMessage(60, DataArbitrage, 5 , CANData, CANStandard))) { // Rmq : tableau = pointeur + ledD8 = !ledD8; + Nb_messT++; + } + } +} + + int LectureSW4(){ int ETAT; int value_SW4_0 = SW4_0.read(); @@ -84,37 +126,150 @@ return ETAT; } +int main() { + //HAL_Init(); + + //uint8_t receivedMessage[8]; + //CAN_RxHeaderTypeDef RxHeader; -int main() { - //pc.baud(115200); - //pc.printf("Affichage serie\n"); + //HAL_CAN_Start(&hcan); + + pc.baud(115200); + pc.printf("Affichage serie Carte 1 \n"); can.frequency(500000); - //CANMessage msg; + CANMessage msg; + //unsigned char REC, TEC ; + 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); // set text cursor to line 3, column 1 - OLED.printf ("GEII"); // print to frame buffer + OLED.printf ("GEII - Bus CAN"); // print to frame buffer OLED.redraw(); // updates actual display transferring frame buffer over I2C bus + */ + //SW1.fall(&AppuiSW1); + SW3.fall(&AppuiSW3); + //antirebond.start(); - SW1.fall(&AppuiSW1); + OLED.locate (0,0); + OLED.printf ("NbRx = 0"); + OLED.locate (1,0); + OLED.printf ("NbTx = 0"); while(1) { value_SW4 = LectureSW4(); + + if (detectionFrontSW1()) + { + if (value_SW4 == 0) { + if (can.write(CANMessage(1975, Data, 5 , CANData, CANStandard))) { // Rmq : tableau = pointeur + ledD6 = !ledD6; + Nb_messT++; + } + } + } + if (detectionFrontSW2()) + { + if (value_SW4 == 0) { + if (can.write(CANMessage(1975, CANStandard))) { // Remote frame + ledD7 = !ledD7; + Nb_messT++; + } + } + } + /* + if (detectionFrontSW3()) + { + if (value_SW4 == 3) { + if (can.write(CANMessage(60, DataArbitrage, 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("Boucle envoi CAN rderrors : %d, CAN tderrors : %d\n", can.rderror(), can.tderror()); + //REC = can.rderror(); + } + + 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=%u", can.rderror()); + OLED.locate (7,0); + OLED.printf("Valeur TEC=%u", can.tderror()); + OLED.redraw(); + wait(0.2); + } +} + + +/* Envoi d'une trame de requete */ +/* +void AppuiSW2() { + if (can.write(CANMessage(1620, CANStandard ))) { + ledD7 = !ledD7; + } +} +*/ + + + +/* Envoi d'une trame de donnees */ +/* +void AppuiSW1() { + if (antirebond.read_ms() > 100) + { + //while(!SW1); + if (value_SW4 == 1) + { + if (can.write(CANMessage(2021, Data, 5 , CANData, CANStandard))) // Rmq : tableau = pointeur + ledD6 = !ledD6; + } + antirebond.reset(); + } +} +*/ +/* +char *conversionIntChaine(int longueurChaine, int Valeur_int, char *ChaineAffichage) +{ + while (longueurChaine >= 0) + { + ChaineAffichage[longueurChaine] = (Valeur_int % 10) + 48; + Valeur_int /= 10; + longueurChaine--; + } + return (ChaineAffichage); +} +*/ + /* OLED.locate (2,0); - OLED.printf("ADC A0=%2.1f", (adc_RV1.read()*100)); - //printf("ADC VRef = %f\n", adc_vref.read()); + OLED.printf("ADC A0=%2.1f", (adc_RV1.read()*100)); OLED.locate (3,0); OLED.printf("Valeur SW4_1=%d", SW4_0.read()); OLED.locate (4,0); OLED.printf("Valeur SW4_2=%d", SW4_1.read()); OLED.locate (5,0); OLED.printf("Valeur SW4=%d", value_SW4); - OLED.redraw(); - wait(1.0); - } -} \ No newline at end of file + */ \ No newline at end of file