; FT817 control device (based on G4JNT work)
; use to set radio to often used frequencys and modes

; PORTA and PORTC used as switch inputs: 
; A0, A1, A2 and A5, C0, C1, C2, C3, C5
; LED o/p on A4
; RS232 o/p (Tx) to FT817 on C4
; A3 used for in circuit serial programming ICSP

	LIST P=16F688
	INCLUDE "P16F688.INC"
	;11 0011 1110 0010
      __config    0x33E4  		; Fail safe disabled, Boden en, Prot dis, RA3 is MCLR ,PWRT en
								; WDT dis, IntOsc I/o on RA4/5
    errorlevel 2
	#define		LED		PORTA, 4
	STACK		=	0xA0		; Indirect addressing doesn't require Page 1 to be set

  	org     0
    clrf    INTCON
    goto    Startup     		; jump to main code
;------------------------------
  org 8             ;
;========================

Startup 

	clrf	PORTA       		; Special setup to disable comparator
	movlw	7          		 	; Ext input, disabled
	movwf	CMCON0

	bsf	STATUS,RP0      		; ram page 1

	clrf	ANSEL

    movlw   b'00000000'     	; Pull Ups
    movwf   OPTION_REG

	movlw	b'00100111'     	; A0-2 and A5 inputs from switchs, output LED on A4
	movwf	PORTA  	
	movwf	WPUA				; weak internal pull-ups on the input switches on PORT A

	movlw	b'00001111'     	; C4 set for UART Tx, C0-3 and C5 inputs for switches
	movwf	PORTC       

	movlw	b'00000000'			; UART Rx interrupt not enabled 
	movwf	PIE1

	bcf	STATUS, RP0

	movlw	b'00100100'			; TXEN, Async, High Speed
	movwf	TXSTA
	movlw	d'25'				; 4MHz Osc, 9600 Baud	
	movwf	SPBRG				; 8 bit generator

	movlw	b'10000000'			; Serial Port Rx interupt not enabled
	movwf	RCSTA

; set LED off
	bcf	LED

; define two constants for the delay loops
	DelCount1 equ 21
 	DelCount2 equ 22

;..........

; flash LED two times on power ON to show PIC circuit is working ok
	bsf	LED
	call Delay100
	bcf	LED
	call Delay100
	bsf	LED
	call Delay100
	bcf	LED
	call Delay100


; check PORTA inputs then PORTC inputs (no PORT B)
; A0, A1, A2, A3 and A5 and C0, C1, C2, C3, C5
; NOTE: PORTC needs pull-up resistors 
; while PORTA has built in / selectable resistors

MainLoop
; first scans switches on PORTA: A0, A1, A2 and A5

; check switch 1 is pressed (i.e. is A0=0), if so PTT ON
	btfss PORTA,0
	CALL RS232_A0

; check switch 2 is pressed (A1=0), if so PTT OFF
	btfss PORTA,1
	CALL RS232_A1

; check switch 3 is pressed (A2=0), if so toggle between VFO A and B
	btfss PORTA,2
	CALL RS232_A2

; check switch 9 (yes 9!) is pressed (A5=0) = 144MHz + offset to go to 23cm transverter
	btfss PORTA,5
	CALL RS232_A5


; NOTE fourth pin of PORTA (i.e. A3 not A4) is used as an output for the LED
; NOTE fifth pin of PORTA is MCLR no pull up R used for ICSP

; then scan switches on PORTC; C0, C1, C2, C3 and C5
; Note: PORTC needs pull up resistors (PORTA has built in / selectable resistors, PORTC does not)

; check switch 4 is pressed (i.e. is C0=0) = 2m SSB calling usb
	btfss PORTC,0
	CALL RS232_C0

; check switch 5 is pressed (C1=0) = 2m SSB + 20 kHz up calling usb
	btfss PORTC,1
	CALL RS232_C1

; check switch 6 is pressed (C2=0) = WADARC 2m net FM
	btfss PORTC,2
	CALL RS232_C2

; check switch 7 is pressed (C3=0) = 80m band PSK31 frequency usb
	btfss PORTC,3
	CALL RS232_C3

; C4 or C5 not used for switches

; check switch 8 is pressed (C5=0) = 144MHz to go to 23cm transverter
	btfss PORTC,5
	CALL RS232_C5


	goto MainLoop		;loop around testing switches


; .................................

; pre-set subroutines

RS232_A0
; switch 1
; keep LED ON to show we are sending data
	bsf	LED

; PPT ON
	movlw	00			;can be any data
	call	Send232
	movlw	00			;can be any data
	call	Send232
	movlw	00			;can be any data
	call	Send232
	movlw	00			;can be any data
	call	Send232				
	movlw	08			;OPCode for PPT ON
	call	Send232

	call Delay100
	call Delay100

	bcf	LED
	return


RS232_A1
; switch 2
; keep LED ON to show we are sending data
	bsf	LED

; PPT OFF
	movlw	00			;can be any data
	call	Send232
	movlw	00			;can be any data
	call	Send232
	movlw	00			;can be any data
	call	Send232
	movlw	00			;can be any data
	call	Send232				
	movlw	88			;OPCode for PPT OFF
	call	Send232

	call Delay100
	call Delay100

	bcf	LED
	return

RS232_A2
; switch 3
; code for the first switch - switch 1
; keep LED ON to show we are sending data
	bsf	LED

