My AD_9833 doesn't work

Hi, I' ve connected my AD_9933 at ARDUINO DUE, but it doesn't work with the follow code:

#include <MD_AD9833.h>     // Include the library
#include <SPI.h>

#define DATA  11      // SPI Data/MOSI pin number.  Meg 2560 uses pin 51
#define CLK   13      // SPI Clock pin number.  Mega 2560 uses pin 52 
#define FNC_PIN 10       // Can be any digital IO pin
boolean bol;
 
MD_AD9833 gen(FNC_PIN);       // Defaults to 25MHz internal reference frequency




void setup() {
    gen.begin();   
    gen.setMode(MD_AD9833::MODE_SQUARE1);
    gen.setActiveFrequency(MD_AD9833::CHAN_0); 
    gen.setFrequency(MD_AD9833::CHAN_0, 10000);  
    //gen.setPhase(MD_AD9833::CHAN_1, 900);
}
 
void loop() {
}

The signal in pin out doesn't work.
Why? Nobody can help me please.

Have you checked these are the correct pins for the Due?

@yun1975, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project :wink: See About the Installation & Troubleshooting category.

Any use ?

Hi @yun1975
This module requires an SPI connection.
The SPI connection requires 4 pins.
You only programmed 2 pins.
See if this tutorial helps solve your problem.

RV mineirin

One way SPI only requires 3 pins, the OP has programmed 3 pins.

@yun1975 have you tried the simple example from Github to sort your pin assignments?

a7

If you're not reading back, you don't need MISO

1 Like

https://www.arduino.cc/en/reference/SPI

Tks, your are right.
RV mineirin

I think that is right, I checked. What do you think? I used the pin-label above Arduino-Due ports

Nobody can help me?

It's been nearly two weeks - what progress have you made?

Look at the table in this page

The Due SPI is on pins 1,3,4 of the icsp connector.

I made the follow connections on ICSP:

  • MOSI -> SDATA
  • SCK -> SCLK
  • SS = FSYNC -> PIN31 on ARDUINODUE

And I used the folllow code:

#include <SPI.h>

/*
FSYNC -> 31
SCLK -> SCK (SPI)
SDATA -> MOSI (SPI)
+Vin = 3,3V
GND -> GND
*/

#define FSYNC 31 // Pin 31 jako ‚chipSelect’

long freq;

void setup()
{
pinMode(FSYNC, OUTPUT);
SPI.begin();
Serial.begin(9600);
digitalWrite(FSYNC, HIGH);
SPI.setDataMode(SPI_MODE2);

/* Poniżej definujemy częstotliwość w Hz */
freq = 1050;

WriteFrequencyAD9833(freq);
}

void loop()
{
/* Pętla jest pusta */
}
void WriteFrequencyAD9833(long frequency)
{
int MSB;
int LSB;
int phase = 0;
long calculated_freq_word;
float AD9833Val = 0.00000000;

/* Tutaj jest zdefiniowana częstotliwość z jaką jest taktowany */
/* AD983x lub AD9837, w przypadku AD9833 jest to 25000000 Hz */
/* dla AD9837 będzie to zapewne 16000000 Hz */
AD9833Val = (((float)(frequency)) / 12500000);
/* Dla 25 MHz mamy 12,5 MHz */
/* Dla 16 MHz będziemy mieli odpowiednio 8 MHz */

calculated_freq_word = AD9833Val * 0x10000000;
MSB = (int)((calculated_freq_word & 0xFFFC000) >> 14);
LSB = (int)(calculated_freq_word & 0x3FFF);
LSB |= 0x4000;
MSB |= 0x4000;
phase &= 0xC000;
WriteRegisterAD9833(0x2100);
WriteRegisterAD9833(LSB);
WriteRegisterAD9833(MSB);
WriteRegisterAD9833(phase);

/* Jedna z poniższych lini musi być zawsze odkomentowana */
/* W zależności jaki chcemy mieć kształt sygnału, tę odkomentowujemy */
// WriteRegisterAD9833(0x2020); // Kwadrat
WriteRegisterAD9833(0x2000); // Sinus
// WriteRegisterAD9837(0x2002); // Trójkąt
}

void WriteRegisterAD9833(int dat)
{
digitalWrite(FSYNC, LOW);
SPI.transfer(highByte(dat));
SPI.transfer(lowByte(dat));
digitalWrite(FSYNC, HIGH);
}

but It doesn't work. Why?
Help me.

I connect AS9833 at ARDUINODUE's SPI as follow:

  • MOSI at SDATA of AD9833
  • SCK at SPCK of AD9833
  • VCC 3,3V at the 3,3V of ARDUINODUE
  • GND at GND of ARDUINODUE
  • FSYNC at PIN 4

Tell me if it is correct.

I used the follow code below:

#include <SPI.h>

/*
FSYNC -> 31
SCLK -> SCK (SPI)
SDATA -> MOSI (SPI)
+Vin = 3,3V
GND -> GND
*/

#define FSYNC 4 // Pin 31 jako ‚chipSelect’

long freq;

void setup()
{
pinMode(FSYNC, OUTPUT);
SPI.begin();
Serial.begin(9600);
digitalWrite(FSYNC, HIGH);
SPI.setDataMode(SPI_MODE2);

/* Poniżej definujemy częstotliwość w Hz */
freq = 1050;

WriteFrequencyAD9833(freq);
}

void loop()
{
/* Pętla jest pusta */
}
void WriteFrequencyAD9833(long frequency)
{
int MSB;
int LSB;
int phase = 0;
long calculated_freq_word;
float AD9833Val = 0.00000000;

/* Tutaj jest zdefiniowana częstotliwość z jaką jest taktowany */
/* AD983x lub AD9837, w przypadku AD9833 jest to 25000000 Hz */
/* dla AD9837 będzie to zapewne 16000000 Hz */
AD9833Val = (((float)(frequency)) / 12500000);
/* Dla 25 MHz mamy 12,5 MHz */
/* Dla 16 MHz będziemy mieli odpowiednio 8 MHz */

calculated_freq_word = AD9833Val * 0x10000000;
MSB = (int)((calculated_freq_word & 0xFFFC000) >> 14);
LSB = (int)(calculated_freq_word & 0x3FFF);
LSB |= 0x4000;
MSB |= 0x4000;
phase &= 0xC000;
WriteRegisterAD9833(0x2100);
WriteRegisterAD9833(LSB);
WriteRegisterAD9833(MSB);
WriteRegisterAD9833(phase);

/* Jedna z poniższych lini musi być zawsze odkomentowana */
/* W zależności jaki chcemy mieć kształt sygnału, tę odkomentowujemy */
// WriteRegisterAD9833(0x2020); // Kwadrat
WriteRegisterAD9833(0x2000); // Sinus
// WriteRegisterAD9837(0x2002); // Trójkąt
}

void WriteRegisterAD9833(int dat)
{
digitalWrite(FSYNC, LOW);
SPI.transfer(highByte(dat));
SPI.transfer(lowByte(dat));
digitalWrite(FSYNC, HIGH);
}

It doesn't work. WHY???

Yes the SPI connections are correct.
But remember that the int data type for the DUE is 32 bits and not 16

How can I do for resolve the problem about 32 bit?

use data type uint16_t that is an unsigned 16 bit

CAn you make any code example please?
Write any example please.

change

int MSB;
int LSB;
int phase = 0;

to

uint16_t MSB;
uint16_t LSB;
uint16_t phase = 0;