    ifndef _ad9835_asm
        #define _ad9835_asm

; define the following in the main program
; for AD9835 SELECT, CLOCK and DATA pins
;
; an example:
;
;#define                    FSYNC           PORTB,0
;#define                    SCLK            PORTB,1
;#define                    SDATA           PORTB,2
;#define                    FSEL            PORTB,3



;**************************************************
;
; (C) Copyright notice
;
; Written by Richard Prinz.
; Copyright 2003 by Richard Prinz.
; OE1RIB Richard.Prinz@MIN.at
;
; Permission to use, copy, and modify this program
; without restriction is hereby granted, as long as
;
; 1) this copyright notice appears in each copy of
;    the program source code and
;
; 2) the source code is not used for commercial
;    purposes.
;
; This program is freely distributable without
; licensing fees and is provided without guarantee
; or warrantee expressed or implied.
;
; This program is -not- in the public domain.
;
;**************************************************



;**************************************************
;
;       Analog AD9835 DDS Routines
;
;**************************************************

Init9835
                call            Disable9835                         ;; 2
                call            SelectByReg                         ;; 2

                ; select PHASE0
                call            SelectPhase0                        ;; 2

                ; clear PHASE0
                clrf            PHASE_H                             ;; 2
                clrf            PHASE_L                             ;; 2
                movlw           PHASE0                              ;; 1
                call            SetPhase                            ;; 2

                return                                              ;; 2                
;----------------------------------------------------------------------------

Disable9835                                                         ;; 203
                ; sleep, reset, clear
                bcf             DDS_STATUS,ENABLED                  ;; 1
                movlw           B'11111000'                         ;; 1
                goto            SelectWrite                         ;; 2+199
;----------------------------------------------------------------------------

Enable9835                                                          ;; 203
                ; enable chip - no sleep, no reset, no clear
                bsf             DDS_STATUS,ENABLED                  ;; 1
                movlw           B'11000000'                         ;; 1
                goto            SelectWrite                         ;; 2+199
;----------------------------------------------------------------------------

SelectByPin                                                         ;; 203
                ; sync, selsrc (freq & phase) = by register PIN = 0
                movlw           B'10100000'                         ;; 1
                bcf             DDS_STATUS,SELECT_MODE              ;; 1
                goto            SelectWrite                         ;; 2+199
;----------------------------------------------------------------------------

SelectByReg                                                         ;; 203
                ; sync, selsrc (freq & phase) = by register REG = 1
                movlw           B'10110000'                         ;; 1
                bsf             DDS_STATUS,SELECT_MODE              ;; 1
                goto            SelectWrite                         ;; 2+199
;----------------------------------------------------------------------------
                                                                    ;; REG / PIN
ToggleFreq                                                          ;; 208 /  10
                btfss           DDS_STATUS,CURRENT_FREQ             ;; 1/2
                goto            SelectFreq1                         ;; 2
                nop                                                 ;; 1
                                                                    ;; always 3
;----------------------------------------------------------------------------
                                                                    ;; REG / PIN
SelectFreq0                                                         ;; 205 /   7
                ;bcf PORTA,1        ; JUST FOR DEBUGGING
                
                bcf             DDS_STATUS,CURRENT_FREQ             ;; 1
                btfss           DDS_STATUS,SELECT_MODE              ;; 1/2
                goto            SelectFreq0Pin                      ;; 2+3
                movlw           B'01010000'                         ;; 1
                goto            SelectWrite                         ;; 2+199
;----------------------------------------------------------------------------

SelectFreq0Pin                                                      ;;   3
                bcf             FSEL                                ;; 1
                return                                              ;; 2
;----------------------------------------------------------------------------
                                                                    ;; REG / PIN
SelectFreq1                                                         ;; 205 /   7
                ;bsf PORTA,1        ; JUST FOR DEBUGGING
                
                bsf             DDS_STATUS,CURRENT_FREQ             ;; 1
                btfss           DDS_STATUS,SELECT_MODE              ;; 1/2
                goto            SelectFreq1Pin                      ;; 2+3
                movlw           B'01011000'                         ;; 1
                goto            SelectWrite                         ;; 2+199
;----------------------------------------------------------------------------

SelectFreq1Pin                                                      ;;   3
                bsf             FSEL                                ;; 1
                return                                              ;; 2
;----------------------------------------------------------------------------

SelectPhase0                                                        ;; 202
                movlw           B'01000000'                         ;; 1
                goto            SelectWrite                         ;; 2+199
;----------------------------------------------------------------------------

SelectPhase1                                                        ;; 202
                movlw           B'01000010'                         ;; 1
                goto            SelectWrite                         ;; 2+199