; toggle betwen VFO A and VFO B
	movlw	00			;can be any data
	call	Send232
	movlw	00			;can be any data
	call	Send232
	movlw	00			;can be any data
	call	Send232
	movlw	00			;can be any data
	call	Send232				
	movlw	81			;OPCode for toggle VFO's
	call	Send232

; longer delay so that the VFO's dont just cycle back and 
; forth if you have the switched presed too long
	call Delay100
	call Delay100

	bcf	LED
	return

; NOTE: see below for RS232_A5, its switch 9 and so out of order

RS232_C0
; switch 4
; keep LED ON to show we are sending data
	bsf	LED

; set FT817 to 2m SSB calling frequency and usb
; this sends 144.300 MHz
; use hex i.e. 44 not decimal d'44'
	movlw	14
	call	Send232
	movlw	43
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232				
	movlw	1			;OPCode for set frequency
	call	Send232

; short delay for FT817 then send more data
; flash LED for each data sent
	call Delay100
	bcf	LED
	call Delay100
	bsf	LED

	movlw	01			; this sets op mode to usb
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232				
	movlw	7			;OPCode for set mode
	call	Send232

	call Delay100
	bcf	LED
	return


RS232_C1
; switch 5
; keep LED ON to show we are sending data
	bsf	LED

; set FT817 to 2m SSB calling frequency + 20 and mode
; this sends 144.320 MHz
; use hex i.e. 44 not decimal d'44'
	movlw	14
	call	Send232
	movlw	43
	call	Send232
	movlw	20
	call	Send232
	movlw	00
	call	Send232				
	movlw	1			;OPCode for set frequency
	call	Send232

; short delay for FT817 then send more data
; flash LED for each data sent
	call Delay100
	bcf	LED
	call Delay100
	bsf	LED

	movlw	01 			; this sets op mode to usb
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232				
	movlw	7			;OPCode for set mode
	call	Send232

	call Delay100
	bcf	LED
	return


RS232_C2
; switch 6
; keep LED ON to show we are sending data
	bsf	LED

; set FT817 to WADARC 2m net frequency and mode
; this sends 145.425 MHz
; use hex i.e. 44 not decimal d'44'
	movlw	14
	call	Send232
	movlw	54
	call	Send232
	movlw	25
	call	Send232
	movlw	00
	call	Send232				
	movlw	01			;OPCode for set frequency
	call	Send232

; short delay for FT817 then send more data
; flash LED for each data sent
	call Delay100
	bcf	LED
	call Delay100
	bsf	LED

	movlw	08			; this sets op mode to FM
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232				
	movlw	07			;OPCode for set mode
	call	Send232

	call Delay100
	bcf	LED
	return

RS232_C3
; switch 7
; set FT817 to PSK31 center frequency
; this sends 3.580 MHz usb (or 14.070 MHz usb)

	bsf	LED
; use hex i.e. 44 not decimal d'44'
	movlw	00
	call	Send232
	movlw	35
	call	Send232
	movlw	80
	call	Send232
	movlw	00
	call	Send232				
	movlw	1			;OPCode for set frequency
	call	Send232

; short delay for FT817 then send more data
; flash LED for each data sent
	call Delay100
	bcf	LED
	call Delay100
	bsf	LED

	movlw	01			; this sets op mode to usb
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232				
	movlw	7			;OPCode for set mode
	call	Send232

	call Delay100
	bcf	LED
	return

RS232_C5
; switch 8
; set FT817 to 146.100 for 1296.100 on my 23cm transverter
; this sends 146.100 MHz

; keep LED ON to show we are sending data
	bsf	LED


	movlw	14
	call	Send232
	movlw	61
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232				
	movlw	1			;OPCode for set frequency
	call	Send232

; short delay for FT817 then send more data
; flash LED for each data sent
	call Delay100
	bcf	LED
	call Delay100
	bsf	LED

	movlw	01			; this sets op mode to usb
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232				
	movlw	7			;OPCode for set mode
	call	Send232

	call Delay100
	bcf	LED
	return

RS232_A5
; switch 9
; set FT817 to 146.250 + 150 kHz for 1296.250 on my 23cm transverter
; this sends 146.250 MHz

; keep LED ON to show we are sending data
	bsf	LED

	movlw	14
	call	Send232
	movlw	62
	call	Send232
	movlw	50
	call	Send232
	movlw	00
	call	Send232				
	movlw	1			;OPCode for set frequency
	call	Send232

; short delay for FT817 then send more data
; flash LED for each data sent
	call Delay100
	bcf	LED
	call Delay100
	bsf	LED

	movlw	01			; this sets op mode to usb
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232
	movlw	00
	call	Send232				
	movlw	7			;OPCode for set mode
	call	Send232

	call Delay100
	bcf	LED
	return


;-----------------

; longer delay
Delay100
	movlw	d'100'
	movwf	DelCount2
LongDelLoop	
	call	Delay1ms
	decfsz	DelCount2
	goto	LongDelLoop
	return

;------------------

; short delay
Delay1ms
	movlw	d'250'
	movwf	DelCount1
DelLoop
	nop
	nop
	decfsz	DelCount1
	goto	DelLoop
	return

;--------------------

; routine to get the PIC to send out the RS232 data
Send232				
	btfss	PIR1, TXIF   ;First set TXENABLE   
	goto	Send232      ;Loop until Tx buffer is empty from previous char
	movwf	TXREG		
	return

;============================================

end
