Go Down

Topic: Regarding SPI working Modes (Read 192 times) previous topic - next topic

zazananan


Hello everyone,

I am currently using an AD9833 to generate a triangular wave and it is using MODE2 of the SPI. I am trying to send the output of the AD9833 generator back to the A0 pin of the same board so that I can see the plot of the triangular waveform having 1kHz frequency, but the serial plotter just won't start. Has anyone got any idea on how I can ix this?

Thank you.

Code: [Select]

#include <SPI.h>

const int SINE = 0x2000;                    // Define AD9833's waveform register value.
const int SQUARE = 0x2028;                  // When we update the frequency, we need to
const int TRIANGLE = 0x2002;                // define the waveform when we end writing.

int wave = 0;
int waveType = TRIANGLE;

const float refFreq = 25000000.0;           // On-board crystal reference frequency

const int FSYNC = 24;                       // Standard SPI pins for the AD9833 waveform generator.
const int CLK = 32;                         // CLK and DATA pins are shared with the TFT display.
const int DATA = 28;

unsigned long freq = 1000;               // Set initial frequency.

int sensorPin = A0;                      // select the input pin for the triangular wave

int sensorValue = 0;

void setup() {

  AD9833reset();                          // Reset AD9833 module after power-up.
  delay(50);

  pinMode(FSYNC, OUTPUT);               
  SPI.begin();                         
  digitalWrite(FSYNC, HIGH);           
  SPI.setDataMode(SPI_MODE2);           
 
}

void loop() {
   
  AD9833setFrequency(freq, waveType);
 
  Serial.begin(9600);
 
  sensorValue = analogRead(sensorPin);
 
  Serial.println(sensorValue);
  Serial.print(" ");
}

void AD9833reset() {
  WriteRegister(0x100);   // Write '1' to AD9833 Control register bit D8.
  delay(10);
}

// Set the frequency and waveform registers in the AD9833.
void AD9833setFrequency(long frequency, int Waveform) {

  long FreqWord = (frequency * pow(2, 28)) / refFreq;

  int MSB = (int)((FreqWord & 0xFFFC000) >> 14);    //Only lower 14 bits are used for data
  int LSB = (int)(FreqWord & 0x3FFF);
 
  //Set control bits 15 ande 14 to 0 and 1, respectively, for frequency register 0
  LSB |= 0x4000;
  MSB |= 0x4000;
 
  WriteRegister(0x2100);   
  WriteRegister(LSB);                  // Write lower 16 bits to AD9833 registers
  WriteRegister(MSB);                  // Write upper 16 bits to AD9833 registers.
  WriteRegister(0xC000);               // Phase register
  WriteRegister(Waveform);             // Exit & Reset to SINE, SQUARE or TRIANGLE
}

void WriteRegister(int dat) {
 
 
  SPI.setDataMode(SPI_MODE2);         
 
  digitalWrite(FSYNC, LOW);           // Set FSYNC low before writing to AD9833 registers
  delayMicroseconds(10);              // Give AD9833 time to get ready to receive data.
 
  SPI.transfer(highByte(dat));        // Each AD9833 register is 32 bits wide and each 16
  SPI.transfer(lowByte(dat));         // bits has to be transferred as 2 x 8-bit bytes.

  digitalWrite(FSYNC, HIGH);          //Write done. Set FSYNC high
}

Go Up