;**************************************************
;
; (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.
;
;**************************************************


;**************************************************
;
; FT-817 QuickTune Version 1.5 / 11.11.2003
;
; Allows tuning control of an automatic tuner via
; the FT-817.
;
; * add TX power reduction & config section
;
;**************************************************

        ifdef __16C84
#define F84
        endif

        ifdef __16F84
#define F84
        endif

        ifdef __16F84A
#define F84
        endif



        ifdef F84
                list            p=16F84A
                #include        <P16F84A.inc>
                __CONFIG        _CP_OFF & _WDT_ON & _PWRTE_ON & _XT_OSC
        else
                list            p=12CE519
                #include        <p12CE519.inc>
                __CONFIG        _CP_OFF & _WDT_ON & _IntRC_OSC & _MCLRE_OFF
        endif
    
                __IDLOCS        H'CAFE'     ;   :-)


;************************************************** 
;
;       Definitions
;
;**************************************************

        ifdef F84
#define         PAGE0           BCF 3,5
#define         PAGE1           BSF 3,5
#define         LED             PORTA,0
#define         KEY             PORTB,0
#define         RS232in         PORTB,6
#define         RS232out        PORTB,7
        else
#define         LED             GPIO,0
#define         RS232out        GPIO,1
#define         RS232in         GPIO,2
#define         KEY             GPIO,3
        endif


;**************************************************
;
;       Variables
;
;**************************************************

                cblock          0x0C
DELAY_L
DELAY_H
RS232_L
RS232_Byte
Mode
EEHigh
EELow
EETemp
                endc
DELAY_T         equ             RS232_L

;==================================================
;
;       *** CHANGE CONFIG SETTINGS HERE ***

; change the speed parameter only if your QuickTune
; dows not work with your FT-817 at 9600 Baud
SPEED           equ             .200


; MODE defines to which mode the FT-817 should be
; be set to produce a tuning carrier
; possible values are:
; PKT = 0x0C, CW = 0x02, CWR = 0x03, FM = 0x08
#define         MODE            0x02


; if the following line is uncommented the TX power
; is reduced during tuning to the value specified by
; POWER
#define         REDUCE_POWER    1


; uncomment one of the following lines to specify
; to which value the TX power should be reduced
; during tuning. works only if REDUCE_POWER is
; also uncommented
; 50 = 5W, 25 = 2.5W, 10 = 1W, 5 = 0.5W
;#define         POWER          50
;#define         POWER          25
#define          POWER          10
;#define         POWER           5

;==================================================


;************************************************** 
;
;       M A I N - E N T R Y
;
;**************************************************

                ORG             0x000
        ifndef F84
                movwf           OSCCAL
        endif
                goto            Start


;************************************************** 
;
;       S U B R O U T I N E S
;       (must be in the first PAGE on a 12C5xx)
;
;**************************************************

;**************************************************
;
;   one RS232 bit delay
;
;               4 Mhz                    10 Mhz
;   -----------------------------------------------
;   2400 Bps    69d                      173d
;   4800 Bps    34d                       86d
;   9600 Bps    15d (93 µs)               43d
;
;**************************************************

DelayBit
                movlw           D'15'                   ; 1
                movwf           DELAY_L                 ; 1
DelayBit01
                nop                                     ; 1
                nop                                     ; 1
                nop                                     ; 1
                decfsz          DELAY_L,f               ; 1 / 2
                goto            DelayBit01              ; 2
                retlw           0                       ; 2


;**************************************************
;
;   1/2 RS232 bit delay
;
;               4 Mhz                    10 Mhz
;   -----------------------------------------------
;   2400 Bps    63d                      157d
;   4800 Bps    31d                       78d
;   9600 Bps    15d (48 µs)               39d
;
;**************************************************

DelayHalfBit
                movlw           D'15'                   ; 1
                movwf           DELAY_L                 ; 1
DelayHalfBit01
                decfsz          DELAY_L,f               ; 1 / 2
                goto            DelayHalfBit01          ; 2
                retlw           0                       ; 2


;**************************************************
; delay parameter
;
; DELAY_H   msb
; DELAY_L   lsb
;
; of the 16 bit number of milliseconds to wait.
; so it is possible to wait from 1ms to 65535ms
; (about 1min) 1min is exact EA60 (60000)
;
; at 4MHz, 1000 instruction cycles are exact 1ms
; completely exact time is not possible. the
; routine always needs 5 instructions at the
; beginning and 1 instruction at the end. so the
; total delay instruction count is always + 6
;**************************************************

