obd + elm327 + lcd1602

Dependencies:   mbed TextLCD

Committer:
Marcelocostanzo
Date:
Mon Jun 01 23:15:05 2020 +0000
Revision:
1:7fa61b250083
Parent:
0:81f7d0ea45b6
Child:
2:17d61cc5f0c0
versao com 57600 de baud, funcionando

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Marcelocostanzo 0:81f7d0ea45b6 1 #include "mbed.h"
Marcelocostanzo 0:81f7d0ea45b6 2 #include "ssd1306.h"
Marcelocostanzo 0:81f7d0ea45b6 3 #include "codes.h"
Marcelocostanzo 0:81f7d0ea45b6 4
Marcelocostanzo 0:81f7d0ea45b6 5 DigitalOut myled(LED1);
Marcelocostanzo 0:81f7d0ea45b6 6
Marcelocostanzo 1:7fa61b250083 7 //Serial pc(USBTX,USBRX);
Marcelocostanzo 0:81f7d0ea45b6 8
Marcelocostanzo 0:81f7d0ea45b6 9 Serial device(PA_11, PA_12);
Marcelocostanzo 0:81f7d0ea45b6 10
Marcelocostanzo 0:81f7d0ea45b6 11 Ticker flipper;
Marcelocostanzo 0:81f7d0ea45b6 12
Marcelocostanzo 0:81f7d0ea45b6 13 SSD1306 lcd (D14, D15); // assumes default I2C address of 0x78
Marcelocostanzo 0:81f7d0ea45b6 14
Marcelocostanzo 0:81f7d0ea45b6 15 void flip(void);
Marcelocostanzo 0:81f7d0ea45b6 16 void OBDInit(void);
Marcelocostanzo 0:81f7d0ea45b6 17 void read_serial(void);
Marcelocostanzo 0:81f7d0ea45b6 18
Marcelocostanzo 0:81f7d0ea45b6 19 void ask_intake_pressure_absolute(void);
Marcelocostanzo 0:81f7d0ea45b6 20 void ask_engine_rpm(void);
Marcelocostanzo 0:81f7d0ea45b6 21 void ask_vehile_speed(void);
Marcelocostanzo 0:81f7d0ea45b6 22 void ask_intake_air_temperature(void);
Marcelocostanzo 0:81f7d0ea45b6 23 void ask_throttle_position(void);
Marcelocostanzo 0:81f7d0ea45b6 24 void ask_oxygen_sensor(void);
Marcelocostanzo 0:81f7d0ea45b6 25 void ask_fuel_rail_pressure(void);
Marcelocostanzo 0:81f7d0ea45b6 26
Marcelocostanzo 0:81f7d0ea45b6 27 char RESPONSE_INTAKE_PRESSURE_ABSOLUTE[64];
Marcelocostanzo 0:81f7d0ea45b6 28 char RESPONSE_ENGINE_RPM[64];
Marcelocostanzo 0:81f7d0ea45b6 29 char RESPONSE_VEHILE_SPEED[64];
Marcelocostanzo 0:81f7d0ea45b6 30 char RESPONSE_INTAKE_AIR_TEMPERATURE[64];
Marcelocostanzo 0:81f7d0ea45b6 31 char RESPONSE_THROTTLE_POSITION[64];
Marcelocostanzo 0:81f7d0ea45b6 32 char RESPONSE_OXYGEN_SENSOR[64];
Marcelocostanzo 0:81f7d0ea45b6 33 char RESPONSE_FUEL_RAIL_PRESSURE[64];
Marcelocostanzo 0:81f7d0ea45b6 34
Marcelocostanzo 0:81f7d0ea45b6 35 char c[64];
Marcelocostanzo 0:81f7d0ea45b6 36
Marcelocostanzo 0:81f7d0ea45b6 37 float INTAKE_PRESSURE_ABSOLUTE;
Marcelocostanzo 0:81f7d0ea45b6 38 int ENGINE_RPM;
Marcelocostanzo 0:81f7d0ea45b6 39 int VEHILE_SPEED;
Marcelocostanzo 0:81f7d0ea45b6 40 int INTAKE_AIR_TEMPERATURE;
Marcelocostanzo 0:81f7d0ea45b6 41 float THROTTLE_POSITION;
Marcelocostanzo 0:81f7d0ea45b6 42 float SHORT_TERM_FUEL_TRIM;
Marcelocostanzo 0:81f7d0ea45b6 43 float OXYGEN_SENSOR;
Marcelocostanzo 0:81f7d0ea45b6 44 float FUEL_RAIL_PRESSURE;
Marcelocostanzo 0:81f7d0ea45b6 45
Marcelocostanzo 0:81f7d0ea45b6 46 int main()
Marcelocostanzo 0:81f7d0ea45b6 47 {
Marcelocostanzo 0:81f7d0ea45b6 48 flipper.attach(&flip, 1.0);
Marcelocostanzo 1:7fa61b250083 49 //pc.baud(115200);
Marcelocostanzo 0:81f7d0ea45b6 50
Marcelocostanzo 1:7fa61b250083 51 lcd.speed (SSD1306::Fast); // set working frequency
Marcelocostanzo 0:81f7d0ea45b6 52 lcd.init(); // initialize SSD1306
Marcelocostanzo 0:81f7d0ea45b6 53 lcd.set_contrast(255);
Marcelocostanzo 0:81f7d0ea45b6 54 lcd.cls(); // clear frame buffer
Marcelocostanzo 0:81f7d0ea45b6 55 lcd.locate (3,2); // set text cursor to line 3, column 1
Marcelocostanzo 0:81f7d0ea45b6 56 lcd.printf ("Starting up"); // print to frame buffer
Marcelocostanzo 0:81f7d0ea45b6 57
Marcelocostanzo 1:7fa61b250083 58 //pc.printf("OBD STM32 v1.1.002");
Marcelocostanzo 1:7fa61b250083 59 //pc.printf("\r\n");
Marcelocostanzo 0:81f7d0ea45b6 60
Marcelocostanzo 1:7fa61b250083 61 device.baud(57600);
Marcelocostanzo 0:81f7d0ea45b6 62 OBDInit();
Marcelocostanzo 0:81f7d0ea45b6 63
Marcelocostanzo 1:7fa61b250083 64 //pc.printf("OBD Inicializado");
Marcelocostanzo 1:7fa61b250083 65 //pc.printf("\r\n");
Marcelocostanzo 0:81f7d0ea45b6 66
Marcelocostanzo 0:81f7d0ea45b6 67 lcd.cls();
Marcelocostanzo 0:81f7d0ea45b6 68 lcd.locate(3,0);
Marcelocostanzo 0:81f7d0ea45b6 69 lcd.printf("OBD Inicializado");
Marcelocostanzo 0:81f7d0ea45b6 70 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 71
Marcelocostanzo 0:81f7d0ea45b6 72 wait_ms(1500);
Marcelocostanzo 0:81f7d0ea45b6 73 lcd.cls();
Marcelocostanzo 0:81f7d0ea45b6 74 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 75
Marcelocostanzo 1:7fa61b250083 76 int FREQ = 80;
Marcelocostanzo 0:81f7d0ea45b6 77
Marcelocostanzo 0:81f7d0ea45b6 78 while(1)
Marcelocostanzo 0:81f7d0ea45b6 79 {
Marcelocostanzo 0:81f7d0ea45b6 80 ask_intake_pressure_absolute();
Marcelocostanzo 1:7fa61b250083 81 lcd.locate(0,0);
Marcelocostanzo 1:7fa61b250083 82 lcd.printf("%+03.2f bar",INTAKE_PRESSURE_ABSOLUTE);
Marcelocostanzo 0:81f7d0ea45b6 83 wait_ms(FREQ);
Marcelocostanzo 1:7fa61b250083 84
Marcelocostanzo 1:7fa61b250083 85 ask_engine_rpm();
Marcelocostanzo 1:7fa61b250083 86 lcd.locate(1,0);
Marcelocostanzo 1:7fa61b250083 87 lcd.printf("%04i RPM",ENGINE_RPM);
Marcelocostanzo 0:81f7d0ea45b6 88 wait_ms(FREQ);
Marcelocostanzo 0:81f7d0ea45b6 89
Marcelocostanzo 1:7fa61b250083 90 //ask_vehile_speed();
Marcelocostanzo 0:81f7d0ea45b6 91 lcd.locate(2,0);
Marcelocostanzo 0:81f7d0ea45b6 92 lcd.printf("%03i Km/h",VEHILE_SPEED);
Marcelocostanzo 1:7fa61b250083 93 //wait_ms(FREQ);
Marcelocostanzo 1:7fa61b250083 94
Marcelocostanzo 1:7fa61b250083 95 ask_intake_air_temperature();
Marcelocostanzo 0:81f7d0ea45b6 96 lcd.locate(3,0);
Marcelocostanzo 0:81f7d0ea45b6 97 lcd.printf("%03i C",INTAKE_AIR_TEMPERATURE);
Marcelocostanzo 1:7fa61b250083 98 wait_ms(FREQ);
Marcelocostanzo 1:7fa61b250083 99
Marcelocostanzo 1:7fa61b250083 100 ask_throttle_position();
Marcelocostanzo 0:81f7d0ea45b6 101 lcd.locate(4,0);
Marcelocostanzo 0:81f7d0ea45b6 102 lcd.printf("%05.2f pc",THROTTLE_POSITION);
Marcelocostanzo 1:7fa61b250083 103 wait_ms(FREQ);
Marcelocostanzo 1:7fa61b250083 104
Marcelocostanzo 1:7fa61b250083 105 ask_oxygen_sensor();
Marcelocostanzo 0:81f7d0ea45b6 106 lcd.locate(5,0);
Marcelocostanzo 0:81f7d0ea45b6 107 lcd.printf("%04.3f mV",OXYGEN_SENSOR);
Marcelocostanzo 1:7fa61b250083 108 wait_ms(FREQ);
Marcelocostanzo 1:7fa61b250083 109
Marcelocostanzo 1:7fa61b250083 110 ask_fuel_rail_pressure();
Marcelocostanzo 0:81f7d0ea45b6 111 lcd.locate(6,0);
Marcelocostanzo 0:81f7d0ea45b6 112 lcd.printf("%06.2f bar",FUEL_RAIL_PRESSURE);
Marcelocostanzo 1:7fa61b250083 113 wait_ms(FREQ);
Marcelocostanzo 1:7fa61b250083 114
Marcelocostanzo 0:81f7d0ea45b6 115 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 116
Marcelocostanzo 0:81f7d0ea45b6 117 //lcd.cls();
Marcelocostanzo 0:81f7d0ea45b6 118
Marcelocostanzo 0:81f7d0ea45b6 119
Marcelocostanzo 0:81f7d0ea45b6 120 }
Marcelocostanzo 0:81f7d0ea45b6 121 }
Marcelocostanzo 0:81f7d0ea45b6 122
Marcelocostanzo 0:81f7d0ea45b6 123 void flip()
Marcelocostanzo 0:81f7d0ea45b6 124 {
Marcelocostanzo 0:81f7d0ea45b6 125 myled = !myled;
Marcelocostanzo 0:81f7d0ea45b6 126 }
Marcelocostanzo 0:81f7d0ea45b6 127
Marcelocostanzo 0:81f7d0ea45b6 128 void ask_intake_pressure_absolute()
Marcelocostanzo 0:81f7d0ea45b6 129 {
Marcelocostanzo 0:81f7d0ea45b6 130 int i = 0;
Marcelocostanzo 0:81f7d0ea45b6 131
Marcelocostanzo 0:81f7d0ea45b6 132 device.printf(PID_INTAKE_PRESSURE_ABSOLUTE);
Marcelocostanzo 0:81f7d0ea45b6 133 device.printf(CR);
Marcelocostanzo 0:81f7d0ea45b6 134 while(!device.readable()){} //fica esperando chegar algo na serial
Marcelocostanzo 0:81f7d0ea45b6 135 while(RESPONSE_INTAKE_PRESSURE_ABSOLUTE[i-1]!= '\r') //chegou algo! vamos ler até o CR
Marcelocostanzo 0:81f7d0ea45b6 136 {
Marcelocostanzo 0:81f7d0ea45b6 137 RESPONSE_INTAKE_PRESSURE_ABSOLUTE[i] = device.getc();
Marcelocostanzo 0:81f7d0ea45b6 138 i++;
Marcelocostanzo 0:81f7d0ea45b6 139 }
Marcelocostanzo 0:81f7d0ea45b6 140
Marcelocostanzo 0:81f7d0ea45b6 141 char DATA_A[2];
Marcelocostanzo 0:81f7d0ea45b6 142 char DATA_B[2];
Marcelocostanzo 0:81f7d0ea45b6 143 char DATA_C[2];
Marcelocostanzo 0:81f7d0ea45b6 144 char DATA_D[2];
Marcelocostanzo 0:81f7d0ea45b6 145
Marcelocostanzo 0:81f7d0ea45b6 146 DATA_A[0] = RESPONSE_INTAKE_PRESSURE_ABSOLUTE[i-3];
Marcelocostanzo 0:81f7d0ea45b6 147 DATA_A[1] = RESPONSE_INTAKE_PRESSURE_ABSOLUTE[i-2];
Marcelocostanzo 0:81f7d0ea45b6 148
Marcelocostanzo 0:81f7d0ea45b6 149 int VAL_A = strtol(DATA_A,NULL,16);
Marcelocostanzo 0:81f7d0ea45b6 150
Marcelocostanzo 0:81f7d0ea45b6 151 //pc.printf("%i\r",VAL_A);
Marcelocostanzo 0:81f7d0ea45b6 152
Marcelocostanzo 0:81f7d0ea45b6 153 INTAKE_PRESSURE_ABSOLUTE = (VAL_A * 0.01f) - 1.0f;
Marcelocostanzo 0:81f7d0ea45b6 154
Marcelocostanzo 1:7fa61b250083 155 //pc.printf("pressao %i\r",INTAKE_AIR_TEMPERATURE);
Marcelocostanzo 0:81f7d0ea45b6 156
Marcelocostanzo 1:7fa61b250083 157 //for(int j = 0; j < i; j++)
Marcelocostanzo 1:7fa61b250083 158 //{
Marcelocostanzo 1:7fa61b250083 159 //pc.printf("%c",RESPONSE_INTAKE_PRESSURE_ABSOLUTE[j]);
Marcelocostanzo 1:7fa61b250083 160 //}
Marcelocostanzo 0:81f7d0ea45b6 161 }
Marcelocostanzo 0:81f7d0ea45b6 162
Marcelocostanzo 0:81f7d0ea45b6 163 void ask_engine_rpm()
Marcelocostanzo 0:81f7d0ea45b6 164 {
Marcelocostanzo 0:81f7d0ea45b6 165 int i = 0;
Marcelocostanzo 0:81f7d0ea45b6 166
Marcelocostanzo 0:81f7d0ea45b6 167 device.printf(PID_ENGINE_RPM);
Marcelocostanzo 0:81f7d0ea45b6 168 device.printf(CR);
Marcelocostanzo 0:81f7d0ea45b6 169 while(!device.readable()){} //fica esperando chegar algo na serial
Marcelocostanzo 0:81f7d0ea45b6 170 while(RESPONSE_ENGINE_RPM[i-1]!= '\r') //chegou algo! vamos ler até o CR
Marcelocostanzo 0:81f7d0ea45b6 171 {
Marcelocostanzo 0:81f7d0ea45b6 172 RESPONSE_ENGINE_RPM[i] = device.getc();
Marcelocostanzo 0:81f7d0ea45b6 173 i++;
Marcelocostanzo 0:81f7d0ea45b6 174 }
Marcelocostanzo 0:81f7d0ea45b6 175
Marcelocostanzo 0:81f7d0ea45b6 176 char DATA_A[2];
Marcelocostanzo 0:81f7d0ea45b6 177 char DATA_B[2];
Marcelocostanzo 0:81f7d0ea45b6 178 char DATA_C[2];
Marcelocostanzo 0:81f7d0ea45b6 179 char DATA_D[2];
Marcelocostanzo 0:81f7d0ea45b6 180
Marcelocostanzo 0:81f7d0ea45b6 181 DATA_A[0] = RESPONSE_ENGINE_RPM[i-5];
Marcelocostanzo 0:81f7d0ea45b6 182 DATA_A[1] = RESPONSE_ENGINE_RPM[i-4];
Marcelocostanzo 0:81f7d0ea45b6 183
Marcelocostanzo 0:81f7d0ea45b6 184 DATA_B[0] = RESPONSE_ENGINE_RPM[i-3];
Marcelocostanzo 0:81f7d0ea45b6 185 DATA_B[1] = RESPONSE_ENGINE_RPM[i-2];
Marcelocostanzo 0:81f7d0ea45b6 186
Marcelocostanzo 0:81f7d0ea45b6 187 int VAL_A = strtol(DATA_A,NULL,16);
Marcelocostanzo 0:81f7d0ea45b6 188 int VAL_B = strtol(DATA_B,NULL,16);
Marcelocostanzo 0:81f7d0ea45b6 189
Marcelocostanzo 0:81f7d0ea45b6 190 //pc.printf("%i\r",VAL_A);
Marcelocostanzo 0:81f7d0ea45b6 191 //pc.printf("%i\r",VAL_B);
Marcelocostanzo 0:81f7d0ea45b6 192
Marcelocostanzo 0:81f7d0ea45b6 193 ENGINE_RPM = ((VAL_A * 256) + VAL_B) / 4;
Marcelocostanzo 0:81f7d0ea45b6 194
Marcelocostanzo 1:7fa61b250083 195 //pc.printf("rpm %i\r",ENGINE_RPM);
Marcelocostanzo 0:81f7d0ea45b6 196
Marcelocostanzo 1:7fa61b250083 197 //for(int j = 0; j < i; j++)
Marcelocostanzo 1:7fa61b250083 198 //{
Marcelocostanzo 1:7fa61b250083 199 //pc.printf("%c",RESPONSE_ENGINE_RPM[j]);
Marcelocostanzo 1:7fa61b250083 200 //}
Marcelocostanzo 0:81f7d0ea45b6 201 }
Marcelocostanzo 0:81f7d0ea45b6 202
Marcelocostanzo 0:81f7d0ea45b6 203 void ask_vehile_speed()
Marcelocostanzo 0:81f7d0ea45b6 204 {
Marcelocostanzo 0:81f7d0ea45b6 205 int i = 0;
Marcelocostanzo 0:81f7d0ea45b6 206
Marcelocostanzo 0:81f7d0ea45b6 207 device.printf(PID_VEHILE_SPEED);
Marcelocostanzo 0:81f7d0ea45b6 208 device.printf(CR);
Marcelocostanzo 0:81f7d0ea45b6 209 while(!device.readable()){} //fica esperando chegar algo na serial
Marcelocostanzo 0:81f7d0ea45b6 210 while(c[i-1]!= '\r') //chegou algo! vamos ler até o CR
Marcelocostanzo 0:81f7d0ea45b6 211 {
Marcelocostanzo 0:81f7d0ea45b6 212 RESPONSE_VEHILE_SPEED[i] = device.getc();
Marcelocostanzo 0:81f7d0ea45b6 213 i++;
Marcelocostanzo 0:81f7d0ea45b6 214 }
Marcelocostanzo 0:81f7d0ea45b6 215
Marcelocostanzo 0:81f7d0ea45b6 216 char DATA_A[2];
Marcelocostanzo 0:81f7d0ea45b6 217 char DATA_B[2];
Marcelocostanzo 0:81f7d0ea45b6 218 char DATA_C[2];
Marcelocostanzo 0:81f7d0ea45b6 219 char DATA_D[2];
Marcelocostanzo 0:81f7d0ea45b6 220
Marcelocostanzo 0:81f7d0ea45b6 221 DATA_A[0] = RESPONSE_VEHILE_SPEED[i-3];
Marcelocostanzo 0:81f7d0ea45b6 222 DATA_A[1] = RESPONSE_VEHILE_SPEED[i-2];
Marcelocostanzo 0:81f7d0ea45b6 223
Marcelocostanzo 0:81f7d0ea45b6 224 int VAL_A = strtol(DATA_A,NULL,16);
Marcelocostanzo 0:81f7d0ea45b6 225
Marcelocostanzo 0:81f7d0ea45b6 226 //pc.printf("%i\r",VAL_A);
Marcelocostanzo 0:81f7d0ea45b6 227
Marcelocostanzo 0:81f7d0ea45b6 228 VEHILE_SPEED = VAL_A;
Marcelocostanzo 0:81f7d0ea45b6 229
Marcelocostanzo 1:7fa61b250083 230 //pc.printf("%i kmh\r",VEHILE_SPEED);
Marcelocostanzo 0:81f7d0ea45b6 231
Marcelocostanzo 1:7fa61b250083 232 //for(int j = 0; j < i; j++)
Marcelocostanzo 1:7fa61b250083 233 //{
Marcelocostanzo 1:7fa61b250083 234 //pc.printf("%c",RESPONSE_VEHILE_SPEED[j]);
Marcelocostanzo 1:7fa61b250083 235 //}
Marcelocostanzo 0:81f7d0ea45b6 236 }
Marcelocostanzo 0:81f7d0ea45b6 237
Marcelocostanzo 0:81f7d0ea45b6 238 void ask_intake_air_temperature()
Marcelocostanzo 0:81f7d0ea45b6 239 {
Marcelocostanzo 0:81f7d0ea45b6 240 int i = 0;
Marcelocostanzo 0:81f7d0ea45b6 241
Marcelocostanzo 0:81f7d0ea45b6 242 device.printf(PID_INTAKE_AIR_TEMPERATURE);
Marcelocostanzo 0:81f7d0ea45b6 243 device.printf(CR);
Marcelocostanzo 0:81f7d0ea45b6 244 while(!device.readable()){} //fica esperando chegar algo na serial
Marcelocostanzo 0:81f7d0ea45b6 245 while(RESPONSE_INTAKE_AIR_TEMPERATURE[i-1]!= '\r') //chegou algo! vamos ler até o CR
Marcelocostanzo 0:81f7d0ea45b6 246 {
Marcelocostanzo 0:81f7d0ea45b6 247 RESPONSE_INTAKE_AIR_TEMPERATURE[i] = device.getc();
Marcelocostanzo 0:81f7d0ea45b6 248 i++;
Marcelocostanzo 0:81f7d0ea45b6 249 }
Marcelocostanzo 0:81f7d0ea45b6 250
Marcelocostanzo 0:81f7d0ea45b6 251 char DATA_A[2];
Marcelocostanzo 0:81f7d0ea45b6 252 char DATA_B[2];
Marcelocostanzo 0:81f7d0ea45b6 253 char DATA_C[2];
Marcelocostanzo 0:81f7d0ea45b6 254 char DATA_D[2];
Marcelocostanzo 0:81f7d0ea45b6 255
Marcelocostanzo 0:81f7d0ea45b6 256 DATA_A[0] = RESPONSE_INTAKE_AIR_TEMPERATURE[i-3];
Marcelocostanzo 0:81f7d0ea45b6 257 DATA_A[1] = RESPONSE_INTAKE_AIR_TEMPERATURE[i-2];
Marcelocostanzo 0:81f7d0ea45b6 258
Marcelocostanzo 0:81f7d0ea45b6 259 int VAL_A = strtol(DATA_A,NULL,16);
Marcelocostanzo 0:81f7d0ea45b6 260
Marcelocostanzo 0:81f7d0ea45b6 261 //pc.printf("%i\r",VAL_A);
Marcelocostanzo 0:81f7d0ea45b6 262
Marcelocostanzo 0:81f7d0ea45b6 263 INTAKE_AIR_TEMPERATURE = VAL_A - 40;;
Marcelocostanzo 0:81f7d0ea45b6 264
Marcelocostanzo 1:7fa61b250083 265 //pc.printf("temp ar %i\r",INTAKE_AIR_TEMPERATURE);
Marcelocostanzo 0:81f7d0ea45b6 266
Marcelocostanzo 1:7fa61b250083 267 //for(int j = 0; j < i; j++)
Marcelocostanzo 1:7fa61b250083 268 //{
Marcelocostanzo 1:7fa61b250083 269 //pc.printf("%c",RESPONSE_INTAKE_AIR_TEMPERATURE[j]);
Marcelocostanzo 1:7fa61b250083 270 //}
Marcelocostanzo 0:81f7d0ea45b6 271 }
Marcelocostanzo 0:81f7d0ea45b6 272
Marcelocostanzo 0:81f7d0ea45b6 273 void ask_throttle_position()
Marcelocostanzo 0:81f7d0ea45b6 274 {
Marcelocostanzo 0:81f7d0ea45b6 275 int i = 0;
Marcelocostanzo 0:81f7d0ea45b6 276
Marcelocostanzo 0:81f7d0ea45b6 277 device.printf(PID_THROTTLE_POSITION);
Marcelocostanzo 0:81f7d0ea45b6 278 device.printf(CR);
Marcelocostanzo 0:81f7d0ea45b6 279 while(!device.readable()){} //fica esperando chegar algo na serial
Marcelocostanzo 0:81f7d0ea45b6 280 while(RESPONSE_THROTTLE_POSITION[i-1]!= '\r') //chegou algo! vamos ler até o CR
Marcelocostanzo 0:81f7d0ea45b6 281 {
Marcelocostanzo 0:81f7d0ea45b6 282 RESPONSE_THROTTLE_POSITION[i] = device.getc();
Marcelocostanzo 0:81f7d0ea45b6 283 i++;
Marcelocostanzo 0:81f7d0ea45b6 284 }
Marcelocostanzo 0:81f7d0ea45b6 285
Marcelocostanzo 0:81f7d0ea45b6 286 char DATA_A[2];
Marcelocostanzo 0:81f7d0ea45b6 287 char DATA_B[2];
Marcelocostanzo 0:81f7d0ea45b6 288 char DATA_C[2];
Marcelocostanzo 0:81f7d0ea45b6 289 char DATA_D[2];
Marcelocostanzo 0:81f7d0ea45b6 290
Marcelocostanzo 0:81f7d0ea45b6 291 DATA_A[0] = RESPONSE_THROTTLE_POSITION[i-3];
Marcelocostanzo 0:81f7d0ea45b6 292 DATA_A[1] = RESPONSE_THROTTLE_POSITION[i-2];
Marcelocostanzo 0:81f7d0ea45b6 293
Marcelocostanzo 0:81f7d0ea45b6 294 int VAL_A = strtol(DATA_A,NULL,16);
Marcelocostanzo 0:81f7d0ea45b6 295
Marcelocostanzo 0:81f7d0ea45b6 296 //pc.printf("%i\r",VAL_A);
Marcelocostanzo 0:81f7d0ea45b6 297
Marcelocostanzo 0:81f7d0ea45b6 298 THROTTLE_POSITION = 100.0f/255.0f;
Marcelocostanzo 0:81f7d0ea45b6 299 THROTTLE_POSITION = THROTTLE_POSITION * VAL_A;
Marcelocostanzo 0:81f7d0ea45b6 300
Marcelocostanzo 1:7fa61b250083 301 //pc.printf("tbi %.2f\r",THROTTLE_POSITION);
Marcelocostanzo 0:81f7d0ea45b6 302
Marcelocostanzo 1:7fa61b250083 303 //for(int j = 0; j < i; j++)
Marcelocostanzo 1:7fa61b250083 304 //{
Marcelocostanzo 1:7fa61b250083 305 //pc.printf("%c",RESPONSE_THROTTLE_POSITION[j]);
Marcelocostanzo 1:7fa61b250083 306 //}
Marcelocostanzo 0:81f7d0ea45b6 307 }
Marcelocostanzo 0:81f7d0ea45b6 308
Marcelocostanzo 0:81f7d0ea45b6 309 void ask_oxygen_sensor()
Marcelocostanzo 0:81f7d0ea45b6 310 {
Marcelocostanzo 0:81f7d0ea45b6 311 int i = 0;
Marcelocostanzo 0:81f7d0ea45b6 312
Marcelocostanzo 0:81f7d0ea45b6 313 device.printf(PID_OXYGEN_SENSOR);
Marcelocostanzo 0:81f7d0ea45b6 314 device.printf(CR);
Marcelocostanzo 0:81f7d0ea45b6 315 while(!device.readable()){} //fica esperando chegar algo na serial
Marcelocostanzo 0:81f7d0ea45b6 316 while(RESPONSE_OXYGEN_SENSOR[i-1]!= '\r') //chegou algo! vamos ler até o CR
Marcelocostanzo 0:81f7d0ea45b6 317 {
Marcelocostanzo 0:81f7d0ea45b6 318 RESPONSE_OXYGEN_SENSOR[i] = device.getc();
Marcelocostanzo 0:81f7d0ea45b6 319 i++;
Marcelocostanzo 0:81f7d0ea45b6 320 }
Marcelocostanzo 0:81f7d0ea45b6 321
Marcelocostanzo 0:81f7d0ea45b6 322 char DATA_A[2];
Marcelocostanzo 0:81f7d0ea45b6 323 char DATA_B[2];
Marcelocostanzo 0:81f7d0ea45b6 324 char DATA_C[2];
Marcelocostanzo 0:81f7d0ea45b6 325 char DATA_D[2];
Marcelocostanzo 0:81f7d0ea45b6 326
Marcelocostanzo 0:81f7d0ea45b6 327 DATA_A[0] = RESPONSE_OXYGEN_SENSOR[i-5];
Marcelocostanzo 0:81f7d0ea45b6 328 DATA_A[1] = RESPONSE_OXYGEN_SENSOR[i-4];
Marcelocostanzo 0:81f7d0ea45b6 329
Marcelocostanzo 0:81f7d0ea45b6 330 DATA_B[0] = RESPONSE_OXYGEN_SENSOR[i-3];
Marcelocostanzo 0:81f7d0ea45b6 331 DATA_B[1] = RESPONSE_OXYGEN_SENSOR[i-2];
Marcelocostanzo 0:81f7d0ea45b6 332
Marcelocostanzo 0:81f7d0ea45b6 333 int VAL_A = strtol(DATA_A,NULL,16);
Marcelocostanzo 0:81f7d0ea45b6 334 int VAL_B = strtol(DATA_B,NULL,16);
Marcelocostanzo 0:81f7d0ea45b6 335
Marcelocostanzo 0:81f7d0ea45b6 336 //pc.printf("%i\r",VAL_A);
Marcelocostanzo 0:81f7d0ea45b6 337 //pc.printf("%i\r",VAL_B);
Marcelocostanzo 0:81f7d0ea45b6 338
Marcelocostanzo 0:81f7d0ea45b6 339 OXYGEN_SENSOR = VAL_A / 200.0f;
Marcelocostanzo 0:81f7d0ea45b6 340
Marcelocostanzo 0:81f7d0ea45b6 341 SHORT_TERM_FUEL_TRIM = ((100.0f / 128.0f) * VAL_B) - 100.0f;
Marcelocostanzo 0:81f7d0ea45b6 342
Marcelocostanzo 1:7fa61b250083 343 //pc.printf("sonda %f\r",OXYGEN_SENSOR);
Marcelocostanzo 1:7fa61b250083 344 //pc.printf("trim %f\r",SHORT_TERM_FUEL_TRIM);
Marcelocostanzo 0:81f7d0ea45b6 345
Marcelocostanzo 1:7fa61b250083 346 //for(int j = 0; j < i; j++)
Marcelocostanzo 1:7fa61b250083 347 //{
Marcelocostanzo 1:7fa61b250083 348 //pc.printf("%c",RESPONSE_OXYGEN_SENSOR[j]);
Marcelocostanzo 1:7fa61b250083 349 //}
Marcelocostanzo 0:81f7d0ea45b6 350 }
Marcelocostanzo 0:81f7d0ea45b6 351
Marcelocostanzo 0:81f7d0ea45b6 352 void ask_fuel_rail_pressure()
Marcelocostanzo 0:81f7d0ea45b6 353 {
Marcelocostanzo 0:81f7d0ea45b6 354 int i = 0;
Marcelocostanzo 0:81f7d0ea45b6 355
Marcelocostanzo 0:81f7d0ea45b6 356 device.printf(PID_FUEL_RAIL_PRESSURE);
Marcelocostanzo 0:81f7d0ea45b6 357 device.printf(CR);
Marcelocostanzo 0:81f7d0ea45b6 358 while(!device.readable()){} //fica esperando chegar algo na serial
Marcelocostanzo 0:81f7d0ea45b6 359 while(RESPONSE_FUEL_RAIL_PRESSURE[i-1]!= '\r') //chegou algo! vamos ler até o CR
Marcelocostanzo 0:81f7d0ea45b6 360 {
Marcelocostanzo 0:81f7d0ea45b6 361 RESPONSE_FUEL_RAIL_PRESSURE[i] = device.getc();
Marcelocostanzo 0:81f7d0ea45b6 362 i++;
Marcelocostanzo 0:81f7d0ea45b6 363 }
Marcelocostanzo 0:81f7d0ea45b6 364
Marcelocostanzo 0:81f7d0ea45b6 365 char DATA_A[2];
Marcelocostanzo 0:81f7d0ea45b6 366 char DATA_B[2];
Marcelocostanzo 0:81f7d0ea45b6 367 char DATA_C[2];
Marcelocostanzo 0:81f7d0ea45b6 368 char DATA_D[2];
Marcelocostanzo 0:81f7d0ea45b6 369
Marcelocostanzo 0:81f7d0ea45b6 370 DATA_A[0] = RESPONSE_FUEL_RAIL_PRESSURE[i-5];
Marcelocostanzo 0:81f7d0ea45b6 371 DATA_A[1] = RESPONSE_FUEL_RAIL_PRESSURE[i-4];
Marcelocostanzo 0:81f7d0ea45b6 372
Marcelocostanzo 0:81f7d0ea45b6 373 DATA_B[0] = RESPONSE_FUEL_RAIL_PRESSURE[i-3];
Marcelocostanzo 0:81f7d0ea45b6 374 DATA_B[1] = RESPONSE_FUEL_RAIL_PRESSURE[i-2];
Marcelocostanzo 0:81f7d0ea45b6 375
Marcelocostanzo 0:81f7d0ea45b6 376 int VAL_A = strtol(DATA_A,NULL,16);
Marcelocostanzo 0:81f7d0ea45b6 377 int VAL_B = strtol(DATA_B,NULL,16);
Marcelocostanzo 0:81f7d0ea45b6 378
Marcelocostanzo 0:81f7d0ea45b6 379 //pc.printf("%i\r",VAL_A);
Marcelocostanzo 0:81f7d0ea45b6 380
Marcelocostanzo 0:81f7d0ea45b6 381 FUEL_RAIL_PRESSURE = ((VAL_A * 256) + VAL_B) * 0.1f;
Marcelocostanzo 0:81f7d0ea45b6 382
Marcelocostanzo 1:7fa61b250083 383 //pc.printf("alta %f\r",FUEL_RAIL_PRESSURE);
Marcelocostanzo 0:81f7d0ea45b6 384
Marcelocostanzo 1:7fa61b250083 385 //for(int j = 0; j < i; j++)
Marcelocostanzo 1:7fa61b250083 386 //{
Marcelocostanzo 1:7fa61b250083 387 //pc.printf("%c",RESPONSE_FUEL_RAIL_PRESSURE[j]);
Marcelocostanzo 1:7fa61b250083 388 //}
Marcelocostanzo 0:81f7d0ea45b6 389 }
Marcelocostanzo 0:81f7d0ea45b6 390
Marcelocostanzo 0:81f7d0ea45b6 391 void read_serial()
Marcelocostanzo 0:81f7d0ea45b6 392 {
Marcelocostanzo 0:81f7d0ea45b6 393 int i = 0;
Marcelocostanzo 0:81f7d0ea45b6 394
Marcelocostanzo 0:81f7d0ea45b6 395 while(!device.readable()){} //fica esperando chegar algo na serial
Marcelocostanzo 0:81f7d0ea45b6 396 while(c[i-1]!= '\r') //chegou algo! vamos ler até o CR
Marcelocostanzo 0:81f7d0ea45b6 397 {
Marcelocostanzo 0:81f7d0ea45b6 398 c[i] = device.getc();
Marcelocostanzo 0:81f7d0ea45b6 399 //pc.printf("%c",c[i]);
Marcelocostanzo 0:81f7d0ea45b6 400 i++;
Marcelocostanzo 0:81f7d0ea45b6 401 }
Marcelocostanzo 1:7fa61b250083 402 //for(int j = 0; j < i; j++)
Marcelocostanzo 1:7fa61b250083 403 //{
Marcelocostanzo 1:7fa61b250083 404 //pc.printf("%c",c[j]);
Marcelocostanzo 1:7fa61b250083 405 //}
Marcelocostanzo 0:81f7d0ea45b6 406 }
Marcelocostanzo 0:81f7d0ea45b6 407
Marcelocostanzo 0:81f7d0ea45b6 408 void OBDInit()
Marcelocostanzo 0:81f7d0ea45b6 409 {
Marcelocostanzo 0:81f7d0ea45b6 410 lcd.locate (4,0);
Marcelocostanzo 0:81f7d0ea45b6 411 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 412 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 413 device.printf(SET_ALL_TO_DEFAULT);
Marcelocostanzo 0:81f7d0ea45b6 414 device.printf(CR);
Marcelocostanzo 1:7fa61b250083 415 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 416 lcd.locate (4,1);
Marcelocostanzo 0:81f7d0ea45b6 417 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 418 lcd.redraw();
Marcelocostanzo 1:7fa61b250083 419 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 420
Marcelocostanzo 0:81f7d0ea45b6 421 lcd.locate (4,2);
Marcelocostanzo 0:81f7d0ea45b6 422 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 423 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 424 device.printf(RESET_ALL);
Marcelocostanzo 0:81f7d0ea45b6 425 device.printf(CR);
Marcelocostanzo 1:7fa61b250083 426 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 427 lcd.locate (4,3);
Marcelocostanzo 0:81f7d0ea45b6 428 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 429 lcd.redraw();
Marcelocostanzo 1:7fa61b250083 430 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 431
Marcelocostanzo 0:81f7d0ea45b6 432 lcd.locate (4,4);
Marcelocostanzo 0:81f7d0ea45b6 433 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 434 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 435 device.printf(LINEFEED_OFF);
Marcelocostanzo 0:81f7d0ea45b6 436 device.printf(CR);
Marcelocostanzo 1:7fa61b250083 437 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 438 lcd.locate (4,5);
Marcelocostanzo 0:81f7d0ea45b6 439 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 440 lcd.redraw();
Marcelocostanzo 1:7fa61b250083 441 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 442
Marcelocostanzo 0:81f7d0ea45b6 443 lcd.locate (4,6);
Marcelocostanzo 0:81f7d0ea45b6 444 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 445 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 446 device.printf(PRINTING_SPACES_OFF);
Marcelocostanzo 0:81f7d0ea45b6 447 device.printf(CR);
Marcelocostanzo 1:7fa61b250083 448 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 449 lcd.locate (4,7);
Marcelocostanzo 0:81f7d0ea45b6 450 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 451 lcd.redraw();
Marcelocostanzo 1:7fa61b250083 452 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 453
Marcelocostanzo 0:81f7d0ea45b6 454 lcd.locate (4,8);
Marcelocostanzo 0:81f7d0ea45b6 455 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 456 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 457 device.printf(MEMORY_OFF);
Marcelocostanzo 0:81f7d0ea45b6 458 device.printf(CR);
Marcelocostanzo 1:7fa61b250083 459 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 460 lcd.locate (4,9);
Marcelocostanzo 0:81f7d0ea45b6 461 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 462 lcd.redraw();
Marcelocostanzo 1:7fa61b250083 463 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 464
Marcelocostanzo 0:81f7d0ea45b6 465 lcd.locate (4,10);
Marcelocostanzo 0:81f7d0ea45b6 466 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 467 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 468 device.printf(SET_TIMEOUT_256);
Marcelocostanzo 0:81f7d0ea45b6 469 device.printf(CR);
Marcelocostanzo 1:7fa61b250083 470 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 471 lcd.locate (4,11);
Marcelocostanzo 0:81f7d0ea45b6 472 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 473 lcd.redraw();
Marcelocostanzo 1:7fa61b250083 474 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 475
Marcelocostanzo 0:81f7d0ea45b6 476 lcd.locate (4,12);
Marcelocostanzo 0:81f7d0ea45b6 477 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 478 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 479 device.printf(TRY_PROTOCOL_0);
Marcelocostanzo 0:81f7d0ea45b6 480 device.printf(CR);
Marcelocostanzo 1:7fa61b250083 481 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 482 lcd.locate (4,13);
Marcelocostanzo 0:81f7d0ea45b6 483 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 484 lcd.redraw();
Marcelocostanzo 1:7fa61b250083 485 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 486
Marcelocostanzo 0:81f7d0ea45b6 487 lcd.locate (4,14);
Marcelocostanzo 0:81f7d0ea45b6 488 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 489 lcd.redraw();
Marcelocostanzo 0:81f7d0ea45b6 490 device.printf(ECHO_OFF);
Marcelocostanzo 0:81f7d0ea45b6 491 device.printf(CR);
Marcelocostanzo 1:7fa61b250083 492 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 493 lcd.locate (4,15);
Marcelocostanzo 0:81f7d0ea45b6 494 lcd.printf (".");
Marcelocostanzo 0:81f7d0ea45b6 495 lcd.redraw();
Marcelocostanzo 1:7fa61b250083 496 wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 497
Marcelocostanzo 1:7fa61b250083 498 //device.printf("ATPP0CSV45");
Marcelocostanzo 1:7fa61b250083 499 //device.printf("ATBRD45");
Marcelocostanzo 1:7fa61b250083 500 //device.printf(CR);
Marcelocostanzo 1:7fa61b250083 501 //wait_ms(200);
Marcelocostanzo 1:7fa61b250083 502 //device.baud(57600);
Marcelocostanzo 1:7fa61b250083 503 //device.printf("ATPP0CON");
Marcelocostanzo 1:7fa61b250083 504 //device.printf(CR);
Marcelocostanzo 1:7fa61b250083 505 //wait_ms(200);
Marcelocostanzo 1:7fa61b250083 506
Marcelocostanzo 1:7fa61b250083 507 //device.printf("ATLP");
Marcelocostanzo 1:7fa61b250083 508 //device.printf(CR);
Marcelocostanzo 1:7fa61b250083 509 //wait_ms(200);
Marcelocostanzo 0:81f7d0ea45b6 510 }