; QEX_FILE_2   (AD9850_7045_KHz)  (OPEN WITH NOTEPAD++ which has line numbers)
; This program is for a Texas Instruments MSP430G2553 microcontroller and drives an
; AD9850 Signal Generator module to produce a fixed frequency of 7.045 MHz
; 
; James L. Kretzschmar AE7AX
; Cheyenne, WY
; 10 February 2017
;--------------------------------INSTRUCTIONS--------------------------------------
; Following the instructions in STEP #5 of the "Guide to Using Texas Instruments
; Code Composer" copy this entire program and then paste into the
; "Empty Assembly-only Project" framework immediately following the lines:
;                         ;---------------------
;                         ; Main loop here
;                         ;---------------------
;-------------------------------INITIALIZATION-------------------------------------
;----------------------(Defines Microcontroller Pins to be used)-------------------
    mov.b   #(BIT1|BIT3|BIT5),&P1DIR ; P1.1, P1.3, P1.5 pins as outputs.
	                             ; P1.1 = Connect to DATA pin on AD9850 module
                                 ; P1.3 = Connect to FQ_UD pin on AD9850 module
                                 ; P1.5 = Connect to W_CLK pin on AD9850 module							 
;-----------------(Load Serial-Load Enable Word into Registers)--------------------
    mov     #0, R6        ; Moves "0" into Register 6
	                      ; BITs (0-15) of the 40-BIT Serial-Load Enable Word
    mov     #0, R7        ; Moves "0" into Register 7
	                      ; BITs (16-31) of the 40-BIT Serial-Load Enable Word           
    mov     #0, R8        ; Moves "0" into Register 8
	                      ; BITs (32-39) of the 40-BIT Serial-Load Enable Word
;--------------------(Load Calculated Tuning Word into Registers)-------------------
;                         (These numbers are for 7.045 MHz)  
    mov     #39908, R10   ; Moves "39908" into Register 10
	                      ; BITs (0-15) of the 40-BIT Tuning Word
    mov     #3693, R11    ; Moves "3693" into Register 11
	                      ; BITs (16-31) of the 40-BIT Tuning Word
    mov     #0, R12       ; Moves "0" into Register 12
	                      ; BITs (32-39) of the 40-BIT Tuning Word
;----------------------------------------------------------------------------------	   
;----------------DEVELOPMENT OF THE 40-BIT SERIAL-LOAD ENABLE WORD-----------------
;---------------(Creates initial W_CLK pulse and intial FQ_UD pulse)---------------	
    bis.b   #BIT5, &P1OUT          ; Clock Pulse "ON"
    nop                            ; Delay
    bic.b   #BIT5, &P1OUT          ; Clock Pulse "OFF"	
    nop                            ; Delay
    nop                            ; Delay
    nop                            ; Delay
    bis.b   #BIT3, &P1OUT          ; FREQ UPDATE "ON"
    nop                            ; Delay
    nop                            ; Delay
    nop                            ; Delay
    bic.b   #BIT3, &P1OUT          ; FREQ UPDATE "OFF"
;-------------(Reads Registers 6, 7, and 8 and creates "1"s and "0"s)-------------
    mov     #0, R5                 ; Zero out Counter    		
READ_R6:
    cmp     #16, R5                ; R5 = the Counter
    jeq     GOTO_R7                ; If Counter = 16 GOTO_R7               
    rrc	    R6                     ; Roll Right thru the "Carry BIT"                    
    jc      MAKE_ONE_R6            ; "Carry BIT" is set (holds a "1") jump 
    jmp     MAKE_ZERO_R6           ; "Carry BIT" is not set (holds a "0") jump   
MAKE_ZERO_R6:  
    bic.b   #BIT1, &P1OUT          ; Put a "0" on DATA line
    bis.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "ON"
    bic.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "OFF"
    bic.b   #BIT1, &P1OUT          ; "0" still on DATA line
    add     #1, R5                 ; Add one to the Counter                 
    jmp     READ_R6                ; Go read another BIT from R6
MAKE_ONE_R6:
    bis.b   #BIT1, &P1OUT          ; Put a "1" on DATA line
    bis.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "ON" 
    bic.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "OFF"
    bic.b   #BIT1, &P1OUT          ; Take "1" off DATA line
    add     #1, R5                 ; Add one to the Counter
    jmp     READ_R6                ; Go read another BIT from R6 
GOTO_R7:
    mov     #0, R5                 ; Zero out Counter
READ_R7:  
    cmp     #16, R5                ; R5 = the Counter                
    jeq     GOTO_R8                ; If Counter = 16 GOTO_R8                                
    rrc	    R7                     ; Roll Right thru the "Carry BIT"                                       
    jc      MAKE_ONE_R7            ; If "Carry BIT" holds a "1" jump           
    jmp     MAKE_ZERO_R7           ; If "Carry BIT" holds a "0" jump                    
MAKE_ZERO_R7:
    bic.b   #BIT1, &P1OUT          ; Put a "0" on DATA line          
    bis.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "ON"
    bic.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "OFF"        
    bic.b   #BIT1, &P1OUT          ; "0" still on DATA line
    add     #1, R5                 ; Add one to the Counter                                  
    jmp     READ_R7                ; Go read another BIT from R7               	
