This problem can be split into a number of sub tasks.
- Values to send
- Sequence values sent in
- How values sent over SPI
As SamMaster pointed out there is an application note from Analog Devices that shows the sequence of values to send to set the frequency to 400 Hz
https://www.analog.com/media/en/technical-documentation/application-notes/AN-1070.pdf
They summarise the five values to send and in what order in the following table:

If I look at the code that SamMaster has written it is writing the correct values in the correct order (I don't have the hardware but I can print the values).
sending: [0x21, 0x00]
sending: [0x50, 0xc7]
sending: [0x40, 0x00]
sending: [0xc0, 0x00]
sending: [0x20, 0x00]
That just leaves bullet 3 that is causing the problems.
The fact that you get changes happening on the hardware suggests that some kind of communication is happening, just not the correct values.
Looking at the limited documentation at https://pypi.org/project/spidev/ there are two likely commands that could be used: xfer
or xfer2
.
The difference between the two are the value of the chip select pin between blocks.
Figure 4 in the data sheet I think is saying that chip select should not be released between the two bytes.
https://www.analog.com/media/en/technical-documentation/data-sheets/ad9833.pdf

That would suggest that xfer2
should be to used to send the blocks and not xfer
as SamMaster has done. Although SamMaster seems to suggest he got it working with xfer
and you were able to set the value to 400 Hz successfully. You would need your scope/logic analyser to see if the GPIO is doing the right thing on the hardware.
At some point in your development you seem to have changed the sequence of values to send. It should be:
send_data(0x2100) # Start
send_data(LSB) # Frequency 14 bits (LSB)
send_data(MSB) # Frequency 14 bits (MSB)
send_data(phase) # Phase value
send_data(0x2000) # End
This could be another source of your error.
I looked at what the values should be that get sent for the different frequency you tested. I calculated the values follows:
For Frequency: 400
Frequency setting: 4295 = 0x10c7 = 0001000011000111
send_data(0x2100)
send_data(0x50c7)
send_data(0x4000)
send_data(0xc000)
send_data(0x2000)
For Frequency: 500
Frequency setting: 5369 = 0x14f9 = 0001010011111001
send_data(0x2100)
send_data(0x54f9)
send_data(0x4000)
send_data(0xc000)
send_data(0x2000)
For Frequency: 600
Frequency setting: 6442 = 0x192a = 0001100100101010
send_data(0x2100)
send_data(0x592a)
send_data(0x4000)
send_data(0xc000)
send_data(0x2000)
For Frequency: 1000
Frequency setting: 10737 = 0x29f1 = 0010100111110001
send_data(0x2100)
send_data(0x69f1)
send_data(0x4000)
send_data(0xc000)
send_data(0x2000)
And finally, I refactored the code to make it easier for me to test the different parts of the code. I'm sharing it here for your information. I had to comment out any of the spi communication parts because I don't have the hardware.
import time
import spidev
# activate spidev module and settings SPI
bus = 0
device = 1
spi = spidev.SpiDev()
spi.open(bus, device)
spi.max_speed_hz = 976000
def output_freq(hz_value):
return int(round((hz_value * 2**28) / 25e6))
def freq_change_start():
ctrl_reg = 0
ctrl_reg += 2**13 # set DB13 (28 bit freq reg)
ctrl_reg += 2**8 # set DB8 (Reset)
return ctrl_reg
def freq_reg_lsb(freq_reg):
fourteen_bit_mask = 0b0011111111111111
write_value = 0
write_value += 2**14 # set DB14
lsb = freq_reg & fourteen_bit_mask
write_value += lsb
return write_value
def freq_reg_msb(freq_reg):
fourteen_bit_mask = 0b0011111111111111
write_value = 0
write_value += 2**14 # set DB14
msb = freq_reg >> 14 & fourteen_bit_mask
write_value += msb
return write_value
def phase_register():
# Currently always the same value
write_value = 0
# Set phase register address
write_value += 2 ** 15 # set DB15
write_value += 2 ** 14 # set DB14
return write_value
def freq_change_end():
ctrl_reg = 0
ctrl_reg += 2**13 # set DB13 (28 bit freq reg)
return ctrl_reg
def word_split(word16):
tx_msb = word16 >> 8
tx_lsb = word16 & 0xFF
return tx_msb, tx_lsb
def send_spi_sequence(sequence):
for word16 in sequence:
two_bytes = word_split(word16)
print(f"\tsending:[{two_bytes[0]:#02x}, {two_bytes[1]:#02x}]")
print(f"\tsend_data({word16:#06x})")
spi.xfer(two_bytes)
# spi.xfer2(two_bytes)
def change_freq(freq_hz):
# Calc values to send
print("For Frequency:", freq_hz)
freq_reg = output_freq(freq_hz)
print(f"Frequency setting: {freq_reg} = {freq_reg:#04x} = {freq_reg:016b}")
ctrl_start = freq_change_start()
print(f"Control register write: {ctrl_start:#04x}")
lsb_value = freq_reg_lsb(freq_reg)
print(f"lsb value: {lsb_value:#04x}")
msb_value = freq_reg_msb(freq_reg)
print(f"lsb value: {msb_value:#04x}")
phase_reg = phase_register()
print(f"Phase register write: {phase_reg:#04x}")
ctrl_end = freq_change_end()
print(f"Control register write: {ctrl_end:#04x}")
# Write values to spi
send_spi_sequence([ctrl_start, lsb_value, msb_value, phase_reg, ctrl_end])
def main():
show_freq_for = 20
change_freq(400)
time.sleep(show_freq_for)
change_freq(500)
time.sleep(show_freq_for)
change_freq(600)
time.sleep(show_freq_for)
change_freq(1000)
time.sleep(show_freq_for)
if __name__ == '__main__':
main()