;	ファイル名：cwdecode.asm          1998.11.15  完成1998.12.21
;                                               jk1xkp
;      参考ｿﾌﾄ：geneLo
;	使用環境
;	ＰＩＣ		PIC16F84-10/P（それ以外にも応用可能）
;                         LCD 表示のアドレス
; |01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20(桁）	
;---------------------------------------------------------------------
;1|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13
;2|40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53
;3|14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 
;4|54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67

;		液晶LCD		20字＊４列 ，ＳＣ2004Ｃ等
;		クロック	10.0MHz付近
;
;
;ピンアサイン：（電源・クリスタル・コントラスト調整ＶＲ等の配線は省略）
;
;	ＰＩＣ１６Ｆ８４		液晶モジュール
;	()内はピン番号
;	RA4(04);R10kでpull up	＝	E(ENABLE)
;	RA3(02)			＝	RS 
;	GND(0V)			＝	R/~W
;	RB0(06)			＝	DB4 
;	RB1(07)			＝	DB5
;	RB2(08)			＝	DB6
;	RB3(09)			＝	DB7
;	GND(0V)			＝	DB3
;	GND(0V)			＝	DB2
;	GND(0V)			＝	DB1
;	GND(0V)			＝	DB0
;	Ra2(01)=CW decoder  input
;	Ra0(17)＝	 no use
;	Ra1(18)＝	送信時 L にするとdecoder の動きを停止する。
;	RB4(10)＝	スピード アップ 
;	RB5(11)＝	スピード ダウン
;	RB6(12)＝	欧文／和文の切り替え
;	RB7(13)	＝	和文／欧文／スピードのAUTO／MANUAL切り替え        
;  ◆クロックは10MHzを想定してあります。それ以外のクロックでは
;	ウェイト数を多少変更してください。
;  ◆モールスから文字への変換は、「モールス練習機能付きエレキーの製作、
;                          p121 CQ Mar.1998 JR7RAR  を参照した。
;  ８bit code  1 0 1 0 0  1 0 0   最初の５桁が符号を表す  ﾂｰﾄﾂｰﾄ
;              			最後の３桁が符号長を表す ４個
;                             よってこれは、「Ｃ」を表す。
;   	1 0 1 1 0  1 1 1  符号長 ７個の場合は、6個目が長点 ﾂｰ を表す「(」
;       0 0 1 1 0  1 1 0  符号長 6個の場合は、6個目が短点 ﾄ を表す 「？」
;        
		.include	16f84.h		; ここでは１６Ｆ８４を選択

		.osc		hs		; オシレータＸＴ
		.wdt		off		; ウォッチドッグＯＦＦ
		.pwrt		on		; パワーアップタイマＯＮ
		.protect	off		; プロテクトＯＦＦ


E		equ	ra.4
RS		equ	ra.3
MC		equ	ra.2		;Morse Code
TX		equ	ra.1		;TX時に Ｌとなるとdecodor停止
		org	0ch

wait_cn		ds	1
wait_cn2	ds	1
wait_ln		ds	1	;LCD行変え時ではない時の待ち時間調整
cn		ds	1
;   --------------- adress 10&H  
cm		ds	1	
d4		ds	1
d8		ds	1
nn		ds	1
splegt		ds 	1	;MORSEcodeの全長に含まれるspace長個数
dsppos		ds	1	;LCDの表示位置；アドレス 1-35
auto		ds	1	;自動時auto=41h[A],手動時auto=4dh[M]
engl		ds	1	;英文時engl=45h[E],和文時engl=4ah[J]
spd		ds	1	;スピード ＷＰＭ 5-40  1word=5文字
n		ds	1	;1長点（短点）の間にｶｳﾝﾄする10msﾊﾟﾙｽの数
				;n=60*100bps/(spd*5word*10spaces)
				;よって n=120/spd  1文字＝10spaces換算 
letr		ds	1	;cwcodeに対応する文字コード
val		ds	1
dec		ds	2
dec_top
val_m		ds	1
cwcd		ds	1	;符号を表す数値 ex.A=01000 010b
;   -------------- adress 20&H
dash		ds	1	;dash長を10ms毎のﾊﾟﾙｽ数で数える
puls_n		ds	1	;MORSEcode全長を10ms毎のﾊﾟﾙｽ数で数える
puls		ds	1	;
length		ds	1	;１MORSEcodeに含まれる長点、短点の合計数
space		ds	1	;space長を10ms毎のﾊﾟﾙｽ数で数える
n2		ds	1	;n2=n*2
n5		ds	1	;n5=n*5
oldRb		ds	1	;port Bの過去の値
newRb		ds	1
ryfig		ds	1	;RTTY信号がfigure codeの時1。letter code=0
rycd		ds	1
poi		ds	1	;相対ポインタカウンタ
cd		ds	1	;ﾁｬｯﾀﾘﾝｸﾞ防止したＭＣ信号
tmp_cd		ds	1	;MC信号２回続けてH？ L?
;    ------------- adress 30&H

		org	0
		
		goto	lcdstart
		org	4

tableJ		addwf	pc,1
		retlw	9fh	;poi=00h/letr=26h;ﾂﾄﾄﾂﾂﾂ;英->和切替え

		retlw	06h	;poi=01h/letr=27h;"HH=[']で返す"
		retlw	0b7h	;poi=02h/letr=28h;"（" 和英切り替え
		retlw	4eh	;poi=03h/letr=29h;"）"	

		incf	poi,1	;poi=04h/letr=2ah
					
		incf	poi,1	;poi=05h/letr=2bh
		incf	poi,1	;poi=06h/letr=2ch

		incf	poi,1	;poi=07h/letr=2dh
		retlw	57h	;poi=08h/letr=2eh;"。"	
		incf	poi,1	;poi=09h/letr=2fh

		retlw	0fdh	;poi=0ah/letr=30h;"0"
		retlw	7dh	;poi=0bh/letr=31h;"1"
		retlw	3dh	;poi=0ch/letr=32h;"2" 

		retlw	1dh	;poi=0dh/letr=33h;"3" 
		retlw	0dh	;poi=0eh/letr=34h;"4"
		retlw	05h	;poi=0fh/letr=35h;"5"

		retlw	85h	;poi=10h/letr=36h;"6" 
		retlw	0c5h	;poi=11h/letr=37h;"7"	
		retlw	0e5h	;poi=12h/letr=38h;"8" 

		retlw	0f5h	;poi=13h/letr=39h;"9"
		retlw	15h	;poi=14h/letr=3ah;ﾄﾄﾄﾂﾄ":"
		incf	poi,1	;poi=15h/letr=3bh		

		incf	poi,1	;poi=16h/letr=3ch
		incf	poi,1	;poi=17h/letr=3dh
		incf	poi,1	;poi=18h/letr=3eh

		retlw	36h	;poi=19h/letr=3fh;"?"
		retlw	17h	;poi=1ah/letr=40h;"@"VA

		movlw	83h	;p=1bh	;letr+poi=0a2h
		movwf	letr	;p=1ch	; 83h+1fh=0a2h
		movlw	1fh	;p=1dh
		movwf	poi	;p=1eh	
		
		retlw	56h	;poi=1fh/letr=0a2h;"「"
		incf	poi,1	;poi=20h/letr=0a3h
		incf	poi,1	;poi=21h/letr=0a4h

		incf	poi,1	;poi=22h/letr=0a5h
		retlw	74h	;poi=23h/letr=0a6h;"ヲ"	
		incf	poi,1	;poi=24h/letr=0a7h

		incf	poi,1	;poi=25h/letr=0a8h
		incf	poi,1	;poi=26h/letr=0a9h
		retlw	86h	;p=27h/letr=0aah+20h;"ハ゜" 濁音
		
		retlw	0cch	;p=28h/letr=0abh+20h;"ヒ゜" 濁音
		retlw	0c6h	;p=29h/letr=0ach+20h;"フ゜" 濁音
		incf	poi,1	;poi=2ah/letr=0adh;X

		incf	poi,1	;poi=2bh/letr=0aeh;X
		incf	poi,1	;poi=2ch/letr=0afh
		retlw	6dh	;poi=2dh/letr=0b0h;"−" 
		
		retlw	0ddh	;poi=2eh/letr=0b1h;"ア"
		retlw	42h	;poi=2fh/letr=0b2h;"イ" 
		retlw	23h	;poi=30h/letr=0b3h;"ウ"

		retlw	0bdh	;poi=31h/letr=0b4h;"エ"
		retlw	45h	;poi=32h/letr=0b5h;"オ" 
		retlw	44h	;poi=33h/letr=0b6h;"カ"

		retlw	0a5h	;poi=34h/letr=0b7h;"キ" 
		retlw	14h	;poi=35h/letr=0b8h;"ク"	
		retlw	0b4h	;poi=36h/letr=0b9h;"ケ"

		retlw	0f4h	;poi=37h/letr=0bah;"コ" 
		retlw	0adh	;poi=38h/letr=0bbh;"サ"
		retlw	0d5h	;poi=39h/letr=0bch;"シ" 

		retlw	0edh	;poi=3ah/letr=0bdh;"ス"
		retlw	75h	;poi=3bh/letr=0beh;"セ" 
		retlw	0e4h	;poi=3ch/letr=0bfh;"ソ"

		retlw	82h	;poi=3dh/letr=0c0h;"タ"
		retlw	24h	;poi=3eh/letr=0c1h;"チ"
		retlw	64h	;poi=3fh/letr=0c2h;"ツ"

		retlw	5dh	;poi=40h/letr=0c3h;"テ" 
		retlw	25h	;poi=41h/letr=0c4h;"ト"
		retlw	43h	;poi=42h/letr=0c5h;"ナ"	

		retlw	0a4h	;poi=43h/letr=0c6h;"ニ"
		retlw	04h	;poi=44h/letr=0c7h;"ヌ"
		retlw	0d4h	;poi=45h/letr=0c8h;"ネ" 

		retlw	34h	;poi=46h/letr=0c9h;"ノ"
		retlw	84h	;poi=47h/letr=0cah;"ハ" 
		retlw	0cdh	;poi=48h/letr=0cbh;"ヒ" 
		
		retlw	0c4h	;poi=49h/letr=0cch;"フ"
		retlw	01h	;poi=4ah/letr=0cdh;"ヘ" 
		retlw	83h	;poi=4bh/letr=0ceh;"ホ"

		retlw	94h	;poi=4ch/letr=0cfh;"マ"
		retlw	2dh	;poi=4dh/letr=0d0h;"ミ"
		retlw	81h	;poi=4eh/letr=0d1h;"ム"

		retlw	8dh	;poi=4fh/letr=0d2h;"メ"
		retlw	95h	;poi=50h/letr=0d3h;"モ"
		retlw	63h	;poi=51h/letr=0d4h;"ヤ" 

		retlw	9dh	;poi=52h/letr=0d5h;"ユ"
		retlw	0c2h	;poi=53h/letr=0d6h;"ヨ"
		retlw	03h	;poi=54h/letr=0d7h;"ラ"

		retlw	0c3h	;poi=55h/letr=0d8h;"リ"
		retlw	0b5h	;poi=56h/letr=0d9h;"ル"
		retlw	0e3h	;poi=57h/letr=0dah;"レ"	

		retlw	54h	;poi=58h/letr=0dbh;"ロ"
		retlw	0a3h	;poi=59h/letr=0dch;"ワ"
		retlw	55h	;poi=5ah/letr=0ddh;"ン"

		retlw	02h	;poi=5bh/letr=0deh;"゛" 濁音
		retlw	35h	;poi=5ch/letr=0dfh;"゜" 半濁音	

		retlw	46h	;poi=5dh/letr=0e0h;"カ゜" 濁音
		retlw	0a6h	;poi=5eh/letr=0e1h;"キ゜" 濁音
		retlw	16h	;poi=5fh/letr=0e2h;"ク゜" 濁音

		retlw	0b6h	;poi=60h/letr=0e3h;"ケ゜" 濁音
		retlw	0f6h	;poi=61h/letr=0e4h;"コ゜" 濁音
		retlw	0aeh	;poi=62h/letr=0e5h;"サ゜" 濁音

		retlw	0d6h	;poi=63h/letr=0e6h;"シ゜" 濁音
		retlw	0eeh	;poi=64h/letr=0e7h;"ス゜" 濁音
		retlw	76h	;poi=65h/letr=0e8h;"セ゜" 濁音

		retlw	0e6h	;poi=66h/letr=0e9h;"ソ゜" 濁音
		incf	poi,1	;poi=67h/letr=0eah;タX
		incf	poi,1	;poi=68h/letr=0ebh;チX

		retlw	66h	;poi=69h/letr=0ech;"ツ゜" 濁音
		retlw	5eh	;poi=6ah/letr=0edh;"テ゜" 濁音
		retlw	26h	;poi=6bh/letr=0eeh;"ト゜" 濁音

		movlw	04h		;04h+26h=2ah="*"
		movwf	poi		;判読不能を表示する文字
		movf	cwcd,0		
		ret			

tableE		addwf	pc,1
		retlw	9fh	;poi=00h/letr=26h;"&";英->和切替え
		incf	poi,1	;poi=01h/letr=27h
		incf	poi,1	;poi=02h/letr=28h
		retlw	4eh	;poi=03h/letr=29h;"）";英->和切替え	
		incf	poi,1	;poi=04h/letr=2ah					
		retlw	55h	;poi=05h/letr=2bh;"+"
		retlw	0cfh	;poi=06h/letr=2ch;","
		retlw	87h	;poi=07h/letr=2dh;"-"
		retlw	57h	;poi=08h/letr=2eh;"."
		retlw	95h	;poi=09h/letr=2fh;"/"
		
		retlw	0fdh	;poi=0ah/letr=30h;"0"
		retlw	7dh	;poi=0bh/letr=31h;"1"
		retlw	3dh	;poi=0ch/letr=32h;"2" 

		retlw	1dh	;poi=0dh/letr=33h;"3" 
		retlw	0dh	;poi=0eh/letr=34h;"4"
		retlw	05h	;poi=0fh/letr=35h;"5"

		retlw	85h	;poi=10h/letr=36h;"6" 
		retlw	0c5h	;poi=11h/letr=37h;"7"	
		retlw	0e5h	;poi=12h/letr=38h;"8" 

		retlw	0f5h	;poi=13h/letr=39h;"9"
		retlw	06h	;poi=14h/letr=3ah;"HH=[:]で返す"
		retlw	7eh	;poi=15hletr=3bh;[;]
		retlw	0b5h	;poi=16h/letr=3ch;"<"

		retlw	8dh	;poi=17h/letr=3dh;"="BT
		retlw	0b7h	;poi=18h/letr=3eh;">"
		retlw	36h	;poi=19h/letr=3fh;"?"
		retlw	17h	;poi=1ah/letr=40h;"VA"=[@]で返す"
		retlw	42h	;poi=1bh/letr=41h;"A"
		retlw	84h	;poi=1ch/letr=42h;"B"
		retlw	0a4h	;poi=1dh/letr=43h;"C"	

		retlw	83h	;poi=1eh/letr=44h;"D"
		retlw	01h	;poi=1fh/letr=45h;"E"
		retlw	24h	;poi=20h/letr=46h;"F"
			
		retlw	0c3h	;poi=21h/letr=47h;"G"
		retlw	04h	;poi=22h/letr=48h;"H"
		retlw	02h	;poi=23h/letr=49h;"I"

		retlw	74h	;poi=24h/letr=4ah;"J"
		retlw	0a3h	;poi=25h/letr=4bh;"K"
		retlw	44h	;poi=26h/letr=4ch;"L"

		retlw	0c2h	;poi=27h/letr=4dh;"M"
		retlw	82h	;poi=28h/letr=4eh;"N"
		retlw	0e3h	;poi=29h/letr=4fh;"O"
	 		
		retlw	64h	;poi=2ah/letr=50h;"P"
		retlw	0d4h	;poi=2bh/letr=51h;"Q"
		retlw	43h	;poi=2ch/letr=52h;"R"
			
		retlw	03h	;poi=2dh/letr=53h;"S"
		retlw	81h	;poi=2eh/letr=54h;"T"
		retlw	23h	;poi=2fh/letr=55h;"U"
					
		retlw	14h	;poi=30h/letr=56h;"V"
		retlw	63h	;poi=31h/letr=57h;"W"
		retlw	94h	;poi=32h/letr=58h;"X"
		
		retlw	0b4h	;poi=33h/letr=59h;"Y"
		retlw	0c4h	;poi=34h/letr=5ah;"Z"

		movlw	04h		;04h+26h=2ah="*"
		movwf	poi		;判読不能を表示する文字
		movf	cwcd,0		
		ret	

rytbl		addwf	pc,1
		retlw	08h	;poi=00h/letr=20h;" "space
		retlw	2dh	;poi=01h/letr=21h;"!"
		retlw	23h	;poi=02h/letr=22h;[ " ]
		retlw	0bh	;poi=03h/letr=23h;"#"

		retlw	25h	;poi=04h/letr=24h;"$"
		retlw	07h	;poi=05h/letr=25h;"%"
		retlw	17h	;poi=06h/letr=26h;"&"

		retlw	35h	;poi=07h/letr=27h;"'"
		retlw	3dh	;poi=08h/letr=28h;"（" 
		retlw	13h	;poi=09h/letr=29h;"）"	

		incf	poi,1	;poi=0ah/letr=2ah
		incf	poi,1	;poi=0bh/letr=2bh
		retlw	0dh	;poi=0ch/letr=2ch;","

		retlw	31h	;poi=0dh/letr=2dh"-"
		retlw	0fh	;poi=0eh/letr=2eh;"."	
		retlw	2fh	;poi=0fh/letr=2fh;"/"

		retlw	1bh	;poi=10h/letr=30h;"0"
		retlw	3bh	;poi=11h/letr=31h;"1"
		retlw	33h	;poi=12h/letr=32h;"2" 

		retlw	21h	;poi=13h/letr=33h;"3" 
		retlw	15h	;poi=14h/letr=34h;"4"
		retlw	03h	;poi=15h/letr=35h;"5"

		retlw	2bh	;poi=16h/letr=36h;"6" 
		retlw	39h	;poi=17/letr=37h;"7"	
		retlw	19h	;poi=18h/letr=38h;"8" 

		retlw	07h	;poi=19h/letr=39h;"9"
		retlw	1dh	;poi=1ah/letr=3ah ":"
		retlw	1fh 	;poi=1bh/letr=3bh ";"

		incf	poi,1	;poi=1ch/letr=3ch
		incf	poi,1	;poi=1dh/letr=3dh
		incf	poi,1	;poi=1eh/letr=3eh

		retlw	27h	;poi=1fh/letr=3fh;"?"
		incf	poi,1	;poi=20h/letr=40h

		retlw	30h	;poi=21h/letr=41h;"A"
		retlw	26h	;poi=22h/letr=42h;"B"
		retlw	1ch	;poi=23h/letr=43h;"C"	

		retlw	24h	;poi=24h/letr=44h;"D"
		retlw	20h	;poi=25h/letr=45h;"E"
		retlw	2ch	;poi=26h/letr=46h;"F"
			
		retlw	16h	;poi=27h/letr=47h;"G"
		retlw	0ah	;poi=28h/letr=48h;"H"
		retlw	18h	;poi=29h/letr=49h;"I"

		retlw	34h	;poi=2ah/letr=4ah;"J"
		retlw	3ch	;poi=2bb/letr=4bh;"K"
		retlw	12h	;poi=2ch/letr=4ch;"L"

		retlw	0eh	;poi=2dh/letr=4dh;"M"
		retlw	0ch	;poi=2eh/letr=4eh;"N"
		retlw	05h	;poi=2fh/letr=4fh;"O"
	 		
		retlw	1ah	;poi=30h/letr=50h;"P"
		retlw	3ah	;poi=31h/letr=51h;"Q"
		retlw	14h	;poi=32h/letr=52h;"R"
			
		retlw	28h	;poi=33h/letr=53h;"S"
		retlw	02h	;poi=34h/letr=54h;"T"
		retlw	38h	;poi=35h/letr=55h;"U"
					
		retlw	1eh	;poi=36h/letr=56h;"V"
		retlw	32h	;poi=37h/letr=57h;"W"
		retlw	2eh	;poi=38h;"X"
		
		retlw	2ah	;poi=39h/letr=59h;"Y"
		retlw	22h	;poi=3ah/letr=5ah;"Z"

		retlw	05h	;poi=3bh/letr=5bh;"["Car.Return
		retlw	10h	;poi=3ch/letr=5ch;"\"Line Feed
		retlw	04h	;poi=3dh/letr=5dh;"]"Car.Return

		retlw	11h	;poi=3eh/letr=5eh;"^"Line Feed
		retlw	09h	;poi=3fh/letr=5fh;"_"spaceの代わり 
		movf	rycd,0	;p=40h/letr=60h
		ret
		
titlefont	addwf	pc,1
		retlw	52h		;"R" dsppos=0
		retlw	45h		;"E" dsppos=1
		retlw	41h		;"A" dsppos=2

		retlw	44h		;"D" dsppos=3
		retlw	45h		;"E" dsppos=4
		retlw	52h		;"R" dsppos=5
		retlw	20h		;"_" dsppos=6		

		retlw	4ah		;"J" dsppos=7
		retlw	4bh		;"K" dsppos=8
		retlw	31h		;"1" dsppos=9

		retlw	58h		;"X" dsppos=10
		retlw	4bh		;"K" dsppos=11
		retlw	50h		;"P" dsppos=12
		
		sleep

tablfontJ	movlw	26h
		movwf	letr		;letrに26hを入れる
					;poi=0ffh
tJ1		incf	poi,1
		movf	poi,0
		call	tableJ
		subwf	cwcd,0
		btfss	3.2
		goto	tJ1
		movf	poi,0
		addwf	letr,1
		ret

tablfontE	movlw	26h
		movwf	letr		;letrに26h-1を入れる
					;poi=0ffh
tE1		incf	poi,1
		movf	poi,0
		call	tableE
		subwf	cwcd,0
		btfss	3.2
		goto	tE1
		movf	poi,0
		addwf	letr,1
		ret

tablrtty	movlw	1fh
		movwf	letr		;letrに20h-1を入れる
		clr	poi
tR1		movf	poi,0
		call	rytbl
		incf	poi,1
		subwf	rycd,0
		btfss	3.2
		goto	tR1
		movf	poi,0
		addwf	letr,1
		ret				

title		mov	d4,#20h		;"_" 
		call	write_lcd4 
		mov	cn,#13
		clr	poi
title0		movf	poi,0		;以下READER_JK1XKP表示
		call	titlefont
		movwf	d4
		call	write_lcd4
		inc	poi
		djnz	cn,title0	
		ret

lcdstart	
		mov	wait_cn,#255
		call	wait_ms
		bsf	status,5	;バンク1
		movlw	01111111b	;Rbﾌﾟﾙｱｯﾌﾟ
		movwf	option
		bcf	status,5	;バンク0

		mov	!ra,#00000110b	; E,RSピン,ra0をOUTに
		mov	!rb,#11110000b	; DB3-DB0ピンをOUTに
		clr	ra
		clr	rb
		clr	cd
		mov	dsppos,#75
		bcf	rbpu		;Rbﾌﾟﾙｱｯﾌﾟ
		call	init_lcd	; ＬＣＤ初期化

		clrb	RS		; adress set
		mov	d4,#10000000b	;cursor 1行目の1桁目
		call	write_lcd4
		setb	RS		;以後文字表示
		
		btfss	portb,7		;RTTYへの分岐
		goto	rttylcd		

		mov	d4,#43h		;"C" 
		call	write_lcd4
		mov	d4,#57h		;"W"
		call	write_lcd4 
		
		call	title

		mov	auto,#41h		;自動auto=41h[A]	 
		mov	engl,#45h		;英文engl=45h[E]
		mov	spd,#15			;ｽﾋﾟｰﾄﾞspd=15wpm
		
		call	dev120_spd	;spd表示後、n=120/spdの計算をする。
		call	putmode		;330us	;LCD右下へ AE15  の表示		
main0		
		clr	cwcd
		clr	dash
		clr	puls_n	
		clr	length
		clr	space
		clr	splegt
		mov	cm,#8
						
main		call	cdchek
		btfsc	cd,0		;morse信号＝L？
		goto	main2
		call	pcheck
		btfss	TX
		goto 	main0
		goto	main		

main2		
		csb	length,#6
		goto	main9
		incf	dash,1
		incf	puls_n,1
		incf	length,1
		call	pcheck	

main3		call	cdchek
		btfss	cd,0		;morse信号＝H？
		goto	main4
		incf	dash,1
		incf	puls_n,1
		call	pcheck
		btfss	TX
		goto 	main0
		goto	main3

main4		
		incf	space,1
		incf	puls_n,1
		csa	n2,space
		goto	main6

		call	cdchek
		btfss	cd,0		;morse信号＝H？
		goto	main5
		call	dashwrite	
		call	spdwr
		call	pcheck
		
		goto 	main2

main5		
		call	pcheck
		btfss	TX
		goto 	main0
		goto	main4
		
main6		
		call	pcheck
		btfss	TX
		goto 	main0	
		call	dashwrite
		clr	dash
		clr	space		
		
main7		clr	c
		rlf	cwcd,1
		djnz	cm,main7
		btfsc	cwcd,2
		incf	length,1
		movlw	11111000b
		andwf	cwcd,0
		xorwf	length,0
		movwf	cwcd
		
		clr	poi
		decf	poi,1
		cse	engl,#45h
		goto	mn7a
		call	tablfontE
		goto	mn7b

mn7a		call 	tablfontJ
		csa	letr,#0a9h
		goto	J_df
		csb	letr,#0afh
		goto	J_df
		add	letr,#20h	;"ハ゜−ホ゜特殊設定"
		goto	mn7aa

J_df		csa	letr,#0dfh
		goto	mn7b
		sub	letr,#2ah	;"カ゜−ト゜特殊設定"
		
mn7aa		call	putfont
		mov	letr,#0deh	;odeh=" ゛" 半濁音
		
mn7b		call 	putfont
		cse	auto,#41h
		goto	$+3
		call	autchangeE
		call	autchangesp
		call	putmode		;330us;LCD右下AE15の表示
		
main8		call	cdchek
		btfsc	cd,0		;morse信号＝H？
		goto	main0

		incf	space,1
		call	pcheck
		btfss	TX
		goto	main0
	
		movf	n5,0		;csbe
		subwf	space,0
		btfss	3.0
		goto	main8	
		mov	letr,#20h	;"_"=20h

		csne	dsppos,#1
		goto	main0
		csne	dsppos,#21
		goto	main0
		csne	dsppos,#41
		goto	main0
		csne	dsppos,#61
		goto	main0
		call 	putfont 
		
		goto	main0

main9		csa	n2,space
		goto	main7
		incf	space,1
		call	pcheck
		btfss	TX
		goto	main0	

		call	cdchek
		btfss	cd,0
		goto	main9
		clr	space	
		goto	main9

cdchek		btfss	MC		;MCのﾁｬｯﾀﾘﾝｸﾞ防止
		bcf	c
		btfsc	MC
		bsf	c
		rlf	tmp_cd,1
		and	tmp_cd,#00000011b
		csne	tmp_cd,#00000011b
		bsf	cd,0
		csne	tmp_cd,#00000000b
		bcf	cd,0
		ret		

putfont		mov	d4,letr
		call	write_lcd4	;LCD表示終了
		csb	dsppos,#75
		clr	dsppos
		incf	dsppos,1
		mov	wait_ln,#80	; wait 80us；改行時に １usにリセット
		mov	cn,#20		;連続２０字を消す(#1,21,41)		

		csne	dsppos,#1
		goto 	ptline1

		csne	dsppos,#21
		goto 	ptline2

		csne	dsppos,#41
		goto 	ptline3
	
		csne	dsppos,#61
		goto 	ptline4

ptfnt1		mov	wait_cn,wait_ln
		call	wait_us		;wait 80us ; wait_cn =80
					;wait 1us when dsppos=1,21,41,61
		ret

ptline1		mov	val,#10000000b	;cursor 1行目
		goto	ptline

ptline2		mov	val,#11000000b	;cursor 2行目
		goto	ptline

ptline3		mov	val,#10010100b	;cursor 3行目
		goto	ptline

ptline4		mov	val,#11010100b	;cursor 4行目
		mov	cn,#15		;連続15字を消す
		goto	ptline

ptline		clrb	RS	
		mov	d4,val		;cursor 	
		call	write_lcd4	;cursor位置決め
		setb	RS		;以後文字表示
		
putspace	mov	d4,#20h		;連続 cn個の space=" " 記入
ptsp		call	write_lcd4
		djnz	cn,ptsp
				
		clrb	RS	
		mov	d4,val		;cursor 	
		call	write_lcd4	;cursor 頭に戻す
		setb	RS		;以後文字表示
		mov	wait_ln,#1		; wait 1us
		goto	ptfnt1


dashwrite	csa	n2,dash
		goto	das1
		bcf	c		;dot
		goto	das2
das1		bsf	c		;dash	
das2		rlf	cwcd,1
		decf	cm,1	
		ret

spdwr		csa	n2,dash
		goto	swr11
		add	splegt,#2	;dot
		goto	swr22
swr11		add	splegt,#4	;dash	
swr22		mov	puls,puls_n
		clr	dash
		clr	space
		ret


autchangeE	csne	cwcd,#10011111b		;9fh:ﾂﾄﾄﾂﾂﾂ 英->和文
		goto	changeJ
		csne	cwcd,#01001110b		;4eh:ﾄﾂﾄﾄﾂﾄ 英->和文
		goto	changeJ
		csne	cwcd,#10110111b		;b7h:ﾂﾄﾂﾂﾄﾂ 和->英文
		goto	changeE
		ret
		
autchangesp	csne	splegt,#0
		ret	
					 
devp_spl	clr	nn		;nn=puls/splegt
		mov	val,puls	;nn:短点相当長さでのパルス数 new n
p_sp1		movf	splegt,0
		subwf	val,1
		incf	nn,1
		csa	splegt,val
		goto	p_sp1	

		csae	nn,n
		goto	spdup
		
		movf	nn,0
		subwf	n,0
		btfss	3.0
		goto	spdwn
		ret

;
;		液晶初期化ルーチン
;
init_lcd
		mov	wait_cn,#15		; wait 15ms
		call	wait_ms
		clrb	RS			; RS='L'
		mov	d8,#00110000b
		call	write_lcd8
		mov	wait_cn,#5		; wait 4.1ms
		call	wait_ms
		
		mov	d8,#00110000b
		call	write_lcd8
		mov	wait_cn,#100		; wait 100us
		call	wait_us
		
		mov	d8,#00110000b
		call	write_lcd8		; 0 0 0011 (3)
		
		mov	d8,#00100000b
		call	write_lcd8		; 0 0 0010 (4bit)
		
		mov	d4,#00101000b	; duty,font set9:Function set
		call	write_lcd4

		mov	d4,#00001000b		; display off
		call	write_lcd4		

		clrb	RS		
		mov	d4,#00000001b	; クリアコマンド:Clear Display
		call	write_lcd4
		mov	wait_cn,#2	; クリアが終わるまで待つ
		call	wait_ms
		
		mov	d4,#00000110b		; entry mode set
		call	write_lcd4

		mov	d4,#00001110b		; display on,cursor on
		call	write_lcd4
		ret
;
;		４ビットモード専用液晶ライトルーチン
;
write_lcd4
		mov	d8,d4
		call	write_lcd8		;上位4ﾋﾞｯﾄ
		
		mov	d8,d4
		swap	d8
		call	write_lcd8		;下位４ﾋﾞｯﾄ
		ret

;
;		８ビットモード専用液晶ライトルーチン
;
write_lcd8
		swap	d8		;d8の上位4bitをRB3,2,1,0へ		
		mov	rb,d8			; ＲＢポートへ出力
		nop
		setb	E			; Ｅピンを'H'
		nop
		clrb	E
		
		mov	wait_cn,#40
							
wait_us					;μｓオーダーのウェイト
wait_us0
		djnz	wait_cn,wait_us0
		ret
		


;
;		ｍｓオーダーのウェイト
;
wait_ms
wait_ms0
		mov	wait_cn2,#0
wait_ms1
		nop	
		nop
		nop
		nop
		nop	
		nop
		nop
		
		djnz	wait_cn2,wait_ms1
		djnz	wait_cn,wait_ms0
		ret


pcheck					;portb 4-7 のcheck
					;Time=9.240ms
		movf	portb,0		
		andlw	11110000b
		movwf	newRb		;portb 4-7の内容をnewRbへ
		csne	newRb,oldRb	;portb 4-7が変化したか？
		goto	pckout	
		
checkmode	mov	oldRb,newRb
		btfss	oldRb,7		;A/Mのcheck
		call	chkAM	
		btfss	oldRb,6		;E/Jのcheck
		call	chkEJ	
		btfss	oldRb,5		;ｽﾋﾟｰﾄﾞﾀﾞｳﾝのcheck
		call	spdwn
		btfss	oldRb,4		;ｽﾋﾟｰﾄﾞｱｯﾌﾟのcheck
		call	spdup	
		call	putmode
		
pckout		mov	wait_cn,#9
		call	wait_ms		;Wait 9 msec
		ret

chkAM		cse	auto,#41h	;自動時auto=41h[A]	
		goto	$+3
		movlw	4dh		;手動auto=4dh[M]
		goto	$+2
		movlw	41h		;自動auto=41h[A]	
		movwf	auto
		ret

chkEJ		cse	engl,#45h	;英文時engl=45h[E]
		goto	$+3
changeJ		movlw	4ah		;和文へ変更engl=4ah[J]	
		goto	$+2
changeE		movlw	45h		;英文へ変更engl=45h[E]
		movwf	engl
chkEJ0		call	putmode		;300us
		ret

spdwn		csbe	spd,#5		
		decf	spd,1		;ｽﾋﾟｰﾄﾞﾀﾞｳﾝ時spd=spd-1
		goto	dev120_spd	;n=120/spd
		
spdup		csae	spd,#40		
		incf	spd,1		;ｽﾋﾟｰﾄﾞｱｯﾌﾟ時spd=spd+1
		goto	dev120_spd	;n=120/spd
		

dev120_spd	call	decim_sp	;70us;ﾓｰﾄﾞ・ｽﾋﾟｰﾄﾞの表示
		call	putmode		;340us
		
		clr	n		;n=120/spd
		mov	val,#120
devs0		movf	spd,0
		subwf	val,1
		incf	n,1
		csa	spd,val
		goto	devs0	

		mov	n2,n
		clr	c
		rlf	n2,1		;2*nを n2 に代入
		mov	n5,n2
		rlf	n5,1		;4*nを n5 に代入
		add	n5,n		;n5=4n+n
		ret


decim_sp	mov	val,spd		;spdを10進数変換
		mov	fsr,#dec_top		; 格納場所初期値
		call	devide			; 最下位変換
		call	devide			; 最上位変換
		ret
;
;		÷１０サブルーチン
;		１０で除算
;
devide
		mov	cn,#8			; 8ビットくり返し
		
		clr	val_m
devide0
		clc
		rl	val
		rl	val_m
		
		movlw	11110110b
		addwf	val_m,0
		btfsc	c
		mov	val_m,w
		btfsc	c
		inc	val
		
		djnz	cn,devide0
		
		dec	fsr
		mov	indirect,val_m		; 余り
		ret

putmode		clrb	RS		; adress set
		mov	d4,#11100100b	;cursor 4行目の17桁目
		call	write_lcd4
		setb	RS		;以後文字表示
		
		mov	d4,auto
		call	write_lcd4
		mov	d4,engl
		call	write_lcd4
		
		mov	d4,dec[0]	;スピード表示 ＷＰＭの 10の位	
		movlw	00110000b		;上位4ﾋﾞｯﾄに ３を入れる
		xorwf	d4,1
		call	write_lcd4
		mov	d4,dec[1]	;スピード表示 ＷＰＭの 1の位	
		movlw	00110000b		;上位4ﾋﾞｯﾄに ３を入れる
		xorwf	d4,1
		call	write_lcd4
		
dspset		csae	dsppos,#21
		goto	dsp01
		csae	dsppos,#41
		goto	dsp21
		csae	dsppos,#61
		goto	dsp41
dsp61		mov	val,#11010100b	 	;cursor 4行目
		add	val,dsppos
		sub	val,#61
		goto	dspset1	

dsp01		mov	val,#10000000b		;cursor 1行目
		add	val,dsppos
		sub	val,#1
		goto	dspset1			
dsp21		mov	val,#11000000b		;cursor 2行目
		add	val,dsppos
		sub	val,#21
		goto	dspset1			
dsp41		mov	val,#10010100b		;cursor 3行目
		add	val,dsppos
		sub	val,#41
		goto	dspset1	

dspset1		clrb	RS	
		mov	d4,val		;cursor 	
		call	write_lcd4	;cursor位置決め
		setb	RS		;以後文字表示
		ret

rttylcd		bsf	porta,0
		mov	d4,#52h		;"R" 
		call	write_lcd4
		mov	d4,#54h		;"T" 
		call	write_lcd4
		mov	d4,#54h		;"T" 
		call	write_lcd4
		mov	d4,#59h		;"Y" 
		call	write_lcd4

		call	title			

		mov	auto,#52h	;表示auto=52h[R]	 
		mov	engl,#59h	;表示engl=59h[Y]
		mov	spd,#45		;ｽﾋﾟｰﾄﾞspd=45bauds
		clr	ryfig

		call	ry45		;LCD右下へ" RY " の表示
		btfsc	MC
		goto	ry0
		call	baudchk		;LCD右下へ RY45  の表示
		goto	$-3		

ry0		call	putfont

ry1		call	baudchk
		
ry2		clr	rycd
		clr	dash
		mov	cm,#5
							
ry3		btfss	TX
		goto 	ry1
		btfss	MC		;rtty信号＝H？
		goto	ry1
		incf	dash,1
		mov	wait_cn,#1
		call	wait_ms

		mov	puls_n,puls
		add	puls_n,#2
		csa	dash,puls_n
		goto	ry3
ry33		btfsc	MC
		goto	ry33
		mov	puls_n,puls
		clr	c
		rrf	puls_n,1
		mov	wait_cn,puls_n
		call	wait_ms

		btfsc	MC
		goto	ry1

ry4		mov	wait_cn,puls	
		call	wait_ms
		btfss	MC
		bcf	c
		btfsc	MC
		bsf	c
		rlf	rycd,1
		djnz	cm,ry4

		mov	wait_cn,puls
		call	wait_ms
		btfss	MC
		goto	ry6
		clr	c
		rlf	rycd,1
		xor	rycd,ryfig
;		mov	rycd,#2ah	;test あと消す
		
ry5		call	tablrtty
		cse	letr,#60h	;rytblの最後の行番
		call	putfont
		
		bcf	rycd,0
		csne	rycd,#3eh	
		clr	ryfig		;Letters code
		csne	rycd,#08h	
		clr	ryfig		;unshift on space;Letters code
		
		csne	rycd,#36h
		bsf	ryfig,0		;Figures code/shift
		
		goto	ry1	

ry6		movf	rycd,0		;rtty信号でない時、無駄表示
		xorlw	11000000b	;｢ﾀ」−「ﾝ」の文字ｺｰﾄﾞへ
		movwf	d4		
		call	write_lcd4
		call	dspset
		goto	ry1	

baudchk				;portb 4 のcheck
		movf	portb,0		
		andlw	00010000b
		movwf	newRb		;portb 4の内容をnewRbへ
		csne	newRb,oldRb	;portb 4が変化したか？
		ret
		mov	oldRb,newRb
		
		btfsc	oldRb,4
		goto	ry80

		csne	spd,#45		;変化時RY50に書替え
		goto	ry50
		csne	spd,#50		;
		goto	ry56
		csne	spd,#56		;
		goto	ry74
		csne	spd,#74		;
		goto	ry45
		
ry45		mov	spd,#45	
		mov	puls,#22
		goto	ry80
ry50		mov	spd,#50
		mov	puls,#20
		goto	ry80
ry56		mov	spd,#56
		mov	puls,#18
		goto	ry80
ry74		mov	spd,#74
		mov	puls,#13

ry80		call	decim_sp	;LCD右下へ 
		call	putmode		;baud rateの表示
		ret
;    ------------------------------------








