obd + elm327 + lcd1602

Dependencies:   mbed TextLCD

Revision:
2:17d61cc5f0c0
Parent:
1:7fa61b250083
--- a/main.cpp	Mon Jun 01 23:15:05 2020 +0000
+++ b/main.cpp	Thu Jun 04 14:37:47 2020 +0000
@@ -1,22 +1,26 @@
 #include "mbed.h"
-#include "ssd1306.h"
 #include "codes.h"
- 
+#include "TextLCD.h"
+
+TextLCD lcd(D8, D9, D4, D5, D6, D7); // rs, e, d4-d7
+
 DigitalOut myled(LED1);
 
 //Serial pc(USBTX,USBRX);
 
 Serial device(PA_11, PA_12);
 
+AnalogIn BUTTON_ADC(A0);
+
 Ticker flipper;
 
-SSD1306 lcd (D14, D15); // assumes default I2C address of 0x78
 
 void flip(void);
 void OBDInit(void);
 void read_serial(void);
 
 void ask_intake_pressure_absolute(void);
+void ask_engine_coolant_temperature(void);
 void ask_engine_rpm(void);
 void ask_vehile_speed(void);
 void ask_intake_air_temperature(void);
@@ -25,6 +29,7 @@
 void ask_fuel_rail_pressure(void);
 
 char RESPONSE_INTAKE_PRESSURE_ABSOLUTE[64];
+char RESPONSE_ENGINE_COOLANT_TEMPERATURE[64];
 char RESPONSE_ENGINE_RPM[64];
 char RESPONSE_VEHILE_SPEED[64];
 char RESPONSE_INTAKE_AIR_TEMPERATURE[64];
@@ -38,6 +43,7 @@
 int ENGINE_RPM;
 int VEHILE_SPEED;
 int INTAKE_AIR_TEMPERATURE;
+int ENGINE_COOLANT_TEMPERATURE;
 float THROTTLE_POSITION;
 float SHORT_TERM_FUEL_TRIM;
 float OXYGEN_SENSOR;
@@ -47,13 +53,10 @@
 {
     flipper.attach(&flip, 1.0);
     //pc.baud(115200);
-        
-    lcd.speed (SSD1306::Fast);  // set working frequency
-    lcd.init();                   // initialize SSD1306
-    lcd.set_contrast(255);
-    lcd.cls();                    // clear frame buffer
-    lcd.locate (3,2);             // set text cursor to line 3, column 1
-    lcd.printf ("Starting up"); // print to frame buffer
+
+    lcd.setCursor(TextLCD::CurOff_BlkOff);      
+    lcd.locate (0,0);            
+    lcd.printf ("Starting up"); 
 
     //pc.printf("OBD STM32 v1.1.002");
     //pc.printf("\r\n");
@@ -65,58 +68,78 @@
     //pc.printf("\r\n");
     
     lcd.cls();
-    lcd.locate(3,0);
+    lcd.locate(0,0);
     lcd.printf("OBD Inicializado");
-    lcd.redraw(); 
     
     wait_ms(1500);
     lcd.cls();
-    lcd.redraw();
     
-    int FREQ = 80;   
+    int FREQ = 1000;   
+    int BUTTON;
        
     while(1) 
     {
-        ask_intake_pressure_absolute();
-        lcd.locate(0,0);
-        lcd.printf("%+03.2f  bar",INTAKE_PRESSURE_ABSOLUTE);
-        wait_ms(FREQ);
+        if ((BUTTON_ADC * 3.3) < 3.10)  BUTTON = 4; //pc.printf("SELECT\n\r");
+        if ((BUTTON_ADC * 3.3) < 1.95)  BUTTON = 3; //pc.printf("LEFT\n\r");
+        if ((BUTTON_ADC * 3.3) < 1.23)  BUTTON = 2; //pc.printf("DOWN\n\r");
+        if ((BUTTON_ADC * 3.3) < 0.50)  BUTTON = 1; //pc.printf("UP\n\r");
+        if ((BUTTON_ADC * 3.3) < 0.20)  BUTTON = 0; //pc.printf("RIGHT\n\r");   
         
-        ask_engine_rpm();
-        lcd.locate(1,0);
-        lcd.printf("%04i   RPM",ENGINE_RPM);
-        wait_ms(FREQ);
+        if(BUTTON == 0)
+        {
+            lcd.cls();
+            ask_engine_rpm();
+            lcd.locate(0,0);
+            lcd.printf("%04i   RPM",ENGINE_RPM);
+            wait_ms(FREQ);
         
-        //ask_vehile_speed();
-        lcd.locate(2,0);
-        lcd.printf("%03i    Km/h",VEHILE_SPEED);
-        //wait_ms(FREQ);
+            ask_engine_coolant_temperature();
+            lcd.locate(1,0);
+            lcd.printf("%04i  C",ENGINE_COOLANT_TEMPERATURE);
+            wait_ms(FREQ);
+        }
         
-        ask_intake_air_temperature();
-        lcd.locate(3,0);
-        lcd.printf("%03i    C",INTAKE_AIR_TEMPERATURE);
-        wait_ms(FREQ);
+        if(BUTTON == 1)
+        {
+            lcd.cls();
+            ask_intake_pressure_absolute();
+            lcd.locate(0,0);
+            lcd.printf("%+03.2f  bar",INTAKE_PRESSURE_ABSOLUTE);
+            wait_ms(FREQ);
         
-        ask_throttle_position();
-        lcd.locate(4,0);
-        lcd.printf("%05.2f  pc",THROTTLE_POSITION);
-        wait_ms(FREQ);
+            ask_vehile_speed();
+            lcd.locate(1,0);
+            lcd.printf("%03i    Km/h",VEHILE_SPEED);
+            wait_ms(FREQ);
+        }
         
-        ask_oxygen_sensor();
-        lcd.locate(5,0);
-        lcd.printf("%04.3f  mV",OXYGEN_SENSOR);
-        wait_ms(FREQ);
+        if(BUTTON == 2)
+        {
+            lcd.cls();
+            ask_intake_air_temperature();
+            lcd.locate(0,0);
+            lcd.printf("%03i    C",INTAKE_AIR_TEMPERATURE);
+            wait_ms(FREQ);
         
-        ask_fuel_rail_pressure();
-        lcd.locate(6,0);
-        lcd.printf("%06.2f bar",FUEL_RAIL_PRESSURE);
-        wait_ms(FREQ);
-    
-        lcd.redraw(); 
+            ask_throttle_position();
+            lcd.locate(1,0);
+            lcd.printf("%05.2f  pc",THROTTLE_POSITION);
+            wait_ms(FREQ);
+        }
         
-        //lcd.cls();
+        if(BUTTON == 3)
+        {
+            lcd.cls();
+            ask_oxygen_sensor();
+            lcd.locate(0,0);
+            lcd.printf("%04.3f  mV",OXYGEN_SENSOR);
+            wait_ms(FREQ);
         
-        
+            ask_fuel_rail_pressure();
+            lcd.locate(1,0);
+            lcd.printf("%06.2f bar",FUEL_RAIL_PRESSURE);
+            wait_ms(FREQ);
+        }
     }
 }
 
