; KD5ZUG telemetry transmitter control program.
; All rights reserved
; Modified and adapted for 4 channels by LU7ABF
; For use at Amsat Argentina LUSAC CW Telemetry Beacon

	list      p=12F675        		; list directive to define processor
	#include <p12f675.inc>          ; processor specific variable definitions
	errorlevel  -302               ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _CP_OFF & _CPD_OFF  

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.

;****************************************************************************
;Defines
;****************************************************************************
#define	BANK1		banksel 0x80	;Select Bank1
#define	BANK0		banksel 0x00	;Select Bank0
;#define	Dit			0x40
;#define Dah			0xc0
;#define	Pause		0x08
#define	AtoD1_H		0x20		;A to D reading from pin 7 most significant bits
#define	AtoD1_L		0x21		;A to D reading from pin 7 least significant bits
#define	AtoD2_H		0x22		;A to D reading from pin 6 most significant bits
#define	AtoD2_L		0x23		;A to D reading from pin 6 least significant bits
#define NumH		0x24		; varible to be converted to BCD,High byte
#define NumL		0x25		; varible to be converted to BCD,low byte	
#define TenK		0x26		; BCD ten thousands
#define Thou		0x27		; BCD thousands
#define Hund		0x28		; BCD hundreds
#define Tens		0x29		; BCD tens
#define	Ones		0x2a		; BCD ones
#define digit		0x2b		; digital input
#define count		0x2c		; used to adjust the delay time
#define	Dit			0x2d		; dit length 
#define	Dah			0x2e		; dah length 
#define	Pause		0x2f		; pause length 
#define	count2		0x30		; number of readings before transmit
#define MaxRead1_H	0x31		; Max reading recorded (high order byte)
#define MaxRead1_L	0x32		; Max reading recorded (low order byte)
#define MinRead1_H	0x33		; Min reading recorded (high order byte)
#define MinRead1_L	0x34		; Min reading recorded (low order byte)
#define DestL		0x35		; subtraction varable low byte (source-dest)
#define DestH		0x36		; subtraction varable high byte
#define	SourceL		0x37		; subtraction varable low byte
#define	SourceH		0x38		; subtraction varable high byte
#define	CuentaH		0x39		; Count High sequence
#define	CuentaL		0x3a		; Count Low sequence
#define	AtoD3_L		0x3b		;A to D reading from pin 5 least significant bits
#define	AtoD3_H		0x3c		;A to D reading from pin 5 most significant bits
#define	AtoD4_L		0x3d		;A to D reading from pin 3 least significant bits
#define	AtoD4_H		0x3e		;A to D reading from pin 3 most significant bits
#define MaxRead2_H	0x3f		; Max reading recorded Channel 2 (high order byte)
#define MaxRead2_L	0x40		; Max reading recorded Channel 2 (low order byte)
#define MinRead2_H	0x41		; Min reading recorded Channel 2 (high order byte)
#define MinRead2_L	0x42		; Min reading recorded Channel 2 (low order byte)
#define MaxRead3_H	0x43		; Max reading recorded Channel 3 (high order byte)
#define MaxRead3_L	0x44		; Max reading recorded Channel 3 (low order byte)
#define MinRead3_H	0x45		; Min reading recorded Channel 3 (high order byte)
#define MinRead3_L	0x46		; Min reading recorded Channel 3 (low order byte)
#define MaxRead4_H	0x47		; Max reading recorded Channel 4 (high order byte)
#define MaxRead4_L	0x48		; Max reading recorded Channel 4 (low order byte)
#define MinRead4_H	0x49		; Min reading recorded Channel 4 (high order byte)
#define MinRead4_L	0x4a		; Min reading recorded Channel 4 (low order byte)
;****************************************************************************
;Reset Vector
;****************************************************************************
	ORG     0x000             ; processor reset vector
	nop						  ; Inserted For ICD2 Use
	goto    Init              ; go to beginning of program
;****************************************************************************
;Initialization
;****************************************************************************
Init

	BANK1				; Switch to Bank1
	call    0x3FF     	; retrieve factory calibration value
	movwf   OSCCAL     	; update register with factory cal value 
; Clear all Registers
		movlw	0x20		;initialize pointer
		movwf	FSR			; to RAM
NEXT	clrf	INDF		; clear INDF register
		incf	FSR,F		; inc pointer
		btfss	FSR,7		; all done?
		goto	NEXT		; no clear next
