-
Notifications
You must be signed in to change notification settings - Fork 43
/
main6.c
495 lines (391 loc) · 13 KB
/
main6.c
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
wearable microphone jammer
- ATTINY85/45/25 chip & AVR-GCC compiler version
- the code is less than 2048 long
CONNECTION
--------
AD9833 PCB to ATTINY85:
FDATA --> PB1 (MOSI / D1 )
SCLK --> PB2 (SCK)
FSYNC --> PB3 (SS / CS )
DGND --> GND
VCC --> VCC
AD9833 PCB to PAM 8403 / TPA3116D2
AOUT --> LEFT AUDIO IN + RIGHT AUDIO IN
AGND --> AUDIO GND
REMEMBER TO CONNECT AD9833 TO VCC/5V AND GND
*/
#include <stdio.h>
#include <stdint.h>
#include <util/delay.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/delay.h>
// default ATTINY85 internal clock speed is only 1MHz ( 8 MHz DIV 8 )
// we are using 16MHz here
#define F_CPU 16000000
/**
* SPI pins - definition for ATTINY85
*/
#define SPI_DDR_PORT DDRB
#define USCK_DD_PIN DDB2
#define DO_DD_PIN DDB1
#define DI_DD_PIN DDB0
/** \name AD9833 waveform output modes
* Parameters of \ref ad9833_set_mode()
* @{
*/
#define AD_OFF 0
#define AD_TRIANGLE 1
#define AD_SQUARE 2
#define AD_SINE 3
/**@}*/
/** \name AD9833 command register bits
* @{
*/
#define AD_B28 13
#define AD_HLB 12
#define AD_FSELECT 11
#define AD_PSELECT 10
#define AD_RESET 8
#define AD_SLEEP1 7
#define AD_SLEEP12 6
#define AD_OPBITEN 5
#define AD_DIV2 3
#define AD_MODE 1
/** @}*/
/** \name AD9833 register addresses
* @{
*/
#define AD_FREQ0 (1<<14)
#define AD_FREQ1 (1<<15)
#define AD_PHASE0 (3<<14)
#define AD_PHASE1 ((3<<14)|(1<<13))
/** @}*/
/** \name AD9833 calculation macros
* @{ */
#define AD_F_MCLK 25000000 ///<Clock speed of the ad9833 reference clock
#define AD_2POW28 268435456 ///<used in calculating output freq
/** Macro that calculates the value for a ad9833 frequency register from a frequency */
#define AD_FREQ_CALC(freq) (uint32_t)(((double)AD_2POW28/(double)AD_F_MCLK*freq)*4)
/** Macro that calculates value for Timer1 output compare from a frequency*/
#define AD_MOD_FREQ_CALC(freq) (F_CPU/(64*(uint32_t)freq))
/** Macro that calculates the value for a ad9833 phase register from a phase in degrees */
#define AD_PHASE_CALC(phase_deg) (uint16_t)((512*phase_deg)/45)
/** @} */
/** Struct that holds all the configuration it's initialized as a global variable
* in the ad9833.c file */
typedef struct {
float freq[2]; ///<Holds the frequencies of
float phase[2];
float mod_freq;
uint8_t freq_out;
uint8_t phase_out;
uint8_t mode;
uint16_t command_reg;
} ad9833_settings_t;
ad9833_settings_t ad_settings; ///<This is used to store all settings.
// randomized infrasound 400 values to swipe between 24kHz - 26kHz
const uint8_t randomized[] PROGMEM =
{
/*
17,21,23,22,25,9,10,9,6,13,20,12,11,10,20,13,19,16,9,15,20,15,5,6,
25,11,13,10,6,23,12,25,21,11,20,7,6,22,9,25,20,24,19,13,15,14,20,
9,5,19,13,17,13,13,25,7,20,21,20,17,21,17,13,9,24,20,20,16,21,9,5,
23,9,11,6,9,16,21,21,24,8,24,24,24,10,9,23,13,9,14,24,25,24,18,6,5,
17,22,21,24,20,16,16,25,15,21,21,11,6,25,15,11,18,18,20,25,13,18,
11,7,13,16,13,5,19,6,7,17,24,6,10,11,13,24,23,21,7,23,14,17,9,9,17,
16,13,13,6,21,19,22,10,8,5,10,22,24,23,15,5,7,17,20,6,7,14,20,10,
25,19,13,5,20,9,24,21,11,6,18,6,13,19,15,18,7,17,21,12,24,21,20,11,
19,20,19,13,19,17,15,18,18,6,20,20,6,20,8,14,14,19,10,20,15,18,6,
12,24,14,5,17,12,25,13,16,8,7,19,21,20,18,11,7,12,17,21,10,17,6,9,
23,14,24,19,12,19,11,18,23,14,20,10,8,10,16,18,12,14,25,12,17,21,
25,21,5,13,11,6,5,12,18,17,18,21,8,16,8,12,22,20,17,12,19,17,24,
14,17,20,22,24,19,16,13,7,21,21,17,5,15,10,23,25,6,17,11,11,15,
13,5,13,11,25,9,11,18,14,20,9,15,10,25,13,17,16,5,19,14,24,15,
19,7,15,18,13,10,21,10,15,19,24,18,19,16,15,19,14,15,23,24,7,11,
9,10,24,11,19,10,12,13,11,14,5,15,13,17,25,9,21,7,20,12,15,10,5,
14,10,10,16,23,20,11,18,17,7,8,11,23,9,12,8,13,14,6,20,18,20,24,
25,21,17,12,7,19,24,12,18,6,13,8,9,19,10,7,19,15,7,24,21,19,16,14,
8,23,20,20,18,24,25,21,10,17,19,21,18,16,21,14,19,15,18,8,23,17,13,
21,7,20,14,11,10,8,11,9,24,18,19,25,12,12,8,19,17,17,24,5,10,11,11,
11,25,12,19,13,6,9,21,10,12,8,14,5,7,22,21,7,15,8,22,18,17,6,16,7
*/
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,19,18,17,16,15,
14,13,12,11,10,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,2,3,
4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,19,18,17,16,15,14,13,
12,11,10,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
18,19,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,2,3,4,5,
6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,19,18,17,16,15,14,13,12,
11,10,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
18,19,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,2,3,4,5,
6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,19,18,17,16,15,14,13,12,
11,10,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
18,19,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,2,3,4,5,
6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,19,18,17,16,15,14,13,12,
11,10,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,
18,19,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,2,3,4,
5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
};
// ***********************************
// ******* SPI SPECIFIC ROUTINES
// ***********************************
/**
* init
*
* SPI initialization
*/
void spi_init()
{
// ATTINY85 pin assignments
// SPI_SS = PORTB3,
// SPI_MOSI = PORTB1
// SPI_MISO = PORTB0,
// SPI_SCK = PORTB2
// Configure SPI pins
//pinMode(SPI_SS, OUTPUT);
//pinMode(SPI_MOSI, OUTPUT);
//pinMode(SPI_MISO, INPUT);
//pinMode(SPI_SCK, OUTPUT);
//digitalWrite(SPI_SCK, HIGH);
//digitalWrite(SPI_MOSI, LOW);
//digitalWrite(SPI_SS, HIGH);
DDRB |= _BV(3); // configure output for SPI CS SS
USICR &= ~(_BV(USISIE) | _BV(USIOIE) | _BV(USIWM1));
USICR |= _BV(USIWM0) | _BV(USICS1) | _BV(USICLK);
SPI_DDR_PORT |= _BV(USCK_DD_PIN); // set the USCK pin as output
SPI_DDR_PORT |= _BV(DO_DD_PIN); // set the DO pin as output
SPI_DDR_PORT &= ~_BV(DI_DD_PIN); // set the DI pin as input
// spi mode 1
USICR |= _BV(USICS0);
}
/**
*
* Send uint8_t via SPI
*
* 'value' Value to be sent
*
* Return:
* Response received from SPI slave
*/
uint8_t spi_send(uint8_t value)
{
USIDR = value;
USISR = _BV(USIOIF); // clear counter and counter overflow interrupt flag
while ( !(USISR & _BV(USIOIF)) ) USICR |= _BV(USITC);
return USIDR;
}
// ----------------------------------------------------------------
// AD8933 programmable signal generator specific routines
// ----------------------------------------------------------------
/**
* a wrapper function for sending 16-bit SPI packets.
* \param packet 16-bit value to be sent over SPI.
*/
static inline void ad9833_send(uint16_t packet)
{
PORTB |= _BV(2); // set PIN PB2 : SCLK HIGH
PORTB &= ~_BV(3); // set PIN PB3 : SS / CS LOW to write over SPI
//delayMicroseconds(2); // Some delay may be needed
// TODO: Are we running at the highest clock rate?
spi_send((uint8_t)(packet>>8)); // Transmit 16 bits 8 bits at a time
spi_send((uint8_t)packet);
PORTB |= _BV(3); // set PIN PB3 : SS / CS HIGH again
PORTB |= _BV(2); // set PIN PB2 : SCLK HIGH
}
/**
* Initializes the AD9833 and the relevant variables.
*/
void ad9833_init(void)
{
// set half awake state
ad9833_send((1<<AD_SLEEP12)|(1<<AD_RESET));
ad_settings.command_reg |= (1<<AD_SLEEP12);
//set some nice default values
ad9833_set_frequency(0, 0);
ad9833_set_frequency(1, 0);
ad9833_set_phase(0, 0);
ad9833_set_phase(1, 0);
ad9833_set_freq_out(0);
ad9833_set_phase_out(0);
}
/**
* Sets the ad9833 output waveform to the one given as a parameter.
* \param mode possible values:
* - AD_OFF
* - AD_TRIANGLE
* - AD_SQUARE
* - AD_SINE
*/
void ad9833_set_mode(uint8_t mode){
ad_settings.mode = mode;
switch (mode){
case AD_OFF:
ad_settings.command_reg |= (1<<AD_SLEEP12);
ad_settings.command_reg |= (1<<AD_SLEEP1);
break;
case AD_TRIANGLE:
ad_settings.command_reg &= ~(1<<AD_OPBITEN);
ad_settings.command_reg |= (1<<AD_MODE);
ad_settings.command_reg &= ~(1<<AD_SLEEP12);
ad_settings.command_reg &= ~(1<<AD_SLEEP1);
break;
case AD_SQUARE:
ad_settings.command_reg |= (1<<AD_OPBITEN);
ad_settings.command_reg &= ~(1<<AD_MODE);
ad_settings.command_reg |= (1<<AD_DIV2);
ad_settings.command_reg &= ~(1<<AD_SLEEP12);
ad_settings.command_reg &= ~(1<<AD_SLEEP1);
break;
case AD_SINE:
ad_settings.command_reg &= ~(1<<AD_OPBITEN);
ad_settings.command_reg &= ~(1<<AD_MODE);
ad_settings.command_reg &= ~(1<<AD_SLEEP12);
ad_settings.command_reg &= ~(1<<AD_SLEEP1);
break;
}
ad9833_send(ad_settings.command_reg);
}
/**
* Selects which frequency register is used to generate the output.
* Also used to select FSK.
* \param phase_out possible values:
* - 0 = use phase register 0
* - 1 = use phase register 1
* - 2 = PSK
*/
void ad9833_set_freq_out(uint8_t freq_out){
ad_settings.freq_out = freq_out;
switch (freq_out){
case 0:
ad_settings.command_reg &= ~(1<<AD_FSELECT);
break;
case 1:
ad_settings.command_reg |= (1<<AD_FSELECT);
break;
case 2:
//TODO
break;
}
ad9833_send(ad_settings.command_reg);
}
/**
* sets the desired ad9833 internal phase register to a value that
* produces the desired phase.
*
* \param reg the desired phase register to be manipulated, either 0 or 1
* \param phase the desired phase
*/
void ad9833_set_phase(uint8_t reg, double phase){
uint16_t reg_reg; //probably should be renamed...
if (reg==1)
reg_reg = AD_PHASE1;
else
reg_reg = AD_PHASE0;
ad_settings.phase[reg] = phase;
ad9833_send(reg_reg | AD_PHASE_CALC(ad_settings.phase[reg]));
}
/**
* Selects which phase register is used to generate the output
* Also used to select PSK
* \param phase_out possible values:
* - 0 = use phase register 0
* - 1 = use phase register 1
* - 2 = PSK
*/
void ad9833_set_phase_out(uint8_t phase_out){
ad_settings.phase_out = phase_out;
switch (phase_out){
case 0:
ad_settings.command_reg &= ~(1<<AD_PSELECT);
break;
case 1:
ad_settings.command_reg |= (1<<AD_PSELECT);
break;
case 2:
//TODO
break;
}
ad9833_send(ad_settings.command_reg);
}
/**
* sets the desired ad9833 internal frequency register to a value that
* produces the desired frequency.
*
* \param reg the desired frequency register to be manipulated, either 0 or 1
* \param freq the desired frequency
*/
void ad9833_set_frequency(uint8_t reg, double freq){
uint32_t freq_reg;
uint16_t reg_reg; //probably should be renamed...
freq_reg = AD_FREQ_CALC(freq);
ad_settings.freq[reg] = freq;
if (reg==1)
reg_reg = AD_FREQ1;
else
reg_reg = AD_FREQ0;
ad9833_send((1<<AD_B28) | ad_settings.command_reg);
ad9833_send(reg_reg | (0x3FFF&(uint16_t)(freq_reg>>2 )));
ad9833_send(reg_reg | (0x3FFF&(uint16_t)(freq_reg>>16)));
}
// -----------------------------------------------------------------------
// MAIN CODE OF THE JAMMER
// -----------------------------------------------------------------------
void main()
{
uint16_t i,j, sequence ; // for noise and delay generation
//initializing SPI
spi_init();
// initializing AD9833 generator
// _delay_ms(100);
// Delay 1 600 000 cycles
// 100ms at 16 MHz
asm volatile (
" ldi r18, 9 \n"
" ldi r19, 30 \n"
" ldi r20, 229 \n"
"1: dec r20 \n"
" brne 1b \n"
" dec r19 \n"
" brne 1b \n"
" dec r18 \n"
" brne 1b \n"
" nop \n"
);
// now initialize the chip
ad9833_init();
// _delay_ms(15);
// Delay 240 000 cycles
// 15ms at 16 MHz
asm volatile (
" ldi r18, 2 \n"
" ldi r19, 56 \n"
" ldi r20, 174 \n"
"1: dec r20 \n"
" brne 1b \n"
" dec r19 \n"
" brne 1b \n"
" dec r18 \n"
" brne 1b \n"
);
//set gen to SQUARE initially
// you may experiment with other types AD_TRIANGLE or AD_SINE
ad9833_set_mode(AD_SQUARE);
ad9833_set_frequency(0, 26000);
while (1)
{
// generating 20Hz INFRASOUND over 25kHZ ULTRASOUND CARRIER
sequence = 0;
for(i=26000; i>24000; i=i - pgm_read_byte(randomized + sequence), sequence++)
{
ad9833_set_frequency(0, (double)i );
};
sequence = 0;
for(i=24000; i<26000; i=i + pgm_read_byte(randomized + sequence), sequence++ )
{
ad9833_set_frequency(0, (double)i );
};
};
}