The variable one_hz needs to be set up for the master clock used to drive the AD9833. In my project, this is a 24 MHz canned oscillator:
one_hz = (2^28 / DDS clock) = 2^28 / 24 x 10^6
one_hz = 11.18481067
Just insert your DDS master clock frequency into the equation to calculate your value of one_hz.
Load variable freq with the desired frequency and away you go.
This will produce a sinusoidal output.
Very best regards.
Code: Select all
Device = 18F4550
Clock = 48
Config
FOSC = HSPLL_HS
Include "utils.bas"
Const HALFPOWER = 16384
CONST ONE_HZ = 11.18481067
Dim fsync As PORTB.0
Dim sclk As PORTB.1
Dim sdata As PORTB.2
Dim tWord As Float
Dim tuningWord As LongInt
Dim highWord As Word
Dim lowWord As Word
Dim freq As LongInt
Public Sub setup()
ADCON1 = $07 ' PORTE as digital (GLCD)
TRISB = $0000
fsync = 1
sclk = 1
sdata = 0
End Sub
Public Sub calculateTuningWord(ByVal frequency As LongInt)
tWord = frequency * one_hz
tWord = tWord + 0.5
tuningWord = tWord
highWord = tuningWord / HALFPOWER
lowWord = tuningWord And $3FFF
highWord = highWord And $3fff
highWord = highWord Or $4000
lowWord = lowWord Or $4000
End Sub
Public Sub sendData(ByVal dataWord As Word)
Dim bitcount As Byte
fsync = 1 ' make sure chip select is disasserted
sclk = 1 ' set clock line idle
DelayUS(100)
fsync = 0 ' indicate a tuning word is coming
bitcount = 0
While bitcount < 16 ' send out each word MSB first
If (dataword And $8000) = $8000 Then
sdata = 1
Else
sdata = 0
End If
DelayUS(10)
sclk = 0
DelayUS(10)
sclk = 1 ' clock the data bit to the AD9833
dataword = dataword << 1
Inc(bitcount)
Wend
fsync = 1 ' end of data frame
End Sub
main:
setup()
freq = 99000 ' required frequency in Hz from AD9833
calculateTuningWord(freq) ' required high and low words are in highWord and lowWord respectively after
' this call
sendData($2100) ' reset the AD9833
sendData(lowWord) ' LSB's of tuning word sent first
sendData(highWord) ' MSB's of tuning word sent next
sendData($8000) ' FREQ1 low word = 0000
sendData($8000) ' FREQ1 high word = 0000
sendData($C000) ' Phase register 0 = 0000
sendData($E000) ' Phase register 1 = 0000
sendData($2000) ' start the AD9833, sinusoidal output
End.