;
		movlw	B'00011111'	; set up high value for minimum read value to work
		movwf	MinRead1_H
		movwf	MinRead2_H
		movwf	MinRead3_H
		movlw	B'11111111'	; pin 5& 3 are outputs and pins 2,4,7,6 are inputs_
		movwf	MinRead1_L	; set up high value for minimum read value to work
		movwf	MinRead2_L
		movwf	MinRead3_L
		movwf	TRISIO		;_bit0=7,bit1=6,bit2=5,bit3=4,bit4=3,bit5=2,
						;_1=input, 0=output
	movlw	B'11010101' ; set internal clock(5),prescale =1:256 assigned to TIMER0_
	movwf	OPTION_REG	;_bit0-2=prescale(100=1:32),bit3=prescale=TIMER0 (=0)_
						;_bit4=trailing edge,bit5=internal clock(0)
						;_bit6=int (1=rising), bit7=pullups (diabled=1)
	clrf	VRCON		; Vref Off (power off the comparator voltage,saves power)
	movlw	b'01011111'	; Configure pin 7 as anlog, Tad=16us_
	movwf	ANSEL		;_bit0=7,bit1=6,bit2=5,bit3=3 (1=analog,0=digital)
						;_bit4-6= A/D clock osc value (101=16)
	BANK0				; BANK 0
	clrf	TMR0		; clear TIMER 0
	clrf	GPIO		;Clear all Ports
	movlw	0x07		
	movwf	CMCON		; Comparator Off, bit0,1,2=1 (00000111) 
	bsf		ADCON0,0	; turn A to D converter to "on", bit0=1
	bsf		ADCON0,7	; right justified A/D, bit 7=1
	call 	Delay_VS	; _ need at least 4 micro seconds before starting A to D_
;****************************************************************************
;MAIN - Main Routine
;****************************************************************************
Main
	movlw	0x04		;number of long delay cycles between transmissions tenia 0x0f
						; con 0x06 daba 30 segundos
	movwf	count2		;if pin 4 is high.
	call	Speed_L		;set speed at 8 words per minute.
;	btfsc	GPIO,5		;if switch on pin 2 is high set at 50 WPM.
;	call	Speed_H		;if pin 4 is high set as 50 WPM.
	BANK1
	movlw	b'01011111'	; Configure pin 7 as anlog, Tad=16us_
	movwf	ANSEL		;_bit0=7,bit1=6,bit2=5,bit3=3 (1=analog,0=digital)
						;_bit4-6= A/D clock osc value (101=16)
	movlw	B'11111111'	; all pins are input
	movwf	TRISIO		;_bit0=7,bit1=6,bit2=5,bit3=4,bit4=3,bit5=2,
						;_1=input, 0=output
	BANK0
;	btfsc	GPIO,3		;if switch on pin 4 is high set as timed transmittion
	call	Wait
	call	Read_A		; read the sensor A to D reading on pin 7
	call	pause
	call	Min1		; record if value is the lowest since power "on"
	call	Max1		; record if value is the highest since power "on"
	call	Read_B		; read the sensor A to D reading on pin 6
	call	pause
	call	Min2		; record if value is the lowest since power "on"
	call	Max2		; record if value is the highest since power "on"
	call	Read_C		; read the sensor A to D reading on pin 5
	call	pause
	call	Min3		; record if value is the lowest since power "on"
	call	Max3		; record if value is the highest since power "on"
	call	Read_D		; read the sensor A to D reading on pin 3
	call	pause
	call	Min4		; record if value is the lowest since power "on"
	call	Max4		; record if value is the highest since power "on"
	BANK1
	movlw	b'01010111'	; Configure pin 7 as analog, Tad=16us_
	movwf	ANSEL		;_bit0=7,bit1=6,bit2=5,bit3=3 (1=analog,0=digital)
						;_bit4-6= A/D clock osc value (101=16)
	movlw	B'11001111'	; pins 2 & 3 (GP4 & GP5) are output 4,5,6,7 (GP0,1,2,3) are inputs_
;	movlw	B'11001011'	; pins 2 & 3 (GP4 & GP5) are output 4,5,6,7 (GP0,1,2,3) are inputs_
	movwf	TRISIO		;_bit0=7,bit1=6,bit2=5,bit3=4,bit4=3,bit5=2,
						;_1=input, 0=output
	BANK0
	;***************************************************************
	call	xmit_on		; turn transmitter on
	call	Delay_S
	call	_L
	call	_U
	call	one
	call	_D
	call	_C
	call	_X
	call	pauselong
	call	_L
	call	_U
	call	seven
	call	_A
	call	_A
	call	pauselong	
	call	_H
	call	_I
	call	pauselong
	call	_H
	call	_I
	call	pauselong
;	call	_A
;	call	_A
;
	call	CuentaUpd	; add one to cuenta and emit
	call	Morse_TenK	; and emit serial number
	call	pauselong
;
; Convert and Emit Reading on A port pin 7 
	call	BCD_A		; convert the 10 bit A to D reading to BCD
;	call	_A			; transmit the letter "A"
	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
						; starting with the hundreds digit
	call	pauselong
;
	call	BCD_C		; convert the 10 bit A to D reading to BCD
	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
						; starting with the hundreds digit
	call	pauselong
;
;
	call	BCD_B		; convert the 10 bit A to D reading to BCD
	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
						; starting with the hundreds digit
	call	pauselong
;
;	call	BCD_D		; convert the 10 bit A to D reading to BCD
;	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
						; starting with the hundreds digit
;	call	pauselong
;
	movf	CuentaH,0
	movwf	NumH
	movf	CuentaL,0
	movwf	NumL
	Call	BCD
	movf	Ones,w
	bnz	noextra
;  Following Code will transmit Channel 1 to 4 minimum and maximum values every ten tlm
;  Channel 1 ************************************************************
	call	_A			; transmit the letter "A"
	call	pauselong
	call	BCD_Min1	; convert the 10 bit A to D reading to BCD
	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
 						; starting with the hundreds digit
	call	pauselong