MAKE_ONE_R7:
    bis.b   #BIT1, &P1OUT          ; Put a "1" on DATA line          
    bis.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "ON"
    bic.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "OFF"
    bic.b   #BIT1, &P1OUT          ; Take "1" off DATA line
    add     #1, R5                 ; Add one to the Counter
    jmp     READ_R7                ; Go read another BIT from R7
GOTO_R8:
    mov     #0, R5                 ; Zero out Counter
READ_R8:                 
    rrc	    R8                     ; Roll Right thru the "Carry BIT"                                        
    jc      MAKE_ONE_R8            ; If "Carry BIT" holds a "1" jump           
    jmp     MAKE_ZERO_R8           ; If "Carry BIT" holds a "0" jump                    
MAKE_ZERO_R8:
    bic.b   #BIT1, &P1OUT          ; Put a "0" on DATA line          
    bis.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "ON"
    bic.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "OFF"        
    add     #1, R5                 ; Add one to the Counter                 
    cmp     #8, R5                 ; Have 8 BITs been read yet?                       
    jeq     SET_CONTROL_BITS       ; If "YES" goto
    bic.b   #BIT1, &P1OUT          ; "0" still on DATA line	
    jmp     READ_R8                ; Go read another BIT from R8               
MAKE_ONE_R8:
    bis.b   #BIT1, &P1OUT          ; Put a "1" on DATA line
    bis.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "ON"
    bic.b   #BIT5, &P1OUT          ; WORD CLOCK Pulse "OFF"
    add     #1, R5                 ; Add one to the Counter
    cmp     #8, R5                 ; Have 8 BITs been read yet?
    jeq     SET_CONTROL_BITS       ; If "YES" goto
    bic.b   #BIT1, &P1OUT          ; Take "1" off DATA line
    jmp     READ_R8                ; Go read another BIT from R8
SET_CONTROL_BITS:
    bis.b   #BIT3, &P1OUT          ; Reset Control Word (FREQ_UPDATE "ON")
    bic.b   #BIT1, &P1OUT          ; Remove "1" (or "0") from DATA line 
    bic.b   #BIT3, &P1OUT          ; Reset Control Word (FREQ_UPDATE "OFF")								
;--------------DEVELOPMENT OF THE 40-BIT TUNING WORD-------------------------
;---------(Reads registers 10, 11, and 12 and creates "1"s and "0"s)---------
    mov     #0, R5                 ; Reset the Counter
READ_R10:              
    rrc	    R10                    
    jc      MAKE_ONE_R10           
    jmp     MAKE_ZERO_R10             
MAKE_ZERO_R10:  
    bic.b   #BIT1, &P1OUT          
    bis.b   #BIT5, &P1OUT
    bic.b   #BIT5, &P1OUT
    bic.b   #BIT1, &P1OUT
    add     #1, R5             	   
    cmp     #16, R5
    jeq     GOTO_R11	
    jmp     READ_R10                
MAKE_ONE_R10:
    bis.b   #BIT1, &P1OUT
    bis.b   #BIT5, &P1OUT
    bic.b   #BIT5, &P1OUT
    bic.b   #BIT1, &P1OUT
    add     #1, R5
    cmp     #16, R5
    jeq     GOTO_R11                 
    jmp     READ_R10
GOTO_R11:
    mov     #0, R5                 
READ_R11:                 
    rrc	    R11                    	
    jc      MAKE_ONE_R11        
    jmp     MAKE_ZERO_R11         
MAKE_ZERO_R11:
    bic.b   #BIT1, &P1OUT          
    bis.b   #BIT5, &P1OUT
    bic.b   #BIT5, &P1OUT          
    bic.b   #BIT1, &P1OUT
    add     #1, R5                 
    cmp     #16, R5
    jeq     GOTO_R12	
    jmp     READ_R11            
MAKE_ONE_R11:
    bis.b   #BIT1, &P1OUT
    bis.b   #BIT5, &P1OUT
    bic.b   #BIT5, &P1OUT
    bic.b   #BIT1, &P1OUT
    add     #1, R5
    cmp     #16, R5
    jeq     GOTO_R12
    jmp     READ_R11
GOTO_R12:
    mov     #0, R5
READ_R12:                 
    rrc	    R12                                       
    jc      MAKE_ONE_R12        
    jmp     MAKE_ZERO_R12        
MAKE_ZERO_R12:
    bic.b   #BIT1, &P1OUT          
    bis.b   #BIT5, &P1OUT
    bic.b   #BIT5, &P1OUT                         
    add     #1, R5	                
    cmp     #8, R5
    jeq     FREQ_UPDATE
    bic.b   #BIT1, &P1OUT
    jmp     READ_R12             
MAKE_ONE_R12:
    bis.b   #BIT1, &P1OUT
    bis.b   #BIT5, &P1OUT
    bic.b   #BIT5, &P1OUT          
    add     #1, R5
    cmp     #8, R5
    jeq     FREQ_UPDATE
    bic.b   #BIT1, &P1OUT
    jmp     READ_R12
FREQ_UPDATE:
    bis.b   #BIT3, &P1OUT          ; FREQ_UPDATE Pulse "ON"
    bic.b   #BIT1, &P1OUT          ; DATA BIT 39 turned "OFF", see Figure 24 datasheet
    bic.b   #BIT3, &P1OUT          ; FREQ_UPDATE Pulse "OFF"
END:
    jmp     END