《单片机汇编程序实例.doc》由会员分享,可在线阅读,更多相关《单片机汇编程序实例.doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、 单片机程序入门小例子(汇编语言) 声明:以下3个例子都是正确的,都已经验证过。希望能给刚刚学习单片机的人一点参考。编写人:大连民族学院自动化专业 例1:流水灯(加按键) ORG 0000HKEY1:MOV A,#0FEH CLR CLOOP1:MOV P2,A RLC A ACALL DELAY JNB P3.7,KEY2 LJMP LOOP1KEY2:MOV A,#0FEHLOOP2:MOV P2,A RL A ACALL DELAY JNB P3.6,KEY1 LJMP LOOP2DELAY:MOV R7,#20 D1:MOV R6,#200 D2:MOV R5,#123 NOP DJN
2、Z R5,$ DJNZ R6,D2 DJNZ R7,D1 RET END例2:数码管动态显示 ORG 0000H AJMP MAIN ORG 0003H AJMP PINT0 ORG 0100H MAIN:MOV SP,#40H CLR IT0 SETB EX0 SETB EA HERE:MOV 30H,#00H MOV 31H,#01H MOV 32H,#02H MOV 33H,#03H LOOP:MOV R0,#30H MOV R1,#4 MOV R3,#0FEH MOV DPTR,#TAB LOOP1:MOV A,R0 MOVC A,A+DPTR MOV P0,A MOV P1,R3 L
3、CALL DELAY INC R0 MOV A,R3 RL A MOV R3,A DJNZ R1,LOOP1 SJMP LOOPTAB:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H DELAY:MOV R6,#6 LD:ACALL DELAY1 DJNZ R6,LDDELAY1:MOV R7,#124 NOP LOOP2:NOP NOP DJNZ R7,LOOP2 RET DELAY2:MOV R7,#20 D1:MOV R6,#200 D2:MOV R5,#123 NOP DJNZ R5,$ DJNZ R6,D2 DJNZ R7,D1 RET
4、 ORG 0200H PINT0:MOV A,#0C0H MOV R3,#0FEH MOV P0,A MOV P1,R3 LCALL DELAY2 RETI END 例3:用汇编语言编写一个秒表程序 ORG 0000H AJMP MAIN ORG 0003H LJMP TINT0 ORG 000BH LJMP TIMER0 ORG 0013H LJMP TINT1 ORG 0100HMAIN:MOV SP,#40H MOV TMOD,#01H MOV TL0,0B0H MOV TH0,03CH SETB TR0 SETB ET0 SETB EX0 SETB EX1 SETB PX0 SETB
5、IT0 SETB IT1 SETB EA HERE:MOV 30H,#00H MOV 31H,#00H MOV 32H,#00H MOV 33H,#00H MOV 34H,#00H MOV R4,#0LOOP:MOV R0,#31H MOV R1,#4 MOV R3,#0F7H MOV DPTR,#TAB MOV R5,#0 LOOP1:MOV A,R0 MOVC A,A+DPTR MOV P0,A MOV P1,R3 INC R5 CJNE R5,#2,DDD CLR P0.7 MOV R5,#0 DDD:LCALL DELAY INC R0 MOV A,R3 RR A MOV R3,A D
6、JNZ R1,LOOP1 SJMP LOOPTAB:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H DELAY:MOV R6,#6 LD:ACALL DELAY1 DJNZ R6,LD DELAY1:MOV R7,#124 NOP LOOP2:NOP NOP DJNZ R7,LOOP2 RETTIMER0:PUSH PSW PUSH ACC MOV TL0,0B0H MOV TH0,03CH INC 30H MOV A,30H CJNE A,#02H,TIME MOV 30H,#00H INC 31H MOV A,31H CJNE A,#0AH,
7、TIME MOV 31H,#00H INC 32H MOV A,32H CJNE A,#0AH,TIME MOV 32H,#00H INC 33H MOV A,33H CJNE A,#6H,TIME MOV 33H,#00H INC 34H MOV A,34H CJNE A,#0AH,TIME MOV 34H,#00H TIME:POP ACC POP PSW RETI TINT0:JNB P3.2,$ INC R4 CJNE R4,#2,EEE SETB TR0 MOV R4,#0 RETI EEE: CLR TR0 RETI TINT1:MOV 30H,#00H MOV 31H,#00H MOV 32H,#00H MOV 33H,#00H MOV 34H,#00H SETB TR0 RETI END