KarenColumbo
Active member
I'm trying to get the AD9833 to accept SPI control instead of using a library. The library ("MD_AD9833.h") works great, but to connect more AD9833 I'd have to use precious PWM pins that I need elsewhere. With direct SPI access I could use a MCP23S17 to connect those little 9833s.
I'd be extremely glad if I could "translate this into SPI":
I tried and tried but I can't seem to get this to work:
Maybe someone has experience with this particular chip and can help me along?
I'd be extremely glad if I could "translate this into SPI":
Code:
#include <SPI.h>
#include <MD_AD9833.h>
// Pins for SPI comm with the AD9833 IC
#define DATA 11 ///< SPI Data pin number
#define CLK 13 ///< SPI Clock pin number
#define FSYNC 9 ///< SPI Load pin number (FSYNC in AD9833 usage)
float noteFrequency[73] = {
32.7032, 34.6478, 36.7081, 38.8909, 41.2034, 43.6535, 46.2493, 48.9994, 51.9131, 55, 58.2705, 61.7354,
65.4064, 69.2957, 73.4162, 77.7817, 82.4069, 87.3071, 92.4986, 97.9989, 103.826, 110, 116.541, 123.471,
130.813, 138.591, 146.832, 155.563, 164.814, 174.614, 184.997, 195.998, 207.652, 220, 233.082, 246.942,
261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440, 466.164, 493.883,
523.251, 554.365, 587.33, 622.254, 659.255, 698.456, 739.989, 783.991, 830.609, 880, 932.328, 987.767,
1046.5, 1108.73, 1174.66, 1244.51, 1318.51, 1396.91, 1479.98, 1567.98, 1661.22, 1760, 1864.66, 1975.53,
2093
};
MD_AD9833 AD(FSYNC);
void setup(void)
{
AD.begin();
AD.setMode(MD_AD9833::MODE_TRIANGLE);
}
void loop (void)
{
for (int i = 0; i <= 72; i++)
{
AD.setFrequency(MD_AD9833::CHAN_0, noteFrequency[i]);
delay(500);
}
}
I tried and tried but I can't seem to get this to work:
Code:
#include <SPI.h>
// Pins for SPI comm with the AD9833 IC
#define DATA 11 ///< SPI Data pin number
#define CLK 13 ///< SPI Clock pin number
#define FSYNC 9 ///< SPI Load pin number (FSYNC in AD9833 usage)
// AD9833 control word and frequency register addresses
const uint16_t AD_CTRL = 0x2000U;
const uint16_t AD_FREQ0 = 0x4000U;
const uint16_t AD_FREQ1 = 0x8000U;
const uint32_t FREQ_FACTOR = 268435456;
const uint32_t F_MCLK = 25000000;
float noteFrequency[73] = {
32.7032, 34.6478, 36.7081, 38.8909, 41.2034, 43.6535, 46.2493, 48.9994, 51.9131, 55, 58.2705, 61.7354,
65.4064, 69.2957, 73.4162, 77.7817, 82.4069, 87.3071, 92.4986, 97.9989, 103.826, 110, 116.541, 123.471,
130.813, 138.591, 146.832, 155.563, 164.814, 174.614, 184.997, 195.998, 207.652, 220, 233.082, 246.942,
261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440, 466.164, 493.883,
523.251, 554.365, 587.33, 622.254, 659.255, 698.456, 739.989, 783.991, 830.609, 880, 932.328, 987.767,
1046.5, 1108.73, 1174.66, 1244.51, 1318.51, 1396.91, 1479.98, 1567.98, 1661.22, 1760, 1864.66, 1975.53,
2093
};
void adWrite(float frequency) {
digitalWrite(CLK, LOW);
digitalWrite(DATA, LOW);
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE2));
digitalWrite(FSYNC, LOW);
SPI.transfer16(AD_CTRL);
uint32_t freqWord = frequency * FREQ_FACTOR / F_MCLK;
Serial.println(freqWord);
uint16_t freqLow = freqWord & 0x3FFF;
uint16_t freqHigh = (freqWord >> 14) & 0x3FFF;
SPI.transfer16(AD_FREQ0 | freqLow);
SPI.transfer16(AD_FREQ1 | freqHigh);
digitalWrite(FSYNC, HIGH);
SPI.endTransaction();
}
void setup() {
Serial.begin(9600);
SPI.begin();
pinMode(DATA, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(FSYNC, OUTPUT);
}
void loop () {
for (int i = 0; i <= 72; i++) {
adWrite(noteFrequency[i]);
Serial.println(noteFrequency[i]);
delay(500);
}
}
Maybe someone has experience with this particular chip and can help me along?