1 | #include <TimerOne.h>
|
2 |
|
3 | #define TSTBY 605 // StandBy-Pulse 600µs + 5µs offset
|
4 | #define TSS 15 // StartHeader setup time 10µs + 5µs offset
|
5 | #define THDR 10 // StartHeader low pulse time 5µs + 5µs offset
|
6 | #define TE 15 // BitPeriod time 10µs + 5µs offset
|
7 |
|
8 | #define BUSPIN 9 // Pin to communicate with set to 9
|
9 | #define PINSTOUT 1 // Pin State Output
|
10 | #define PINSTIN 0 // Pin State Input
|
11 |
|
12 | bool pinState = PINSTOUT; // pinState global
|
13 |
|
14 | void setup()
|
15 | {
|
16 | Serial.begin(9600);
|
17 |
|
18 | StandByPulse(); // Activate the standby-pulse
|
19 | StartHeaderPulse();
|
20 | AKNSequence(true);
|
21 | AdressPulse();
|
22 | AKNSequence(false);
|
23 | }
|
24 |
|
25 | void loop()
|
26 | {
|
27 |
|
28 | /* add main program code here */
|
29 |
|
30 | }
|
31 |
|
32 | void StandByPulse()
|
33 | {
|
34 | if (pinState != PINSTOUT){ pinMode(BUSPIN, OUTPUT); pinState = PINSTOUT; } // Set bus pin to output // Set pinState global to output
|
35 |
|
36 | digitalWrite(BUSPIN, HIGH); // Write high digital signal
|
37 | delayMicroseconds(TSTBY); // let standby-pulse time pass
|
38 | }
|
39 |
|
40 | void StartHeaderPulse()
|
41 | {
|
42 | digitalWrite(BUSPIN, LOW); // High to Low transition for THDR
|
43 | delayMicroseconds(THDR); // THDR Time
|
44 |
|
45 | if (pinState != PINSTOUT){ pinMode(BUSPIN, OUTPUT); pinState = PINSTOUT; } // Set bus pin to output // Set pinState global to output
|
46 |
|
47 | // Low to High transition
|
48 | digitalWrite(BUSPIN, HIGH); // High to Low transition escaping THDR pulse
|
49 |
|
50 | /* Bit Period*/
|
51 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
52 | digitalWrite(BUSPIN, LOW); // Send "0"
|
53 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
54 | /* Bit Period*/
|
55 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
56 | digitalWrite(BUSPIN, HIGH); // Send "1"
|
57 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
58 | /* Bit Period*/
|
59 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
60 | digitalWrite(BUSPIN, LOW); // Send "0"
|
61 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
62 | /* Bit Period*/
|
63 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
64 | digitalWrite(BUSPIN, HIGH); // Send "1"
|
65 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
66 | /* Bit Period*/
|
67 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
68 | digitalWrite(BUSPIN, LOW); // Send "0"
|
69 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
70 | /* Bit Period*/
|
71 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
72 | digitalWrite(BUSPIN, HIGH); // Send "1"
|
73 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
74 | /* Bit Period*/
|
75 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
76 | digitalWrite(BUSPIN, LOW); // Send "0"
|
77 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
78 | /* Bit Period*/
|
79 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
80 | digitalWrite(BUSPIN, HIGH); // Send "1"
|
81 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
82 | /* Bit Period*/
|
83 | }
|
84 |
|
85 | void AdressPulse()
|
86 | {
|
87 | if (pinState != PINSTOUT){ pinMode(BUSPIN, OUTPUT); pinState = PINSTOUT; } // Set bus pin to output // Set pinState global to output
|
88 |
|
89 | // Low Transiton cause of NoSAK
|
90 | digitalWrite(BUSPIN, LOW); // Write high digital signal
|
91 |
|
92 | /* Bit Period*/
|
93 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
94 | digitalWrite(BUSPIN, HIGH); // Send "1"
|
95 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
96 | /* Bit Period*/
|
97 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
98 | digitalWrite(BUSPIN, LOW); // Send "0"
|
99 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
100 | /* Bit Period*/
|
101 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
102 | digitalWrite(BUSPIN, HIGH); // Send "1"
|
103 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
104 | /* Bit Period*/
|
105 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
106 | digitalWrite(BUSPIN, LOW); // Send "0"
|
107 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
108 |
|
109 | digitalWrite(BUSPIN, HIGH); // High transition to be able to make a High-To-Low again
|
110 | /* Bit Period*/
|
111 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
112 | digitalWrite(BUSPIN, LOW); // Send "0"
|
113 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
114 |
|
115 | digitalWrite(BUSPIN, HIGH); // High transition to be able to make a High-To-Low again
|
116 | /* Bit Period*/
|
117 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
118 | digitalWrite(BUSPIN, LOW); // Send "0"
|
119 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
120 |
|
121 | digitalWrite(BUSPIN, HIGH); // High transition to be able to make a High-To-Low again
|
122 | /* Bit Period*/
|
123 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
124 | digitalWrite(BUSPIN, LOW); // Send "0"
|
125 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
126 |
|
127 | digitalWrite(BUSPIN, HIGH); // High transition to be able to make a High-To-Low again
|
128 | /* Bit Period*/
|
129 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
130 | digitalWrite(BUSPIN, LOW); // Send "0"
|
131 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
132 | /* Bit Period*/
|
133 | }
|
134 |
|
135 | void AKNSequence(bool isMAK)
|
136 | {
|
137 | if (pinState != PINSTOUT){ pinMode(BUSPIN, OUTPUT); pinState = PINSTOUT; } // Set bus pin to output // Set pinState global to output
|
138 | if (isMAK)
|
139 | {
|
140 | /* MAK */
|
141 | digitalWrite(BUSPIN, LOW); // Write low digital signal
|
142 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
143 | digitalWrite(BUSPIN, HIGH); // Write high digital signal
|
144 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
145 | /* MAK */
|
146 | }
|
147 | else if (!isMAK)
|
148 | {
|
149 | /* noMAK */
|
150 | digitalWrite(BUSPIN, HIGH); // Write high digital signal
|
151 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
152 | digitalWrite(BUSPIN, LOW); // Write low digital signal
|
153 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
154 | /* noMAK */
|
155 | }
|
156 |
|
157 | if (pinState = PINSTOUT){ pinMode(BUSPIN, INPUT); pinState = PINSTIN; } // Set bus pin to input // Set pinState global to Input
|
158 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
159 | Serial.println(digitalRead(BUSPIN)); // Read SAK
|
160 | delayMicroseconds(TE / 2); // Half-Bit-Period
|
161 | }
|