;
	call	BCD_Max1	; convert the 10 bit A to D reading to BCD
	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
						; starting with the hundreds digit
	call	pauselong
;  Channel 3 ************************************************************
	call	_B			; transmit the letter "B"
	call	pauselong
	call	BCD_Min3	; convert the 10 bit A to D reading to BCD
	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
						; starting with the hundreds digit
	call	pauselong

	call	BCD_Max3	; convert the 10 bit A to D reading to BCD
	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
						; starting with the hundreds digit
	call	pauselong
;  Channel 2 ************************************************************
	call	_C			; transmit the letter "C"
	call	pauselong
	call	BCD_Min2	; convert the 10 bit A to D reading to BCD
	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
 						; starting with the hundreds digit
	call	pauselong

	call	BCD_Max2	; convert the 10 bit A to D reading to BCD
	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
						; starting with the hundreds digit
	call	pauselong
;  Channel 4 ************************************************************
;	call	_D			; transmit the letter "C"
;	call	pauselong
;	call	BCD_Min4	; convert the 10 bit A to D reading to BCD
;	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
; 						; starting with the hundreds digit
;	call	pauselong
;
;	call	BCD_Max4	; convert the 10 bit A to D reading to BCD
;	call	Morse_Thou	; transmit the BCD A to D reading. 3 digits_
;						; starting with the hundreds digit
;	call	pauselong
; Transmits Final text message
	call	Final		; transmit final message
	call	pauselong
noextra					
	call	_K
	goto	Main 		; do it again

;********************************************************************
; Sub Routines
;********************************************************************
Speed_L					; set at 5 WPM
	movlw	0x40		; tenia 0x55
	movwf	Dit
	movlw	0xc0		; tenia	0xff
	movwf	Dah
	movlw	0x08		; tenia 0x0b
	movwf	Pause
	return
;*******************************************************************
;Speed_H					;set at 10 WPM
;	movlw	0x10
;	movwf	Dit
;	movlw	0x30
;	movwf	Dah
;	movlw	0x02
;	movwf	Pause
;	return
;*******************************************************************
Read_A	
	bcf		ADCON0,3	;set AtoD on pin 6 (bit 3 and 4 =01) 
	bcf		ADCON0,2	;
	call 	Delay_VS
	bsf		ADCON0,1	; start the A to D conversion
AtoD1
	btfss	PIR1,6
	goto	AtoD1
	bcf		PIR1,6
	BANK1
	movf	ADRESL,0
	BANK0
	movwf	AtoD1_L
	movf	ADRESH,0
	movwf	AtoD1_H
	return
;******************************************************************
Read_B	
	bcf		ADCON0,3	;set AtoD on pin 6 (bit 3 and 4 =01) 
	bsf		ADCON0,2	;
	call 	Delay_VS
	bsf		ADCON0,1	; start the A to D conversion
AtoD2
	btfss	PIR1,6
	goto	AtoD2
	bcf		PIR1,6
	BANK1
	movf	ADRESL,0
	BANK0
	movwf	AtoD2_L
	movf	ADRESH,0
	movwf	AtoD2_H
	return
;******************************************************************
Read_C	
	bsf		ADCON0,3	;set AtoD on pin 6 (bit 3 and 4 = 10) 
	bcf		ADCON0,2	;
	call 	Delay_VS
	bsf		ADCON0,1	; start the A to D conversion
AtoD3
	btfss	PIR1,6
	goto	AtoD3
	bcf		PIR1,6
	BANK1
	movf	ADRESL,0
	BANK0
	movwf	AtoD3_L
	movf	ADRESH,0
	movwf	AtoD3_H
	return
;*******************************************************************
Read_D	
	bcf		ADCON0,3	;set AtoD on pin 6 (bit 3 and 4 =11) 
	bsf		ADCON0,2	;
	call	Delay_VS
	bsf		ADCON0,1	; start the A to D conversion
AtoD4
	btfss	PIR1,6
	goto	AtoD4
	bcf		PIR1,6
	BANK1
	movf	ADRESL,0
	BANK0
	movwf	AtoD4_L
	movf	ADRESH,0
	movwf	AtoD4_H
	return
;*******************************************************************
BCD_TERM
	bcf		STATUS, C	;divide NumH NumL by 4
	rrf		NumH, f
	rrf		NumL, f
	bcf		STATUS, C
	rrf		NumH, f
	rrf		NumL, f
	movf	NumL,0	
	movlw	0xc4			; substract value -189 (it is zero degrees C) now c4
	subwf	NumL,1			; place result in NumL
	bcf		STATUS, C
	btfss	NumL,7				; check if negative
	goto	completed
	call	_M					; Emit M signaling - 
	call	pauselong
	clrf	NumH
	comf	NumL,1
completed
	clrf	NumH
	return
;*******************************************************************
BCD_A						; prepare A/D pin 7 for BCD conversion
	movf	AtoD1_H,0
	movwf	NumH
	movf	AtoD1_L,0
	movwf	NumL
	call	BCD_TERM
	call	BCD
	return
