[ บทความ : ตัวอย่างสำหรับ CP-SB31 V2 ตอนที่ 5 ] ทดสอบติดต่อกับ Keypad 4x3 ของ CP-SB31V2

ตัวอย่างซอร์สโค้ดสำหรับติดต่อกับ Keypad 4x3 (ใช้ PortC ของ 8255) กับบอร์ด CP-SB31V2 ที่ติดตั้ง ET-DEBUG31 เป็นดังนี้



	;/*********************************/;
	;/* Demo Program For Keyboard 4X3 */;
	;/* Hardware  :  ET-CPSB31 V2.0   */;
	;/* Complier  :  Cross-32  V2.0   */;
	;/*********************************/;
	;

	;/***************************************/;
	;/* Equate List Port I/O ET-CPSB31 V2.0 */;
	;/***************************************/;
	;
	PORTA      EQU     0E0E0H                  ; Port A 8255
	PORTB      EQU     0E0E1H                  ; Port B 8255
	PORTC      EQU     0E0E2H                  ; Port C 8255
	CONTRL     EQU     0E0E3H                  ; Control Port 8255
	SPEAKER    EQU     ACC.7                   ; PC7 = Speaker
	KEY_PRES   EQU     00H                     ; FLAG_BUF.0
	KEY_STAS   EQU     01H                     ; FLAG_BUF.1

	            ORG     20H                     ; CPU-RAM Buffer
	FLAG_BUF:   DS     1
	ROW:        DS     1
	COLUM:      DS     1
	;

	            ORG     2200H                   ; Strat Program For "ET-DEBUG31"
	
	MAIN:       MOV     FLAG_BUF,#00H           ; Clear All Flag
	            MOV     R2,#02H                 ; Power-on Delay
	PWR_DLY1:   MOV     R3,#00H
	PWR_DLY2:   MOV     R4,#00H
	            DJNZ    R4,$
	            DJNZ    R3,PWR_DLY2
	            DJNZ    R2,PWR_DLY1
	            ;
	            MOV    DPTR,#CONTRL             ; Control Port 8255
	            MOV    A,#81H                   ; PA,PB,PCH = Output,PCL = Input
	            MOVX   @DPTR,A
	            ;
	INIT_SER:   MOV     A,#0FDH                 ; Set baud rate 9600
	            MOV     TH1,A
	            MOV     TL1,A
	            MOV     TMOD,#00100000B         ; Timer1 Mode 1
	            CLR     ES                      ; Disable serial interupt
	            CLR     ET1                     ; Disable timer1 interupt
	            SETB    TR1                     ; Set timer1 control
	            MOV     SCON,#01010000B         ; Serial mode 1
	            ;
	            LCALL   PRINT_SER
	            DB     0DH,0AH
	            DB     "Hardware : ET-CPSB31 V2.0",0DH,0AH
	            DB     "ETT CO.,LTD",0DH,0AH
	            DB     "Test Scan Key 4X3"
	            DB     0DH,0AH,00H
	            ;
	LOOP:       LCALL   SCANKEY
	            CJNE    A,#0FFH,DSP_KEY
	            SJMP    LOOP
	            ;
	DSP_KEY:    LCALL   TX_2ASC             ; Display Keypress
	            MOV     A,#0DH
	            LCALL   TX_BYTE
	            SJMP    LOOP


	;/********************************/;
	;/*  Scan Keyboard Matrix  (4x3) */;
	;/*  Output    : ACC (code 0..F) */;
	;/*            : FFH (Not press) */;
	;/*  Register  : ACC,B,R2,DPTR   */;
	;/*  Sub. Call : KEY_DLY         */;
	;/*            : KEY_NEW         */;
	;/********************************/;
	;
	SCANKEY:    MOV     ROW,#4              ; Counter for Scan 4x3
	            MOV     COLUM,#11101111B    ; Data for Scan Colum
	SCAN1:      MOV     DPTR,#PORTC         ; Port Scan Key
	            MOV     A,COLUM
	            CLR     SPEAKER             ; off Sound
	            MOVX    @DPTR,A             ; Out Colum (PC4-PC7)
	            MOVX    A,@DPTR             ; In Row (PC0-PC3)
	            ANL     A,#0FH
	            CJNE    A,#0FH,PRESS        ; Check row only (PC0-PC3)
	            MOV     A,COLUM             ; Scan Next Colum
	            RL      A
	            MOV     COLUM,A
	            DJNZ    ROW,SCAN1
	            MOV     A,#0FFH             ; Not Keypress
	            CLR     KEY_PRES            ; not key Press
	            SJMP    END_SCAN
	            ;
	PRESS:      LCALL   KEY_DLY             ; Debounce key
	            MOV     A,COLUM
	            CLR     SPEAKER             ; off Sound
	            MOVX    @DPTR,A             ; Out Colum (PC4-PC7)
	            MOVX    A,@DPTR             ; In Row (PC0-PC3)
	            ANL     A,#0FH
	            CJNE    A,#0FH,KEY_CODE     ; Row (PC0-PC3)
	            SJMP    SCANKEY             ; No keybounce or noise
	KEY_CODE:   LCALL   KEY_NEW
	            JNB     KEY_STAS,SCAN1      ; if old key don't care
	            MOV     B,A                 ; Row code (Low)
	            MOV     A,COLUM
	            ANL     A,#0F0H             ; Colum (High Byte)
	            ORL     A,B                 ; Mix Row & Colum
	            MOV     B,A                 ; Row & Colum code
	            MOV     DPTR,#KEY_TAB
	            MOV     R2,#0FFH
	CODE1:      INC     R2
	            MOV     A,R2
	            MOVC    A,@A+DPTR
	            CJNE    A,B,CODE1
	            MOV     A,R2                ; Number code 0..F
	            LCALL   BEEP
	END_SCAN:   RET
	
	;/**********************************/;
	;/*     Check Old & New key        */;
	;/*  KEY_STAS = 0 is old press Key */;
	;/*  KEY_STAS = 1 is 1st press Key */;
	;/**********************************/;
	;
	KEY_NEW:    CLR     KEY_STAS            ; Next time = old key
	            JB      KEY_PRES,END_NEW    ; Jump if old key press
	            SETB    KEY_PRES            ; Next time = old press
	            SETB    KEY_STAS            ; Next time = old key
	END_NEW:    RET

	;/****************/;
	;/*   Delay For  */;
	;/* Debounce key */;
	;/****************/;
	;
	KEY_DLY:    MOV     R6,#01FH
	KEY_DL1:    MOV     R7,#0FFH
	            DJNZ    R7,$
	            DJNZ    R6,KEY_DL1
	            RET

	;/*********************/;
	;/* Table of Key Code */;
	;/*********************/;
	;
	KEY_TAB:    DB     0DEH,0B7H,0D7H,0E7H     ; 0 1 2 3
	            DB     0BBH,0DBH,0EBH,0BDH     ; 4 5 6 7
	            DB     0DDH,0EDH,0BEH,0EEH     ; 8 9 * #

	;/******************************/;
	;/* Sound Beep on Mini Speaker */;
	;/* Used 8255 Port PC3 to BEEP */;
	;/******************************/;
	;
	BEEP:       PUSH    ACC
	            PUSH    DPL
	            PUSH    DPH
	            MOV     R3,#0FFH        ; Long (Note)
	BEEP1:      MOV     DPTR,#PORTC
	            MOVX    A,@DPTR
	            SETB    SPEAKER         ; Sound On
	            MOVX    @DPTR,A
	            MOV     R2,#0FFH        ; Frequency (High Pulse)
	            DJNZ    R2,$
	            MOVX    A,@DPTR
	            CLR     SPEAKER         ; Sound OFF
	            MOVX    @DPTR,A
	            MOV     R2,#1FH         ; Frequency (Low Pulse)
	            DJNZ    R2,$
	            DJNZ    R3,BEEP1
	            POP     DPH
	            POP     DPL
	            POP     ACC
	            RET

	;/*************************/;
	;/* Send 1-Byte to RS-232 */;
	;/* Input   : ACC         */;
	;/* Output  : Serial port */;
	;/*************************/;
	;
	TX_BYTE:    PUSH   IE
	            CLR    TI
	            MOV    SBUF,A
	            JNB    TI,$
	            CLR    TI
	            POP    IE
	            RET

	;/******************************/;
	;/*    Send 2 byte of ASCII    */;
	;/*  Input : ACC (HEX)         */;
	;/*  Example : A = A3H         */;
	;/*          : send 41H,33H    */;
	;/******************************/;
	;
	TX_2ASC:    LCALL   HEX_2ASC
	            LCALL   TX_BYTE
	            MOV     A,B
	            LCALL   TX_BYTE
	            RET

	;/******************************/;
	;/*   Convert HEX to ASCII     */;
	;/* Input    :  A              */;
	;/* Output   :  A (high-byte)  */;
	;/*          :  B (low-byte)   */;
	;/******************************/;
	;
	HEX_2ASC:   PUSH    ACC
	            LCALL   HEX_TO_ASC
	            MOV     B,A                 ; B = Lo byte
	            POP     ACC
	            SWAP    A
	            LCALL   HEX_TO_ASC
	            RET

	;/********************************/;
	;/*  Convert hex (0-F) to ASCII  */;
	;/*  Input   : ACC (HEX code)    */;
	;/*  Output  : ACC (ASCII code)  */;
	;/********************************/;
	;
	HEX_TO_ASC: PUSH    PSW
	            CLR     CY
	            ANL     A,#00001111B
	            CJNE    A,#09H,HTOA1
	            SETB    CY
	HTOA1:      JNB     CY,HTOA2            ;> 0AH
	            ORL     A,#30H              ;< 0AH
	            SJMP    END_HTOA
	HTOA2:      SUBB    A,#09H              ;> 0AH
	            ORL     A,#40H
	END_HTOA:   POP     PSW
	            RET

	;/************************************/;
	;/*  Print Data data to Serial Port  */;
	;/*  to display buffer.              */;
	;/*  Usage    : LCALL PRINT_SER      */;
	;/*           : DB   'xxxx',00       */;
	;/*  Register : ACC                  */;
	;/*  Note     : last byte must be 00 */;
	;/************************************/;
	;
	PRINT_SER:  POP     DPH
	            POP     DPL
	PRINT1:     CLR     A
	            MOVC    A,@A+DPTR
	            CJNE    A,#00H,PRINT2
	            SJMP    PRINT3
	PRINT2:     LCALL   TX_BYTE
	            INC     DPTR
	            SJMP    PRINT1
	PRINT3:     PUSH    DPL
	            PUSH    DPH
	            RET

	            END
                

สามารถ download ไฟล์ตัวอย่างของบอร์ด พร้อม assembler ได้เลยครับ


เขียนโดย : ETT
Author : ETT team
e-mail : sales@etteam.com
วันที่ทำการปรับปรุง : ๒๒ ธ.ค. ๒๕๔๓