        list            p=16F84
        #include        <p16F84.inc>

        __CONFIG        _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC



;**************************************************
;
; (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.
;
;**************************************************



;**************************************************
;
;       Schematic / AD9835 version 
;
;**************************************************
;                                                                          
;   16F84             
;   4 MHz                          AD9835
;   +------+                       +-----+
;   |      |----------B0=FSYNC-----|     |---FOUT----
;   |      |  B0-B7   B1=SCLK      |     |
;   |      |----------B2=SDATA-----|     |
;   |      |          B3=FSEL      |     |
;   |      |                       +-----+
;   |      |          
;   |      |  A2    Key                                                    
;   |      |---------/ -                                                   
;   |      |  A3    LED                                                    
;   |      |-------->|--                                               
;   +------+                                                               
;                                                                          


;************************************************** 
;
;       Definitions
;
;**************************************************

#define PAGE0           BCF 3,5
#define PAGE1           BSF 3,5
;#define Debug          1

#define                 FSYNC           PORTB,0
#define                 SCLK            PORTB,1
#define                 SDATA           PORTB,2
#define                 FSEL            PORTB,3

#define                 MARTIN1_VIS     B'00101100'



;**************************************************
;
; frequency word for the AD9835 is calculated as
; follows: (see Analog Devices reference manual)
; 
; FHZ / (XTAL / 2**32)
;
; for example 1000Hz at clock rate of 50Mhz =
;
; 1000 / ( 50000000 / 4294967295 ) = 0x00014F8B
;
;**************************************************



;**************************************************
;
;       Variables
;
;**************************************************

                cblock     0x0C
W_TEMP
STATUS_TEMP

VIS_CODE
VIS_PARITY
TEMP
TEMP1
                endc



;**************************************************
;
;       Module Definitions + Variables
;
;**************************************************

#include        "Tools.h"
#include        "AD9835.h"



;************************************************** 
;
;       M A I N - E N T R Y
;
;**************************************************

                ORG             0x000
                goto            Start



;**************************************************
;
;       Start of main program - Init's
;
;**************************************************

Start

                PAGE1

                ; set PORTA
                ; A0 -
                ; A1 -
                ; A2 - Key
                ; A3 - Led
                ; A4 -
                movlw           B'00000100'
                movwf           TRISA

                ; set PORTB
                ; B0 - FSYNC
                ; B1 - SCLK
                ; B2 - SDATA
                ; B3 - 
                ; B4 - 
                ; B5 - 
                ; B6 - 
                ; B7 - 
                movlw           B'00000000'
                movwf           TRISB

                PAGE0

                clrf            PORTA
                clrf            PORTB

                clrf            DDS_H
                clrf            DDS_L
                clrf            DDS_STATUS

                bsf             FSYNC
                bcf             SCLK
                bcf             SDATA

                callEX          Init9835
                callEX          SelectByPin

                     

;**************************************************
;
;       Main Loop
;
;**************************************************

NCOLoop 
                ; is key pressed
                btfss           PORTA,2                 ; 1 / 2
                goto            NCOLoop
ProcessCmd
                ; key debounce 5ms
                movlw           0x00
                movwf           DELAY_H
                movlw           0x05
                movwf           DELAY_L
                callEX          Delay
                btfss           PORTA,2
                goto            NCOLoop
KeySettle
                btfsc           PORTA,2
                goto            KeySettle

                ; toggle led at A3
                movlw           B'00001000'
                xorwf           PORTA,f

                callEX          Init9835
                callEX          Enable9835
                
                movlw           MARTIN1_VIS
                movwf           VIS_CODE
                callEX          Send_SSTV_VIS

                call            Send_SSTV_Color_Bar
                
                callEX          Disable9835

                goto            NCOLoop


Send_SSTV_Color_Bar

;----- Send SSTV Color TestBar ------------------------------

                movlw           0xFF
                movwf           TEMP
CB_Loop
                callEX          Send_SSTV_SYNC

;----- GREEN -----

                movlw           0x01
                movwf           TEMP1
G_LOOP

                ; set & select 2300 Hz  (FREQ1)
                movlw           0x00
                movwf           FREQ_H
                movlw           0x03
                movwf           FREQ_M
                movlw           0x03
                movwf           FREQ_M1
                movlw           0xC0
                movwf           FREQ_L
                callEX          SetFreqEX

                ; ensure that FREQ1 = 2300
                btfss           DDS_STATUS,CURRENT_FREQ
                goto            G_LOOP

                ; wait 72ms
                DelayCPU        0x00, 0x48

                ; set & select 1500 Hz  (FREQ0)
                movlw           0x00
                movwf           FREQ_H
                movlw           0x01
                movwf           FREQ_M
                movlw           0xF7
                movwf           FREQ_M1
                movlw           0x51
                movwf           FREQ_L
                callEX          SetFreqEX

                ; wait 72ms
                DelayCPU        0x00, 0x48

                decfsz          TEMP1,f
                goto            G_LOOP

                ; KORREKTUR
                ;DelayCPU       0x00, 0x02

;----- BLUE -----

                movlw           0x04
                movwf           TEMP1
B_LOOP

                ; select 2300 Hz  (FREQ1)
                callEX          SelectFreq1

                ; wait 18ms
                DelayCPU        0x00, 0x12

                ; select 1500 Hz  (FREQ0)
                callEX          SelectFreq0

                ; wait 18ms
                DelayCPU        0x00, 0x12

                decfsz          TEMP1,f
                goto            B_LOOP

                ; KORREKTUR
                DelayCPU        0x00, 0x02

;----- RED -----

                movlw           0x02
                movwf           TEMP1
R_LOOP

                ; select 2300 Hz  (FREQ1)
                callEX          SelectFreq1

                ; wait 36ms
                DelayCPU        0x00, 0x24

                ; select 1500 Hz  (FREQ0)
                callEX          SelectFreq0

                ; wait 36ms
                DelayCPU        0x00, 0x24

                decfsz          TEMP1,f
                goto            R_LOOP

                ; KORREKTUR
                DelayCPU        0x00, 0x01

                
                decfsz          TEMP,f
                goto            CB_Loop
                
                return



;************************************************** 
;
;       SSTV Routines
;
;**************************************************

                ORG             0x200              
Send_SSTV_SYNC

;----- Send SSTV Sync ------------------------------

                ; set & select 1200 Hz
                movlw           0x00
                movwf           FREQ_H
                movlw           0x01
                movwf           FREQ_M
                movlw           0x92
                movwf           FREQ_M1
                movlw           0xA7
                movwf           FREQ_L
                callEX          SetFreqEX

                ; wait 5ms
                DelayCPU        0x00, 0x05

                return

Send_SSTV_VIS

;----- Send VIS Header ------------------------------

                ; set & select 1900 Hz
                movlw           0x00                                ;; 1
                movwf           FREQ_H                              ;; 1
                movlw           0x02                                ;; 1
                movwf           FREQ_M                              ;; 1
                movlw           0x7D                                ;; 1
                movwf           FREQ_M1                             ;; 1
                movlw           0x88                                ;; 1
                movwf           FREQ_L                              ;; 1
                callEX          SetFreqEX                           ;; 6+824
                
                ; wait 300ms
                DelayCPU            0x01, 0x2C
                
                ; set & select 1200 Hz
                movlw           0x00                                ;; 1
                movwf           FREQ_H                              ;; 1
                movlw           0x01                                ;; 1
                movwf           FREQ_M                              ;; 1
                movlw           0x92                                ;; 1
                movwf           FREQ_M1                             ;; 1
                movlw           0xA7                                ;; 1
                movwf           FREQ_L                              ;; 1
                callEX          SetFreqEX                           ;; 6+824

                ; wait 10ms
                DelayCPU        0x00, 0x0A

                ; select 1900 Hz
                callEX          ToggleFreq                          ;; 6+7

                ; wait 300ms
                DelayCPU        0x01, 0x2C

                ; select 1200 Hz
                callEX          ToggleFreq                          ;; 6+7

                ; wait 30ms
                DelayCPU        0x00, 0x1E

;----- Send VIS Data Byte ------------------------------

                clrf            VIS_PARITY
                bsf             DDS_STATUS,TFLAG1
                movlw           .7                                  ;; 1
                movwf           TEMP                                ;; 1
VIS_BIT_Loop
                rrf             VIS_CODE,f                          ;; 1
                btfss           STATUS,C                            ;; 1/2
                goto            VIS_BIT_Low
VIS_BIT_High
                incf            VIS_PARITY,f
                
                ; set & select 1100 Hz
                movlw           0x00                                ;; 1
                movwf           FREQ_H                              ;; 1
                movlw           0x01                                ;; 1
                movwf           FREQ_M                              ;; 1
                movlw           0x71                                ;; 1
                movwf           FREQ_M1                             ;; 1
                movlw           0x19                                ;; 1
                movwf           FREQ_L                              ;; 1
                callEX          SetFreqEX                           ;; 6+816
                goto            VIS_BIT_Wait
VIS_BIT_Low
                ; set & select 1300 Hz
                movlw           0x00                                ;; 1
                movwf           FREQ_H                              ;; 1
                movlw           0x01                                ;; 1
                movwf           FREQ_M                              ;; 1
                movlw           0xB4                                ;; 1
                movwf           FREQ_M1                             ;; 1
                movlw           0x35                                ;; 1
                movwf           FREQ_L                              ;; 1
                callEX          SetFreqEX                           ;; 6+816            
VIS_BIT_Wait
                ; wait 30ms
                DelayCPU        0x00, 0x1E

                decfsz          TEMP,f                              ;; 1/2
                goto            VIS_BIT_Loop                        ;; 2

;----- Send VIS Parity Bit (even) ------------------------------

                btfss           DDS_STATUS,TFLAG1
                goto            VIS_Trailer
VIS_Parity_Bit
                bcf             DDS_STATUS,TFLAG1
                incf            TEMP,f
                btfss           VIS_PARITY,0    ; if number of 1 bits in VIS
                goto            VIS_BIT_Low     ; byte is even = parity low
                btfsc           VIS_PARITY,0    ; if not even
                goto            VIS_BIT_High    ; = parity bit high

;----- Send VIS Trailer ------------------------------

VIS_Trailer
                ; set & select 1200 Hz
                movlw           0x00
                movwf           FREQ_H
                movlw           0x01
                movwf           FREQ_M
                movlw           0x92
                movwf           FREQ_M1
                movlw           0xA7
                movwf           FREQ_L
                callEX          SetFreqEX

                ; wait 30ms
                DelayCPU        0x00, 0x1E

                return



;**************************************************
;
;       Tool routines
;
;**************************************************

                ORG             0x300              
#include        "Tools.asm"
#include        "AD9835.asm"

        end



