Skip to content

Commit 71be842

Browse files
committed
commit a basic example for lab 5
1 parent fce5c28 commit 71be842

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed
27.7 KB
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
bool isAddress = true;
2+
bool isCommand = false;
3+
byte command;
4+
5+
void setWriteModeRS485() {
6+
byte port = PORTD;
7+
PORTD |= 1 << PD1;
8+
delay(1);
9+
}
10+
11+
ISR(USART1_TX_vect)
12+
{
13+
PORTD &= ~(1 << PD1);
14+
}
15+
16+
void setup() {
17+
delay(200);
18+
pinMode(LED_BUILTIN, OUTPUT);
19+
digitalWrite(LED_BUILTIN, HIGH);
20+
21+
DDRD |= 1 << PD1;
22+
PORTD &= ~(1 << PD1);
23+
24+
Serial.begin(9600);
25+
Serial1.begin(9600, SERIAL_8N1);
26+
UCSR1B |= (1 << UCSZ12) | (1 << TXCIE1);
27+
}
28+
29+
void loop() {
30+
if (Serial.available()) {
31+
byte inByte = Serial.read();
32+
if (isAddress) {
33+
setWriteModeRS485();
34+
UCSR1B |= 1 << TXB81;
35+
Serial1.write(inByte);
36+
isAddress = false;
37+
isCommand = true;
38+
} else if (isCommand) {
39+
command = inByte;
40+
isCommand = false;
41+
setWriteModeRS485();
42+
UCSR1B &= ~(1 << TXB81);
43+
Serial1.write(inByte);
44+
if (command == 0xB1) isAddress = true;
45+
} else {
46+
isAddress = true;
47+
setWriteModeRS485();
48+
UCSR1B &= ~(1 << TXB81);
49+
Serial1.write(inByte);
50+
}
51+
}
52+
if (Serial1.available()) {
53+
byte inByte1 = Serial1.read();
54+
Serial.write(inByte1);
55+
}
56+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#define B 1
2+
#define U 2
3+
#define C 3
4+
#define H 4
5+
#define A 5
6+
#define K 6
7+
#define R 7
8+
#define O 8
9+
#define M 9
10+
#define N 10
11+
#define T 11
12+
#define S 12
13+
#define V 13
14+
#define Y 14
15+
#define _ 15
16+
17+
const byte SLAVE_ADDRESS = 55;
18+
const byte DATA_ARRAY_LENGTH = 24;
19+
20+
byte data_array[DATA_ARRAY_LENGTH] = {
21+
B, U, C, H, A, K, _,
22+
R, O, M, A, N, _,
23+
T, A, R, A, S, O, V, Y, C, H,
24+
_
25+
};
26+
27+
void setWriteModeRS485() {
28+
PORTD |= 1 << PD2;
29+
delay(1);
30+
}
31+
32+
ISR(USART_TX_vect) {
33+
PORTD &= ~(1 << PD2);
34+
}
35+
36+
int writeDataToMaster() {
37+
unsigned short checkSumCRC = get_crc8(data_array, DATA_ARRAY_LENGTH - 1);
38+
data_array[DATA_ARRAY_LENGTH - 1] = checkSumCRC;
39+
40+
for(int k = 0; k < 5; k++){
41+
for (int i = 0; i < DATA_ARRAY_LENGTH; i++) {
42+
byte byteToSend = data_array[i];
43+
if (k == 1 && i == DATA_ARRAY_LENGTH - 2) {
44+
byteToSend ^= (1 << 0);
45+
} else if (k == 4 && i == 3) {
46+
byteToSend ^= (1 << 0) | (1 << 2) | (1 << 5);
47+
}
48+
Serial.write(byteToSend);
49+
}
50+
}
51+
}
52+
53+
void setup() {
54+
delay(200);
55+
DDRD = 0b00000111;
56+
PORTD = 0b11111000;
57+
Serial.begin(9600, SERIAL_8N1);
58+
UCSR0B |= (1 << UCSZ02) | (1 << TXCIE0);
59+
UCSR0A |= (1 << MPCM0);
60+
delay(1);
61+
}
62+
63+
void loop() {
64+
if (Serial.available()) {
65+
byte inByte = Serial.read();
66+
if (SLAVE_ADDRESS == inByte) {
67+
UCSR0A &= ~(1 << MPCM0);
68+
setWriteModeRS485();
69+
writeDataToMaster();
70+
delay(100);
71+
}
72+
}
73+
}
74+
75+
uint8_t get_crc8(const uint8_t *data, size_t array_length) {
76+
const uint8_t poly = 0x7;
77+
uint8_t crc = 0x00;
78+
79+
for (size_t i = 0; i < array_length; i++) {
80+
crc ^= data[i];
81+
for (int j = 0; j < 8; j++) {
82+
if (crc & 0x80) {
83+
crc = (crc << 1) ^ poly;
84+
} else {
85+
crc <<= 1;
86+
}
87+
}
88+
}
89+
return crc ^ 0x00;
90+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#define ZERO 16
2+
#define ONE 17
3+
#define TWO 18
4+
#define THREE 19
5+
#define SIX 20
6+
#define DOT 21
7+
#define _ 15
8+
9+
const byte SLAVE_ADDRESS = 65;
10+
const byte DATA_ARRAY_LENGTH = 11;
11+
byte data_array[DATA_ARRAY_LENGTH] = { TWO, SIX, DOT, ONE, ONE, DOT, TWO, ZERO, ZERO, THREE, _ };
12+
13+
void setWriteModeRS485() {
14+
PORTD |= 1 << PD2;
15+
delay(1);
16+
}
17+
18+
ISR(USART_TX_vect)
19+
{
20+
PORTD &= ~(1 << PD2);
21+
}
22+
23+
void writeDataToMaster() {
24+
unsigned short checkSumCRC = get_crc8(data_array, DATA_ARRAY_LENGTH - 1);
25+
data_array[DATA_ARRAY_LENGTH - 1] = checkSumCRC;
26+
27+
for(int k = 0; k < 5; k++) {
28+
for (int i = 0; i < DATA_ARRAY_LENGTH; i++) {
29+
byte byteToSend = data_array[i];
30+
if (k == 2 && i == 0) {
31+
byteToSend ^= (1 << 3);
32+
} else if (k == 3 && i == 1) {
33+
byteToSend ^= ((1 << 3) | (1 << 7)) ;
34+
}
35+
Serial.write(byteToSend);
36+
}
37+
}
38+
}
39+
40+
void setup() {
41+
delay(200);
42+
DDRD = 0b00000111;
43+
PORTD = 0b11111000;
44+
Serial.begin(9600, SERIAL_8N1);
45+
UCSR0B |= (1 << UCSZ02) | (1 << TXCIE0);
46+
UCSR0A |= (1 << MPCM0);
47+
delay(1);
48+
}
49+
50+
void loop() {
51+
if (Serial.available()) {
52+
byte inByte = Serial.read();
53+
if (SLAVE_ADDRESS == inByte) {
54+
UCSR0A &= ~(1 << MPCM0);
55+
setWriteModeRS485();
56+
writeDataToMaster();
57+
delay(100);
58+
}
59+
}
60+
}
61+
62+
uint8_t get_crc8(const uint8_t *data, size_t array_length) {
63+
const uint8_t poly = 0x7;
64+
uint8_t crc = 0x00;
65+
66+
for (size_t i = 0; i < array_length; i++) {
67+
crc ^= data[i];
68+
for (int j = 0; j < 8; j++) {
69+
if (crc & 0x80) {
70+
crc = (crc << 1) ^ poly;
71+
} else {
72+
crc <<= 1;
73+
}
74+
}
75+
}
76+
return crc ^ 0x00;
77+
}

0 commit comments

Comments
 (0)