AD9833 calculating tuning hex values/writing to registers

•  By: normnet Today 00:36 Forum: The Lounge Starter: normnet Views: 0 Replies: 2 Snapmaker 3-in-1 3D Printer Good to know. By: Stephen Moss Yesterday 11:49 Forum: The Lounge Starter: Oldhack Views: 0 Replies: 41 Hello Now that it is Snowing outside I have to stay in, I thought ( or not thinking at all) it would bring in a few coins to advertise on the Proton Forum, electronic stuff would be good. I am not sure if this would be viable, I wouldent mind as many forums have sidebar Advertisements, it takes time and Money to keep up the Forum and keep... I think this is the crux of the issue, you may have many registered forum users/unregistered visitors but it is only the "usual crowd" that... a) use it often enough to want to keep it going and b) along with Les are also the primary source of solutions, whereas the occasional user/unregistered visitors only use the forum when they need a...

# Thread: AD9833 calculating tuning hex values/writing to registers – 960 days old

1. 0

## AD9833 calculating tuning hex values/writing to registers

Good Evening all

Im trying to make a simple signal generator using a AD9833 module. I have all the hardware rigged up and it works fine. Im
struggling to calculate the hex serial values for the frequency registers and how they actually work. . The program below produces a 5MHZ output and is fine.

I used the the calculator at http://www.analog.com/en/design-cent...ls/ad9833.html for the hex values below

tuning word = freq x 0.09313. The result is then split up into a upper and lower word etc and written to the frequency registers. Im trying to work out how
5Mhz calcs to FREQ1 = B333 8CCC and FREQ0 = 7333 4CCC - complete output is :- 2100 7333 4CCC B333 8CCC C000 E000 2000 etc

Alan

Start:
MsbFirst_H, [0x2100 \16 ]
MsbFirst_H, [0x7333 \16 ]
MsbFirst_H, [0x4ccc \ 16 ]
MsbFirst_H, [0xb333 \ 16 ]
MsbFirst_H, [0x8ccc \ 16 ]
MsbFirst_H, [0xC000 \ 16 ]
MsbFirst_H, [0x2000 \ 16 ]

2. 0

## Re: AD9833 calculating tuning hex values/writing to registers

From the manual:

Numeric Representations
The compiler recognizes several different numeric representations: -
Binary is prefixed by %. i.e. %01000101
Hexadecimal is prefixed by \$. i.e. \$0A
Character byte is surrounded by quotes. i.e. "a" represents a value of 97
Decimal values need no prefix.
Floating point is created by using a decimal point. i.e. 3.14

Try replacing the 0x2100 for example with \$2100.

Norm

3. 0

## Re: AD9833 calculating tuning hex values/writing to registers

Hi

Thanks for the reply. Im trying to do a manual calculation to create the correct hex values for the AD9833. My values dont correspond to the calculator link ones, for freq above about 1KHZ.
For example at 5MHZ FR0 the serial output should be 2100 7333 4CCC B333 8CCC C000 E000 2000 . Tuning dword = freq x 2^28 / 25 MHz. for example this would be 42949673 which is what I get. The calculation after that seems to go wrong for some reason.

Many Thanks

Alan

4. 0

## Re: AD9833 calculating tuning hex values/writing to registers

Hi

This is how you calculate the frequency register , great little device with a max freq of 12.5 Mhz and able to produce square , sine and a triangle waveform ..

From what you obtained from the calculator:5C29 4A3D is for frequency register 09C29 8A3D is for frequency register 1 Using this formula from AN-1070, For 4MHz output using
25Mhz

Get the first 14 bits for LSB, 01110000101001Then add 01 for D15 and D14 for the address of frequency register 0 0101110000101001 = 0x5C29Then the remaining 14 bits for MSB, 00101000111101Then add 01 for D15 and D14 for the address of frequency register 0 0100101000111101 = 0x4A3D

This is the code :-
T_W = Freq * 10.7374184
FTW = T_W
LSB = FTW & \$3FFF
MSB = FTW >>14 & \$3FFF
LSB.14 = 1 : LSB.15 = 0
MSB.14 = 1 : MSB.15 = 0
FR00 = LSB
FR01 = MSB

Just do the rest ...

Thanks

Alan

5. 0

## Re: AD9833 calculating tuning hex values/writing to registers

G'day Alan,
When you're happy with your finished code how about a WIKI article?
I'm sure it would be useful for others, especially as it's not always easy to find content in the forum.
Best wishes
John

6. 0

## Re: AD9833 calculating tuning hex values/writing to registers

Yes John I will post some code , they are a great little device good for VFO with a Oled display and rotatory encoder. The 34 and 35 device have higher
frequency ratings - PS .. the device data sheets are over complicated and not that clear for a simple interface.

