# AD9833 programming in raspberry pi using python

Hello everyone,

I developed a code in python for raspberry pi for program AD9833 IC. Im using SPI protocol and it does not work even Im sending the correct sequence(in my opinion).

Can someone point out what is  im doing wrong in the code.

I made same code work in Arduino(using C).

Python code(the trouble)

import spidev
import time
#preparing SPI
spi=spidev.SpiDev()
spi.open(0,1)
spi.max_speed_hz=976000
#Intialization
freq_out=400
Two28 = 268435456 #this value equal to the 2^28
phase =0

#this method is responsible for sending data through SPI bus
def send_data(input):
tx_msb=input>>8
tx_lsb=input & 0xFF
spi.xfer([tx_msb,tx_lsb])
print(input)

#calculation for the frequency
freq_word=int(round(float(freq_out*Two28)/25000000))

#frequency word divide to two parts as MSB and LSB.
# FFFC ->1111 1111 1111 1100 0000 0000
MSB = (freq_word & 0xFFFC000)>>14
# 3FFF ->0011 1111 1111 1111
LSB = (freq_word & 0x3FFF)

#DB15 and DB14 are set to 0 and 1
LSB |= 0x4000
#DB15 and DB14 are set to 0 and 1
MSB |= 0x4000
#DB15, DB14,DB13 = 110 DB12 =x
#respectively, which is the address for Phase Register 0.
#The remaining 12 bits are the data bits and are all 0s in this case
phase |= 0xC000
#0x2100 - Control Register
send_data(0x2100)
#sending LSB and MSB
send_data(LSB)
send_data(MSB)
#sending phase
send_data(phase)
#0x2000 - Exit Reset
send_data(0x2000)

Arduino code(this is working)

long freq; //32-bit global frequency variable

#include <SPI.h>
#include "TimerOne.h"

// Define the FSYNC (used for SD funtion)
#define FSYNC 2

void setup()
{
Timer1.initialize(1);
Timer1.pwm(9, 512);

pinMode(FSYNC, OUTPUT); //FSYNC

Serial.begin(9600); // start serial communication at 9600bps

digitalWrite(FSYNC, HIGH);

SPI.setDataMode(SPI_MODE2); // requires SPI Mode for AD9837
SPI.begin();

delay(100); //A little set up time, just to make sure everything's stable

//Initial frequency
freq = 500;
}

void loop()
{

}

{
//
int MSB;
int LSB;
int phase = 0;

//We can't just send the actual frequency, we have to calculate the "frequency word".
//This amounts to ((desired frequency)/(reference frequency)) x 0x10000000.
//calculated_freq_word will hold the calculated result.
long calculated_freq_word;

//Once we've got that, we split it up into separate bytes.
MSB = (int)((calculated_freq_word & 0xFFFC000)>>14); //14 bits
LSB = (int)(calculated_freq_word & 0x3FFF);

//Set control bits DB15 ande DB14 to 0 and one, respectively, for frequency register 0
LSB |= 0x4000;
MSB |= 0x4000;

phase |= 0xC000;

//delay(500);

//Set the frequency==========================

//Power it back up

}

//This is the guy that does the actual talking to the AD9837