				;file c:\users\edenlai\desktop\esk310\matrix keyboard\Matrix keyboard.c
				;1	/************************************************************
				;2	* PROGRAM : Matrix keyboard
				;3	* Wire Connection:
				;4	*   ESK303 (VDD) connects to ESK310 (5V)
				;5	*   ESK303 (GND) connects to ESK310 (GND)
				;6	*   ESK303 (C0~C6) connects to ESK310 ((JP7)a~g)
				;7	*   ESK303 (VDD) connects to ESK310 ((JP8)4)
				;8	*   ESK303 (F0~F7) connects to ESK310 ((JP24)0~7)
				;9	* Created on 2021/08/27 by Best Modules Corp.
				;10	*************************************************************/
				;11	#include <HT66F2390.h>
				;12	#include "setup.h"
				;13	
				;14	void main()
				;15	{	
				@code .SECTION 'CODE'
				include HT66F2390.inc
0000	1F0B	clr     PBP
0001	2802	jmp     _main_startup1
				@start .SECTION 'CODE'
				_main_startup1:
				@start .SECTION 'CODE'
0002	0F00	mov     a, 0H
0003	008B	mov     PBP, a
0004	2841	jmp     _main
0005	063F	and     a, LVRC
0006	4F5B	dc	04F5BH
0007	6D66	jmp     D66H
0008	077D	mov     a, FD3H
0009	677F	call    F7FH
000A	7C77	sz      ra[119].0
000B	5E58	snz     ra[88]
000C	7179	set     ra[121].2
000D	1483	inc     MP1L
				L000E:
000E	3F84	sz      MP1H.7
000F	2812	jmp     L0012
0010	0702	mov     a, [02H]
0011	0003	ret
				L0012:
0012	1B04	rrca    MP1H
0013	0E3F	and     a, 3FH
0014	0089	mov     TBHP, a
0015	1B03	rrca    MP1L
0016	0087	mov     TBLP, a
0017	1D05	tabrd   ACC
0018	3C0A	sz      C
0019	0708	mov     a, TBLH
001A	0003	ret
				;16		setup();
				_main:
				_main:
0041	203C	call    _setup
				;17		unsigned char i,KEY;
				;18		
				;19		while(1)
				;20		{	 
				;21			do
				;22			{ 	
				;23				KEY=Scan();
				_L20:
0042	201B	call    _Scan
0043	4080	mov     ra, a
				;24			} while(KEY==16);	   
0044	4700	mov     a, ra
0045	0A10	sub     a, 10H
0046	3D0A	sz      Z
0047	2842	jmp     _L20
				;25											
				;26			SEGMENT_Port=SEGMENT_TAB[KEY];				      	//Display key value
0048	0F0A	mov     a, AH
0049	4300	add     a, ra
004A	0083	mov     MP1L, a
004B	0F80	mov     a, 80H
004C	1F04	clr     MP1H
004D	1384	adcm    a, MP1H
004E	200E	call    L000E
004F	009B	mov     PC, a
				;27		}	
0050	2842	jmp     _L20
0051	2851	jmp     $
				L0D66:
				org	0d66h
0D66	0000	nop
				L0F7F:
				org	0f7fh
0F7F	0000	nop
				;28	}
				;file c:\users\edenlai\desktop\esk310\matrix keyboard\setup.h
				;1	#define		SEGMENT_Port	_pc
				;2	#define		SEGMENT_PortC	_pcc
				;3	#define		KEY_Port		_pf						
				;4	#define		KEY_PortC		_pfc						
				;5	#define		KEY_PortPU		_pfpu	
				;6	const unsigned char SEGMENT_TAB[] = 
				;7										{0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07
				;8										,0x7F,0x67,0x77,0x7C,0x58,0x5E,0x79,0x71};	//Seven Segment Table
				;9	
				;10	void setup()										
				;11	{	
				;12		_wdtc=0xAB;								    	//Turn off the watchdog timer
				_setup:
				_setup:
003C	0FAB	mov     a, ABH
003D	00BE	mov     WDTC, a
				;13		SEGMENT_Port=0;									//Set SEGMENT_Port as 0
003E	1F1B	clr     PC
				;14		SEGMENT_PortC=0;								//Set SEGMENT_Port to output mode
003F	1F1C	clr     PCC
0040	0003	ret
				;15	}
				;16	
				;17	unsigned char Scan()
				;18	{	
				;19		unsigned char i,KEY=0;
0021	5F00	clr     ra
				;20		KEY_Port=0xFE;					         		//KEYPort[3:0]=1110
				_Scan:
				_Scan:
001B	0FFE	mov     a, FEH
001C	00A4	mov     PF, a
				;21		KEY_PortC=0xF0; 								//KEY_PortC[7:4]/[3:0] as input/output attributes
001D	0FF0	mov     a, F0H
001E	00A5	mov     PFC, a
				;22		KEY_PortPU=0xF0;			   					//Enable KEY_PortPU[7:4] boost resistance
001F	0FF0	mov     a, F0H
0020	00A6	mov     PFPU, a
				;23		for(i=0;i<4;i++)								
0036	4700	mov     a, ra
0037	0A10	sub     a, 10H
0038	390A	snz     Z
0039	2822	jmp     _L7
				;24		{	
				;25			if(!(KEY_Port & 0x80)) break;		      	//Check (KEY_Port[4]) is pressed
				_L7:
0022	3BA4	snz     PF7
0023	283A	jmp     _L3
				;26			KEY++;										//KEY+1
0026	5480	inc     ra
0027	283A	jmp     _L3
				;27			if(!(KEY_Port & 0x40)) break;			   	//Check (KEY_Port[5]) is pressed
0024	3F24	sz      PF6
0025	2828	jmp     _L4
				;28			KEY++;										//KEY+1
002A	0F02	mov     a, 2H
002B	4380	addm    a, ra
002C	283A	jmp     _L3
				;29			if(!(KEY_Port & 0x20)) break;			   	//Check (KEY_Port[6]) is pressed
				_L4:
0028	3EA4	sz      PF5
0029	282D	jmp     _L5
				;30			KEY++;										//KEY+1
002F	0F03	mov     a, 3H
0030	4380	addm    a, ra
0031	283A	jmp     _L3
				;31			if(!(KEY_Port & 0x10)) break;			   	//Check (KEY_Port[7]) is pressed
				_L5:
002D	3E24	sz      PF4
002E	2832	jmp     _L6
				;32			KEY++;										//KEY+1
				_L6:
0032	0F04	mov     a, 4H
0033	4380	addm    a, ra
				;33			KEY_Port<<=1; 	   							//Update scan code
0034	0724	mov     a, PF
0035	03A4	addm    a, PF
				;34		}
				;35		return KEY;	
				;36	}
				_L3:
003A	4700	mov     a, ra
003B	0003	ret
				;37	
				;38	void Delay1ms(unsigned short delay)					//Delay1ms
				;39	{	
				;40		unsigned short i;											
				;41		for(i=0;i<delay;i++) GCC_DELAY(2000);			//GCC_DELAY(0.5us)
				;42	}	
				;43	
				;44	void Delay10ms(unsigned short delay)				//Delay10ms
				;45	{	
				;46		unsigned short i;											
				;47		for(i=0;i<delay;i++) GCC_DELAY(20000);			//GCC_DELAY(0.5us)
				;48	}
				;49	
				data .SECTION 'DATA'
				__pc DB DUP (?) ; __pc
				__pcc DB DUP (?) ; __pcc
				__pf DB DUP (?) ; __pf
				__pfc DB DUP (?) ; __pfc
				__pfpu DB DUP (?) ; __pfpu
				__wdtc DB DUP (?) ; __wdtc
				ra DB DUP (?)