Regards

Alan

7. 0

## Re: AD9833 calculating tuning hex values/writing to registers

A few years too late but thought id repost anyway in case it helps someone one day

• Hello everyone

I thought id post this bit of code i wrote a little while ago, i didnt realsie people where trying to use these devices, Anyway there is a bit of excess code in here but it does work and with a few settings it is easy to select the frequency and shape of the waveform, the device i did this for had two dds ic on it and it is possible to select both chip and both registers. it has also be written entirly in picbasic.

Hope it helps,

One word of warning as several people have found out, they are small, very small :-)

Device = 16F877

XTAL = 20

Symbol DPin PORTC.4 'Data port DDS 1
Symbol CPin = PORTC.3 'Clock port DDS 1
Symbol DDS_CE1 = PORTC.2 'Chip Enble DDS 1
Symbol DDS_CE2 = PORTC.1 'Chip Enble DDS 2

Output DDS_CE1 'Sets Pin to an Output
Output DDS_CE2 'Sets Pin to an Output

Dim Frequency As DWord 'Desired Frequency
Dim DDS_OP As Float 'DDS Output
Dim FTW As DWord 'Frequency Tunning word
Dim FTWLSB As FTW.WORD0 'First word LSB of the Frequency tuning word
Dim FTWMSB As FTW.WORD1 'Second word MSB of the Frequency tuning word
Dim FTWLSB1 As Word '
Dim DDS_Chip As Byte 'Selects DDS chip DDS1 = 1, DDS2 = 2
Dim DDS_Waveform As Byte 'Select Waveforms Type Sine = 1, Triangle = 2, Square = 3 (TTL)
Dim DDS_Control_Register As Word
Dim Freq_Reg As Bit 'Selects Frequency Register REG0 or REG1

Dim var1 As Byte
Dim key As Byte

Include "proton_4.inc"
Cls

'Initialise DDS1
DDS_CE1 = 0 'Activate DDS (Set Chip Enable Low)
SHOut DPin,CPin,5, [\$2100\16] 'Apply Reset
SHOut DPin,CPin,5, [\$2000\16] 'Remove reset
DDS_CE1 = 1 'Disable DDS (Set Chip Enable High)

'Initialise DDS2
DDS_CE2 = 0 'Activate DDS (Set Chip Enable Low)
SHOut DPin,CPin,5, [\$2100\16] 'Apply Reset
SHOut DPin,CPin,5, [\$2000\16] 'Remove reset
DDS_CE2 = 1 'Disable DDS (Set Chip Enable High)

DDS_Control_Register = \$2100
DDS_Chip = 1

loop:
key = ""
var1 = InKey
key = LookUp var1,["1","2","3","A","4","5","6","B","7","8","9","C ","* ","0","#","D"]
Print At 1,1,key,

Select key

Case "1"
Print At 1,6,"Button 1"
Frequency = 1000 'Sets Desired Output Frequency
DDS_Waveform = 1 'Sets waveform type
Freq_Reg = 0 'Point to which register
DDS_Chip = 2 'DDS Chip 1

GoSub FTW_CALC:

Case "2"
Print At 1,6,"Button 2"
Frequency = 2000 'Sets Desired Output Frequency
DDS_Waveform = 1 'Sets waveform type
Freq_Reg = 0 'Point to which register
DDS_Chip = 2 'DDS Chip 1

GoSub FTW_CALC

Case "3"
Print At 1,6,"Button 3"
Frequency = 1000 'Sets Desired Output Frequency
DDS_Waveform = 1 'Sets waveform type
Freq_Reg = 1 'Point to which register
DDS_Chip = 2 'DDS Chip 1

GoSub FTW_CALC

Case "4"
Print At 1,6,"Button 4"
Frequency = 2000 'Sets Desired Output Frequency
DDS_Waveform = 1 'Sets waveform type
Freq_Reg = 1 'Point to which register
DDS_Chip = 2 'DDS Chip 1

GoSub FTW_CALC

Case "5"
Print At 1,6,"Button 5"
Frequency = 1000 'Sets Desired Output Frequency
DDS_Waveform = 3 'Sets waveform type
Freq_Reg = 1 'Point to which register
DDS_Chip = 2 'DDS Chip 2

GoSub FTW_CALC

Case "6"
Print At 1,6,"Button 6"
Frequency = 1000 'Sets Desired Output Frequency
DDS_Waveform = 1 'Sets waveform type
Freq_Reg = 1 'Point to which register
DDS_Chip = 2 'DDS Chip 2

GoSub FTW_CALC

