Skip to content

Commit c7bccd4

Browse files
authored
Add files via upload
1 parent 52503da commit c7bccd4

File tree

13 files changed

+720
-0
lines changed

13 files changed

+720
-0
lines changed

Distance Measurement RTX-MID-868.xlsx

16.8 KB
Binary file not shown.

PwmTransceiver.cpp

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
/*
2+
Created by: Esposito Vittorio
3+
GitHub: https://github.com/VittorioEsposito
4+
5+
J1850-PWM-Transceiver-Library
6+
*/
7+
8+
#include <Arduino.h>
9+
#include "avr/interrupt.h"
10+
#include <PwmTransceiver.h>
11+
12+
PwmTransceiver::PwmTransceiver(uint8_t RX_PIN, uint8_t TX_PIN)
13+
{
14+
_RX_PIN = RX_PIN;
15+
pinMode(_RX_PIN, INPUT);
16+
_TX_PIN = TX_PIN;
17+
pinMode(_TX_PIN, OUTPUT);
18+
}
19+
20+
PwmTransceiver::PwmTransceiver(uint8_t RX_PIN, uint8_t TX_PIN, uint8_t MODE_PIN, uint8_t EN_PIN)
21+
{
22+
_RX_PIN = RX_PIN;
23+
pinMode(_RX_PIN, INPUT);
24+
_TX_PIN = TX_PIN;
25+
pinMode(_TX_PIN, OUTPUT);
26+
_MODE_PIN = MODE_PIN;
27+
pinMode(_MODE_PIN, OUTPUT);
28+
_EN_PIN = EN_PIN;
29+
pinMode(_EN_PIN, OUTPUT);
30+
}
31+
32+
void PwmTransceiver::setRxPin(uint8_t RX_PIN)
33+
{
34+
_RX_PIN = RX_PIN;
35+
pinMode(_RX_PIN, INPUT);
36+
}
37+
38+
void PwmTransceiver::setTxPin(uint8_t TX_PIN)
39+
{
40+
_TX_PIN = TX_PIN;
41+
pinMode(_TX_PIN, OUTPUT);
42+
}
43+
44+
void PwmTransceiver::setEnablePin(uint8_t EN_PIN)
45+
{
46+
_EN_PIN = EN_PIN;
47+
pinMode(_EN_PIN, OUTPUT);
48+
}
49+
50+
void PwmTransceiver::setTxRxModePin(uint8_t MODE_PIN)
51+
{
52+
_MODE_PIN = MODE_PIN;
53+
pinMode(_MODE_PIN, OUTPUT);
54+
}
55+
56+
void PwmTransceiver::setLogic(bool logic)
57+
{
58+
_logic = logic;
59+
}
60+
61+
uint8_t PwmTransceiver::getRxPin()
62+
{
63+
return _RX_PIN;
64+
}
65+
66+
uint8_t PwmTransceiver::getTxPin()
67+
{
68+
return _TX_PIN;
69+
}
70+
71+
void PwmTransceiver::begin(uint32_t BITRATE)
72+
{
73+
_BITRATE = BITRATE;
74+
if (_BITRATE > 15) {
75+
// Use microseconds
76+
_BIT_TIME = 1000000 / _BITRATE; // uS
77+
_high_speed = true;
78+
} else {
79+
// Use milliseconds
80+
_BIT_TIME = 1000 / _BITRATE; // mS
81+
_high_speed = false;
82+
}
83+
_HALF_BIT_TIME = _BIT_TIME / 2;
84+
_ONE_THIRD_BIT_TIME = _BIT_TIME / 3;
85+
_TWO_THIRD_BIT_TIME = _BIT_TIME * 2 / 3;
86+
if (_logic) attachInterrupt(digitalPinToInterrupt(_RX_PIN), isrPwmDecoder, RISING);
87+
else attachInterrupt(digitalPinToInterrupt(_RX_PIN), isrPwmDecoder, FALLING);
88+
flagDecoder = false;
89+
}
90+
91+
void PwmTransceiver::powerDown()
92+
{
93+
digitalWrite(_EN_PIN, LOW);
94+
delay(15);
95+
}
96+
97+
void PwmTransceiver::powerUp()
98+
{
99+
digitalWrite(_EN_PIN, HIGH);
100+
delay(15);
101+
}
102+
103+
void PwmTransceiver::enableTxMode()
104+
{
105+
digitalWrite(_MODE_PIN, HIGH);
106+
delayMicroseconds(500);
107+
}
108+
109+
void PwmTransceiver::enableRxMode()
110+
{
111+
digitalWrite(_MODE_PIN, LOW);
112+
delayMicroseconds(500);
113+
}
114+
115+
void PwmTransceiver::println(String str)
116+
{
117+
str += '\n';
118+
print(str);
119+
}
120+
121+
void PwmTransceiver::print(String str)
122+
{
123+
uint16_t size = str.length() + 1;
124+
char txt[size];
125+
str.toCharArray(txt, size);
126+
uint16_t i;
127+
for (i = 0; i < size; i++)
128+
{
129+
PwmTransceiver::print(txt[i]);
130+
}
131+
}
132+
133+
void PwmTransceiver::print(char txt[])
134+
{
135+
uint16_t size = strlen(txt);
136+
uint16_t i;
137+
for (i = 0; i < size; i++)
138+
{
139+
PwmTransceiver::print(txt[i]);
140+
}
141+
}
142+
143+
void PwmTransceiver::print(char c)
144+
{
145+
int i;
146+
for (i = 7; i >= 0; i--)
147+
{
148+
PwmTransceiver::send((c >> i) % 2);
149+
}
150+
}
151+
152+
void PwmTransceiver::print(uint8_t n)
153+
{
154+
int i;
155+
for (i = 7; i >= 0; i--)
156+
{
157+
PwmTransceiver::send((n >> i) % 2);
158+
}
159+
}
160+
161+
void PwmTransceiver::send(bool b)
162+
{
163+
if (!_logic) b = !b;
164+
if (_high_speed)
165+
{
166+
if (b)
167+
{
168+
/* Send 66% HIGH, 33% LOW */
169+
digitalWrite(_TX_PIN, HIGH);
170+
delayMicroseconds(_TWO_THIRD_BIT_TIME);
171+
digitalWrite(_TX_PIN, LOW);
172+
delayMicroseconds(_ONE_THIRD_BIT_TIME);
173+
}
174+
else
175+
{
176+
/* Send 33% HIGH, 66% LOW */
177+
digitalWrite(_TX_PIN, HIGH);
178+
delayMicroseconds(_ONE_THIRD_BIT_TIME);
179+
digitalWrite(_TX_PIN, LOW);
180+
delayMicroseconds(_TWO_THIRD_BIT_TIME);
181+
}
182+
}
183+
else
184+
{
185+
if (b)
186+
{
187+
/* Send 66% HIGH, 33% LOW */
188+
digitalWrite(_TX_PIN, HIGH);
189+
delay(_TWO_THIRD_BIT_TIME);
190+
digitalWrite(_TX_PIN, LOW);
191+
delay(_ONE_THIRD_BIT_TIME);
192+
}
193+
else
194+
{
195+
/* Send 33% HIGH, 66% LOW */
196+
digitalWrite(_TX_PIN, HIGH);
197+
delay(_ONE_THIRD_BIT_TIME);
198+
digitalWrite(_TX_PIN, LOW);
199+
delay(_TWO_THIRD_BIT_TIME);
200+
}
201+
}
202+
}
203+
204+
static void PwmTransceiver::isrPwmDecoder()
205+
{
206+
timeOld = millis();
207+
flagDecoder = true;
208+
}
209+
210+
void PwmTransceiver::receive()
211+
{
212+
if (flagDecoder)
213+
{
214+
flagDecoder = false;
215+
216+
if (_high_speed)
217+
{
218+
delayMicroseconds(_HALF_BIT_TIME);
219+
}
220+
else
221+
{
222+
delay(_HALF_BIT_TIME);
223+
}
224+
225+
//https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit
226+
227+
if (_logic)
228+
{
229+
_rx_char ^= (-digitalRead(_RX_PIN) ^ _rx_char) & (1 << _count);
230+
}
231+
else
232+
{
233+
_rx_char ^= (-(!digitalRead(_RX_PIN)) ^ _rx_char) & (1 << _count);
234+
}
235+
236+
if (_count > 0)
237+
{
238+
_count--;
239+
}
240+
else
241+
{
242+
_count = 7;
243+
_RX_BUFFER[_BUFFER_INDEX] = _rx_char;
244+
_BUFFER_INDEX++;
245+
_rx_char = 0x00;
246+
}
247+
}
248+
}
249+
250+
bool PwmTransceiver::isReceiving()
251+
{
252+
if (_BUFFER_INDEX > BUFFER_SIZE - 1)
253+
{
254+
// Buffer full
255+
_BUFFER_INDEX = BUFFER_SIZE - 1;
256+
return false;
257+
}
258+
// Check if millis has resetted
259+
if (millis() < timeOld) timeOld = millis();
260+
261+
// Check the lost of synchronicity
262+
if (millis() > (timeOld + _timeOut))
263+
{
264+
_rx_char = 0x00;
265+
_count = 7;
266+
return false;
267+
}
268+
else
269+
{
270+
return true;
271+
}
272+
}
273+
274+
bool PwmTransceiver::available()
275+
{
276+
return (_BUFFER_INDEX > 0);
277+
}
278+
279+
String PwmTransceiver::readString()
280+
{
281+
String str = "";
282+
int i;
283+
for (i=0; i<_BUFFER_INDEX; i++)
284+
{
285+
str += _RX_BUFFER[i];
286+
_RX_BUFFER[i] = 0x00;
287+
}
288+
_BUFFER_INDEX = 0;
289+
return str;
290+
}
291+
292+
byte PwmTransceiver::CRC8(String str)
293+
{
294+
byte size = str.length() + 1;
295+
char txt[size];
296+
str.toCharArray(txt, size);
297+
return CRC8(txt, size - 1);
298+
}
299+
300+
byte PwmTransceiver::CRC8(byte data[], byte len)
301+
{
302+
byte crc = 0x00;
303+
while (len--) {
304+
byte extract = *data++;
305+
for (byte tempI = 8; tempI; tempI--) {
306+
byte sum = (crc ^ extract) & 0x01;
307+
crc >>= 1;
308+
if (sum) {
309+
crc ^= 0x8C;
310+
}
311+
extract >>= 1;
312+
}
313+
}
314+
return crc;
315+
}