;******************************************************************
BCD_B						; prepare A/D on pin 6 for BCD conversion
	movf	AtoD2_H,0
	movwf	NumH
	movf	AtoD2_L,0		; get low byte
	movwf	NumL
	bcf		STATUS, C		;divide NumH NumL by 8
	rrf		NumH, f
	rrf		NumL, f
	bcf		STATUS, C
	rrf		NumH, f
	rrf		NumL, f
	bcf		STATUS, C
	rrf		NumH, f
	rrf		NumL, f
	movlw	0x2b
	subwf	NumL,1			; restar 43
	clrf	NumH
	Call	BCD
	return
;*****************************************************************
BCD_C						; prepare A/D on pin 5 for BCD conversion
	movf	AtoD3_H,0
	movwf	NumH
	movf	AtoD3_L,0
	movwf	NumL
	call	BCD_TERM
	call	BCD
	return
;*****************************************************************
;BCD_D						; prepare A/D on pin 3 for BCD conversion
;	movf	AtoD4_H,0
;	movwf	NumH
;	movf	AtoD4_L,0
;	movwf	NumL
;	Call	BCD
;	return
;*****************************************************************
BCD_Min1					; prepare Minimum reading BCD conversion
	movf	MinRead1_H,0
	movwf	NumH
	movf	MinRead1_L,0
	movwf	NumL
	call	BCD_TERM
	call	BCD
	return
;*****************************************************************
BCD_Max1					; prepare Maximum reading BCD conversion
	movf	MaxRead1_H,0
	movwf	NumH
	movf	MaxRead1_L,0
	movwf	NumL
	call	BCD_TERM
	call	BCD
	return
;*****************************************************************
BCD_Min2					; prepare Minimum reading BCD conversion
	movf	MinRead2_H,0
	movwf	NumH
	movf	MinRead2_L,0		; get low byte
	movwf	NumL
	bcf		STATUS, C	;divide NumH NumL by 8
	rrf		NumH, f
	rrf		NumL, f
	bcf		STATUS, C
	rrf		NumH, f
	rrf		NumL, f
	bcf		STATUS, C
	rrf		NumH, f
	rrf		NumL, f
	movlw	0x2b
	subwf	NumL,1			; restar 43
	clrf	NumH
	Call	BCD
	return
;*****************************************************************
BCD_Max2					; prepare Maximum reading BCD conversion
	movf	MaxRead2_H,0
	movwf	NumH
	movf	MaxRead2_L,0		; get low byte
	movwf	NumL
	bcf		STATUS, C	;divide NumH NumL by 8
	rrf		NumH, f
	rrf		NumL, f
	bcf		STATUS, C
	rrf		NumH, f
	rrf		NumL, f
	bcf		STATUS, C
	rrf		NumH, f
	rrf		NumL, f
	movlw	0x2b
	subwf	NumL,1			; restar 43
	clrf	NumH
	Call	BCD
	return
;*****************************************************************
BCD_Min3					; prepare Minimum reading BCD conversion
	movf	MinRead3_H,0
	movwf	NumH
	movf	MinRead3_L,0
	movwf	NumL
	call	BCD_TERM
	call	BCD
	return
;*****************************************************************
BCD_Max3					; prepare Maximum reading BCD conversion
	movf	MaxRead3_H,0
	movwf	NumH
	movf	MaxRead3_L,0
	movwf	NumL
	call	BCD_TERM
	call	BCD
	return
;*****************************************************************
BCD_Min4					; prepare Minimum reading BCD conversion
	movf	MinRead4_H,0
	movwf	NumH
	movf	MinRead4_L,0
	movwf	NumL
	Call	BCD
	return
;*****************************************************************
BCD_Max4					; prepare Maximum reading BCD conversion
	movf	MaxRead4_H,0
	movwf	NumH
	movf	MaxRead4_L,0
	movwf	NumL
	Call	BCD
	return
;*****************************************************************
CuentaUpd					; suma uno en CuentaL y carry a CuentaH
	incf	CuentaL,f			
	skpnz					; if carry do not update CuentaH
	incf	CuentaH,f	
Noover
	movf	CuentaH,0
	movwf	NumH
	movf	CuentaL,0
	movwf	NumL
	Call	BCD
	return
;******************************************************************
BCD						; convert number in NumH and NumL converts
						; to 5 digit BCD number contained in 
						; registers TenK, Thou, Hund, Tens, Ones
	swapf	NumH,w
	andlw	b'00001111'
	addlw	b'11110000'
	movwf	Thou
	addwf	Thou,f
	addlw	b'11100010'
	movwf	Hund
	addlw	b'00110010'
	movwf	Ones
	
	movf	NumH,w
	andlw	b'00001111'
	addwf	Hund,f
	addwf	Hund,f
	addwf	Ones,f
	addlw	b'11101001'
	movwf	Tens
	addwf	Tens,f
	addwf	Tens,f
	
	swapf	NumL,w
	andlw	b'00001111'
	addwf	Tens,f
	addwf	Ones,f
	
	rlf		Tens,f
	rlf		Ones,f
	comf	Ones,f
	rlf		Ones,f
	
	movf	NumL,w
	andlw	b'00001111'
	addwf	Ones,f
	rlf		Thou,f
	
	movlw	b'00000111'
	movwf	TenK
	movlw	b'00001010'