Case "7"
Print At 1,6,"Button 7"
Frequency = 2000 'Sets Desired Output Frequency
DDS_Waveform = 1 'Sets waveform type
Freq_Reg = 1 'Point to which register
DDS_Chip = 2 'DDS Chip 2

GoSub FTW_CALC

Case "8"
Print At 1,6,"Button 8"
Frequency = 2500 'Sets Desired Output Frequency
DDS_Waveform = 2 'Sets waveform type
Freq_Reg = 1 'Point to which register
DDS_Chip = 2 'DDS Chip 2

GoSub FTW_CALC

Case "9"
Print At 1,6,"Button 9"
Case "0"
Print At 1,6,"Button 0"
Case "A"
Print At 1,6,"Button A"
Case "B"
Print At 1,6,"Button B"
Case "C"
Print At 1,6,"Button C"
Case "D"
Print At 1,6,"Button D"
Case "*"
Print At 1,6,"Button *"
Case "#"
Print At 1,6,"Button #"

Case Else
Cls
Print At 2,1, "No Button Press"
RSOut "hello"

EndSelect

GoTo loop

FTW_CALC: 'Calculates Frequency tuning words

DDS_OP = Frequency/0.03725290298 'Equation to produce DDS tuning word Master CLK / 2pwr28

'Extract LSB Tuning Word
FTW = DDS_OP 'DDS_OP (Float) = Freq tuning word (Dword) << bit shift doesn't work with float
FTWLSB1 = FTWLSB 'Extract LSB and put it in LSB1

'Extract MSB tuning word
FTW = FTW << 2 'Shift Bits Left twice

ProgrammeDDS:

Select Freq_Reg

Case 0
FTWLSB1.15 = 0 'Set LSB bit 15 & 14 to 01 (Code to write to DDS Freq Reg 0)
FTWLSB1.14 = 1
FTWMSB.15 = 0 'Set MSB bit 15 & 14 to 01 (Code to write to DDS Freq Reg 0)
FTWMSB.14 = 1
DDS_Control_Register.11 = 0 ' Tells DDS to use Frequency Register 0
Case 1
FTWLSB1.15 = 1 'Set LSB bit 15 & 14 to 10 (Code to write to DDS Freq Reg 1)
FTWLSB1.14 = 0
FTWMSB.15 = 1 'Set MSB bit 15 & 14 to 10 (Code to write to DDS Freq Reg 1)
FTWMSB.14 = 0
DDS_Control_Register.11 = 1 ' Tells DDS to use Frequency Register 1
EndSelect

Select DDS_Waveform 'Sets the control register code to produce coressponding waveforms

Case 1 'Sine
DDS_Control_Register.5 = 0 DS_Control_Register.1 = 0
Case 2 'Triangle
DDS_Control_Register.5 = 0 DS_Control_Register.1 = 1
Case 3 'Square
DDS_Control_Register.5 = 1 DS_Control_Register.1 = 0 DS_Control_Register.3 = 1
EndSelect

Select DDS_Chip

Case 1 'Programmes DDS 1

DDS_CE1 = 0 'Activate DDS (Set Chip Enable Low)
SHOut DPin,CPin,5, [FTWLSB1\16] 'Set Frequency
SHOut DPin,CPin,5, [FTWMSB\16]
DDS_Control_Register.8 = 0 'Removes reset from DDS chip
SHOut DPin,CPin,5, [DDS_Control_Register\16] 'Writes to Control Register removing reset
DDS_CE1 = 1 'Disable DDS (Set Chip Enable High)

Case 2 'Programmes DDS 2

DDS_CE2 = 0 'Activate DDS (Set Chip Enable Low)
SHOut DPin,CPin,5, [FTWLSB1\16] 'Set Frequency
SHOut DPin,CPin,5, [FTWMSB\16]
DDS_Control_Register.8 = 0 'Removes reset from DDS chip
SHOut DPin,CPin,5, [DDS_Control_Register\16] 'Writes to Control Register removing reset
DDS_CE2 = 1 'Disable DDS (Set Chip Enable High)

EndSelect

GoTo loop
End

8. 0

## Re: AD9833 calculating tuning hex values/writing to registers

Hello Anthony,
Would you create a WIKI article please? Things get lost on the forum and the WIKI makes it easy to find in future. I don't want to do it myself as then you wouldn't be able to edit it in future. If you do agree let me know via a post and I'll publish it for you so that others can see it.
Cheers
John

9. 0

## Re: AD9833 calculating tuning hex values/writing to registers

10. There are quite a few pre built modules on Ebay. I use this one. It is about 1.3 inches square and has an on-board crystal and adjustable gain amplifier

There are currently 1 users browsing this thread. (0 members and 1 guests)