;----------------------------------------------------------------------------

SelectPhase2                                                        ;; 202
                movlw           B'01000100'                         ;; 1
                goto            SelectWrite                         ;; 2+199
;----------------------------------------------------------------------------

SelectPhase3                                                        ;; 202
                movlw           B'01000110'                         ;; 1
                goto            SelectWrite                         ;; 2+199
;----------------------------------------------------------------------------

SetPhase                                                            ;; 408
                movwf           PHASE_TEMP                          ;; 1
                iorlw           B'00011000'                         ;; 1
                movwf           DDS_H                               ;; 1
                movf            PHASE_L,w                           ;; 1
                movwf           DDS_L                               ;; 1
                call            Write9835       ; 8 LSB             ;; 2+197

                movf            PHASE_TEMP,w                        ;; 1
                iorlw           B'00001001'                         ;; 1
                movwf           DDS_H                               ;; 1
                movf            PHASE_H,w                           ;; 1
                movwf           DDS_L                               ;; 1
                goto            Write9835       ; 8 MSB             ;; 2+197
;----------------------------------------------------------------------------
;
;   Update DDS chip with a new frequency word
;
;   Note! Since there is no way to update all 32 bits simultaneously
;   in the AD9832, we have to toggle between the two frequency
;   registers. Example: Load reg0, switch to reg0, load reg1, switch
;   to reg1 etc. etc. This is done to prevent glitches occurring when
;   two or more bytes change value at the same time.
;   (See AD9832 data sheet...)
;
;----------------------------------------------------------------------------

SetFreqEX                                                           ;; 824
                btfss           DDS_STATUS,CURRENT_FREQ             ;; 1/2
                goto            SetFreqEX1                          ;; 2
                nop                                                 ;; 1
                                                                    ;; always 3

SetFreqEX0                                                          ;; 821
                movlw           FREQ0                               ;; 1
                call            SetFreq                             ;; 2+816
                goto            SelectFreq0                         ;; 2

SetFreqEX1                                                          ;; 821
                movlw           FREQ1                               ;; 1
                call            SetFreq                             ;; 2+816
                goto            SelectFreq1                         ;; 2
;----------------------------------------------------------------------------

SetFreq                                                             ;; 816
                movwf           FREQ_TEMP                           ;; 1
                iorlw           B'00110000'     ; 8L LSB            ;; 1
                movwf           DDS_H                               ;; 1
                movf            FREQ_L,w                            ;; 1
                movwf           DDS_L                               ;; 1
                call            Write9835                           ;; 2+197

                movf            FREQ_TEMP,w                         ;; 1
                iorlw           B'00100001'     ; 8H LSB            ;; 1
                movwf           DDS_H                               ;; 1
                movf            FREQ_M1,w                           ;; 1
                movwf           DDS_L                               ;; 1
                call            Write9835                           ;; 2+197

                movf            FREQ_TEMP,w                         ;; 1
                iorlw           B'00110010'     ; 8L MSB            ;; 1
                movwf           DDS_H                               ;; 1
                movf            FREQ_M,w                            ;; 1
                movwf           DDS_L                               ;; 1
                call            Write9835                           ;; 2+197

                movf            FREQ_TEMP,w                         ;; 1
                iorlw           B'00100011'     ; 8H MSB            ;; 1
                movwf           DDS_H                               ;; 1
                movf            FREQ_H,w                            ;; 1
                movwf           DDS_L                               ;; 1
                goto            Write9835                           ;; 2+197
;----------------------------------------------------------------------------

SelectWrite                                                         ;; 199
                movwf           DDS_H                               ;; 1
                clrf            DDS_L                               ;; 1

Write9835                                                           ;; 197
                bcf             FSYNC                               ;; 1
                movlw           .16                                 ;; 1
                movwf           DDS_BIT_TEMP                        ;; 1

Write9835Loop:                                                      ;; 194
                btfss           DDS_H,7                             ;; 1/2
                bcf             SDATA                               ;; 1
                btfsc           DDS_H,7                             ;; 1/2
                bsf             SDATA                               ;; 1
                                                                    ;; always 5

                bsf             SCLK                                ;; 1
                bcf             SCLK                                ;; 1
                rlf             DDS_L,f                             ;; 1
                rlf             DDS_H,f                             ;; 1
                decfsz          DDS_BIT_TEMP,f                      ;; 1/2
                goto            Write9835Loop                       ;; 2

                bsf             FSYNC                               ;; 1
                return                                              ;; 2
;----------------------------------------------------------------------------

    endif