Lb1
	addwf	Ones,f
	decf	Tens,f
	btfss	STATUS,0
	goto	Lb1
Lb2
	addwf	Tens,f
	decf	Hund,f
	btfss	STATUS,0
	goto	Lb2
Lb3
	addwf	Hund,f
	decf	Thou,f
	btfss	STATUS,0
	goto	Lb3
Lb4
	addwf	Thou,f
	decf	TenK,f
	btfss	STATUS,0
	goto	Lb4
	
	return
;***************************************************************
Morse_TenK				; Convert BCD number to Morse code
	movlw	0x09
	subwf	TenK,0
	Btfsc	STATUS,2
	call	nine
	movlw	0x08
	subwf	TenK,0
	Btfsc	STATUS,2
	call	eight
	movlw	0x07
	subwf	TenK,0
	Btfsc	STATUS,2
	call	seven
	movlw	0x06
	subwf	TenK,0
	Btfsc	STATUS,2
	call	six
	movlw	0x05
	subwf	TenK,0
	Btfsc	STATUS,2
	call	five
	movlw	0x04
	subwf	TenK,0
	Btfsc	STATUS,2
	call	four
	movlw	0x03
	subwf	TenK,0
	Btfsc	STATUS,2
	call	three
	movlw	0x02
	subwf	TenK,0
	Btfsc	STATUS,2
	call	two
	movlw	0x01
	subwf	TenK,0
	Btfsc	STATUS,2
	call	one
;	movlw	0x00		; these four instruction are commented to avoid zero emission for thousands
;	subwf	TenK,0
;	Btfsc	STATUS,2
;	call	zero
Morse_Thou
	movlw	0x09
	subwf	Thou,0
	Btfsc	STATUS,2
	call	nine
	movlw	0x08
	subwf	Thou,0
	Btfsc	STATUS,2
	call	eight
	movlw	0x07
	subwf	Thou,0
	Btfsc	STATUS,2
	call	seven
	movlw	0x06
	subwf	Thou,0
	Btfsc	STATUS,2
	call	six
	movlw	0x05
	subwf	Thou,0
	Btfsc	STATUS,2
	call	five
	movlw	0x04
	subwf	Thou,0
	Btfsc	STATUS,2
	call	four
	movlw	0x03
	subwf	Thou,0
	Btfsc	STATUS,2
	call	three
	movlw	0x02
	subwf	Thou,0
	Btfsc	STATUS,2
	call	two
	movlw	0x01
	subwf	Thou,0
	Btfsc	STATUS,2
	call	one
	movlw	0x00		; these 3 instructions are to avoid emit zero on Thousands if zero on TenK
	subwf	TenK,0	
	btfsc	STATUS,2
	goto	Morse_Hund
	movlw	0x00
	subwf	Thou,0
	Btfsc	STATUS,2
	call	zero
Morse_Hund	
	movlw	0x09
	subwf	Hund,0
	Btfsc	STATUS,2
	call	nine
	movlw	0x08
	subwf	Hund,0
	Btfsc	STATUS,2
	call	eight
	movlw	0x07
	subwf	Hund,0
	Btfsc	STATUS,2
	call	seven
	movlw	0x06
	subwf	Hund,0
	Btfsc	STATUS,2
	call	six
	movlw	0x05
	subwf	Hund,0
	Btfsc	STATUS,2
	call	five
	movlw	0x04
	subwf	Hund,0
	Btfsc	STATUS,2
	call	four
	movlw	0x03
	subwf	Hund,0
	Btfsc	STATUS,2
	call	three
	movlw	0x02
	subwf	Hund,0
	Btfsc	STATUS,2
	call	two
	movlw	0x01
	subwf	Hund,0
	Btfsc	STATUS,2
	call	one
;
	movlw	0x00		; these 8 instructions are to avoid emit zero on Hund if zero on Thous
	subwf	TenK,0		; and zero on TenK
	btfsc	STATUS,2
	goto	Hund_Chk
	goto	EmitHund
Hund_Chk
	movlw	0x00		; these 6 instructions are to avoid emit zero on Hund if zero on Thous
	subwf	Thou,0		; and zero on TenK
	btfsc	STATUS,2
	goto	Morse_Tens
EmitHund
	movlw	0x00
	subwf	Hund,0
	Btfsc	STATUS,2
	call	zero
;
Morse_Tens	
	movlw	0x09
	subwf	Tens,0
	Btfsc	STATUS,2
	call	nine
	movlw	0x08
	subwf	Tens,0
	Btfsc	STATUS,2
	call	eight
	movlw	0x07
	subwf	Tens,0
	Btfsc	STATUS,2
	call	seven
	movlw	0x06
	subwf	Tens,0
	Btfsc	STATUS,2
	call	six
	movlw	0x05
	subwf	Tens,0
	Btfsc	STATUS,2
	call	five
	movlw	0x04
	subwf	Tens,0
	Btfsc	STATUS,2
	call	four
	movlw	0x03
	subwf	Tens,0
	Btfsc	STATUS,2
	call	three
	movlw	0x02
	subwf	Tens,0
	Btfsc	STATUS,2
	call	two
	movlw	0x01
	subwf	Tens,0
	Btfsc	STATUS,2
	call	one
