forked from F4GOJ/AD9850
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AD9850.cpp
93 lines (74 loc) · 1.96 KB
/
AD9850.cpp
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
/******************************************************************************************************************
* Arduino library for AD9850
* Created 23/08/2014
* Christophe Caiveau [email protected]
*
* Use this library freely
*
* Hardware connections :
* W_CLK -> any pin
* FQ_UD -> any pin
* DATA/D7 -> any pin
* RESET -> any pin
*
* Functions :
* dds.begin(W_CLK pin, FQ_UD pin, DATA pin, RESET pin); Initialize the output pins and master reset the AD9850
* dds.calibrate(frequency); Compensation of crystal oscillator frequency
* dds.setfreq(frequency,phase); frequency in Hz, phase coded on 5 bits
* dds.down(); power down mode reducing the dissipated power from 380mW to 30mW @5V
* dds.up(); wake-up the AD9850
*
* AD9850 datasheet at http://www.analog.com/static/imported-files/data_sheets/AD9850.pdf
*
*****************************************************************************************************************/
#include <AD9850.h>
AD9850 DDS;
AD9850::AD9850() {
}
void AD9850::begin(int w_clk, int fq_ud, int data, int reset) {
W_CLK = w_clk;
FQ_UD = fq_ud;
DATA = data;
RESET = reset;
deltaphase = 0;
phase = 0;
calibFreq = 125000000;
begin_priv();
}
void AD9850::begin_priv() {
pinMode(W_CLK, OUTPUT);
pinMode(FQ_UD, OUTPUT);
pinMode(DATA, OUTPUT);
pinMode(RESET, OUTPUT);
pulse(RESET);
pulse(W_CLK);
pulse(FQ_UD);
}
void AD9850::update() {
for (int i=0; i<4; i++, deltaphase>>=8) {
shiftOut(DATA, W_CLK, LSBFIRST, deltaphase & 0xFF);
}
shiftOut(DATA, W_CLK, LSBFIRST, phase & 0xFF);
pulse(FQ_UD);
}
void AD9850::setfreq(double f, uint8_t p) {
deltaphase = f * 4294967296.0 / calibFreq;
phase = p << 3;
update();
}
void AD9850::down() {
pulse(FQ_UD);
shiftOut(DATA, W_CLK, LSBFIRST, 0x04);
pulse(FQ_UD);
}
void AD9850::up() {
update();
}
void AD9850::calibrate(double TrimFreq)
{
calibFreq = TrimFreq;
}
void AD9850::pulse(int pin) {
digitalWrite(pin, HIGH);
digitalWrite(pin, LOW);
}