Delay
                comf            DELAY_H,f   ; 1     1
                comf            DELAY_L,f   ; 1     1
                incf            DELAY_L,f   ; 1     1
                btfsc           STATUS,Z    ; 1/2   1/2
                incf            DELAY_H,f   ; 1     1
; ------------------------------------- = 5 bei eintritt immer 5 cycles


; inner loop for 1000 instruction cycles
; at 4MHz each instruction takes 4 clock
; cycles
; 1000 instruction cycles = 1ms
; ---------------------------------------Cycles-----
Delay01
                movlw           D'248'      ; 1     1
                movwf           DELAY_T     ; 1     1
Delay02
                nop                         ; 1     248x1
                decfsz          DELAY_T,f   ; 1/2   247x1 + 1x2
                goto            Delay02     ; 2     247x2
                
                clrwdt                      ; 1     1
                ;nop                        ; 1     1
                nop                         ; 1     1
; ------------------------------------- = 995 cycles

                incf            DELAY_L,f   ; 1     1
                btfsc           STATUS,Z    ; 1/2   1/2
                incfsz          DELAY_H,f   ; 1/2   1/2
                goto            Delay01     ; 2     2 innerhalb loop immer 5 cycles
; ------------------------------------- = 5 cycles

                retlw           0           ; 2     bei austritt obige 5 cycles + 1
; ------------------------------------- = 6 cycles


; Time to wait after command to FT-817
TXDelay
                movlw           .0
                movwf           DELAY_H
                movlw           SPEED
                movwf           DELAY_L
                call            Delay
                retlw           0


;**************************************************
;
;       receive RS 232 Byte
;
;**************************************************

RX232
                clrf            RS232_Byte
                movlw           8
                movwf           RS232_L
RX23201                
                btfss           RS232in
                goto            RX23201
                
                call            DelayHalfBit
                
                btfss           RS232in
                goto            RX23201
RX23202
                call            DelayBit
                
                btfsc           RS232in
                bcf             STATUS,C

                btfss           RS232in
                bsf             STATUS,C

                rrf             RS232_Byte,f
                decfsz          RS232_L,f
                goto            RX23202

                call            DelayBit
                btfsc           RS232in
                goto            RX232

                retlw           0


;**************************************************
;
;       send RS 232 Byte
;
;**************************************************

TX232
                movwf           RS232_Byte
                movlw           8
                movwf           RS232_L

                ; StartBit
                bcf             RS232out
                call            DelayBit
                bsf             RS232out
                call            DelayBit

                ; 8 DataBits
TX23202
                rrf             RS232_Byte,f
                
                btfsc           STATUS,C
                bcf             RS232out
                
                btfss           STATUS,C
                bsf             RS232out
                
                call            DelayBit
                
                decfsz          RS232_L,f
                goto            TX23202

                ; 1. StopBit
                bcf             RS232out
                call            DelayBit

                ; 2. StopBit
                call            DelayBit

                retlw           0


;**************************************************
;
;       Start of main program
;
;**************************************************
        
