您好,欢迎来电子发烧友网! ,新用户?[免费注册]

您的位置:电子发烧友网>源码下载>汇编编程>

矩阵键盘控制程序汇总(汇编语言)

大小:0.16 MB 人气: 2017-09-06 需要积分:0
矩阵键盘控制程序汇总(汇编语言)

1、矩阵键盘控制数码管移位显示

;本程序按下矩阵键盘的按钮实现数码管的0123456789....移位显示,并在按下矩阵键盘的"P1.3*1.6"键后个位实现闪烁功能
;按键行列值表示如下:
;----P1.7---P1.6---P1.5---P1.4-------
;----03H----02H----01H----00H----P1.0
;----07H----06H----05H----04H----P1.1
;----0BH----0AH----09H----08H----P1.2
;----0FH----0EH----0DH----0CH----P1.3
;///////////////////////////////////////////
 
org 0000h
ajmp main
org 0030h

CLR P3.3
setb p2.4;
setb p2.5;
setb p2.6;
setb p2.7;
MOV P2,#0001111B
 
main:
 mov dptr,#tab                  ;将表头位置送入DPT
 mov   30h,#00h                     ;将30显示单元清零 
 mov   31h,#00h                     ;将30显示单元清零 
 mov   32h,#00h                     ;将30显示单元清零 
 mov   33h,#00h                     ;将30显示单元清零 
 mov 35h,#00h                   ;将30显示单元清零   
loop:                                     
 jb f0,kk1                     ;f0若等于一则跳到KK1处
 mov 35h,#00h                  
kk2:                                      
 lcall display             ;调用显示程序                    
 lcall key             ;调用键盘描程序                  
 cpl a                          ;将A的值取反                     
 jz  loop               ;无键按下则返回                
 mov a,34h                      ;有键按下将键值送入ACC           
 cjne a,#0ah,norp                   ;若A键未按下则返回到LOOP处       
 setb f0                         ;若A键按下则将F0值1   
 ljmp loop                          ;返回LOOP处不断的循环  
kk1:                                      
 inc 35h                        ;将35H向上加一
 mov a,35h                      ;将35H中的值送入ACC
 cjne a,#10,kk2                  ;ACC中的值与10相等则跳转至KK2
 mov 35h,#00h                   ;若ACC等于零则将35H清零
 cpl 00h                        ;将00H位取反
 ajmp kk2                        ;跳转至KK2处
                                          
norp:                                     
 mov 33h,32h                    ;将32H显示单元的内容送入33H  
 mov 32h,31h                        ;将31H显示单元的内容送入32H  
 mov 31h,30h                        ;将30H单元的内容送入31H      
 mov a,34h                      ;将键值送入ACC               
 mov 30h,a                      ;将ACC的值送入30H            
 ljmp loop                       ;返回主程序循环              
display:
   mov  p2,#0ffh            ;将P2口置高电平                           
   mov r7,#100              ;软件计数器赋值                           
dd1:                                                                                       
   jnb 00h,dd1d         ;若00H等0跳至DD1D
   ajmp dd1b             ;若00H等于1则跳转至DD1B
dd1d:          mov a,30h                ;将显示缓冲区30H中的值送入ACC              
   movc a,@a+dptr           ;查表取要显示的数并把查表的结果送入ACC     
   mov p0,a                 ;将要显示的数送入P0口                      
   clr p2.3                 ;开个位显示单元                            
   lcall delay100us         ;延时100US                                 
   setb p2.3                ;关个位显示单                              
   ajmp dd1c                                                        
dd1b:                                                
   mov a,30h                ;将显示缓冲区30H中的值送入ACC              
   movc a,@a+dptr           ;查表取要显示的数并把查表的结果送入ACC     
   mov p0,a                 ;将要显示的数送入P0口                      
   setb p2.3                ;开个位显示单元                            
   lcall delay100us         ;延时100US                                 
   setb p2.3                ;开个位显示单元                           
