06-27-2008, 03:54 PM
|
#21 (permalink)
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
Well, this is what I've done so far, ignore the serial stuff, I was doing that out of naivety, (actually, it might work if you have a rs232>OBDII adapter), feel free to take whatever you want out of it. Basically all I was interested in was instant mpg, speed, and rpm at the moment, so all the calculations and such should work. The display code definitely works as tested, haven't hooked it into a vehicle yet though. I'm waiting on a serial>usb adapter for my laptop to continue work on it.
Code:
/*
OpenScanGauge
*/
#include <LCD4Bit.h>
#include <stdio.h>
LCD4Bit lcd = LCD4Bit(4);
double vss;
double maf;
double mpg;
int rpm;
//pins to use for data
int busneg=0;
int buspos=1;
//arrays to store values returned from ECU
char vss_byte[10];
char maf_byte[10];
char rpm_byte[10];
//hex commands for speed, MAF, and rpm
char* vss_cmd[6] = {"61", "6A", "F1", "01", "0D", "8B"};
char* maf_cmd[6] = {"61", "6A", "F1", "01", "10", "C7"};
char* rpm_cmd[6] = {"61", "6A", "F1", "01", "0C", "96"};
//array to look up binary values for hex chars
char* hexconvert[15][2] = {{"0","0000"},{"1","0001"},{"2","0010"},{"3","0011"},{"4","0100"},{"5","0101"},{"6","0110"},{"7","0111"},{"8","1000"},{"9","1001"},{"A","1010"},{"B","1011"},{"C","1100"},{"D","1101"},{"E","1110"},{"F","1111"}}
char vss_str[5];
char mpg_str[4];
char rpm_str[5];
void setup() {
lcd.init();
}
void loop() {
getValues();
printOutput();
}
void printOutput() {
lcd.clear();
lcd.cursorTo(1,0);
lcd.printIn("MPG: ");
lcd.cursorTo(1,5);
mpg = (710.7 * vss) / maf;
sprintf(mpg_str, "%f", mpg);
lcd.printIn(mpg_str);
lcd.cursorTo(1,9);
lcd.printIn("Speed: ");
lcd.cursorTo(1,16);
vss = vss * 0.6214;
sprintf(vss_str, "%f", vss);
lcd.printIn(vss_str);
lcd.cursorTo(3,0);
lcd.printIn("RPM: ");
lcd.cursorTo(3,5);
sprintf(rpm_str, "%d", rpm);
lcd.printIn(rpm_str);
delay (1000);
}
void getValues() {
pwm_send(vss_cmd);
vss_byte = pwm_read();
vss = vss_byte[5];
pwm_send(rpm_cmd);
rpm_byte = pwm_read();
rpm = .25 * (rpm_byte[5] * 256 + rpm_byte[6]);
pwm_send(maf_cmd);
maf_byte = pwm_read();
maf = .01 * ((256 * maf_byte[5]) + maf_byte[6]);
}
void pwm_send(char* cmd[]) {
char bincmd[] = hex2bin(cmd[]);
pinmode(busneg, OUTPUT);
//send SOF and drop back to neutral
AnalogWrite(buspos, 255);
digitalWrite(busneg, LOW);
delayMicroseconds(32);
AnalogWrite(buspos, 0);
digitalWrite(busneg, LOW);
//loop through array of bits
for (int i=0; i < 48; i++) {
//send 0
if bincmd[i] = '0' {
analogwrite(buspos, 255);
digitalwrite(busneg, LOW);
delaymicroseconds(16);
AnalogWrite(buspos, 0);
digitalWrite(busneg, LOW);
}
//send 1
else if (bincmd[i] = '1') {
analogwrite(buspos, 255);
digitalwrite(busneg, LOW);
delaymicroseconds(8);
AnalogWrite(buspos, 0);
digitalWrite(busneg, LOW);
}
}
//send EOF to be here
}
char[] pwm_read() {
pinmode(busneg, INPUT);
//code for reading values here (complex methinks)
}
//convert hex command to binary
char[] hex2bin(char* cmd[]) {
char bincmd[];
int binloc = 0;
for(int i=0; i < 6; i++) {
for(int j=0; j < 15; j++) {
if (cmd[i][0] = hexconvert[j][0]) {
for (int k=; k < 4; k++) {
bincmd[binloc] = hexconvert[j][1][k];
binloc++;
}
}
}
for(int j=0; j < 15; j++) {
if (cmd[i][1] = hexconvert[j][0]) {
for (int k=; k < 4; k++) {
bincmd[binloc] = hexconvert[j][1][k];
binloc++;
}
}
}
}
return bincmd;
}
Last edited by n8thegr8; 06-30-2008 at 03:23 PM..
|
|
|
Today
|
|
|
Other popular topics in this forum...
|
|
|
06-27-2008, 03:55 PM
|
#22 (permalink)
|
OBDuino coder
Join Date: Jun 2008
Location: Montréal, QC
Posts: 212
Titine - '13 Hyundai Sonata Hybrid
Thanks: 3
Thanked 10 Times in 8 Posts
|
I made changes in my previous code, check message
http://ecomodder.com/forum/showthrea...html#post37505
As always, untested as I have no hardware!!
__________________
2013 Hyundai Sonata Hybrid
|
|
|
06-27-2008, 03:58 PM
|
#23 (permalink)
|
OBDuino coder
Join Date: Jun 2008
Location: Montréal, QC
Posts: 212
Titine - '13 Hyundai Sonata Hybrid
Thanks: 3
Thanked 10 Times in 8 Posts
|
LOL we posted at the same time
__________________
2013 Hyundai Sonata Hybrid
|
|
|
06-28-2008, 12:58 PM
|
#24 (permalink)
|
EcoModding Lurker
Join Date: Jun 2008
Location: ohio
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
|
I have been working on my own obd II MPG Gauge for a while now using the elm323 and the pic16f877a but have run into quite a few snags. I think im going to give up and start working with you guys. Hopefully I can be of some help. I have ordered all of the parts and went with the 16x2 LCD. I have not ordered an obd cable yet but I am thinking of ordering the standard male 10 pin for $5.
|
|
|
06-28-2008, 05:08 PM
|
#25 (permalink)
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
yeah, I built my cable with the connector from mouser for my usb>obdII reader I built (also a pic btw, pic18f2455) I mainly built it for diagnostics and for checking my arduino up against. SO...I'll only be working on PWM, as that's the only car I have access to, BUT the code should work on VPW as well with slight modifications (vpw uses only the 1 data line instead of 2, but same logically and physically otherwise).
I have found a few more resources about it, and also, I now have a noob knowledge of how differential bus works :P. apparently, you send the signal on the bus+ line, and send the complement of that signal on the bus- line, so from what I understand, when high on bus+, you need to pull bus- low, and vice versa. If I am wrong, PLEASE correct me. Anywho, so I don't think I need any extra hardware to bit bang it, so I'll be working on functions for sending the triggers and converting the hex code to the appropriate high's and low's. Here's a little more about pwm (and vpw for that matter) for those interested:
SAE J1850 Description, Electrical Interface Bus
|
|
|
06-28-2008, 05:19 PM
|
#26 (permalink)
|
EcoModding Lurker
Join Date: Jun 2008
Location: ohio
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
|
I have never worked with the quino board before. Do I need anything special to connect my lcd and my MCZ33290EF or is it just plug and play?
|
|
|
06-28-2008, 06:30 PM
|
#27 (permalink)
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
the arduino platform is generally pretty flexible. Nearly all of it's pins can be reassigned to do certain tasks. typically, all you need to do to hook it up to a peripheral is wire it to whichever pins you want to use to drive it. Check out the playground on Arduino - HomePage. they have a lot of really good examples and code to get you started. I would also suggest looking into the 4bit LCD library, so it will cut down on how many wires you have to connect and free up some pins to use for other things (cuts it from 8 wires to 4), that's what I use with my lcd.
|
|
|
06-29-2008, 11:33 AM
|
#29 (permalink)
|
EcoModding Lurker
Join Date: Jun 2008
Location: USA
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
sweet, that looks like a pretty neat little project. I've turned up some more on VPW and PWM, they both use "bit symbols", so it's not the fact that the bus is high or low that matters, it's for how long the bus is high or low (in microseconds). This is going to be interesting, I got started on the code to generate the necessary pulses. Here's some more reading material for those interested in the timings for bits:
Third draft of Eval Eng article
The one of interest for PWM is the very last table at the bottom of the page.
|
|
|
06-29-2008, 01:39 PM
|
#30 (permalink)
|
EcoModding Lurker
Join Date: Jun 2008
Location: ohio
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
|
In the mpguino forum they used the pulse width but last I read it was innacurate in terms of MPG. Wouldnt it be more accurate to use your liters x 14.7% to get your fuel use per revolution and multiply that by your rpm. With a little conversion you get gallons per hour. Divide MPH by GPH and get MPG. I dont know if that would be easier or not but as long as your getting MPG I guess it doesnt matter.
|
|
|
|