;
;
	movlw	0x00		; these 12 instructions are to avoid emit zero on Hund if zero on Thous
	subwf	TenK,0		; and zero on TenK
	btfsc	STATUS,2
	goto	Hund_Chk1
	goto	EmitTens
Hund_Chk1
	movlw	0x00		; these 6 instructions are to avoid emit zero on Hund if zero on Thous
	subwf	Thou,0		; and zero on Thou
	btfsc	STATUS,2
	goto	Tens_Chk
	goto	EmitTens
Tens_Chk
	movlw	0x00		; these 6 instructions are to avoid emit zero on Hund if zero on Thous
	subwf	Hund,0		; and zero on Hund
	btfsc	STATUS,2
	goto	Morse_Ones
EmitTens
	movlw	0x00
	subwf	Tens,0
	Btfsc	STATUS,2
	call	zero
;
Morse_Ones	
	movlw	0x09
	subwf	Ones,0
	Btfsc	STATUS,2
	call	nine
	movlw	0x08
	subwf	Ones,0
	Btfsc	STATUS,2
	call	eight
	movlw	0x07
	subwf	Ones,0
	Btfsc	STATUS,2
	call	seven
	movlw	0x06
	subwf	Ones,0
	Btfsc	STATUS,2
	call	six
	movlw	0x05
	subwf	Ones,0
	Btfsc	STATUS,2
	call	five
	movlw	0x04
	subwf	Ones,0
	Btfsc	STATUS,2
	call	four
	movlw	0x03
	subwf	Ones,0
	Btfsc	STATUS,2
	call	three
	movlw	0x02
	subwf	Ones,0
	Btfsc	STATUS,2
	call	two
	movlw	0x01
	subwf	Ones,0
	Btfsc	STATUS,2
	call	one
	movlw	0x00
	subwf	Ones,0
	Btfsc	STATUS,2
	call	zero
	return
;******************************************************************	
xmit_on					; Turns transmitter on
	bsf		GPIO,5		; turn pin2 high, turns transmitter carrier "on"
	call	pauselong
	return
;*******************************************************************	
xmit_off	
	bcf		GPIO,5		; turns transmitter "off"
	return

;*******************************************************************
dit
	movf	Dit,0		; load dit duration value
	movwf	count		; load the dit duration time into the countdown file
	call	Tone		; call the tone generation subroutine
	call	pause		; space
	return
;******************************************************************
dah
	movf	Dah,0		; load dit duration value
	movwf	count		; load the dit duration time into the countdown file
	call	Tone		; call the tone generation subroutine
	call	pause		; space
	return

;******************************************************************
pauselong
	call pause
	call pause
	call pause
	call pause
	call pause
	return
pause					; Pause between Morse characters
	movf	Pause,0
	movwf	count
pause_1
	bcf		INTCON,2
P_loop
	btfss	INTCON,2
	goto	P_loop
	decfsz	count,1
	goto	pause_1
	return
;***************************************************************	
Tone					; 700 Hertz tone
	bcf		INTCON,2	;clear timer interupt flag 
	movlw	0xf5		;f4=640Hz tenia 0xf5  (tiene f5 en la baliza del transponder)
	movwf	TMR0
	bsf		GPIO,4		; start tone modulation, turn pin4 "on"
high_
	btfss	INTCON,2	; timer 0 overflow interup flag
	goto	high_		; delay loop
	bcf		INTCON,2	;clear timer interupt flag
	movlw	0xf8		;f4=640Hz tenia 0xf5  (tiene f5 en la baliza del transponder)
	movwf	TMR0
	bcf		GPIO,4		;turn pin4 "off"
low_
	btfss	INTCON,2	; timer 0 overflow interup flag
	goto	low_			;delay loop
	decfsz	count,1
	goto	Tone
	return
	
	;***************************************************************	
;Tone2					; extra tone not used
;	bcf		INTCON,2	;clear timer interupt flag
;	movlw	0xd0
;	movwf	TMR0
;	bsf		GPIO,4		; start tone modulation, turn pin4 "on"
;high2_
;	btfss	INTCON,2	; timer 0 overflow interup flag
;	goto	high2_		; delay loop
;	bcf		INTCON,2	;clear timer interupt flag
;	movlw	0xd0
;	movwf	TMR0
;	bcf		GPIO,4		;turn pin4 "off"
;low2_
;	btfss	INTCON,2	; timer 0 overflow interup flag
;	goto	low2_			;delay loop
;	decfsz	count,1
;	goto	Tone2
;	return
	;***************************************************************	
;Tone3					; extra tone not used
;	bcf		INTCON,2	;clear timer interupt flag
;	movlw	0xe0
;	movwf	TMR0
;	bsf		GPIO,4		; start tone modulation, turn pin4 "on"
;high3_
;	btfss	INTCON,2	; timer 0 overflow interup flag
;	goto	high3_		; delay loop
;	bcf		INTCON,2	;clear timer interupt flag
;	movlw	0xe0
;	movwf	TMR0
;	bcf		GPIO,4		;turn pin4 "off"
;low3_
;	btfss	INTCON,2	; timer 0 overflow interup flag
;	goto	low3_			;delay loop
;	decfsz	count,1
;	goto	Tone3
;	return
;*****************************************************************
Delay_VS
	nop
	nop
	nop
	nop
	nop	
	return