dd1c:                                                                                      
   mov a,31h                ;将显示缓冲区别31H中的值送入A             
   movc a,@a+dptr           ;查表取要显示的数并把查表的结果送入ACC    
   mov p0,a                 ;将要显示的数送入P0口                     
   clr p2.2                 ;开十位显示单元                           
   lcall delay100us         ;延时100US                                
   setb p2.2                ;关十位显示单元                           
                                                                                            
   mov a,32h                ;将显示缓冲区32H中的值送入ACC            
   movc a,@a+dptr           ;查表取要显示的数并把查表的结果送入ACC   
   mov p0,a                 ;将要显示的数送入P0口                    
   clr p2.1                 ;开百位显示单元                          
   lcall delay100us         ;延时100US                               
   setb p2.1                ;关百位显示单元                         
                                                                                         
   mov a,33h                ;将显示缓冲区33H中的值送入ACC                                                  
   movc a,@a+dptr           ;查表取要显示的数并把查表的结果送入ACC 
   mov p0,a                 ;将要显示的数送入P0口                  
   clr p2.0                 ;开千位显示单元                        
   lcall delay100us         ;延时100US                             
   setb p2.0                ;关千位显示单元                        
   djnz r7,dd1              ;R7不等于0返回至DD1处                  
   ret                      ;R7等于0子程序返回                     
 
KEY:  LCALL KS            ;调用检测按键子程序                           
      JNZ K1                                   ;有键按下继续                            
 ljmp key_exit                         ;无键按下退出KEY子程序                   
K1:  LCALL delay4ms                              ;返回继续检测有无按键按下                
     LCALL delay4ms                                                                       
     LCALL KS                                    ;有键按下继续延时去抖动                  
     JNZ K2                                      ;再一次调用检测按键程序                  
 ljmp key_exit                         ;无键按下退出KEY子程序                   
K2: MOV R2,#0EFH                             ;将扫描值送入 R2暂存                     
      MOV R4,#00H                              ;将第一列的列值00H送入R4暂存,R4用于存放列
K3: MOV P1,R2                                ;将R2的值送入P1口                        
L6: JB P1.0,L1                               ;P1.0等于1跳转到L1                       
      MOV A,#00H                               ;将第一行的行值00H送入ACC                
      AJMP LK                                  ;跳转到键值处理程序                      
L1: JB P1.1,L2                               ;P1.1等于1跳转到L2                       
     MOV A,#04H                                  ;将第二行的行值04H送入ACC                   
     AJMP LK                                     ;跳转到键值理程序进行键值处理            
L2: JB P1.2,L3                                   ;P1.2等于1跳转到L3                       
     MOV A,#08H                                  ;将第三行的行值08H送入ACC                   
     AJMP LK                                     ;跳转到键值处理程序                      
L3: JB P1.3,NEXT                             ;P1.3等于1跳转到NEXT处                   
     MOV A,#0cH                                  ;将第四行的行值0CH送入ACC                   
LK: ADD A,R4                                 ;行值与列值相加后的键值送入A             
  mov 34h,a                    ;将A中的值送入34h暂存                    
K4: ;LCALL delay4ms                          ;                                        
  lcall display                    ;调用动态显示子程序                      
     LCALL KS                                    ;                                        
     JNZ K4                                      ;按键没有松开继续返回检测                
     RET                                         ;按键松开返回                            
                                                                                          
NEXT:                                                                                     
    INC  R4                                      ;将列值加一                              
    MOV A,R2                                     ;将R2的值送入A                           
    JNB ACC.7,key_exit                           ;扫描完成跳至KEY处进行下一回合的扫描     
    RL A                                         ;扫描未完成将A中的值右移一位进行下一列的扫描  
    MOV R2,A                                     ;将ACC的值送入R2暂存                     
    AJMP K3                                      ;跳转到K3继续                            
key_exit:                                                                                 
  mov a,#0ffh                      ;无键按下则将A赋成0FFH。                 
  ret                                                                       
                                                                                          
KS: MOV P1,#0FH                              ;将P1口高四位置0低四位值1                
     MOV A,P1                                    ;读P1口                                  
     XRL A,#0FH                                  ;将A中的值与A中的值相异或                
     RET                                         ;子程序返回                              
delay4ms:             ;4ms延时
      MOV R5,#08H
L7: MOV R6,#0FAH
L8: DJNZ R6,L8
     DJNZ R5,L7
     RET
delay100us:                                      ;100us延时
   mov r5,#50
   djnz r5,$
   ret
tab: db 0c0h,0f9h,0a4h,0b0h,99h,92h,82h,0f8h,80h,90h,88h,83h,0c6h,0a1h,86h,8eh
end

 

 
 
 
 
 
 
 

2、矩阵键盘控制数码管移位显示

