⑴ 求單片機編程找最大數
程序如下:
MOV
R0,
#21H
;起始地址.
MOV
MAX,#00H
;先用0當做最大值.
MOV
R2,
#20H
;數據長度.
LOOP:
MOV
A,
@R0
;取來一個數據.
CLR
C
SUBB
A,
MAX
;減「最大值」.
JNC
rel1
;不夠減就算了.
MOV
A,
@R0
;夠減,就把這個數據.
MOV
MAX,A
;當做最大值.
rel1:
INC
R0
;轉向下一個.
DJNZ
R2,
LOOP
;循環.
SJMP
$
;結束.
END
⑵ 單片機數碼管編程 匯編語言,急
U16INC MACRO U16,;;16位無符號型變數加1
LOCALZZ
INC U16+1
MOV A,U16+1
JNZ ZZ
MOV U16+1,#0
INC U16
ZZ:
ENDM
U16MOV MACRO U16,VAL;;16位無符號型變數賦值
MOV U16+1,#LOW(VAL)
MOV U16,#HIGH(VAL)
ENDM
U16DAYV MACRO U16,VAL,;;16位無符號型變數判斷大小,最後根據Cy判斷
MOV A,U16+1
CLR C
SUBB A,#LOW(VAL)
MOV A,U16
SUBB A,#HIGH(VAL)
ENDM
WX1 EQU P2.0
WX2 EQU P2.1
WX3 EQU P2.2
WX4 EQU P2.3
S4 EQU P1.3
S5 EQU P1.4
LED1 EQU P1.6;;兩個LED
LED2 EQU P1.7
JDQ EQU P1.5;;繼電器
SMGBZ1 EQU 0X30
DS1 EQU 0X31
ZT EQU DS1+4
JSBZ1 EQU ZT+1;0-39的計數
JSBZ2 EQU ZT+2;計時標志,用於閃爍和計時
JSBZ3 EQU JSBZ2+2;;繼電器通斷兩次的計時和計數
ORG 0
JMP MAIN
ORG 0XB
JMP TimeInterupt
MAIN:
MOV SP,#0X70
MOV R0,#0x70
CLR A
MA1: MOV @R0,A ;數據全清0
DJNZ R0,MA1
CALL JDQLC_STOP
CALL Time0Init
;主循環
LOOP: CALL AJPD
CALL RUN
JMP lOOP
;主運行函數
RUN: MOV A,ZT
CJNE A,#1,RR1
JMP RUN1
RR1: CJNE A,#2,RR2
JMP RUN2
RR2: RET
;;0-39循環
RUN1:
MOV DS1+0,#0X10
MOV DS1+1,#0X10
MOV A,JSBZ1
MOV B,#10
DIV AB
MOV DS1+2,A
MOV DS1+3,B
U16DAYV JSBZ2,1000
JC RUN1_1
U16MOV JSBZ2,0
INC JSBZ1
MOV A,JSBZ1
CJNE A,#39+1,RUN1_1
MOVJSBZ1,#0
RUN1_1:
RET
;;FFFF閃爍
RUN2: U16DAYV JSBZ2,500
JC RUN2_1
MOV DS1+0,#0X10
MOV DS1+1,#0X10
MOV DS1+2,#0X10
MOV DS1+3,#0X10
SETB LED1
SETB LED2
U16DAYV JSBZ2,1000
JC RUN2_2
U16MOV JSBZ2,0
JMP RUN2_2
RUN2_1:
MOV DS1+0,#0X0F
MOV DS1+1,#0X0F
MOV DS1+2,#0X0F
MOV DS1+3,#0X0F
CLR LED1
CLR LED2
RUN2_2:
CALL JDQLC
RET
;按鍵判斷
AJPD:
JNB S4,AJ1
JNB S5,AJ2
RET
AJ1: JNB S4,$
MOV ZT,#2
CALL JDQLC_START
RET
AJ2: CALL JDQLC_STOP
SETB LED1
SETB LED2
JNB S5,$
MOV ZT,#1
MOV JSBZ1,#0
U16MOV JSBZ2,0
RET
JDQLC_START:
U16MOV JSBZ3,0
RET
JDQLC_STOP:
U16MOV JSBZ3,30000
SETB JDQ
RET
;繼電器通斷兩次
JDQLC:
U16DAYV JSBZ3,1000
JNC JDQ_1
CLR JDQ;通1s
RET
JDQ_1: U16DAYV JSBZ3,1500
JNC JDQ_2
SETB JDQ;斷0.5s
RET
JDQ_2: U16DAYV JSBZ3,1500+1000
JNC JDQ_3
CLR JDQ;通1s
RET
JDQ_3: U16DAYV JSBZ3,3000
JNC JDQ_4
SETB JDQ;斷
JDQ_4: RET
;;定時器初始化
Time0Init:
MOV TMOD,#0X01
MOV TH0,#HIGH(65536-1000)
MOV TL0,#LOW(65536-1000)
SETB TR0
SETB ET0
SETB EA
RET
;;數碼顯示
SMXS:
INC SMGBZ1
MOV A,SMGBZ1
SUBB A,#4
JC SMXS1
MOV SMGBZ1,#0
SMXS1:
MOV P2,#0XF0
MOV A,SMGBZ1
ADD A,#DS1
MOV R0,A
MOV A,@R0
MOV DPTR,#TABD
MOVC A,@A+DPTR
MOV P0,A
MOV DPTR,#TABW
MOV A,SMGBZ1
MOVC A,@A+DPTR
MOV P2,A
RET
TABD:;段碼
DB0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
DB0X88,0X83,0XC6,0XA1,0X86,0X8E,0XFF,0XBF
TABW:;位碼
DB0xF1,0xF2,0xF4,0xF8
;;定時器0中斷
TimeInterupt:
PUSH PSW
PUSH ACC
MOV TH0,#HIGH(65536-1000)
MOV TL0,#LOW(65536-1000)
CALL SMXS
U16INC JSBZ2
U16DAYV JSBZ3,30000
JNC TimeOut
U16INC JSBZ3
TimeOut:
POP ACC
POP PSW
RETI
END