Delay_S						; Short delay
	movlw	0x40
	movwf	count
DS_Delay
	bcf		INTCON,2
DS_loop
	btfss	INTCON,2
	goto	DS_loop
	decfsz	count,1
	goto	DS_Delay
	return
;*****************************************************************
Delay_L						; Long delay
	movlw	0xff
	movwf	count
DL_Delay
	bcf		INTCON,2
DL_loop
	btfss	INTCON,2
	goto	DL_loop
	decfsz	count,1
	goto	DL_Delay
	return
;********************************************************************
Wait					; One minute delay
	bcf		GPIO,5		; turns transmitter "off"
	call	Delay_L
	decfsz 	count2,1
	goto	Wait
	return
;*******************************************************************
Final					; input Final Message Here
	call	pauselong
	call	_U
	call	_N
	call	pauselong
	call	_M
	call	_E
	call	_N
	call	_S
	call	_A
	call	_J
	call	_E
	call	pauselong
	call	_D
	call	_E
	call	pauselong
	call	_P
	call	_A
	call	_Z
	call	pauselong
	call	_S
	call	_C
	call	_O
	call	_U
	call	_T
	call	_S
	call	pauselong
	call	_S
	call	_I
	call	_E
	call	_M
	call	_P
	call	_R
	call	_E
	call	pauselong
	call	_L
	call	_I
	call	_S
	call	_T
	call	_O
	call	_S
	call	pauselong
	call	_Z
	call	one
	call	five
	call	pauselong
	call	_D
	call	two
	return
;*******************************************************************
Max1						; Used to capature Maximum value
	movf	AtoD1_L,0
	movwf	SourceL
	movf	AtoD1_H,0
	movwf	SourceH
	movf	MaxRead1_H,0
	movwf	DestH
	movf	MaxRead1_L,0
	movwf	DestL
	call	Subtract
	btfss	STATUS,0
	call	MaxUpdate1
	return
;*******************************************************************
Min1						; Record Minimum value
	movf	AtoD1_L,0
	movwf	SourceL
	movf	AtoD1_H,0
	movwf	SourceH
	movf	MinRead1_H,0
	movwf	DestH
	movf	MinRead1_L,0
	movwf	DestL
	call	Subtract
	btfsc	STATUS,0
	call	MinUpdate1
	return
;*******************************************************************
Max2						; Used to capature Maximum value
	movf	AtoD2_L,0
	movwf	SourceL
	movf	AtoD2_H,0
	movwf	SourceH
	movf	MaxRead2_H,0
	movwf	DestH
	movf	MaxRead2_L,0
	movwf	DestL
	call	Subtract
	btfss	STATUS,0
	call	MaxUpdate2
	return
;*******************************************************************
Min2						; Record Minimum value
	movf	AtoD2_L,0
	movwf	SourceL
	movf	AtoD2_H,0
	movwf	SourceH
	movf	MinRead2_H,0
	movwf	DestH
	movf	MinRead2_L,0
	movwf	DestL
	call	Subtract
	btfsc	STATUS,0
	call	MinUpdate2
	return	
;*******************************************************************
Max3						; Used to capature Maximum value
	movf	AtoD3_L,0
	movwf	SourceL
	movf	AtoD3_H,0
	movwf	SourceH
	movf	MaxRead3_H,0
	movwf	DestH
	movf	MaxRead3_L,0
	movwf	DestL
	call	Subtract
	btfss	STATUS,0
	call	MaxUpdate3
	return
;*******************************************************************
Min3						; Record Minimum value
	movf	AtoD3_L,0
	movwf	SourceL
	movf	AtoD3_H,0
	movwf	SourceH
	movf	MinRead3_H,0
	movwf	DestH
	movf	MinRead3_L,0
	movwf	DestL
	call	Subtract
	btfsc	STATUS,0
	call	MinUpdate3
	return	
;*******************************************************************
Max4						; Used to capature Maximum value
	movf	AtoD4_L,0
	movwf	SourceL
	movf	AtoD4_H,0
	movwf	SourceH
	movf	MaxRead4_H,0
	movwf	DestH
	movf	MaxRead4_L,0
	movwf	DestL
	call	Subtract
	btfss	STATUS,0
	call	MaxUpdate4
	return
;*******************************************************************
Min4						; Record Minimum value
	movf	AtoD4_L,0
	movwf	SourceL
	movf	AtoD4_H,0
	movwf	SourceH
	movf	MinRead4_H,0
	movwf	DestH
	movf	MinRead4_L,0
	movwf	DestL
	call	Subtract
	btfsc	STATUS,0
	call	MinUpdate4
	return	
;********************************************************************
Subtract	; a 16 bit subtraction sub program.  Source - Dest.  
			; will return a STATUS,0=1 if a negative result is produced.
			; the high order byte is in "H" the low order byte is in "L"
	movf	SourceL,W	
	subwf	DestL,1		
	movf	SourceH,W	
	btfss	STATUS,C
	incfsz	SourceH,W	
	subwf	DestH,1		
	return
