1
1
/*
2
2
Manchester Decoding, reading by delay rather than interrupt
3
- Rob Ward July 2014
3
+ Rob Ward August 2014
4
4
This example code is in the public domain.
5
5
Use at your own risk, I will take no responsibility for any loss whatsoever its deployment.
6
6
Visit https://github.com/robwlakes/ArduinoWeatherOS for the latest version and
7
- documentation. Filename: DebugManchester.ino ReadMe: DebugMan.md
8
-
9
- NB: Choosing the wrong polarity will still provide "data", but will be incorrect
10
- even thought the two delays are working properly!!!
11
- Remember to check both Polarity settings for sensible data.
7
+ documentation. Filename: DebugManchester.ino
12
8
13
- DebugVersion_09, 28tyh July 2014
9
+ DebugVersion_10, 1stAugust 2014
14
10
15
11
*/
16
12
@@ -19,11 +15,10 @@ int RxPin = 8; //The number of signal from the Rx
19
15
int ledPin = 13 ; // The number of the onboard LED pin
20
16
21
17
// Variables for Manchester Receiver Logic:
22
- word sDelay = 240 ; // Small Delay about 1/4 of bit duration try like 220 to 480
23
- word lDelay = 480 ; // Long Delay about 1/2 of bit duration try like 440 to 880 , 1/4 + 1/2 = 3/4
18
+ word sDelay = 250 ; // Small Delay about 1/4 of bit duration try like 250 to 500
19
+ word lDelay = 500 ; // Long Delay about 1/2 of bit duration try like 500 to 1000 , 1/4 + 1/2 = 3/4
24
20
byte polarity = 1 ; // 0 for lo->hi==1 or 1 for hi->lo==1 for Polarity, sets tempBit at start
25
- byte tempBit ; // Reflects the required transition polarity
26
- byte bitState ; // State of the RxPin 3/4 way through Bit Waveform
21
+ byte tempBit = 1 ; // Reflects the required transition polarity
27
22
byte discards = 0 ; // how many leading "bits" need to be dumped, usually just a zero if anything eg discards=1
28
23
boolean firstZero = false ;// has it processed the first zero yet? This a "sync" bit.
29
24
boolean noErrors = true ; // flags if signal does not follow Manchester conventions
@@ -33,7 +28,7 @@ byte headerHits = 0; //Counts the number of "1"s to determine a header
33
28
// Variables for Byte storage
34
29
byte dataByte = 0 ; // Accumulates the bit information
35
30
byte nosBits = 0 ; // Counts to 8 bits within a dataByte
36
- byte maxBytes = 6 ; // Set the bytes collected after each header. NB if set too high, any end noise will cause an error
31
+ byte maxBytes = 5 ; // Set the bytes collected after each header. NB if set too high, any end noise will cause an error
37
32
byte nosBytes = 0 ; // Counter stays within 0 -> maxBytes
38
33
// Variables for multiple packets
39
34
byte bank = 0 ; // Points to the array of 0 to 3 banks of results from up to 4 last data downloads
@@ -61,7 +56,7 @@ void setup() {
61
56
Serial.begin (115200 );// make it fast so it dumps quick!
62
57
pinMode (RxPin, INPUT);
63
58
pinMode (ledPin, OUTPUT);
64
- Serial.println (" Debug Manchester Version GIT-Hub V01 " );
59
+ Serial.println (" Debug Manchester Version 09 " );
65
60
lDelay=2 *sDelay ;// just to make sure the 1:2 ratio is established. They can have some other ratio if required
66
61
Serial.print (" Using a delay of 1/4 bitWaveform " );// +-15% and they still seem to work ok, pretty tolerant!
67
62
Serial.print (sDelay ,DEC);
@@ -91,7 +86,7 @@ void setup() {
91
86
92
87
// Main routines, find header, then sync in with it, get a packet, and decode data in it, plus report any errors.
93
88
void loop (){
94
- tempBit=polarity^1 ; // these begin as opposites for each packet
89
+ tempBit=polarity^1 ; // these begin the opposites for a packet
95
90
noErrors=true ;
96
91
firstZero=false ;
97
92
headerHits=0 ;
@@ -108,18 +103,16 @@ void loop(){
108
103
noErrors=false ;// something has gone wrong, polarity has changed too early, ie always an error
109
104
}// exit and retry
110
105
else {
111
- // now grab the bitState before setting up for next bit & correct for polarity
112
- bitState = tempBit ^ polarity;// Polarity=1, invert OR Polarity=0, ignore
113
- // Now work on what the next bit waveform is going to be
106
+ byte bitState = tempBit ^ polarity;// if polarity=1, invert the tempBit or if polarity=0, leave it alone.
114
107
delayMicroseconds (lDelay);
115
108
// now 1 quarter into the next bit pattern,
116
- if (digitalRead (RxPin)==tempBit){ // if RxPin has NOT swapped, then next data bit value IS swapping
109
+ if (digitalRead (RxPin)==tempBit){ // if RxPin has not swapped, then bitWaveform is swapping
117
110
// If the header is done, then it means data change is occuring ie 1->0, or 0->1
118
- // data transition detection must swap, so it loops for the opposite transition in the next bit waveform
111
+ // data transition detection must swap, so it loops for the opposite transitions
119
112
tempBit = tempBit^1 ;
120
- }// end of detecting no transition at end of bit waveform
113
+ }// end of detecting no transition at end of bit waveform, ie end of previous bit waveform same as start of next bitwaveform
121
114
122
- // Now process the tempBit state and interept data as definite 0 or 1's, (Polarity corrected by now)
115
+ // Now process the tempBit state and make data definite 0 or 1's, allow possibility of Pos or Neg Polarity
123
116
if (bitState==1 ){ // 1 data could be header or packet
124
117
if (!firstZero){
125
118
headerHits++;
@@ -142,7 +135,6 @@ void loop(){
142
135
// we have our header, chewed up any excess and here is a zero
143
136
if ((!firstZero)&&(headerHits>=headerBits)){ // if first zero, it has not been found previously
144
137
firstZero=true ;
145
- // Serial.print("!");
146
138
}// end of finding first zero
147
139
add (bitState);
148
140
}// end of dealing with a zero
@@ -170,7 +162,7 @@ void add(byte bitData){
170
162
// Serial.print("B");
171
163
}
172
164
if (nosBytes==maxBytes){
173
- hexBinDump ();// for debug purposes dump out in hex and bainary
165
+ hexBinDump ();// for debug purposes dump out in hex and binary
174
166
// analyseData();//later on develop your own analysis routines
175
167
}
176
168
}
@@ -212,3 +204,18 @@ void eraseManchester(){
212
204
213
205
214
206
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+
0 commit comments