51单片机汇编语言实验及代码

单片机汇编语言实验及代码

以下内容为本人实验课亲自编写且通过51单片机运行实现的实验代码,有错误或者改进的地方请批评指正。

实验一:开发环境的应用

(1)将数据存储器的40H-4FH的内容复制成00H-0FH,并将40H-4FH的内容整体复制到外部数据存储器的1050H-105FH.
(2)P1口接八只二极管,使二极管逐个循环点亮

ORG 0000H
AJMP START
ORG 0100H
START:
      MOV A,#00H
	  MOV R0,#40H
LOOP:
      MOV @R0,A
	  INC A
	  INC R0
	  CJNE A,#10H,LOOP
SECOND:
      MOV DPTR,#1050H
	  MOV R1,#40H
COPY:
      MOV A,@R1
	  MOVX @DPTR,A
	  INC DPTR
	  INC R1
	  CJNE R1,#50H,COPY
	  SJMP $
	  END

实验二:外部中断

(1)用单脉冲信号申请中断,在中断服务程序中对输出信号进行翻转(可通过P1.0接一二极管观察)

ORG 0000H
LJMP START
ORG 0003H
LJMP INT
ORG 0100H
START:
      SETB EX0
	  SETB PX0
	  SETB IT0
	  SETB EA
	  SJMP $
INT:
	  CPL P1.0
	  RETI
	  END

(2)用单脉冲信号申请中断,要求对中断进行累加计数,并将计数结果显示在二极管上

ORG 0000H
LJMP START
ORG 0003H
LJMP INT
ORG 0100H
START:
      MOV A,#00H
	  MOV P1,A
	  SETB EX0
	  SETB PX0
	  SETB IT0
	  SETB EA
	  SJMP $
INT:  
	  MOV P1,A
	  INC A
	  RETI
	  END

实验三:定时器实验

(1)用定时器1的方式1定时,P1.0节发光二极管,使二极管每秒闪烁五次

ORG 0000H
LJMP MAIN
ORG 001BH
LJMP INT
ORG 0100H
MAIN:
     MOV TMOD,#10H
	 MOV TH1,#3CH
	 MOV TL1,#0B0H
	 MOV R2,#2
	 SETB EA
	 SETB ET1
	 SETB TR1
	 SJMP $
INT:
     DJNZ R2,NEXT
	 CPL P1.0
	 MOV R2,#2
NEXT:
     MOV TH1,#3CH
	 MOV TL1,#0B0H
	 SETB TR1
	 RETI
	 END

(2)用定时器1的方式2计数,T1引脚接单脉冲,用P1口接二极管显示计数。

ORG 0000H
LJMP MAIN
ORG 001BH
LJMP LOOP
ORG 0100H
MAIN:
     MOV TMOD,#60H
	 MOV TH1,#00H
	 MOV TL1,#00H
	 SETB EA
	 SETB ET1
	 SETB TF1
	 SETB TR1
	 SJMP $
LOOP:
     MOV P1,TL1
	 LJMP LOOP
	 RETI
	 END

实验四:串行通信

波特率2400 bit/s。PC发送8个字节的数据给单片机,单片机接受完数据后发送两个字节55H和AAH给PC 机(使用查询方式)。

ORG 0000H
AJMP MAIN
ORG 0030H
MAIN:
MOV SCON,#50H
MOV PCON,#00H
MOV TMOD,#20H
SETB TR1
MOV	TL1,#0F3H
MOV TH1,#0F3H
LOOP:
    MOV R0,#30H
	MOV R1,#08H
WAIT1:
    JBC RI,RECEIVE
	SJMP WAIT1
RECEIVE:
    MOV A,SBUF
	MOV @R0,A
	INC R0
	DJNZ R1,WAIT1
	MOV A,#55H
	MOV SBUF,A
WAIT21:
    JBC TI,SEND
	SJMP WAIT21
SEND:
    MOV A,#0AAH
	MOV SBUF,A
WAIT22:
    JNB TI,WAIT22
	CLR TI
	SJMP LOOP
	END

实验五:数码管显示及键盘扫描

(1)将30H-32H中存储的6位十进制数在6个数码管上显示出来(要求编写一个将内存单元中的压缩BCD码转换为两个七段显示码的子程序和延时子程序)

ORG 0000H
LJMP MAIN
ORG 0030H