@@ -207,7 +230,7 @@
     device.printf(PID_VEHILE_SPEED);
     device.printf(CR);
     while(!device.readable()){} //fica esperando chegar algo na serial
-    while(c[i-1]!= '\r')        //chegou algo! vamos ler até o CR
+    while(RESPONSE_VEHILE_SPEED[i-1]!= '\r')        //chegou algo! vamos ler até o CR
     {  
         RESPONSE_VEHILE_SPEED[i] = device.getc();
         i++;
@@ -260,7 +283,42 @@
     
     //pc.printf("%i\r",VAL_A);
         
-    INTAKE_AIR_TEMPERATURE = VAL_A - 40;;
+    INTAKE_AIR_TEMPERATURE = VAL_A - 40;
+    
+    //pc.printf("temp ar %i\r",INTAKE_AIR_TEMPERATURE);
+    
+    //for(int j = 0; j < i; j++)
+    //{ 
+        //pc.printf("%c",RESPONSE_INTAKE_AIR_TEMPERATURE[j]);
+    //}
+}
+
+void ask_engine_coolant_temperature()
+{
+    int i = 0;
+    
+    device.printf(PID_ENGINE_COOLANT_TEMPERATURE);
+    device.printf(CR);
+    while(!device.readable()){} //fica esperando chegar algo na serial
+    while(RESPONSE_ENGINE_COOLANT_TEMPERATURE[i-1]!= '\r')        //chegou algo! vamos ler até o CR
+    {  
+        RESPONSE_ENGINE_COOLANT_TEMPERATURE[i] = device.getc();
+        i++;
+    } 
+    
+    char DATA_A[2];
+    char DATA_B[2];
+    char DATA_C[2];
+    char DATA_D[2];
+        
+    DATA_A[0]  = RESPONSE_ENGINE_COOLANT_TEMPERATURE[i-3];
+    DATA_A[1] = RESPONSE_ENGINE_COOLANT_TEMPERATURE[i-2];
+     
+    int VAL_A = strtol(DATA_A,NULL,16);   
+    
+    //pc.printf("%i\r",VAL_A);
+        
+    ENGINE_COOLANT_TEMPERATURE = VAL_A - 40;
     
     //pc.printf("temp ar %i\r",INTAKE_AIR_TEMPERATURE);