-
Notifications
You must be signed in to change notification settings - Fork 1
/
magloop_rx.h
269 lines (231 loc) · 6.43 KB
/
magloop_rx.h
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#ifndef _MAGLOOPRX
#define _MAGLOOPRX
#ifdef _MAGLOOPTX
#error Cannot implement receiver and transmitter on same device! only include magloop_rx.h _or_ magloop_tx.h
#endif
//**************************************************************************************
// 5KHz ASK receiver for magnetic loop communications
// Runs on ATMEGA88, ATMEGA168 and ATMEGA328P based boards, possiply others too,
// Dzl 2014
//**************************************************************************************
#include <avr/signal.h>
#include <math.h>
#define SET(x,y) (x |=(1<<y))
#define CLR(x,y) (x &= (~(1<<y)))
#define CHK(x,y) (x & (1<<y))
#define TOG(x,y) (x^=(1<<y))
//*****************************************************************************
// Timer interrupt
//*****************************************************************************
class TMagloopRX
{
public:
volatile float I; //-Inphase detector signal
volatile float Q; //-Quadrature detector signal
volatile float signal; //-Detector signal magnitude
volatile float smax; //-Signal peak detector w. decay
volatile unsigned char TFLAG; //-Sync flag
volatile unsigned char tstate; //-Sampler sequencer
volatile float II; //-Intermediate inphase signal
volatile float QQ; //-Intermediate quadrature signal
volatile float x0; //-Intermediate signal
volatile unsigned char data_enabled; //-Data decoder enabled flag
volatile unsigned char tcnt; //-Sample time divider for serial decoder
volatile unsigned char rxstate; //-Data decoder state
volatile unsigned char data; //-Data decoder shift register
volatile unsigned char cnt; //-Data decoder time divider
volatile float thresh; //-Data slicer threshold
volatile unsigned char data_ready; //-Data decoded flag
volatile unsigned char analog_channel; //-Current analog channel
//---------------------------------------------------
// Constructor
//---------------------------------------------------
TMagloopRX()
{
TFLAG=0;
tcnt=100;
tstate=0;
signal=0;
x0=0;
smax=0;
data_enabled=0;
rxstate=0;
data=0;
cnt=1;
thresh=8.0;
data_ready=0;
analog_channel=0;
}
//---------------------------------------------------
// Set up
//---------------------------------------------------
void begin()
{
data_enabled=false;
ADMUX=0xC0|analog_channel;
ADCSRA=0xA6;
ADCSRB=0x05;
TCCR1A=0x00;
TCCR1B=0x09;
TCCR1C=0;
// OCR1A=2403;
OCR1A=2400;
OCR1B=1200;
SET(TIMSK1,OCIE1A);
}
//---------------------------------------------------
// Read signal magnitude
//---------------------------------------------------
float getSignal()
{
cli();
float f=signal;
sei();
return f;
}
//---------------------------------------------------
// Read decayed peak signal
//---------------------------------------------------
float getMax()
{
cli();
float f=smax;
sei();
return f;
}
//---------------------------------------------------
// Synchronize to receiver
//---------------------------------------------------
unsigned char sync()
{
if(TFLAG)
{
TFLAG=0;
return 1;
}
return 0;
}
//---------------------------------------------------
// Set analog channel for next aqusition
//---------------------------------------------------
void setChannel(unsigned char channel)
{
analog_channel=channel;
}
//---------------------------------------------------
// Check for decoded data
//---------------------------------------------------
unsigned char available()
{
data_enabled=true;
return data_ready;
}
//---------------------------------------------------
// Read decoded data
//---------------------------------------------------
unsigned char read()
{
data_ready=false;
return data;
}
};
//---------------------------------------------------
// MagLoop instance
//---------------------------------------------------
TMagloopRX magLoopRX;
//---------------------------------------------------
// Interrupt service routine
//---------------------------------------------------
SIGNAL(TIMER1_COMPA_vect)
{
ADMUX=0xC0|magLoopRX.analog_channel;
SET(TIFR1,OCF1B); //-Retrigger ADC
SET(TIFR1,OCF1A); //-Retrigger INT
switch(magLoopRX.tstate++)
{
case 0:
magLoopRX.x0=sqrt(magLoopRX.II+magLoopRX.QQ);
magLoopRX.II=(float)ADC;
break;
case 1:
magLoopRX.QQ=(float)ADC;
magLoopRX.signal=magLoopRX.signal*0.9+magLoopRX.x0*0.1;
break;
case 2:
magLoopRX.I=magLoopRX.II-=(float)ADC;
magLoopRX.II*=magLoopRX.II;
break;
case 3:
magLoopRX.Q=magLoopRX.QQ-=(float)ADC;
magLoopRX.QQ*=magLoopRX.QQ;
magLoopRX.tstate=0;
if(magLoopRX.tcnt)
magLoopRX.tcnt--;
else
{
magLoopRX.tcnt=3;
if(magLoopRX.signal>magLoopRX.smax)
magLoopRX.smax=magLoopRX.signal;
magLoopRX.smax*=0.995;
magLoopRX.TFLAG=1; //-Measurement done
if(magLoopRX.data_enabled)
{
switch(magLoopRX.rxstate)
{
case 0:
magLoopRX.thresh=magLoopRX.smax/2;
if(magLoopRX.signal<magLoopRX.thresh)
{
magLoopRX.cnt=14;
magLoopRX.data=0;
magLoopRX.rxstate=1;
magLoopRX.data_ready=false;
}
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
if(magLoopRX.cnt--)
break;
else
{
magLoopRX.cnt=9;
magLoopRX.rxstate++;
magLoopRX.data>>=1;
if(magLoopRX.signal>magLoopRX.thresh)
{
magLoopRX.data|=0x80;
}
if(magLoopRX.rxstate==9)
magLoopRX.data_ready=true;
}
break;
case 9:
if(magLoopRX.cnt--)
break;
else
{
magLoopRX.cnt=150;
magLoopRX.rxstate=10;
}
break;
case 10:
if(magLoopRX.signal>magLoopRX.thresh)
magLoopRX.rxstate=0;
if(magLoopRX.cnt)
magLoopRX.cnt--;
else
magLoopRX.rxstate=0;
break;
}
}
}
break;
}
}
#endif