MAIN:
   MOV 30H,#12H
   MOV 31H,#34H
   MOV 32H,#56H
   MOV R0,#30H
   MOV R1,#20H
   
BCD:
   MOV A,@R0
   SWAP A
   ANL A,#0FH
   ACALL CTAB
   MOV R2,A
   MOV A,@R0
   ANL A,#0FH
   ACALL CTAB
   MOV R3,A
   
   MOV DPTR,#8002H
   MOV A,R1
   MOVX @DPTR,A
   MOV DPTR,#8004H
   MOV A,R2
   MOVX @DPTR,A
   MOV A,R1
   RR A
   MOV R1,A
   LCALL DLEY
   
   MOV DPTR,#8002H
   MOV A,R1
   MOVX @DPTR,A
   MOV DPTR,#8004H
   MOV A,R3
   MOVX @DPTR,A
   MOV A,R1
   RR A
   MOV R1,A
   LCALL DLEY
   INC R0
   CJNE R0,#33H,BCD
   LJMP MAIN
   
CTAB:
   PUSH DPH
   PUSH DPL
   MOV DPTR,#TAB
   MOVC A,@A+DPTR
   POP DPL
   POP DPH
   RET
   
TAB:
   DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,77H,7CH,39H,5EH,79H,71H
DLEY:
   MOV R7,#02H
 DL:MOV R6,#0FFH
 DL1:DJNZ R6,DL1
      DJNZ R7,DL
	  RET  
	  END

(2)扫描矩阵键盘,将对应键盘值送LED显示

     ORG 0000H
       LJMP START
       ORG 1000H
START:
	   ACALL   KEY1
	   AJMP    START
KEY1:  ACALL   KEY14
	   JNZ     KEY3
KEY2:  ACALL   KEY16 
	   AJMP    KEY1 
KEY3:  ACALL   KEY16
	   ACALL   DELAY 
	   ACALL   KEY14
	   JNZ     KEY4
	   ACALL   KEY16
	   AJMP    KEY1

KEY4:  MOV     R2, #0FEH 
	   MOV     R4, #00H

KEY5:  MOV     DPTR,#8002H 
	   MOV     A, R2 
	   MOVX    @DPTR, A 
	   MOV     DPTR,#8001H
	   MOVX    A,@DPTR 
	   JB      ACC.0,KEY6 
	   MOV     A, #00H 
	   AJMP    KEY9

KEY6:  JB      ACC.1,KEY7 
	   MOV     A, #06H
	   AJMP    KEY9 

KEY7:  JB      ACC.2,KEY8
	   MOV     A, #0CH
	   AJMP    KEY9
	    
KEY8:  JB      ACC.3, KEY12 
	   MOV     A, #12H 
KEY9:  ADD     A, R4 
KEY10: MOV     R5,A
KEY11: ACALL   KEY14  
	   JNZ     KEY11 
	   MOV     A,R5
	   ACALL   KEY15
	   RET 

KEY12: INC     R4  
	   MOV     A, R2  
	   JNB     ACC.5,KEY13  
	   RL      A  
	   MOV     R2, A  
	   AJMP    KEY5 
	
KEY13: AJMP    KEY1 
KEY14: MOV     DPTR,#8002H 
	   MOV     A,#00H 
	   MOVX    @DPTR,A 
	   MOV     DPTR,#8001H
	   MOVX    A,@DPTR 
	   CPL     A 
	   ANL     A, #0FH 
	   RET

KEY15: MOV     DPTR,#TABLE
	   MOVC    A,@A+DPTR
	   MOV     R3,A

KEY16: MOV     DPTR,#8002H
       MOV     A,#01H
	   MOVX    @DPTR,A
       MOV     DPTR,#8004H
	   MOV     A,R3
	   MOVX    @DPTR,A
	   ACALL   DELAY
	   RET

DELAY: MOV     R7,#12
LOOP:  MOV     R6,#250
	   DJNZ    R6,$
	   DJNZ    R7,LOOP
	   RET
TABLE:

	   DB      7EH,7EH,5EH,79H,71H,3FH,7EH,7EH,39H,4FH,5BH,06H,7EH,7EH,7CH,7DH,6DH,66H	,7EH,7EH,77H,6FH,7FH,07H

       END

实验六:D/A和A/D