Start

        ifdef F84

                PAGE1

                ; set PORTA
                ; A7 - 0 
                ; A6 - 0 
                ; A5 - 0 
                ; A4 - 0 
                ; A3 - 0
                ; A2 - 0
                ; A1 - 0
                ; A0 - 0 out led
                
                movlw           B'00000000'
                movwf           TRISA

                ; set PORTB
                ; B7 - 0 out RS232 
                ; B6 - 1 in RS232
                ; B5 - 0 
                ; B4 - 0 
                ; B3 - 0 
                ; B2 - 0 
                ; B1 - 0 
                ; B0 - 1 key 
                
                movlw           B'01000001'
                movwf           TRISB

                ; option register
                ; /RBPU - 1 disabled PORTB pull up's
                ; INTEDG -1 int on rising edge on RB0/INT
                ; T0CS  - 0 internal timer 0 clock source
                ; T0SE  - 1 high to low edge
                ; PSA   - 1 prescaler to wdt
                ; PS2   - 1 -+
                ; PS1   - 1  +- wdt rate 1:64
                ; PS0   - 0 -+

                movlw           B'11011110'
                movwf           OPTION_REG

                PAGE0

                ; intcon
                ; GIE  - 0 global interrupt enable
                ; EEIE - 0 EE write cmplete int enable
                ; T0IE - 0 TMR0 overflow int enable
                ; INTE - 1 RB0/INT int enable
                ;          (wake up from sleep when key
                ;           was pressed)
                ; RBIE - 0 PORTB change int enable
                ; T0IF - 0 TMR0 overflowed
                ; INTF - 0 RB0/INT int occured
                ; RBIF - 0 PORTB changed

                movlw           B'00010000'
                movwf           INTCON

                clrf            PORTA
                clrf            PORTB

        else

                ; set GPIO
                ; GP7 - 0 not implemented
                ; GP6 - 0 not implemented
                ; GP5 - 0 
                ; GP4 - 0
                ; GP3 - 1 in key
                ; GP2 - 1 in RS232 in
                ; GP1 - 0 out RS232 out
                ; GP0 - 0 out LED
                
                movlw           B'00001100'
                tris            GPIO

                ; option register
                ; /GWPU - 0 enabled wake up on pin change (GP0,1,3)
                ;          (wake up from sleep when key
                ;           was pressed)
                ; /GPPU - 1 disabled weak pullup (GP0,1,3)
                ; T0CS  - 0 internal timer 0 clock source
                ; T0SE  - 1 high to low edge
                ; PSA   - 1 prescaler to wdt
                ; PS2   - 1 -+
                ; PS1   - 1  +- wdt rate 1:64
                ; PS0   - 0 -+
                
                movlw           B'01011110'
                option

                clrf            GPIO
                
        endif

                ; check if wdt reset
                btfsc           STATUS,NOT_TO
                goto            PowerUp
                btfss           STATUS,NOT_PD
                goto            PowerUp

                ; if it was a WDT reset during RS232
                ; cimmunication with the FT-817 so there
                ; was an error so blink the led 3 times

                bcf             LED
                movlw           0x01
                movwf           DELAY_H
                movlw           0x00
                movwf           DELAY_L
                call            Delay
                
                bsf             LED
                movlw           0x01
                movwf           DELAY_H
                movlw           0x00
                movwf           DELAY_L
                call            Delay
                
                bcf             LED
                movlw           0x01
                movwf           DELAY_H
                movlw           0x00
                movwf           DELAY_L
                call            Delay
                
                bsf             LED
                movlw           0x01
                movwf           DELAY_H
                movlw           0x00
                movwf           DELAY_L
                call            Delay
                
                bcf             LED
                movlw           0x01
                movwf           DELAY_H
                movlw           0x00
                movwf           DELAY_L
                call            Delay
                
                bsf             LED
                movlw           0x01
                movwf           DELAY_H
                movlw           0x00
                movwf           DELAY_L
                call            Delay
                
                bcf             LED


;**************************************************
;
;       Main Loop
;
;**************************************************

PowerUp
                clrf            Mode
                bsf             LED         ; LED on
                
                ; is key pressed ?
                btfss           KEY
                goto            PowerDown

;--------------------------------------------------
BackupMode
                clrwdt

                ; send read mode CAT command (0x03)
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x03
                call            TX232

                ; READ BACK 5 BYTE
                ; read mode from FT-817 and save it
                clrwdt
                call            RX232
                clrwdt
                call            RX232
                clrwdt
                call            RX232
                clrwdt
                call            RX232
                clrwdt
                call            RX232

                movf            RS232_Byte,w
                movwf           Mode

                ; take care of WFM and FMN which cannot
                ; be set via cat. instead set to FM which
                ; results in the right mode
                movlw           0x06
                subwf           Mode,w
                btfss           STATUS,Z
                goto            BackupMode_1
				
                movlw           0x08
                movwf           Mode
                goto            BackupMode_2

BackupMode_1
                movlw           0x88
                subwf           Mode,w
                btfss           STATUS,Z
                goto            BackupMode_2

                movlw           0x08
                movwf           Mode

BackupMode_2                
                ; key stil pressed ?
                btfss           KEY
                goto            PowerDown

;--------------------------------------------------
        ifdef REDUCE_POWER
BackupPower
                clrwdt

                ; send read EEPROM (0xBB) to
                ; read current TX power
                ; EEPROM location 0x0079
                ; WARNING: this is an udocumented
                ; CAT command and may damage your Radio!!!
                movlw           0x00
                call            TX232
                movlw           0x79
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0xBB
                call            TX232

                ; READ BACK 2 BYTES
                ; read EEPROM data from FT-817 and save it
                clrwdt
                call            RX232
                movf            RS232_Byte,w
                movwf           EEHigh                
                movwf           EETemp

                clrwdt
                call            RX232
                movf            RS232_Byte,w
                movwf           EELow

                ; key stil pressed ?
                btfss           KEY
                goto            PowerDown
        endif
        
