单片机电子琴电路结构及工作原理 第3页

单片机电子琴电路结构及工作原理 第3页
BREAK: PUSH ACC          ;  中断产生方波,从P1.0口输出
       PUSH PSW
       MOV TL0,20H
       MOV TH0,21H
       CPL P1.0
       POP PSW
       POP ACC
       RETI
TABLE: DW 65030,64968,64898,64820,64777,64684,64580 ; 7,6,5,4,3,2,1
       END
方案二:产生按键音符
       ORG 0000H
       SJMP START
       ORG 000BH
       AJMP INT_0
       ORG 0030H
START: MOV P1,#00H
       SETB EA
       SETB ET0                       ;计数器0
       MOV TMOD,#02H
       MOV TH1,#09CH
       MOV TL1,#09CH                 ;定时器初植
       CLR TR0                        ;定时器不允许
SCAN:                                 ;键盘扫描
       MOV A,P2                   ;判断键盘按下,跳到SCAN_1,若未按,继续扫描
       CJNE A,#0FFH,SCAN_1
       NOP
       SJMP SCAN
SCAN_1:ACALL DELAY ;是不是真的要按下?如果是,则跳转SU_KEY查询是哪个按下?
       MOV A,P2
       CJNE A,#0FFH,SU_KEY
       NOP
       SJMP SCAN
SU_KEY:JNB P2.1,MU_1                  ;判断是哪个键按下?
       JNB P2.2,MU_2
       JNB P2.3,MU_3
       JNB P2.4,MU_4
       JNB P2.5,MU_5
       JNB P2.6,MU_6
       JNB P2.7,MU_7
       SJMP SCAN
                                     ;R1信号周期
MU_1:  MOV R1,#19                ;如果第一个按键按下,则R1=19,同时程序转NEXT
       SJMP NEXT
MU_2:  MOV R1,#17            
       SJMP NEXT
MU_3:  MOV R1,#15
       SJMP NEXT
MU_4:  MOV R1,#14
       SJMP NEXT
MU_5:  MOV R1,#13
       SJMP NEXT
MU_6:  MOV R1,#11
       SJMP NEXT
MU_7:  MOV R1,#10
       SJMP NEXT
NEXT:  MOV A,R1               
       MOV R0,A
       SETB TR0                       ;启动定时器0                        
NEXT_1:MOV A,P2                     ; 没键按下继续执行,有键按下,返回读引脚
       CJNE A,#0FFH,NEXT_1
       ACALL DELAY
       MOV A,P2                       ;真的没键按下?
       CJNE A,#0FFH,NEXT_1
       CLR TR0                        ;不响
       AJMP SCAN                     ;扫描键盘去
INT_0:                                 ;中断程序
      DJNZ R0,RE                      ;R0不等于0时,返回
      CPL P1.0
      MOV A,R1
      MOV R0,A
RE:   RETI

上一页  [1] [2] [3] [4] [5] 下一页

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有