-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAD9851.h
159 lines (124 loc) · 3.15 KB
/
AD9851.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
#ifndef AD9851_h
#define AD9851_h
#include <limits.h>
struct DDS {
int rst;
int data;
int fq;
int clk;
};
struct DDS dds_init(int rst_pin, int data_pin, int fq_pin, int clk_pin);
static void pulse(char pin);
void dds_reset(DDS dds);
static unsigned long calcDataWord(unsigned long in_freq);
void writeFreq(DDS dds, unsigned long freq);
static void sweepUp(DDS dds, unsigned long c_freq, unsigned long s_dev, unsigned long s_step, unsigned int delay_us);
static void sweepDn(DDS dds, unsigned long c_freq, unsigned long s_dev, unsigned long s_step, unsigned int delay_us);
void sweepTone(DDS dds, unsigned long c_freq, unsigned long s_dev, long s_step, unsigned int delay_us);
struct DDS dds_init(int rst_pin, int data_pin, int fq_pin, int clk_pin)
{
DDS dds_pin;
dds_pin.rst = rst_pin;
dds_pin.data = data_pin;
dds_pin.fq = fq_pin;
dds_pin.clk = clk_pin;
pinMode(rst_pin, OUTPUT);
pinMode(data_pin, OUTPUT);
pinMode(fq_pin, OUTPUT);
pinMode(clk_pin, OUTPUT);
return dds_pin;
}
static void pulse(char pin)
{
digitalWrite(pin, HIGH);
digitalWrite(pin, LOW);
}
void dds_reset(DDS dds)
{
digitalWrite(dds.data, LOW);
digitalWrite(dds.fq, LOW);
digitalWrite(dds.clk, LOW);
pulse(dds.rst);
pulse(dds.clk);
pulse(dds.fq);
}
static unsigned long calcDataWord(unsigned long in_freq)
{
unsigned long result = 0;
for(char i=0; i<32; i++)
{
result = result << 1;
in_freq = in_freq << 1;
if(in_freq >= 180000000UL)
{
in_freq = in_freq - 180000000UL;
result++;
}
}
return result;
}
void writeFreq(DDS dds, unsigned long in_freq)
{
unsigned long data_word;
data_word = calcDataWord(in_freq * 1UL);
for(int i=0; i<32; i++)
{
digitalWrite(dds.data, (data_word & 0x01));
pulse(dds.clk);
data_word >>= 1;
}
for(int i=0; i<8; i++)
{
if(i<1)
digitalWrite(dds.data, HIGH);
else
digitalWrite(dds.data, LOW);
pulse(dds.clk);
}
pulse(dds.fq);
}
static void sweepUp(DDS dds, unsigned long c_freq, unsigned long s_dev, unsigned long s_step, unsigned int delay_us)
{
unsigned int t = (unsigned int)(s_dev / s_step);
if(s_step > s_dev)
s_step = s_dev;
for(unsigned int i=0; i<t; i++)
{
writeFreq(dds, 1UL*c_freq - s_dev/2 + i*s_step);
delayMicroseconds(delay_us);
}
}
static void sweepDn(DDS dds, unsigned long c_freq, unsigned long s_dev, unsigned long s_step, unsigned int delay_us)
{
unsigned int t = (unsigned int)(s_dev / s_step);
if(s_step > s_dev)
s_step = s_dev;
for(unsigned int i=0; i<t; i++)
{
writeFreq(dds, 1UL*c_freq + s_dev/2 - i*s_step);
delayMicroseconds(delay_us);
}
}
void sweepTone(DDS dds, unsigned long c_freq, unsigned long s_dev, long s_step, unsigned int delay_us)
{
if(s_dev > (2*c_freq))
s_dev = 2*c_freq;
if(s_step > 0)
{
if((s_dev / s_step) > (UINT_MAX - 1))
s_step = s_dev / (UINT_MAX - 1);
sweepUp(dds, c_freq, s_dev, s_step, delay_us);
}
else if(s_step < 0)
{
s_step = -1 * s_step;
if((s_dev / s_step) > (UINT_MAX - 1))
s_step = s_dev / (UINT_MAX - 1);
sweepDn(dds, c_freq, s_dev, s_step, delay_us);
}
else
{
writeFreq(dds, 1UL*c_freq);
}
}
#endif