1. 程式人生 > >鍵盤輸入的數字在串列埠除錯助手顯示,然後再到數碼管(組合語言版本)

鍵盤輸入的數字在串列埠除錯助手顯示,然後再到數碼管(組合語言版本)

//實驗目的:按下按鍵就在數碼管上顯示相應數字,並且傳送到串列埠除錯助手上
org 00H
ljmp start

org 30H
start: 	 
        
	   mov P0,#0xff//設定初始值
	   mov dptr,#numb
	   lcall en
	   lcall UART_init 
       here:
	   		lcall  keyin
	        sjmp here  //好比是一個while(1)迴圈

;首先3/8譯碼器使能	並且選中第一個數碼管用於顯示
en:
   	clr   P1.4
	setb  P1.3
	clr   P1.0
	clr   P1.1
	clr   P1.2
ret

//uart初始化
UART_init:

   mov TMOD,#0x20  //0010 0000   說明在定時器1的工作方式2下工作
   mov  TH1,#0xFD
   mov  TL1,#0xFD 
   setb TR1  //定時器1執行控制位=1,說明開始計時
   mov SCON,#0x50 //0101 0000   前兩位說明在串列埠工作方式1  第三位SM2在方式1設定為0  第四位REN=1允許串列埠接收 
 ret

//延時函式	延時為6ms左右,這個延時主要用於按鍵去抖動
delay: 	 
       mov R0,#100
	   
	   j1:
	       mov R1,#30
	       djnz R0,j2
		   ljmp quit
	   j2:
		   djnz R1,j2
      	   ljmp j1
       quit:
ret
//按鍵函式
keyin:
	   j10:
		   clr  P2.3//keyout1
		   setb P2.2//keyout2
		   setb P2.1//keyout3
		   setb P2.0//keyout4
	   j11:
	       jb P2.4,j12
		   lcall delay
		   jb P2.4,j12
		   mov A,#0
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#0

	   j12:
	       jb P2.5,j13
		   lcall delay
		   jb P2.5,j13
		   mov A,#1
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#1
	   j13:
	       jb P2.6,j14
		   lcall delay
		   jb P2.6,j14
		   mov A,#2
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#2
       j14:
	       jb P2.7,j15
		   lcall delay
		   jb P2.7,j15
		   mov A,#3
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#3
	   j15:
		    jnb P2.4,j15
			jnb P2.5,j15
			jnb P2.6,j15
			jnb P2.7,j15



//---------------------------------------
	    j20:
		   setb P2.3//keyout1
		   clr  P2.2//keyout2
		   setb P2.1//keyout3
		   setb P2.0//keyout4
		j21:
	       jb P2.4,j22
		   lcall delay
		   jb P2.4,j22
		   mov A,#4
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#4

	   j22:
	       jb P2.5,j23
		   lcall delay
		   jb P2.5,j23
		   mov A,#5
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#5
	   j23:
	       jb P2.6,j24
		   lcall delay
		   jb P2.6,j24
		   mov A,#6
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#6
       j24:
	       jb P2.7,j25
		   lcall delay
		   jb P2.7,j25
		   mov A,#7
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#7
		j25:
		    jnb P2.4,j25
			jnb P2.5,j25
			jnb P2.6,j25
			jnb P2.7,j25

//---------------------------------------
		j30:
		   setb P2.3//keyout1
		   setb P2.2//keyout2
		   clr  P2.1//keyout3
		   setb P2.0//keyout4
		j31:
	       jb P2.4,j32
		   lcall delay
		   jb P2.4,j32
		   mov A,#8
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#8

	   j32:
	       jb P2.5,j33
		   lcall delay
		   jb P2.5,j33
		   mov A,#9
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#9
	   j33:
	       jb P2.6,j34
		   lcall delay
		   jb P2.6,j34
		   mov A,#10
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#10
       j34:
	       jb P2.7,j35
		   lcall delay
		   jb P2.7,j35
		   mov A,#11
		   movc A,@A+dptr
		   mov P0,A 
		   mov SBUF,#11
		j35:
		    jnb P2.4,j35
			jnb P2.5,j35
			jnb P2.6,j35
			jnb P2.7,j35



//---------------------------------------
		j40:
		   setb P2.3//keyout1
		   setb P2.2//keyout2
		   setb P2.1//keyout3
		   clr  P2.0//keyout4
	 
		j41:
	       jb P2.4,j42
		   lcall delay
		   jb P2.4,j42
		   mov A,#12
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#12

	   j42:
	       jb P2.5,j43
		   lcall delay
		   jb P2.5,j43
		   mov A,#13
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#13
	   j43:
	       jb P2.6,j44
		   lcall delay
		   jb P2.6,j44
		   mov A,#14
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#14
       j44:
	       jb P2.7,j45
		   lcall delay
		   jb P2.7,j45
		   mov A,#15
		   movc A,@A+dptr
		   mov P0,A
		   mov SBUF,#15
		j45:
		    jnb P2.4,j45
			jnb P2.5,j45
			jnb P2.6,j45
			jnb P2.7,j45
ret
    						  
//0-F

numb:
     DB 0x0c0,0x0F9,0x0A4,0x0B0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E
end