;本程序按下矩阵键盘的按钮实现数码管的0123456789....移位显示,并在按下矩阵键盘的"P1.3*1.6"键后个位实现闪烁功能
;按键行列值表示如下:
;----P1.7---P1.6---P1.5---P1.4-------
;----03H----02H----01H----00H----P1.0
;----07H----06H----05H----04H----P1.1
;----0BH----0AH----09H----08H----P1.2
;----0FH----0EH----0DH----0CH----P1.3
;///////////////////////////////////////////
 
org 0000h
ajmp main
org 0030h

CLR P3.3
setb p2.4;
setb p2.5;
setb p2.6;
setb p2.7;
MOV P2,#0001111B
 
main:
 mov dptr,#tab                  ;将表头位置送入DPT
 mov   30h,#00h                     ;将30显示单元清零 
 mov   31h,#00h                     ;将30显示单元清零 
 mov   32h,#00h                     ;将30显示单元清零 
 mov   33h,#00h                     ;将30显示单元清零 
 mov 35h,#00h                   ;将30显示单元清零   
loop:                                     
 jb f0,kk1                     ;f0若等于一则跳到KK1处
 mov 35h,#00h                  
kk2:                                      
 lcall display             ;调用显示程序                    
 lcall key             ;调用键盘描程序                  
 cpl a                          ;将A的值取反                     
 jz  loop               ;无键按下则返回                
 mov a,34h                      ;有键按下将键值送入ACC           
 cjne a,#0ah,norp                   ;若A键未按下则返回到LOOP处       
 setb f0                         ;若A键按下则将F0值1   
 ljmp loop                          ;返回LOOP处不断的循环  
kk1:                                      
 inc 35h                        ;将35H向上加一
 mov a,35h                      ;将35H中的值送入ACC
 cjne a,#10,kk2                  ;ACC中的值与10相等则跳转至KK2
 mov 35h,#00h                   ;若ACC等于零则将35H清零
 cpl 00h                        ;将00H位取反
 ajmp kk2                        ;跳转至KK2处
                                          
norp:                                     
 mov 33h,32h                    ;将32H显示单元的内容送入33H  
 mov 32h,31h                        ;将31H显示单元的内容送入32H  
 mov 31h,30h                        ;将30H单元的内容送入31H      
 mov a,34h                      ;将键值送入ACC               
 mov 30h,a                      ;将ACC的值送入30H            
 ljmp loop                       ;返回主程序循环              
display:
   mov  p2,#0ffh            ;将P2口置高电平                           
   mov r7,#100              ;软件计数器赋值                           
dd1:                                                                                       
   jnb 00h,dd1d         ;若00H等0跳至DD1D
   ajmp dd1b             ;若00H等于1则跳转至DD1B
dd1d:          mov a,30h                ;将显示缓冲区30H中的值送入ACC              
   movc a,@a+dptr           ;查表取要显示的数并把查表的结果送入ACC     
   mov p0,a                 ;将要显示的数送入P0口                      
   clr p2.3                 ;开个位显示单元                            
   lcall delay100us         ;延时100US                                 
   setb p2.3                ;关个位显示单                              
   ajmp dd1c                                                        
dd1b:                                                
   mov a,30h                ;将显示缓冲区30H中的值送入ACC              
   movc a,@a+dptr           ;查表取要显示的数并把查表的结果送入ACC     
   mov p0,a                 ;将要显示的数送入P0口                      
   setb p2.3                ;开个位显示单元                            
   lcall delay100us         ;延时100US                                 
   setb p2.3                ;开个位显示单元                           
dd1c:                                                                                      
   mov a,31h                ;将显示缓冲区别31H中的值送入A             
   movc a,@a+dptr           ;查表取要显示的数并把查表的结果送入ACC    
   mov p0,a                 ;将要显示的数送入P0口                     
   clr p2.2                 ;开十位显示单元                           
   lcall delay100us         ;延时100US                                
   setb p2.2                ;关十位显示单元                           
                                                                                            
   mov a,32h                ;将显示缓冲区32H中的值送入ACC            
   movc a,@a+dptr           ;查表取要显示的数并把查表的结果送入ACC   
   mov p0,a                 ;将要显示的数送入P0口                    
   clr p2.1                 ;开百位显示单元                          
   lcall delay100us         ;延时100US                               
   setb p2.1                ;关百位显示单元                         
                                                                                         
   mov a,33h                ;将显示缓冲区33H中的值送入ACC                                                  
   movc a,@a+dptr           ;查表取要显示的数并把查表的结果送入ACC 
   mov p0,a                 ;将要显示的数送入P0口                  
   clr p2.0                 ;开千位显示单元                        
   lcall delay100us         ;延时100US                             
   setb p2.0                ;关千位显示单元                        
   djnz r7,dd1              ;R7不等于0返回至DD1处                  
   ret                      ;R7等于0子程序返回                     
 