(1)利用实验箱上的电位器提供模拟量输入,将模拟量转换成二进制数字量,将转换的结果用P1口输出到二极管显示;

ORG 0000H
LJMP MAIN
ORG 0100H
MAIN:
    MOV DPTR,#8000H
	MOV A,#00H
	MOVX @DPTR,A
	MOV R2,#100
	DJNZ R2,$
	MOVX A,@DPTR
	MOV P1,A
	LJMP MAIN

	END

(2)用DAC0832将一个数值转换为模拟电压,该模拟电压接ADC0809的一个模拟输入进行模/数转换,转换结果通过P1口送到二极管显示。

ORG 0000H
LJMP MAIN
ORG 0100H
MAIN:
    MOV A,#90H
	MOV DPTR,#9000H
	MOVX @DPTR,A
	MOV DPTR,#8000H
	MOVX @DPTR,A
	MOV R2,#64H
	DJNZ R2,$
	MOVX A,@DPTR
	MOV P1,A
	SJMP $
	END

实验七:电子钟

实现一个24小时制的电子钟程序,在6个数码管上显示时分秒(用定时器0中断更新计时,以压缩BCD码形式保存在内部RAM的30H、31H和32H单元);可以通过串行口调时!

ORG  0000H
AJMP MAIN
ORG  000BH
LJMP CLOCK
ORG  0023H
LJMP INPUT
ORG  0100H

MAIN:
    MOV  TMOD,#21H
    CLR  A
	MOV  30H,A
	MOV  31H,A
	MOV  32H,A

	MOV  TH0,#3CH
	MOV  TL0,#0B0H
	MOV  50H,#14H	  ;50 * 20
	MOV  TH1,#0F3H
	MOV  TL1,#0F3H	  ;波特率

	MOV  SCON,#50H	
	SETB PS
	MOV  IE,#92H	
	SETB TR1
	SETB TR0
	
SLED:
    MOV  R0,#30H
	MOV  R1,#20H
	LJMP LED
	
CLOCK:

	MOV  TH0,#3CH
	MOV  TL0,#0B0H
	DJNZ 50H,RETURN
	MOV  50H,#14H
	
	MOV  A,#01H
	ADD  A,32H
	DA   A
	MOV  32H,A
	CJNE A,#60H,RETURN
	MOV  32H,#00H
	
	MOV  A,#01H
	ADD  A,31H
	DA   A
	MOV  31H,A
	CJNE A,#60H,RETURN
	MOV  31H,#00H
	
	MOV  A,#01H
	ADD  A,30H
	DA   A
	MOV  30H,A
	CJNE A,#24H,RETURN
	MOV  30H,#00H
	
RETURN:

	RETI	
	
LED:
    MOV  A,@R0
	SWAP A
	ANL  A,#0FH
	ACALL CTAB
	MOV  R2,A
	
	MOV  A,@R0
	ANL  A,#0FH
	LCALL CTAB
	MOV  R3,A
	
	MOV  DPTR,#8002H
	MOV  A,R1
	MOVX @DPTR,A
	MOV  DPTR,#8004H
	MOV  A,R2
	MOVX @DPTR,A
	MOV  A,R1
	RR   A
	MOV  R1,A
	LCALL DLY
	
	MOV  DPTR,#8002H
	MOV  A,R1
	MOVX @DPTR,A
	MOV  DPTR,#8004H
	MOV  A,R3
	MOVX @DPTR,A
	MOV  A,R1
	RR   A
	MOV  R1,A
	LCALL DLY
	
	INC  R0
	CJNE R0,#33H,LED
	LJMP SLED
	
CTAB:
    PUSH DPH
	PUSH DPL
	MOV  DPTR,#TAB
	MOVC A,@A+DPTR
	POP  DPL
	POP  DPH
	RET
	
INPUT:

	CLR  RI
	CLR  EA
	MOV  30H,SBUF
	JNB  RI,$
	CLR  RI
	MOV  31H,SBUF
	JNB  RI,$
	CLR  RI
	MOV  32H,SBUF
	SETB EA
	RETI

TAB:
    DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH
	
DLY:
    MOV  R7,#02H
 DL:
    MOV  R6,#0FFH
DL1:
    DJNZ R6,DL1
	DJNZ R7,DL
	RET
	
	END

你可能感兴趣的:(实验代码,51单片机,实验及代码)