AD 9832 DDS

General discussion on mikroBasic.
Author
Message
janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: AD 9832 DDS

#31 Post by janni » 18 Feb 2011 02:48

We all miss Warren :( . Especially for the warmth he radiated into this forum.

As for the Print_Dec procedure, it could cause problems if not all variables were fitting into the same RAM bank. The following should be insensitive to variables' location, provided the string parameters are placed in BANK0 or 1. If placed in higher memory banks, one has to set the IRP bit before calling the procedure.

Code: Select all

module Print_Dec_16_Pro
'***********************************************************************************************
'* *
'* PRINT_DEC() by Warren Schroeder - September 23, 2006
'* Compiled with mikroBASIC 5.0.0.1 and tested with 16F876A
'* *
'* PRINT_DEC is a routine that accepts any unsigned number up to 32 bits and converts
'* it to a string of decimal numbers which can also include a user-inserted decimal point.
'* The maximum size of the string is 11 characters: 10 decimal integers + a decimal point.
'* *
'* The result string can be formatted (including an optional decimal point)
'* based on a #'s template and the following justification codes:
'* RJ = Right-Justified with Leading Blank Spaces
'* RJZ = Right-Justified with Leading Zeroes
'* *
'* This is the prototype of the function:
'* Print_Dec(_number as longint,_format as string[11],_justify as byte,_target as string[11])
'* *
'* Examples: *
'* print_dec(123456789, "#####.#####", RJ, mystr)
'* Printed Result in mystr: " 1234.56789" *
'* print_dec(mynumber, "#.######", RJZ, mystr) where mynumber = 64321
'* Printed Result in mystr: "0.064321"
'*
'* 14 Nov 2009
'*   Modified to Compile for the mikroElectronika PRO Basic with the assistance of Durango and Janni
'*   Janni. Compiled with Basic PRO v3.2 and verified on EasyPIC4 board using 16F88.
'***********************************************************************************************

Const RJ as byte = 1
Const RJZ as byte = 2

'*******************************************************************8
' Need to place this here above implements

sub procedure Print_Dec(dim _number as longint,
     dim byref _format as string[11],
     dim _justify as byte,
     dim byref _target as string[11])

'********************************************************************

implements

 sub procedure Print_Dec(dim _number as longint,
    dim byref _format as string[11],
    dim _justify as byte,
    dim byref _target as string[11])

  dim tmp0, tmp1, flen, start, ascii as byte

  dim doff as short            ' ***Dim'ed doff as a short because it is
                               ' given a value of -1 a few lines down. ***

  tmp0 = 0
  flen = 0
  doff = -1

  FSR = byte(@_target)       ' location for final string
  While tmp0 <> 11
        INDF = 0
        inc(tmp0)
        inc(FSR)
  Wend

  FSR = byte(@_format)        ' location of formatting scheme'
  While INDF <> 0
        If INDF = 46 Then doff = flen End If ' get decimal position
        inc(FSR) ' move to next memory position
        inc(flen)
  Wend

  R0 = 32
  asm
     b32:
  end asm
  R1 = 10
  FSR = byte(@_target) + 10
  _number=_number << 1
  asm
     inloop:
         rlf INDF,1
         movlw 246
         addwf INDF,0
         btfsc STATUS,0
         movwf INDF
         decf FSR,F
         decfsz R1,F
         goto inloop
         decfsz R0,F
         goto b32
  end asm
  start = 12 - flen
  tmp1 = 0
  FSR = byte(@_target)
  While tmp1 < flen
        ascii = 48
        If tmp1 = doff Then
           INDF = 46
           dec(start)
        Else
            FSR = FSR + start
            tmp0 = INDF
            If tmp0 = 0 Then
               If _justify = RJ Then
                  If tmp1 < doff Then ascii = 32 End If
               End If
            End If
            FSR = FSR - start
         INDF = tmp0 + ascii
      End If
  inc(FSR)
  inc(tmp1)
  Wend
  INDF = 0
 end sub
end.

oliverb
Posts: 570
Joined: 24 May 2007 15:09

Re: AD 9832 DDS

#32 Post by oliverb » 22 Feb 2011 11:37

Thanks for the help with the print_dec function. I finally got a test jig together today using C-grid connectors for the EasyPIC and DDS-60, and a 5v to 9v converter from an old Ethernet card to power the DDS.

I've moved the communication pins to RC1,RC3,RC5 so hopefully it'll be compatible with hardware SPI. Later I plan on combining it with some other SPI hardware so I need confidence that the interface is standard (-ish). I put 100R resistors in the link to prevent ground noise from the EasyPIC from showing up in the DDS output.

Random thought: From the datasheet it appears I'm supposed to send one bit of data by itself to put the DDS-60 into serial mode as it starts from cold in parallel, does anyone do this or does the first serial write just happen to do this anyway?

Had a worried few minutes of getting rubbish frequencies until I realised the reference frequency wasn't programmed. It also didn't help that early versions of the MB help file have the keypad pinout completely wrong, it seems correct in MBpro.

I've put the output into our spectrum analyser, mostly it looks very clean apart from some odd spur frequencies that I haven't mapped. So far they seem to be down at the -40dB level or better.

I might try and move the set frequency and set clock functions to keypad "A" and "B".

Copy'nPaste
Posts: 573
Joined: 25 Apr 2006 15:39
Location: Cape Town, South Africa

Re: AD 9832 DDS

#33 Post by Copy'nPaste » 22 Feb 2011 12:10

The DDS 60 board hardwires the AD9851 into serial mode.
In my code I use one keypad for both set frequency and set clock, depending on two buttons to select each function.
For some reason, the DDS requires two writes, not sure why.
I have a re-write to Pro somewhere in the future, but a long way off :(
"Copy'nPaste"

oliverb
Posts: 570
Joined: 24 May 2007 15:09

Re: AD 9832 DDS

#34 Post by oliverb » 22 Feb 2011 16:03

Entry to the serial mode, see Figure 17, is via the parallel mode,
which is se lect ed by default after a RESET is asserted. One needs
only to program the fi rst eight bits (word W0) with the se quence
xxxxx011 as shown in Figure 17 to change from par al lel to serial
mode. The W0 programming word may be sent over the 8-bit
data bus or hardwired as shown in Figure 18. After serial mode
is achieved, the user must follow the programming se quence of
Figure 19.
XXXXX011
What the DDS-60 board does is to hardwire the D0-3 pins to generate the "Enter serial mode" command in parallel format the very first time it is written to.

I would speculate that the process of switching from parallel to serial mode is causing it to "eat" the very first serial write attempt. After it is in serial mode it recieves commands normally so the second write succeeds?

oliverb
Posts: 570
Joined: 24 May 2007 15:09

Re: AD 9832 DDS

#35 Post by oliverb » 23 Feb 2011 15:17

Found the reason for the double write: change the SPI part of "ddswrite" to:

Code: Select all

    ClearBit(PORTC,1)                         'Sync low enable write
    byte_s = swap_bits(lo(cfreq))                 ' Swap over and write LSB
    Soft_Spi_Write(byte_s)
    byte_s = swap_bits(hi(cfreq))                 ' Swap over and write next byte
    Soft_Spi_Write(byte_s)
    byte_s = swap_bits(higher(cfreq))             ' Swap over and write next byte
    Soft_Spi_Write(byte_s)
    byte_s = swap_bits(highest(cfreq))            ' Swap over and write next byte
    Soft_Spi_Write(byte_s)
    byte_s = swap_bits($01)                   ' Swap over and write config byte
    Soft_Spi_Write(byte_s)
    SetBit(PORTC,1)                           ' Sync high disable write
What is happening is that the old value of cfreq is passed to the function in parameter "f", then cfreq is recalculated based on the new frequency setting, but the old value gets written. The second time round cfreq has been updated so f contains the right value.

As for the mode switch "ddsclear" takes care of it, though if I understand correctly 8 zero bits would be sufficient provided ddswrite is called immediately after.

Post Reply

Return to “mikroBasic General”