KEY:  LCALL KS            ;调用检测按键子程序                           
      JNZ K1                                   ;有键按下继续                            
 ljmp key_exit                         ;无键按下退出KEY子程序                   
K1:  LCALL delay4ms                              ;返回继续检测有无按键按下                
     LCALL delay4ms                                                                       
     LCALL KS                                    ;有键按下继续延时去抖动                  
     JNZ K2                                      ;再一次调用检测按键程序                  
 ljmp key_exit                         ;无键按下退出KEY子程序                   
K2: MOV R2,#0EFH                             ;将扫描值送入 R2暂存                     
      MOV R4,#00H                              ;将第一列的列值00H送入R4暂存,R4用于存放列
K3: MOV P1,R2                                ;将R2的值送入P1口                        
L6: JB P1.0,L1                               ;P1.0等于1跳转到L1                       
      MOV A,#00H                               ;将第一行的行值00H送入ACC                
      AJMP LK                                  ;跳转到键值处理程序                      
L1: JB P1.1,L2                               ;P1.1等于1跳转到L2                       
     MOV A,#04H                                  ;将第二行的行值04H送入ACC                   
     AJMP LK                                     ;跳转到键值理程序进行键值处理            
L2: JB P1.2,L3                                   ;P1.2等于1跳转到L3                       
     MOV A,#08H                                  ;将第三行的行值08H送入ACC                   
     AJMP LK                                     ;跳转到键值处理程序                      
L3: JB P1.3,NEXT                             ;P1.3等于1跳转到NEXT处                   
     MOV A,#0cH                                  ;将第四行的行值0CH送入ACC                   
LK: ADD A,R4                                 ;行值与列值相加后的键值送入A             
  mov 34h,a                    ;将A中的值送入34h暂存                    
K4: ;LCALL delay4ms                          ;                                        
  lcall display                    ;调用动态显示子程序                      
     LCALL KS                                    ;                                        
     JNZ K4                                      ;按键没有松开继续返回检测                
     RET                                         ;按键松开返回                            
                                                                                          
NEXT:                                                                                     
    INC  R4                                      ;将列值加一                              
    MOV A,R2                                     ;将R2的值送入A                           
    JNB ACC.7,key_exit                           ;扫描完成跳至KEY处进行下一回合的扫描     
    RL A                                         ;扫描未完成将A中的值右移一位进行下一列的扫描  
    MOV R2,A                                     ;将ACC的值送入R2暂存                     
    AJMP K3                                      ;跳转到K3继续                            
key_exit:                                                                                 
  mov a,#0ffh                      ;无键按下则将A赋成0FFH。                 
  ret                                                                       
                                                                                          
KS: MOV P1,#0FH                              ;将P1口高四位置0低四位值1                
     MOV A,P1                                    ;读P1口                                  
     XRL A,#0FH                                  ;将A中的值与A中的值相异或                
     RET                                         ;子程序返回                              
delay4ms:             ;4ms延时
      MOV R5,#08H
L7: MOV R6,#0FAH
L8: DJNZ R6,L8
     DJNZ R5,L7
     RET
delay100us:                                      ;100us延时
   mov r5,#50
   djnz r5,$
   ret
tab: db 0c0h,0f9h,0a4h,0b0h,99h,92h,82h,0f8h,80h,90h,88h,83h,0c6h,0a1h,86h,8eh
end
 
 
 
 
 
 
 
 
 
 

3、矩阵键盘按钮=液晶1602流动显示预定字符

