Skip to content

Commit 2155147

Browse files
Merge pull request #16 from BastelBaus/master
2 parents ba28480 + 490f69b commit 2155147

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

AD9833.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ AD9833 :: AD9833 ( uint8_t FNCpin, uint32_t referenceFrequency ) {
6060
* This MUST be the first command after declaring the AD9833 object
6161
* Start SPI and place the AD9833 in the RESET state
6262
*/
63-
void AD9833 :: Begin ( void ) {
64-
SPI.begin();
63+
void AD9833 :: Begin (SPIClass* spiBus ) {
64+
65+
if ( spiBus == NULL) {
66+
this->_spiBus = &SPI;
67+
_spiBus->begin();
68+
} else {
69+
this->_spiBus = spiBus;
70+
// no begin, assumed SPI is stared outside
71+
}
6572
delay(100);
6673
Reset(); // Hold in RESET until first WriteRegister command
6774
}
@@ -344,7 +351,10 @@ void AD9833 :: WriteRegister ( int16_t dat ) {
344351
/*
345352
* We set the mode here, because other hardware may be doing SPI also
346353
*/
347-
SPI.setDataMode(SPI_MODE2);
354+
const SPISettings settings{200000, MSBFIRST, SPI_MODE2};
355+
_spiBus->beginTransaction(settings);
356+
// Old:
357+
//SPI.setDataMode(SPI_MODE2);
348358

349359
/* Improve overall switching speed
350360
* Note, the times are for this function call, not the write.
@@ -356,9 +366,11 @@ void AD9833 :: WriteRegister ( int16_t dat ) {
356366
//delayMicroseconds(2); // Some delay may be needed
357367

358368
// TODO: Are we running at the highest clock rate?
359-
SPI.transfer(highByte(dat)); // Transmit 16 bits 8 bits at a time
360-
SPI.transfer(lowByte(dat));
369+
_spiBus->transfer(highByte(dat)); // Transmit 16 bits 8 bits at a time
370+
_spiBus->transfer(lowByte(dat));
361371

362372
WRITE_FNCPIN(HIGH); // Write done
373+
_spiBus->endTransaction() ;
374+
363375
}
364376

AD9833.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
/*
22
* AD9833.h
3-
*
3+
*
44
* Copyright 2016 Bill Williams <wlwilliams1952@gmail.com, github/BillWilliams1952>
5-
*
5+
*
66
* This program is free software; you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by
88
* the Free Software Foundation; either version 2 of the License, or
99
* (at your option) any later version.
10-
*
10+
*
1111
* This program is distributed in the hope that it will be useful,
1212
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1313
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414
* GNU General Public License for more details.
15-
*
15+
*
1616
* You should have received a copy of the GNU General Public License
1717
* along with this program; if not, write to the Free Software
1818
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
1919
* MA 02110-1301, USA.
20-
*
21-
*
20+
*
21+
*
2222
*/
2323

24+
// update
25+
2426
#ifndef __AD9833__
2527

2628
#define __AD9833__
@@ -52,25 +54,27 @@
5254

5355
#define PHASE_WRITE_CMD 0xC000 // Setup for Phase write
5456
#define PHASE1_WRITE_REG 0x2000 // Which phase register
55-
#define FREQ0_WRITE_REG 0x4000 //
57+
#define FREQ0_WRITE_REG 0x4000 //
5658
#define FREQ1_WRITE_REG 0x8000
5759
#define PHASE1_OUTPUT_REG 0x0400 // Output is based off REG0/REG1
5860
#define FREQ1_OUTPUT_REG 0x0800 // ditto
5961

6062
typedef enum { SINE_WAVE = 0x2000, TRIANGLE_WAVE = 0x2002,
6163
SQUARE_WAVE = 0x2028, HALF_SQUARE_WAVE = 0x2020 } WaveformType;
62-
64+
6365
typedef enum { REG0, REG1, SAME_AS_REG0 } Registers;
6466

6567
class AD9833 {
6668

6769
public:
68-
70+
6971
AD9833 ( uint8_t FNCpin, uint32_t referenceFrequency = 25000000UL );
7072

7173
// Must be the first command after creating the AD9833 object.
72-
void Begin ( void );
73-
74+
// Must be the first command after creating the AD9833 object.
75+
// if nothing is given (i.e. NULL) default SPI is used
76+
void Begin (SPIClass *spiBus = NULL);
77+
7478
// Setup and apply a signal. Note that any calls to EnableOut,
7579
// SleepMode, DisableDAC, or DisableInternalClock remain in effect
7680
void ApplySignal ( WaveformType waveType, Registers freqReg,
@@ -112,13 +116,13 @@ class AD9833 {
112116
// Enable / Disable Internal Clock
113117
void DisableInternalClock ( bool enable );
114118

115-
// Return actual frequency programmed in register
119+
// Return actual frequency programmed in register
116120
float GetActualProgrammedFrequency ( Registers reg );
117121

118122
// Return actual phase programmed in register
119123
float GetActualProgrammedPhase ( Registers reg );
120124

121-
// Return frequency resolution
125+
// Return frequency resolution
122126
float GetResolution ( void );
123127

124128
private:
@@ -133,7 +137,7 @@ class AD9833 {
133137
uint32_t refFrequency;
134138
float frequency0, frequency1, phase0, phase1;
135139
Registers activeFreq, activePhase;
140+
SPIClass *_spiBus;
136141
};
137142

138143
#endif
139-

0 commit comments

Comments
 (0)