;FastAVR Basic Compiler, ver. 2.0.1, by MicroDesign
;created 12:17:38 נל, 11-04-2001
;
.include	"C:\AVRTOOLS\FASTAVR\inc\2313DEF.INC"
;

		.DSEG
n:			.byte	 1

		
		.CSEG
.ORG	0
		rjmp	reset

reset:
		ldi		yl,low(RAMEND)
		out		SPL,yl
		sbiw	yl,32
		ldi		r24,low(RAMEND-95)
		ldi		xl,0x60
		clr		r3
		clr		r4
CRam:	st		X+,r4
		sbiw	r24,1
		brne	CRam
		ldi		zl,0x80
		out		MCUCR,zl
		rcall	LcdIni

;******	USERS BASIC CODE **********************

;-Line--0018----DdrB=&hff						' PortB is output for LEDs--
		ldi		zl,low(255)
		out		ddrb,zl

;-Line--0019----PortB=&hff                      ' turn LEDs Off--
		ldi		zl,low(255)
		out		portb,zl

;-Line--0020----Lcd "STK-200 Key() on LCD"		' wellcome message--
		ldi		zl,low(S00*2)
		ldi		zh,high(S00*2)
		rcall	_LSc

;-Line--0022----Do--
L0000:

;-Line--0023----n=Key()						' n becomes pressed key--
		rcall	_Key
		sts		n,zl

;-Line--0025----If n>0 Then 				' if key is pressed--
		lds		r24,n
		ldi		zl,low(0)
		inc		zl
		cp		r24,zl
		brlo	L0003
L0004:

;-Line--0026----Locate 2, 1: Lcd "Pressed Key:   "--
		ldi		r24,0xBF
		ldi		zl,low(1)
		add		r24,zl
		rcall	_LCtr
		ldi		zl,low(S01*2)
		ldi		zh,high(S01*2)
		rcall	_LSc

;-Line--0027----Locate 2,13: Lcd n		' display key code--
		ldi		r24,0xBF
		ldi		zl,low(13)
		add		r24,zl
		rcall	_LCtr
		lds		zl,n
		rcall	_B2Str
		rcall	_LBW

;-Line--0028----PortB=Not n			' key on LED also--
		lds		zl,n
		com		zl
		out		portb,zl

;-Line--0029----End If--
L0003:
L0002:

;-Line--0030----Loop--
		rjmp	L0000
L0001:

;****** END OF USER BASIC CODE ****************

; String constants:
S00:	.db	"STK-200 Key() on LCD", 0
S01:	.db	"Pressed Key:   ", 0


;////// ByteToStr /////////////////////////
_B2Str:	clt
		push	yl
		clr		zh

;////// ByteWordToStr /////////////////////
_BWS:	clr		r25
		ldi		r24,100
		rcall	_Asc
		ldi		r24,10
		rcall	_Asc
		subi	zl,-48
		st		-y,zl
		clr		zl
		st		-y,zl
		pop		yl
		mov		zl,yl
		ret

;////// TO Ascii //////////////////////////
_Asc:	ldi		r22,48
_Asc1:	cp		zl,r24
		cpc		zh,r25
		brlo	_Asc2
		sub		zl,r24
		sbc		zh,r25
		inc		r22
		brne	_Asc1
_Asc2:	brts	_Asc3
		cpi		r22,48
		breq	_Asc4
		set
_Asc3:	st		-y,r22
_Asc4:	ret

;////// LcdInit ///////////////////////////
LcdIni:	ldi		r24,0x38
		rcall	_LCtr
		ldi		r24,0x06
		rcall	_LCtr
		ldi		r24,0x0c
		rcall	_LCtr
		ldi		r24,0x01
		rcall	_LCtr
		ret

;////// Lcd Wait No bussy /////////////////
_LcdWt:	ldi		xh,0x80
		ldi		xl,0x00
		ld		r25,x
		rol		r25
		brcs	_LcdWt
		ret

;////// Lcd Control Write /////////////////
_LCtr:	rcall	_LcdWt
		st		x,r24
		ret

;////// Lcd Write /////////////////////////
_Lch:	rcall	_LcdWt
		ldi		xh,0xc0
		ldi		xl,0x00
		st		x,r24
		ret

;////// LCD Byte & Word ///////////////////
_LBW:	ld		r24,-z
		tst		r24
		breq	_LBW1
		rcall	_Lch
		rjmp	_LBW
_LBW1:	ret

;////// LCD string constants //////////////
_LSc:	lpm
		tst		r0
		breq	_LSc1
		mov		r24,r0
		rcall	_Lch
		adiw	zl,1
		rjmp	_LSc
_Lsc1:	ret

;////// _Waitms ///////////////////////////
_Wms:	ldi		r20,20
_Wms1:	ldi		r21,66
_Wms2:	dec		r21
		brne	_Wms2
		dec		r20
		brne	_Wms1
		dec		zl
		brne	_Wms
		ret

;////// _waitus ///////////////////////////
_wus:	ldi		r22,12
_wus1:	dec		r22
		brne	_wus1
		dec		zl
		brne	_wus
		ret

;////// Key ///////////////////////////
_Key:	in		zl,ddrd
		ldi		r24,255
		com		r24
		and		zl,r24
		out		ddrd,zl
		in		zl,portd
		ori		zl,255
		out		portd,zl
		in		r24,pind
		andi	r24,255
		cpi		r24,255
		breq	_Key1
		ldi		zl,20
		rcall	_Wms
		in		zl,pind
		andi	zl,255
		cp		r24,zl
		brne	_Key1
		com		zl
		andi	zl,255
		ret

;////// NoKey /////////////////////////
_NKey:	in		r24,pind
		andi	r24,255
		cpi		r24,255
		breq	_Kout
		rjmp	_NKey
_Key1:	clr		zl
_Kout:	ret

;System Global Variables: 0  bytes
;User   Global Variables: 1  bytes