PwmTransceiver.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Created by: Esposito Vittorio
3+
GitHub: https://github.com/VittorioEsposito
4+
5+
J1850-Arduino-Transceiver-Library
6+
*/
7+
8+
#include <Arduino.h>
9+
#include "avr/interrupt.h"
10+
11+
#define BUFFER_SIZE 1526
12+
13+
class PwmTransceiver
14+
{
15+
16+
static volatile bool flagDecoder;
17+
static volatile unsigned long timeOld;
18+
19+
private:
20+
uint8_t _RX_PIN;
21+
uint8_t _TX_PIN;
22+
uint8_t _EN_PIN;
23+
uint8_t _MODE_PIN;
24+
uint32_t _BITRATE;
25+
uint32_t _BIT_TIME;
26+
uint32_t _HALF_BIT_TIME;
27+
uint32_t _ONE_THIRD_BIT_TIME;
28+
uint32_t _TWO_THIRD_BIT_TIME;
29+
bool _high_speed = false;
30+
bool _logic = true; // 1: Direct; 0: Inverse
31+
char _RX_BUFFER[BUFFER_SIZE];
32+
uint16_t _BUFFER_INDEX = 0;
33+
int _count = 7;
34+
35+
uint16_t _timeOut = 100; // mS
36+
char _rx_char = 0x00;
37+
38+
public:
39+
PwmTransceiver(uint8_t RX_PIN, uint8_t TX_PIN);
40+
PwmTransceiver(uint8_t RX_PIN, uint8_t TX_PIN, uint8_t MODE_PIN, uint8_t EN_PIN);
41+
void setRxPin(uint8_t RX_PIN);
42+
void setTxPin(uint8_t TX_PIN);
43+
void setEnablePin(uint8_t EN_PIN);
44+
void setTxRxModePin(uint8_t MODE_PIN);
45+
void setLogic(bool logic);
46+
uint8_t getRxPin();
47+
uint8_t getTxPin();
48+
49+
void begin(uint32_t BITRATE);
50+
void powerDown();
51+
void powerUp();
52+
void enableTxMode();
53+
void enableRxMode();
54+
55+
void println(String str);
56+
void print(String str);
57+
void print(char txt[]);
58+
void print(char c);
59+
void print(uint8_t n);
60+
void send (bool b);
61+
62+
static void isrPwmDecoder();
63+
void receive();
64+
bool isReceiving();
65+
bool available();
66+
String readString();
67+
68+
byte CRC8(String str);
69+
byte CRC8(byte data[], byte len);
70+
71+
};

0 commit comments

Comments
 (0)