Beginner compilation C51

1, Comprehensive design experiment 1: on chip parallel I/O port application
1. Purpose of the experiment:
Learn to master the application of MCU I/O port.
2. Experimental tools:
Computer, Keil μ Vision, Puzhong experimental instrument.
3. Experiment content:
Assembly language programming and implementation in the experimental instrument: detect the state of three keys in the circuit, and realize different actions when pressing the keys. Specific requirements:
Press the key 1: the static nixie tube displays the key number, the four LED indicators are lit in turn, in the state of walking light, and the time interval between the lighting of adjacent indicators is about 1 second, which is continuously cycled;
Press key 2: the static nixie tube displays the key number, the relay acts, and an LED is on;
Press key 3: all actions end;

;P1.0 is key0 
;P1.1 is key1
;P1.2 is key2
;P1.4 is relay
;P0.0-7 is led 0
;P2.0-7 is degital displayer
ORG 0000H
	MOV P0,#0FEH
	MOV P1,#0FFH
	MOV P2,#0FFH
KEYSCAN:
	JNB P1.0,KEY0DOWN
	JNB P1.1,KEY1DOWN
	JNB P1.2,KEY2DOWN
	JMP KEYSCAN

KEY0DOWN:
	CALL DELAY200ms
	MOV A,#0
	CALL KEYNUMDISP
	CALL LEDINTERM
	JMP KEYSCAN


KEY1DOWN:
	CALL DELAY200ms
	MOV A,#1
	CALL KEYNUMDISP
	CALL RELAYLED
	JMP KEYSCAN

KEY2DOWN:
	CALL DELAY200ms
	MOV A,#2
	CALL SHUTUP
	JMP KEYSCAN



DELAY200ms:
	MOV R5,#50
DELAY200ms1:
	MOV R6,#10
DELAY200ms2:
	MOV R7,#100
	DJNZ R7,$
	DJNZ R6,DELAY200ms2
	DJNZ R5,DELAY200ms1
	RET

KEYNUMDISP:
	MOV DPTR,#DUANXIAN
	MOVC A,@A+DPTR
	MOV P2,A
	RET

LEDINTERM:
;MOV P2,#0FEH
	CALL DELAY200ms
	CALL DELAY200ms
	CALL DELAY200ms
	CALL DELAY200ms
	MOV A,P0
	RL A
	MOV P0,A
	RET

RELAYLED:
	CLR P1.4
	RET

SHUTUP:
	MOV P0,#0FFH
	MOV P1,#0FFH
	MOV P2,#0FFH
	RET

DUANXIAN: DB 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
END

Possible errors:
1.JNB and JB will not automatically stack the current PC value, so using RET in KEY0DOWN (KEY1DOWN) will jump to ORG 0000H, so JMP KEYSCAN will jump to the main function for execution. This operation will cause that only key 0 works when key 0 and key 1 are pressed at the same time. This can be used to realize the sorting of key priority.
2. It is better not to specify the address (ORG) for the code at will. This operation will greatly increase the time spent burning the program. All code can be written directly after the main function

2, Comprehensive design experiment 2: application of on-chip timer / counter
1. Purpose of the experiment:
Learn to master the application of MCU timer / counter.
2. Experimental tools:
Computer, Keil μ Vision, Puzhong experimental instrument.
3. Experiment content: assembly language programming and implementation in the experimental instrument:
Key 1 is connected to INT0 and key 2 is connected to INT1;
Press key 1: start the on-chip timer, use the timer interrupt mode to realize 1-9 seconds timing, and display the seconds on the static nixie tube;
Press key 2 to stop timer timing;

;P2 IS DIGTAL DISPLAYER
;P3.2 IS KEY1 P3.3 IS KEY2
COUNTER DATA 30H
ORG 0000H
	LJMP 0100H  ;MAIN PROGRAM
ORG 0003H
	LJMP INT_0  ;INT0 PROGRAM
ORG 000BH
	LJMP INT_T0 ;INTT0 PROGRAM
ORG 0013H
	LJMP INT_1  ;INT1 PROGRAM

ORG 0100H
	MOV A,#0
	MAIN:
	;INT 
	MOV SP,#60 ;SET SP
	MOV IE,#85H;ENABLE T0 AND T1 
	MOV IP,#00H;PRIORITY ALL LOW
	SETB IT0;LOW V TRICKER
	SETB IT1;LOW V TRICKER
	
	JMP $

	INT_0:
	MOV TMOD,#01H;T0 MOD1 INNER
	MOV TH0,#0D8H;COUNT 10MS
	MOV TL0,#0F0H
	MOV COUNTER,#100;DO THIS 100 TIMES
	SETB ET0;ENABLE TT0
	SETB TR0;START TO DO THINGS
	RETI

	INT_1:
	CLR ET0;
	RETI

	INT_T0:
	DJNZ COUNTER,NEXT1
	MOV COUNTER,#100
	CALL DISPNUM
NEXT1:
	MOV TH0,#0D8H
	ORL TL0,#0F0H
	RETI

DISPNUM:
	INC A
	CJNE A,#10,NEXT2
	MOV A,#0
	
NEXT2:
	MOV DPTR,#ARRAY
	PUSH ACC
	MOVC A,@A+DPTR
	MOV P2,A
	POP ACC
	RET

ARRAY:
	DB 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
	END

Possible errors:
1. when calling NEXT2 in DISPNUM, the lookup table instruction was used to change the value of ACC, so it was necessary to stack ACC before operation. Otherwise, the light will not be on.

3, Comprehensive design experiment 3: application of on-chip serial communication interface
1. Purpose of the experiment:
Learn to master the serial port application of single chip microcomputer.
2. Experimental tools:
Computer, Keil μ Vision, Puzhong experimental instrument.
3. Experiment content: assembly language programming and implementation in the experimental instrument:
The serial assistant sends a decimal number of 0-9, which is received by the tester and displayed on the static nixie tube.

;P2 IS DIGTAL DISPLAYER
ORG 0000H
LJMP MAIN
ORG 0023H
LJMP INTS

ORG 0100H
MAIN:
	MOV TMOD,#20H;T1 MOD 2
	MOV TH1,#0F3H;USE 4800bps CALCULATE 
	MOV TL1,#0F3H
	MOV PCON,#80H;SMOD=1
	SETB TR1;START TO COUNT
	MOV SCON,#50H;SM0=0,SM1=1-->MOD1,REN=1
	MOV IE,#90H;EA=1 ES=1

DISP:
	MOV DPTR,#ARRAY
	MOV A,SBUF
	SUBB A,#030H
	MOVC A,@A+DPTR
	MOV P2,A
	JMP DISP

INTS:
	PUSH ACC
	MOV A,SBUF
	CLR RI
	MOV SBUF,A
DO:
	JB TI,NEXT
	JMP DO
NEXT:
	CLR TI
	POP ACC
	RETI

ARRAY:
	DB 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
	END

Possible errors:
1. The difference between the data sent by the serial port and the decimal 0-9 is 0030H, which can be used only after processing the data from the serial port.

Keywords: Single-Chip Microcomputer

Added by evo4ever on Wed, 03 Nov 2021 21:13:24 +0200