-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAD9850.cpp
260 lines (235 loc) · 7.46 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
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
/*
* AD9850.cpp
*
* Copyright 2016 Bill Williams <[email protected], github/BillWilliams1952>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* Normal Serial frequency Writes: 1.06 msec for a 40 bit update
* Normal Phase writes: 232-240 usec
*
* Code at: https://github.com/Billwilliams1952/AD9850-Library-Arduino
*
*/
// LOTS TODO: ----------- WORK IN PROGRESS ------------
#include "AD9850.h"
// Define AD9850 using serial clocking
AD9850 :: AD9850 ( uint8_t freqUpdate, uint8_t wordClock, uint8_t reset,
uint8_t powerDownPin, uint8_t dataPin ) {
this->freqUpdate = freqUpdate;
this->wordClock = wordClock;
this->reset = reset;
Init();
data2 = powerDownPin; // Connected to pin D2 on AD9850
pinMode(data2,OUTPUT);
digitalWrite(data2,LOW); // Not in power down
data7 = dataPin; // data pin for serial transfers
pinMode(data7,OUTPUT);
digitalWrite(data7,LOW);
phaseByte = 0;
serialLoad = true;
useDirectPort = false;
// Special case for POWER_DOWN: Pulse freqUpdate first, then wordClock
// 8 bits where POWER_DOWN is bit D2 and D0 D1 are 0 0. The rest
// are don't cares.
}
/*
* Define AD9850 using predefined pins. Either setup using serial or
* parallel modes. Uses Direct Port writes for maximum speed.
* See AD9850.h files for pin definitions
* TODO: !!!! NOT DONE YET!!!! Need
*/
AD9850 :: AD9850 ( bool useSerialLoad, uint8_t reset ) {
this->reset = reset;
pinMode(reset,OUTPUT);
digitalWrite(reset,LOW);
// Use Direct port manipulations.....
useDirectPort = true;
if ( useSerialLoad ) {
serialLoad = true;
pinMode(DIRECT_DATA_PIN,OUTPUT);
digitalWrite(DIRECT_DATA_PIN,LOW);
pinMode(DIRECT_POWER_DOWN_PIN,OUTPUT);
digitalWrite(DIRECT_POWER_DOWN_PIN,LOW);
}
else {
serialLoad = false;
// The entire port is already defined
DATA_DIRECTION_PORT = B00000000; // All outputs;
DATA_PORT = B00000011; // Setup Serial read
}
pinMode(DIRECT_FREQ_UPDATE_PIN,OUTPUT);
digitalWrite(DIRECT_FREQ_UPDATE_PIN,LOW);
pinMode(DIRECT_WORD_CLOCK_PIN,OUTPUT);
digitalWrite(DIRECT_WORD_CLOCK_PIN,LOW);
pinMode(reset,OUTPUT);
digitalWrite(reset,LOW); // not in reset mode
phase = 0.0;
frequency = 0.0;
freqWord = 0x0000;
powerDown = false;
}
// Define AD9850 using parallel clocking using non-sequential bits.
// This will require 8 separate digitalWrites for each pin. Very
// slow.
AD9850 :: AD9850 ( uint8_t freqUpdate, uint8_t wordClock, uint8_t reset,
uint8_t data7, uint8_t data6, uint8_t data5, uint8_t data4,
uint8_t data3, uint8_t data2, uint8_t data1, uint8_t data0 ) {
this->freqUpdate = freqUpdate;
this->wordClock = wordClock;
this->reset = reset;
Init();
// this->powerDown = powerDown;
this->data0 = data0;
this->data1 = data1;
this->data2 = data2;
this->data3 = data3;
this->data4 = data4;
this->data5 = data5;
this->data6 = data6;
this->data7 = data7;
phaseByte = PARALLEL_MODE;
serialLoad = false;
useDirectPort = false;
}
/*
* Private function to initialize common pins and variables
*/
void AD9850 :: Init ( void ) {
pinMode(freqUpdate,OUTPUT);
digitalWrite(freqUpdate,LOW);
pinMode(wordClock,OUTPUT);
digitalWrite(wordClock,LOW);
pinMode(reset,OUTPUT);
digitalWrite(reset,LOW); // not in reset mode
phase = 0.0;
frequency = 0.0;
freqWord = 0x0000;
powerDown = false;
}
void AD9850 :: ApplySignal ( float frequencyInHz, float phaseInDeg ) {
CalculateFrequencyWord(frequencyInHz);
CalculatePhaseByte(phaseInDeg);
if ( serialLoad ) LoadSerial();
else LoadParallel();
}
void AD9850 :: SetFrequency ( float frequencyInHz ) {
// 32 bit frequency word
CalculateFrequencyWord(frequencyInHz);
// Now write it out, either by Serial or by Parallel
if ( serialLoad ) LoadSerial();
else LoadParallel();
}
void AD9850 :: IncrementFrequency ( float frequencyInHz ) {
SetFrequency(frequency+frequencyInHz);
}
/*
* Private function to calculate the frequency word given a frequency
* in Hertz.
*/
void AD9850 :: CalculateFrequencyWord ( float frequencyInHz ) {
// TODO: More realistic number here!!
if ( frequencyInHz > 25000000.0 ) frequencyInHz = 25000000.0;
else if ( frequencyInHz < 0.0 ) frequencyInHz = 0.0;
frequency = frequencyInHz;
freqWord = (uint32_t)(frequencyInHz * BITS_PER_HZ);
}
void AD9850 :: SetPhase ( float phaseInDeg ) {
CalculatePhaseByte(phaseInDeg);
if ( serialLoad ) LoadSerial();
else LoadParallel();
}
void AD9850 :: IncrementPhase ( float phaseInDeg ) {
SetPhase(phase+phaseInDeg);
}
/*
* Private function to calculate the phase byte (5 bits) given a phase
* in degrees.
*/
void AD9850 :: CalculatePhaseByte ( float phaseInDeg ) {
phaseInDeg = fmod(phaseInDeg,360);
if ( phaseInDeg < 0 ) phaseInDeg += 360;
phase = phaseInDeg;
phaseByte = (((uint8_t)(phaseInDeg * BITS_PER_DEG)) << 3);
}
/*
* From Analog Devices Product Description sheet:
* This is the master reset function; when set high, it clears all
* registers (except the input register), and the DAC output goes to
* cosine 0 after additional clock cycles.
*/
void AD9850 :: Reset ( void ) {
digitalWrite(freqUpdate,HIGH);
PULSE_HIGH(reset); // Default to parallel mode
digitalWrite(freqUpdate,LOW);
PULSE_HIGH(wordClock); // Force into Serial mode
PULSE_HIGH(freqUpdate); // Now each wordClock start serial input
// followed by freqUpdate when complete
}
/*
* Enable / Disable the Powerdown function
*/
void AD9850 :: PowerDown ( bool enable ) {
powerDown = enable;
if ( serialLoad )
LoadSerial();
else {
}
}
/*
* Private function to setup for a serial load. 40 bits of data.
*/
void AD9850 :: LoadSerial ( void ) {
// freqWord and phaseByte
uint32_t freq = freqWord;
for ( uint8_t i = 0; i < 32; i++ ) {
// digitalWrites are SLOW
// Use digitalWriteFast here
digitalWrite(data7,(uint8_t)(freq & 0x1));
// Use digitalWriteFast here
PULSE_HIGH(wordClock);
freq >>= 1;
}
uint8_t phaseVal = phaseByte;
if ( powerDown ) phaseVal |= POWER_DOWN_BIT;
else phaseVal &= ~POWER_DOWN_BIT;
for ( uint8_t i = 0; i < 8; i++ ) {
// Use digitalWriteFast here
digitalWrite(data7,phaseVal & 0x01);
PULSE_HIGH(wordClock);
phaseVal >>= 1;
}
// Use digitalWriteFast here
PULSE_HIGH(freqUpdate); // Should see frequency/phase now.
}
/*
* Private function to perform a Parallel load the 40 bits (5 bytes) of
* frequency, phase, and control data. Check if this is a direct port
* write, and if so, use the PORTX direct write function.
*/
void AD9850 :: LoadParallel ( void ) {
// freqWord and phaseByte
// Load control byte with 5 bits of phase in D7-D3 then 000 in
// D2-D0 (PPPPP000) pulse it in as first word. Then pulse in
// the four frequency bytes. ?? Check this ??
digitalWrite(data0,0); // Setup for parallel transfer
digitalWrite(data1,0); // ditto
digitalWrite(data2,powerDown); // data2 powerDown bit
uint8_t phase = phaseByte;
if ( powerDown ) phase |= POWER_DOWN_BIT;
else phase &= ~POWER_DOWN_BIT;
phase &= ~SERIAL_MODE;
}