;按压矩阵键盘按钮=液晶1602显示预定字符
;按键行列值表示如下:                     
;----P1.7---P1.6---P1.5---P1.4-------     
;----03H----02H----01H----00H----P1.0     
;----07H----06H----05H----04H----P1.1     
;----0BH----0AH----09H----08H----P1.2    
;----0FH----0EH----0DH----0CH----P1.3  
;///////////////////////////////////////////       
RS EQU P2.0     ;决定要传送的是命令还是数据   
RW EQU P2.1                 ;决定要读还是写,一般写的时候多.     
E  EQU P2.2                 ;时钟信号                           
SJ EQU P0                   ;数据线                             
org 00h
ajmp main
org 030h            
main:
mov sp,#60h    
main1:           
lcall delay                              ;延时一段时间                  
MOV SJ,#38H                              ;
ACALL ENABLE                             ;写指令38H                     
MOV SJ,#38H                              ;
ACALL ENABLE                             ;写指令38H                     
MOV SJ,#38H                              ;
ACALL ENABLE                             ;写指令38H                     
MOV SJ,#38H                              ;
ACALL ENABLE                             ;写指令38H                     
MOV SJ,#08H                              ;
ACALL ENABLE                             ;写指令08H                     
MOV SJ,#01H                              ;
ACALL ENABLE                             ;写指令01H                    
MOV SJ,#06H                              ;
ACALL ENABLE                             ;写指令06H                     
MOV SJ,#0cH                          ;
ACALL ENABLE                             ;写指令0CH                    
 mov dptr,#table              ;将表头地址送入DPTR
 lcall key                      ;调用键盘描程序
 mov b,#02h                   ;将立即数送入寄存器B
 mul ab                       ;将键值与B中的数相乘
 mov 30h,a                    ;将相乘后的结果送入30H暂存
 movc a,@a+dptr                ;查表并将所查得结果送入ACC中
 mov  31h,a            ;将ACC中的数送入31H
 mov  a,30h            ;送30H中的数至ACC
 inc  a                ;将ACC中的值加一
 movc a,@a+dptr                ;查表并所查到的低半字节送入ACC中
 mov  dpl,a            ;将ACC中的值送入数据指针的低8位?
mov  dph,31h                          ;将ACC中的值送入数据指针的高8位
 
MOV SJ,#80H;wei zhi                      ;第一行位置           
ACALL ENABLE                             ;调用写时序
call write1                              ;调用写子程序
jmp main1                                ;返回主程序循环
ENABLE:     ;写指令时序子程序    
CLR RS
CLR RW 
CLR E 
ACALL DELAY 
SETB E 
RET
write1:     ;写字符串 子程序 
mov r1,#00h                              
a1:mov a,r1                        
movc a,@a+dptr                     
call write2                        
inc r1                             
cjne r1,#10h,a1                    
ret                                
                                   
write2:                                  ;写单个字符子程序            
mov SJ,a                
setb rs                            
clr rw
clr e
call delay
setb e
ret

delay:                                ;130ms延时子程序
mov r7,#255
d1:mov r6,#255
d2:djnz r6,d2
djnz r7,d1
ret
KEY: LCALL KS                           ;调用检测按键子程序                                            
     JNZ K1                             ;有键按下继续                                     
     LCALL DELAY2                       ;无键按下调用延时去抖动程序                       
     AJMP KEY                           ;返回继续检测有无按键按下                         
K1:  LCALL DELAY2                                                                         
     LCALL DELAY2                       ;有键按下继续延时去抖动                           
     LCALL KS                           ;再一次调用检测按键程序                           
     JNZ K2                             ;确认有按下进行下一步                             
     AJMP KEY                           ;无键按下返回继续检测                             
K2: MOV R2,#0EFH                    ;将扫描值送入 R2暂存                              
     MOV R4,#00H                        ;将第一列的列值00H送入R4暂存,R4用于存放列值。     
K3: MOV P1,R2                       ;将R2的值送入P1口                                 
L6: JB P1.0,L1                      ;P1.0等于1跳转到L1                                
     MOV A,#00H                         ;将第一行的行值00H送入ACC                         
     AJMP LK                            ;跳转到键值处理程序                               
L1: JB P1.1,L2                      ;P1.1等于1跳转到L2                                
     MOV A,#04H                         ;将第二行的行值送入ACC                            
     AJMP LK                            ;跳转到键值理程序进行键值处理                     
L2: JB P1.2,L3                          ;P1.2等于1跳转到L3                                
     MOV A,#08H                         ;将第三行的行值送入ACC                            
     AJMP LK                            ;跳转到键值处理程序                               
L3: JB P1.3,NEXT                    ;P1.3等于1跳转到NEXT处                            
     MOV A,#0cH                         ;将第四行的行值送入ACC                            
LK: ADD A,R4                        ;行值与列值相加后的键值送入A                      
     PUSH ACC                           ;将A中的值送入堆栈暂存                            