;--------------------------------------------------
SetMode
                clrwdt

                movlw           MODE
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x07
                call            TX232

                call            TXDelay

                ; key stil pressed ?
                btfss           KEY
                goto            RestoreMode

;--------------------------------------------------
        ifdef REDUCE_POWER
ReducePower
                clrwdt

                ; send write EEPROM (0xBC) to
                ; write the new TX power
                ; EEPROM location 0x0079
                ; WARNING: this is an udocumented
                ; CAT command and may damage your Radio!!!
             
                movlw           0x00
                call            TX232
                movlw           0x79
                call            TX232

                ; set new power
                ; possible values on a FT-817 are
                ; 00 - High 5,0 W
                ; 01 - L3   2,5 W
                ; 10 - L2   1,0 W
                ; 11 - L1   0,5 W

        if POWER==50
                bcf             EETemp, 1
                bcf             EETemp, 0
        endif
        if POWER==25
                bcf             EETemp, 1
                bsf             EETemp, 0
        endif
        if POWER==10
                bsf             EETemp, 1
                bcf             EETemp, 0
        endif
        if POWER==5
                bsf             EETemp, 1
                bsf             EETemp, 0
        endif

                movf            EETemp, w
                call            TX232
                
                movf            EELow, w
                call            TX232
                
                movlw           0xBC
                call            TX232

                call            TXDelay
                call            TXDelay
                call            TXDelay

                ; key stil pressed ?
                btfss           KEY
                goto            RestorePower
        endif
	
;--------------------------------------------------
LockIt
                clrwdt

                ; send lock frontpanel 
                ; CAT command (0x00)
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232

                call            TXDelay
                
                ; key stil pressed ?
                btfss           KEY
                goto            UnLock
                                
;--------------------------------------------------
PTTOn
                clrwdt

                ; PTT ON CAT command (0x08)
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x08
                call            TX232

                call            TXDelay

;--------------------------------------------------
WaitKey
                ; now wait til key is released
                clrwdt
                btfsc           KEY
                goto            WaitKey

;--------------------------------------------------
PTTOff
                clrwdt

                ; PTT OFF CAT command (0x88)
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x88
                call            TX232
                
                call            TXDelay

;--------------------------------------------------
UnLock
                clrwdt

                ; unlock FT817 CAT command (0x80)
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x80
                call            TX232
                        
                call            TXDelay

;--------------------------------------------------
        ifdef REDUCE_POWER
RestorePower
                clrwdt

                ; send read EEPROM (0xBB) to
                ; read current TX power
                ; EEPROM location 0x0079
                ; WARNING: this is an udocumented
                ; CAT command and may damage your Radio!!!
                movlw           0x00
                call            TX232
                movlw           0x79
                call            TX232

                movf            EEHigh, w
                call            TX232
                
                movf            EELow, w
                call            TX232
                
                movlw           0xBC
                call            TX232

                call            TXDelay
                call            TXDelay
                call            TXDelay
        endif

;--------------------------------------------------
RestoreMode
                clrwdt

                ; restore mode back to was it was
                ; before key was pressed
                ; send set mode CAT command (0x07)
                movf            Mode,w
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x00
                call            TX232
                movlw           0x07
                call            TX232
                        
                call            TXDelay

;--------------------------------------------------

PowerDown
                bcf             LED         ; LED off
WaitKey1

            ; set WDT prescaler to 1:128
        ifdef F84
                movlw           B'11011111'
                movwf           OPTION_REG
        else
                movlw           B'01011111'
                option
        endif

                sleep

            ; set WDT prescaler back to 1:64
        ifdef F84
                movlw           B'11011110'
                movwf           OPTION_REG
        else
                movlw           B'01011110'
                option
        endif
                
                ; key pressed - start tuning
                btfss           KEY
                goto            WaitKey1
                
                goto            PowerUp
                


;**************************************************
;
; OSCILLIATOR CALIBRATION VALUE FOR 12C5x TYPES
;
; WARNING: EITHER YOU CHANGE THIS EXAMPLE FROM
;          0x7C TO THE VALUE OF THE DEVICE YOU
;          ARE BURNING OR YOU HAVE A PROGRAMMER
;          WHICH PRESERVES IT AUTOMATICALY. IN
;          THIS CASE YOU CAN DELETE THIS BLOCK
;          COMPLETE
;
;**************************************************
        ifndef F84
                org             0x3FF
                movlw           0x7C        ; osc calibration !!
        endif


        end