;******************************************************************	
MaxUpdate1
	movf	AtoD1_L,0	; the CurrentReading lower bits register.
	movwf	MaxRead1_L	; move the maxium presure reading lower bits to _
	movf	AtoD1_H,0	; move current presure reading lower bits back into W register.
	movwf	MaxRead1_H	;skip this step else replace maxRead with CurrentRead
	return
;*******************************************************************
MinUpdate1
	movf	AtoD1_L,0	; the CurrentReading lower bits register.
	movwf	MinRead1_L	; move the maxium presure reading lower bits to _
	movf	AtoD1_H,0	; move current presure reading lower bits back into W register.
	movwf	MinRead1_H	;skip this step else replace maxRead with CurrentRead
	return
;******************************************************************
MaxUpdate2
	movf	AtoD2_L,0	; the CurrentReading lower bits register.
	movwf	MaxRead2_L	; move the maxium presure reading lower bits to _
	movf	AtoD2_H,0	; move current presure reading lower bits back into W register.
	movwf	MaxRead2_H	;skip this step else replace maxRead with CurrentRead
	return
;*******************************************************************
MinUpdate2
	movf	AtoD2_L,0	; the CurrentReading lower bits register.
	movwf	MinRead2_L	; move the maxium presure reading lower bits to _
	movf	AtoD2_H,0	; move current presure reading lower bits back into W register.
	movwf	MinRead2_H	;skip this step else replace maxRead with CurrentRead
	return
;******************************************************************
MaxUpdate3
	movf	AtoD3_L,0	; the CurrentReading lower bits register.
	movwf	MaxRead3_L	; move the maxium presure reading lower bits to _
	movf	AtoD3_H,0	; move current presure reading lower bits back into W register.
	movwf	MaxRead3_H	;skip this step else replace maxRead with CurrentRead
	return
;*******************************************************************
MinUpdate3
	movf	AtoD3_L,0	; the CurrentReading lower bits register.
	movwf	MinRead3_L	; move the maxium presure reading lower bits to _
	movf	AtoD3_H,0	; move current presure reading lower bits back into W register.
	movwf	MinRead3_H	;skip this step else replace maxRead with CurrentRead
	return
;******************************************************************
MaxUpdate4
	movf	AtoD4_L,0	; the CurrentReading lower bits register.
	movwf	MaxRead4_L	; move the maxium presure reading lower bits to _
	movf	AtoD4_H,0	; move current presure reading lower bits back into W register.
	movwf	MaxRead4_H	;skip this step else replace maxRead with CurrentRead
	return
;*******************************************************************
MinUpdate4
	movf	AtoD4_L,0	; the CurrentReading lower bits register.
	movwf	MinRead4_L	; move the maxium presure reading lower bits to _
	movf	AtoD4_H,0	; move current presure reading lower bits back into W register.
	movwf	MinRead4_H	;skip this step else replace maxRead with CurrentRead
	return
;******************************************************************		
;*************************MORSE Code***********************************
one
	call	dit
	call	dah
	call	dah
	call	dah
	call	dah
	call	pause
	return
two					; 2 and J
	call	dit
_J	call	dit
	call	dah
	call	dah
	call	dah
	call	pause
	return
three				; 3 amd W
	call	dit
	call	dit
_W	call	dit
	call	dah
	call	dah
	call	pause
	return
four				; and A, U, V
	call	dit
_V	call	dit
_U	call	dit
_A	call	dit
	call	dah
	call	pause
	return
five				; and H, S, I, E
	call	dit
_H	call	dit
_S	call	dit
_I	call	dit
_E	call	dit
	call	pause
	return
six
	call	dah
	call	dit
	call	dit
	call	dit
	call	dit
	call	pause
	return
seven				; 7 and B
	call	dah
_B	call	dah
	call	dit
	call	dit
	call	dit
	call	pause
	return
eight				; and Z and D
	call	dah
_Z	call	dah
_D	call	dah
	call	dit
	call	dit
	call	pause
	return
nine				; and N, G
	call	dah
	call	dah
_G	call	dah
_N	call	dah
	call	dit
	call	pause
	return
zero				; and O, M, T
	call	dah
	call	dah
_O	call	dah
_M	call	dah
_T	call	dah
	call	pause
	return	
_C
	call	dah
	call	dit
	call	dah
	call	dit
	call	pause
	return
_F					;F and R
	call	dit
_R	call	dit
	call	dah
	call	dit
	call	pause
	return
_K
	call	dah			;K
	call	dit	
	call	dah
	call	pause
	return
_L
	call	dit			;L
	call	dah
	call	dit
	call	dit
	call	pause
	return
_P						; P
	call	dit
	call	dah
	call	dah
	call	dit
	call	pause
	return
_Q
	call	dah
	call	dah
	call	dit
	call	dah
	call	pause
	return
_X
	call	dah
	call	dit
	call	dit
	call	dah
	call	pause
	return
_Y
	call	dah
	call	dit
	call	dah
	call	dah
	call	pause
	return
;**************************************************************
	END