K4: LCALL DELAY2                    ;调用延时去抖动程序                               
     LCALL KS                           ;调用按键检测程序                                 
     JNZ K4                             ;按键没有松开继续返回检测                         
     POP ACC                            ;将堆栈的值送入ACC                                
     RET                                                                                  
                                                                                          
NEXT:                                                                                     
    INC  R4                             ;将列值加一                                       
    MOV A,R2                            ;将R2的值送入A                                    
    JNB ACC.7,KEY                       ;扫描完成跳至KEY处进行下一回合的扫描              
    RL A                                ;扫描未完成将A中的值右移一位进行下一列的扫描      
    MOV R2,A                            ;将ACC的值送入R2暂存                              
    AJMP K3                             ;跳转到K3继续                                     
KS: MOV P1,#0FH                     ;将P1口高四位置0低四位值1                         
     MOV A,P1                           ;读P1口                                           
     XRL A,#0FH                         ;将A中的值与A中的值相异或                         
     RET                                ;子程序返回                                       
DELAY2:                                 ;40ms延时去抖动子程序8*FA*2=40ms                  
      MOV R5,#08H                                                                         
L7: MOV R6,#0FAH
L8: DJNZ R6,L8
     DJNZ R5,L7
     RET
table: dw table0,table1,table2,table3,table4,table5,table6,table7,table8      
 dw table9,table10,table11,table12,table13,table14,table15
table0: db "  0451-82314775 "  ;矩阵键盘P1.0*1.4
table1: db "  www.pp51.com  "  ;矩阵键盘P1.0*1.5
table2: db "pp51com@163.com "  ;矩阵键盘P1.0*1.6
table3: db "pp51com@tom.com "  ;矩阵键盘P1.0*1.7
table4: db "the 4th key     "  ;矩阵键盘P1.1*1.4
table5: db "the 5th key     "  ;矩阵键盘P1.1*1.5
table6: db "the 6th key     "  ;矩阵键盘P1.1*1.6
table7: db "the 7th key     "  ;矩阵键盘P1.1*1.7
table8: db "the 8th key     "  ;矩阵键盘P1.2*1.4
table9: db "the 9th key     "  ;矩阵键盘P1.2*1.5
table10: db "the 10th key    " ;矩阵键盘P1.2*1.6
table11: db "the 11th key    " ;矩阵键盘P1.2*1.7
table12: db "the 12th key    " ;矩阵键盘P1.3*1.4
table13: db "the 13th key    " ;矩阵键盘P1.3*1.5
table14: db "the 14th key    " ;矩阵键盘P1.3*1.6
table15: db "the 15th key    " ;矩阵键盘P1.3*1.7
end
 

4、矩阵键盘数码管显示键值

           RELAY  EQU  P1.3
           BEEP   EQU  P3.7
;------------------------------------------------------
           ORG  0000H
           JMP  MAIN
;------------------------------------------------------
MAIN:      MOV  SP,#60H
           MOV  30H,#00H          
LOOP1:
           CALL  KEY_IN
           CALL  KEY_PLAER
           JMP  LOOP1
;-----------------------------------------------------
KEY_IN:    MOV  P1,#0F0H          ;置列线为0,行线为1
           MOV  A,P1
           ANL  A,#0F0H
           MOV  B,A
           MOV  P1,#0FH           ;置列线为1,行线为0
           MOV  A,P1
           ANL  A,#0FH
           ORL  A,B               ;高四位与低四位重新组合
           CJNE  A,#0FFH,KEY_IN1  ;0FFH为末按键
           RET
KEY_IN1:   MOV  B,A
           MOV  DPTR,#KEYTABLE
           MOV  R3,#0FFH           ;
KEY_IN2:   INC  R3
           MOV  A,R3
           MOVC  A,@A+DPTR
           CJNE  A,B,KEY_IN3
           MOV  A,R3             ;找到,取顺序码            
           MOV  30H,A
           CALL  BEEP_BL
           RET
KEY_IN3:   CJNE  A,#0FFH,KEY_IN2  ;末完,继续查
           RET                    ;0FFH为结束码
;----------------------------------------------
;蜂鸣器响一声子程序
;----------------------------------------------
BEEP_BL:
           MOV  R6,#100
  BL1:     CALL  DEX1
           CPL  BEEP
           DJNZ  R6,BL1
           MOV  R5,#25
           CALL  DELAY
           RET
 DEX1:     MOV  R7,#180
 DEX2:     NOP
           DJNZ  R7,DEX2
           RET
