-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster.ino
162 lines (131 loc) · 3.82 KB
/
master.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#define CLK 2
#define MISO 3
#define MOSI 4
#define CS 5
#define CP 15
// period in ms
// R: CLK, B: MISO, G: MOSI, Y: CS
char in_char = 0;
int in_byte[8] = {0,0,0,0,0,0,0,0};
int parity_bit = 0;
void setup() {
Serial.begin(9600);
pinMode(CLK,OUTPUT);
pinMode(MISO,OUTPUT);
pinMode(MOSI,OUTPUT);
pinMode(CS,OUTPUT);
for (unsigned int i = 0; i < 5; i ++) {
test();
}
digitalWrite(MOSI,HIGH);
pinMode(MISO, INPUT);
}
void test() {
delay(500);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
delay(500);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
}
void print_bits () {
Serial.print(">>> ");
Serial.print(in_char);
Serial.print(" | ");
Serial.print(in_byte[7]);
Serial.print(in_byte[6]);
Serial.print(in_byte[5]);
Serial.print(in_byte[4]);
Serial.print(in_byte[3]);
Serial.print(in_byte[2]);
Serial.print(in_byte[1]);
Serial.println(in_byte[0]);
}
void SPI() {
// START PHASE
digitalWrite(CLK, HIGH); digitalWrite(CS,HIGH); delay(1); digitalWrite(MOSI,LOW);
delay(CP);
delay(CP); digitalWrite(CLK, LOW);
// BIT 0 PHASE
if (in_byte[0] == 0){digitalWrite(MOSI,LOW);} else {digitalWrite(MOSI,HIGH);}
delay(CP); digitalWrite(CLK, HIGH);
delay(CP); digitalWrite(CLK, LOW);
// BIT 1 PHASE
if (in_byte[1] == 0){digitalWrite(MOSI,LOW);} else {digitalWrite(MOSI,HIGH);}
delay(CP); digitalWrite(CLK, HIGH);
delay(CP); digitalWrite(CLK, LOW);
// BIT 2 PHASE
if (in_byte[2] == 0){digitalWrite(MOSI,LOW);} else {digitalWrite(MOSI,HIGH);}
delay(CP); digitalWrite(CLK, HIGH);
delay(CP); digitalWrite(CLK, LOW);
// BIT 3 PHASE
if (in_byte[3] == 0){digitalWrite(MOSI,LOW);} else {digitalWrite(MOSI,HIGH);}
delay(CP); digitalWrite(CLK, HIGH);
delay(CP); digitalWrite(CLK, LOW);
// BIT 4 PHASE
if (in_byte[4] == 0){digitalWrite(MOSI,LOW);} else {digitalWrite(MOSI,HIGH);}
delay(CP); digitalWrite(CLK, HIGH);
delay(CP); digitalWrite(CLK, LOW);
// BIT 5 PHASE
if (in_byte[5] == 0){digitalWrite(MOSI,LOW);} else {digitalWrite(MOSI,HIGH);}
delay(CP); digitalWrite(CLK, HIGH);
delay(CP); digitalWrite(CLK, LOW);
// BIT 6 PHASE
if (in_byte[6] == 0){digitalWrite(MOSI,LOW);} else {digitalWrite(MOSI,HIGH);}
delay(CP); digitalWrite(CLK, HIGH);
delay(CP); digitalWrite(CLK, LOW);
// BIT 7 PHASE
if (in_byte[7] == 0){digitalWrite(MOSI,LOW);} else {digitalWrite(MOSI,HIGH);}
delay(CP); digitalWrite(CLK, HIGH);
delay(CP); digitalWrite(CLK, LOW);
// PARITY PHASE
if (parity_bit == 0){digitalWrite(MOSI,LOW);} else {digitalWrite(MOSI,HIGH);}
delay(CP); digitalWrite(CLK, HIGH);
delay(CP); digitalWrite(CLK, LOW);
// END PHASE
digitalWrite(MOSI,HIGH);
delay(CP); digitalWrite(CLK, HIGH);
delay(CP); digitalWrite(CLK, LOW);
}
int parity(int which) {
parity_bit = in_byte[0];
for (unsigned int i = 1; i < 8; i++) {
parity_bit = in_byte[i] ^ parity_bit;
}
return parity_bit;
// if return 1: odd, 0: even
}
void transmit() {
in_char = Serial.read();
if (1) {
// 0: LSB, 7: MSB
in_byte[0] = (int)in_char & 0b1;
in_byte[1] = ((int)in_char & 0b10) >> 1;
in_byte[2] = ((int)in_char & 0b100) >> 2;
in_byte[3] = ((int)in_char & 0b1000) >> 3;
in_byte[4] = ((int)in_char & 0b10000) >> 4;
in_byte[5] = ((int)in_char & 0b100000) >> 5;
in_byte[6] = ((int)in_char & 0b1000000) >> 6;
in_byte[7] = ((int)in_char & 0b10000000) >> 7;
parity_bit = parity((int)in_char);
print_bits();
SPI(); // do SPI PROTOCOL
}
}
void start(int which) {
if (which == 0) { // the slave?
delay(10);
digitalWrite(MOSI,HIGH);
transmit(); // the actual transmission procedure
}
}
void loop() {
if (Serial.available() > 0) {
start(0);
}
digitalWrite(CS,LOW);
}