Skip to content

Commit 0b0ae45

Browse files
committed
2 parents 08a5117 + a0fab5f commit 0b0ae45

File tree

11 files changed

+291
-209
lines changed

11 files changed

+291
-209
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ here are many examples implemented in this library. One of the examples is below
5656
#define CAN_31K25BPS 5
5757
#define CAN_33KBPS 6
5858
#define CAN_40KBPS 7
59-
#define CAN_47KBPS 19
59+
#define CAN_47K619BPS 19
6060
#define CAN_50KBPS 8
6161
#define CAN_80KBPS 9
6262
#define CAN_83K3BPS 10

examples/OBDII_PIDs/OBDII_PIDs.ino

Lines changed: 0 additions & 155 deletions
This file was deleted.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*************************************************************************************************
2+
OBD-II_PIDs TEST CODE
3+
Loovee, Longan Labs 2022
4+
5+
Query
6+
send id: 0x7df
7+
dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0
8+
9+
Response
10+
From id: 0x7E9 or 0x7EA or 0x7EB
11+
dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option)
12+
13+
https://en.wikipedia.org/wiki/OBD-II_PIDs
14+
***************************************************************************************************/
15+
16+
#include <SPI.h>
17+
#include "mcp_can.h"
18+
19+
/* Please modify SPI_CS_PIN to adapt to different baords.
20+
21+
CANBed V1 - 17
22+
CANBed M0 - 3
23+
CAN Bus Shield - 9
24+
CANBed 2040 - 9
25+
CANBed Dual - 9
26+
OBD-2G Dev Kit - 9
27+
Hud Dev Kit - 9
28+
*/
29+
30+
#define SPI_CS_PIN 9
31+
32+
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
33+
34+
#define PID_ENGIN_PRM 0x0C
35+
#define PID_VEHICLE_SPEED 0x0D
36+
#define PID_COOLANT_TEMP 0x05
37+
38+
#define CAN_ID_PID 0x7DF
39+
40+
void set_mask_filt()
41+
{
42+
// set mask, set both the mask to 0x3ff
43+
44+
CAN.init_Mask(0, 0, 0x7FC);
45+
CAN.init_Mask(1, 0, 0x7FC);
46+
47+
// set filter, we can receive id from 0x04 ~ 0x09
48+
49+
CAN.init_Filt(0, 0, 0x7E8);
50+
CAN.init_Filt(1, 0, 0x7E8);
51+
52+
CAN.init_Filt(2, 0, 0x7E8);
53+
CAN.init_Filt(3, 0, 0x7E8);
54+
CAN.init_Filt(4, 0, 0x7E8);
55+
CAN.init_Filt(5, 0, 0x7E8);
56+
}
57+
58+
void sendPid(unsigned char __pid) {
59+
unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0};
60+
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp);
61+
}
62+
63+
bool getRPM(int *r)
64+
{
65+
sendPid(PID_ENGIN_PRM);
66+
unsigned long __timeout = millis();
67+
68+
while(millis()-__timeout < 1000) // 1s time out
69+
{
70+
unsigned char len = 0;
71+
unsigned char buf[8];
72+
73+
if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data
74+
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
75+
76+
if(buf[1] == 0x41)
77+
{
78+
*r = (256*buf[3]+buf[4])/4;
79+
return 1;
80+
}
81+
}
82+
}
83+
84+
return 0;
85+
}
86+
87+
void setup()
88+
{
89+
Serial.begin(115200);
90+
while(!Serial);
91+
92+
// below code need for OBD-II GPS Dev Kit
93+
// pinMode(A3, OUTPUT);
94+
// digitalWrite(A3, HIGH);
95+
96+
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
97+
Serial.println("CAN init fail, retry...");
98+
delay(100);
99+
}
100+
Serial.println("CAN init ok!");
101+
set_mask_filt();
102+
}
103+
104+
void loop() {
105+
106+
int __rpm = 0;
107+
108+
int ret = getRPM(&__rpm);
109+
110+
if(ret)
111+
{
112+
Serial.print("Engin Speed: ");
113+
Serial.print(__rpm);
114+
Serial.println(" rpm");
115+
}else Serial.println("get Engin Speed Fail...");
116+
117+
delay(500);
118+
}
119+
120+
// END FILE

0 commit comments

Comments
 (0)