DELAY:                    ;延时R5×10MS
           MOV  R6,#50
DELAY1:    MOV  R7,#100
           DJNZ  R7,$
           DJNZ  R6,DELAY1
           DJNZ  R5,DELAY
           RET
;-----------------------------------------------------
KEYTABLE:
    DB  0EEH,0EDH,0EBH,0E7H,0DEH  ;0,1,2,3,4,  顺序码
    DB  0DDH,0DBH,0D7H,0BEH,0BDH  ;5,6,7,8,9,
    DB  0BBH,0B7H,07EH,07DH,07BH  ;A,B,C,D,E,
    DB  077H,0FFH                 ;F      0FF为结束码
;-----------------------------------------------------
;键值显示子程序
;使用一个数码管显示
;-----------------------------------------------------
KEY_PLAER:
            
             MOV  A,30H
             MOV  DPTR,#TAB      ;取段码表地址
             MOVC  A,@A+DPTR     ;查显示数据对应段码
             MOV  P0,A           ;段码放入P0口
             CLR  P2.7           ;第一个数码管亮
             RET               
;-----------------------------------------------------
TAB:
 DB  0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H
 DB  80H,90H,88h,83h,0c6h,0a1h,86h,8eh
;-----------------------------------------------------
    END
 

5、矩阵键盘声音提示数码管输出

;本程序按下矩阵键盘的按钮实现数码管的0 1 2 3 4 5 6 7 8 9 A B C D E F,同时喇叭输出声音提示
;///////////////////////////////////////////
 org 0000h
 ajmp main
 org 000bh
 ajmp timer0
 org 0030h
main:
 mov 30h,#100
 mov tmod,#01h
 mov ie,#82h
 mov th0,#high(64536)
 mov tl0,#low(64536)
 mov dptr,#tab
 lcall key
 movc a,@a+dptr
 mov p0,a
 CLR P2.0
 ljmp main
timer0:   ;1ms timer
 mov th0,#high(64536)
 mov tl0,#low(64536)
 cpl p3.7
 djnz 30h,t_e
 mov 30h,#100
 clr tr0
t_e: reti
  
KEY: LCALL KS
     JNZ K1
     LCALL DELAY2
     AJMP KEY
K1:  LCALL DELAY2
     LCALL DELAY2
     LCALL KS
     JNZ K2
     AJMP KEY
K2:  setb tr0
 MOV R2,#0EFH
     MOV R4,#00H
K3: MOV P1,R2
L6: JB P1.0,L1 
     MOV A,#00H
     AJMP LK
L1: JB P1.1,L2
     MOV A,#04H
     AJMP LK
L2: JB P1.2,L3              
     MOV A,#08H
     AJMP LK
L3: JB P1.3,NEXT
     MOV A,#0cH
LK: ADD A,R4
     PUSH ACC
K4: LCALL DELAY2
     LCALL KS
     JNZ K4
     POP ACC
     
     RET
     
NEXT:
    INC  R4
    MOV A,R2
    JNB ACC.7,KEY
    RL A
    MOV R2,A
    AJMP K3
KS: MOV P1,#0FH
     MOV A,P1
     XRL A,#0FH
     RET
DELAY2:
      MOV R5,#08H
L7: MOV R6,#0FAH
L8: DJNZ R6,L8
     DJNZ R5,L7
     RET
tab: db 0c0h,0f9h,0a4h,0b0h,99h,92h,82h,0f8h,80h,90h,88h,83h,0c6h,0a1h,86h,8eh
end
 

6、最简单的键盘控制

;当按压键盘P1.4,P1.5,P1.6,P1.7的时候会看到P2口作出的反映,此为平凡经典入门教程范例之一
main:mov p1,#0ffh  ;把11111111(FF)移动到P1口,使P1的8个口在没有键盘置低的情况下输出高电
loop:mov a,p1  ;把P1的值移动到寄存器A
mov p2,a  ;把寄存器A的值移动到P2,就此完成在了P1输入状态控制P2输出状态的最简过程
ljmp loop  ;这是个重要的命令,否则上面的语句将失去作用,详解请参看平凡单片机入门教程
END
 

非常好我支持^.^

(0) 0%

不好我反对

(0) 0%

      发表评论

      用户评论
      评价:好评中评差评

      发表评论,获取积分! 请遵守相关规定!