-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfgenerator.cpp
More file actions
375 lines (346 loc) · 10.2 KB
/
rfgenerator.cpp
File metadata and controls
375 lines (346 loc) · 10.2 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
The MIT License (MIT)
Copyright (c) 2016 Madis Kaal <mast@nomad.ee>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include "keypad.hpp"
#include "lcd.hpp"
#define TICKCOUNT 0xc0
// frequency sweep sync output pin set/reset
#define SYNC_HIGH() (PORTB|=4)
#define SYNC_LOW() (PORTB&=~4)
LCD display;
KeyPad keypad;
// sine table for AM modulation
uint8_t am_sintable[32] = {
128,153,177,199,218,234,245,253,
255,253,245,234,218,199,177,153,
128,103,79 ,57 ,38 ,22 ,11 ,3 ,
1 ,3 ,11 ,22 ,38 ,57 ,79 ,103
};
int32_t fm_sintable[32] = {
0L ,14631L,28701L,41667L,53033L,62360L,69290L,73558L,
75000L,73558L,69290L,62360L,53033L,41667L,28701L,14631L,
0L ,-14631L,-28701L,-41667L,-53033L,-62360L,-69290L,-73558L,
-75000L,-73558L,-69290L,-62360L,-53033L,-41667L,-28701L,-14631L
};
uint32_t ftable[32];
typedef union
{
uint64_t u64;
uint32_t halves[2];
} Z;
class DDS
{
public:
DDS() {}
#define RESET_HIGH() (PORTC|=2)
#define RESET_LOW() (PORTC&=~2)
#define WCLK_HIGH() (PORTC|=1)
#define WCLK_LOW() (PORTC&=~1)
#define FQUD_HIGH() (PORTB|=1)
#define FQUD_LOW() (PORTB&=~1)
void reset()
{
RESET_HIGH();
_delay_ms(1);
RESET_LOW();
WCLK_LOW();
FQUD_LOW();
}
void write(uint8_t b)
{
PORTD=b;
WCLK_HIGH();
WCLK_LOW();
}
// write value to DDS
void setvalue(uint32_t value)
{
FQUD_LOW();
write(0x00);
write(value>>24);
write(value>>16);
write(value>>8);
write(value);
FQUD_HIGH();
PORTD=0; // keep data bus low for quick keypad checks
}
// calculate value to write into DDS
// this is quite slow
uint32_t calcvalue(uint32_t f)
{
Z z;
// if wanted frequency is over half the clock frequency
// then aim the low alias (resulting from mixing the DDS output
// with DDS clock frequency) at the wanted frequency instead
if (f>62500000)
f=125000000-f;
z.halves[1]=f; // quicker and faster way to multiply with
z.halves[0]=0; // 2**32
// the module has 125MHz clock
return (uint32_t)(z.u64/125000000L);
}
void setfrequency(uint32_t f)
{
setvalue(calcvalue(f));
}
};
DDS dds;
void fset(const char* name,int32_t value)
{
display.clear();
display.prints(name);
display.printc('=');
display.printn(value);
display.prints("\r\n");
dds.setfrequency(value);
}
ISR(TIMER0_OVF_vect)
{
// reset timer for next interrupt
TCNT0=TICKCOUNT;
}
ISR(WDT_vect)
{
}
void error(const char* msg)
{
display.clear();
keypad.flush();
display.prints(msg);
while (!keypad.ready()) {
wdt_reset();
WDTCSR=(1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0) ; // 2sec timout, interrupt+reset
keypad.scan();
}
keypad.flush();
}
/*
I/O configuration
-----------------
I/O pin direction DDR PORT
PC0 DDS write clk output 1 1
PC1 DDS reset output 1 1
PC2 keypad input input 0 1
PC3 keypad input input 0 1
PC4 keypad input input 0 1
PC5 keypad input input 0 1
PD0 data0 output 1 0
PD1 data1 output 1 0
PD2 data2 output 1 0
PD3 data3 output 1 0
PD4 data4 output 1 0
PD5 data5 output 1 0
PD6 data6 output 1 0
PD7 data7 output 1 0
PB0 dds FQ_UD output 1 0
PB1 AM modulation sine output output 1 0
PB2 sweep sync out output 1 0
PB3 LCD R/S output 1 0
PB4 LCD E output 1 0
PB5 unused output 1 0
*/
int main(void)
{
uint16_t i;
uint8_t j;
MCUSR=0;
MCUCR=0;
// I/O directions
DDRC=0x03;
DDRD=0xff;
DDRB=0x3f;
// initial state
PORTC=0x3f;
PORTD=0x00;
PORTB=0x00;
//
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
// configure watchdog
WDTCSR=(1<<WDE) | (1<<WDCE);
WDTCSR=(1<<WDE) | (1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0) ; // 2sec timout, interrupt+reset
// configure timer0 for periodic interrupts
TCCR0B=5; // timer0 clock prescaler to 1024
TIMSK0=1; // enable overflow interrupts
TCNT0=TICKCOUNT;
display.reset();
display.cursoronoff(1);
sei();
int32_t f=0,fa=1000000L,fb=9000000L,fc,fd;
dds.reset();
fset("FA",fa);
while (1) {
sleep_cpu(); // timer interrupt wakes us up
wdt_reset();
WDTCSR=(1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0) ; // 2sec timout, interrupt+reset
keypad.scan();
if (keypad.ready()) {
char c=keypad.getch();
if (c>='0' && c<='9')
{
f=f*10+(c-'0');
display.printc('\r');
display.printn(f);
}
else {
switch (c) {
case 'D':
f=f/10;
display.printc('\r');
if (f)
display.printn(f);
display.prints(" \b");
break;
case '#':
if (fa<75000) {
error("Unable <75KHz");
}
else {
fset("FC",fa);
display.prints("Modulation: FM");
j=0;
for (j=0;j<32;j++) {
ftable[j]=dds.calcvalue(fa+fm_sintable[j]);
}
// make sure all keys are released before
// beginning because keypad scans are done
// very frequently
while (keypad.readall()) {
wdt_reset();
WDTCSR=(1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0) ; // 2sec timout, interrupt+reset
}
cli();
while (1) {
wdt_reset();
WDTCSR=(1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0) ; // 2sec timout, interrupt+reset
dds.setvalue(ftable[j++]);
j&=(COUNTOF(ftable)-1);
_delay_us(11);
if (keypad.pressed())
break;
}
sei();
}
while (keypad.pressed()) {
wdt_reset();
WDTCSR=(1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0) ; // 2sec timout, interrupt+reset
}
keypad.flush();
fset("FA",fa);
f=0;
break;
case '*':
fset("FC",fa);
f=dds.calcvalue(fa);
display.prints("Modulation: AM");
j=0;
while (keypad.readall()) {
wdt_reset();
WDTCSR=(1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0) ; // 2sec timout, interrupt+reset
}
TCCR1A=0x81;
TCCR1B=0x09; // clock prescaler 1
OCR1AH=0;
cli();
while (1) {
wdt_reset();
WDTCSR=(1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0) ; // 2sec timout, interrupt+reset
dds.setvalue(f);
OCR1AL=am_sintable[j++];
j&=(COUNTOF(am_sintable)-1);
_delay_us(12);
if (keypad.pressed())
break;
}
sei();
TCCR1A=0;
PORTB&=~2;
while (keypad.readall())
{
wdt_reset();
WDTCSR=(1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0) ; // 2sec timout, interrupt+reset
}
keypad.flush();
fset("FA",fa);
f=0;
break;
case 'A':
if (f>124999900L || f<0)
error("Unable, must be\r\n0..124999900");
else
fa=f;
fset("FA",fa);
f=0;
break;
case 'B':
if (f>124999900L || f<0)
error("Unable, must be\r\n0..124999900");
else
fb=f;
fset("FB",fb);
f=0;
break;
case 'C':
fc=(fb-fa);
if (fc<0) {
fc=0-fc;
f=fb;
fb=fa;
fa=f;
}
display.clear();
display.prints("FA=");
display.printn(fa);
display.prints("\r\n");
display.prints("FB=");
display.printn(fa+fc);
while (!keypad.ready()) {
wdt_reset();
WDTCSR=(1<<WDIE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0) ; // 2sec timout, interrupt+reset
SYNC_HIGH();
fd=0;
for (i=0;i<256;i++) {
f=fa+(fd>>8);
fd+=fc;
dds.setfrequency(f);
_delay_us(500);
}
SYNC_LOW();
keypad.scan();
_delay_ms(10);
dds.setfrequency(fa);
_delay_ms(10);
keypad.scan();
}
keypad.flush();
fset("FA",fa);
f=0;
break;
}
}
}
}
}