;*************************************************
;* 本程序是一个LED灯的循环闪烁程序,用INT键来切换*
;* 闪烁模式 *www.dangdangdz.com
;*************************************************
include <p16f877.inc>
;*----
MODESEL equ 20h
MODESELB equ 21h
Count equ 22h
Count1 equ 23h
Count2 equ 24h
PORTDB equ 25h
;*-----
org 0
goto start
org 4
goto ISR
org 10
start
clrf PORTD ;清D口
movlw 00h
movwf MODESEL ;初始化模式选择寄存器
movwf MODESELB
movlw b'10010000'
movwf INTCON ;初始化中断控制
bsf STATUS,RP0
clrf TRISD ;设D口全为输出
movlw b'10111111'
OPTION ;选择INT下降沿有效
bcf STATUS,RP0
call FMsel
movwf PORTDB
movwf PORTD
main btfsc PORTB,0 ;\
goto $+6 ; \
call Delay ; 按键去抖动
btfsc PORTB,0
goto $+3 ; /
call FMsel ;/
movwf PORTDB
movf MODESELB,W ;
movwf MODESEL ;
call LongDelay
bcf STATUS,C
rlf PORTDB,1
btfsc STATUS,C
bsf PORTDB,0
movf PORTDB,W
movwf PORTD
goto main
;*----------
Delay ; call指令占用2个指令周期
clrf Count ; 清 Count占用1个指令周期
Dloop
decfsz Count,f ; 这两行指令将延时
goto Dloop ; (256 * 3) -1 个指令周期
return ; return占用2个指令周期
;*----------------
LongDelay
clrf Count
clrf Count1
movlw 0x01
movwf Count2
LDloop
decfsz Count,f
goto LDloop
decfsz Count1,f
goto LDloop
decfsz Count2,f
goto LDloop
return
;*---------------
FMsel
movf MODESEL,w
movwf MODESELB
movf MODESEL,w
addwf PCL
retlw b'11111000'
retlw b'11110000'
retlw b'11100000'
retlw b'11000000'
retlw b'10000000'
movlw 00h
movwf MODESELB
bsf INTCON,GIE
retlw b'11111000'
return
;*-------------
ISR
btfss INTCON,INTF
goto $+3
bcf INTCON,INTF
incf MODESEL
retfie
end
单片机控制LED灯闪烁的程序编制方法
1:程序开始(START)
2:A=00000000B
3:PORT 0
4:延迟(DELAY)
5:A=11111111B
6:PORT 0
7:延迟(DELAY)
8:反回开始
延迟子程序编制方法:
1:R6=0
2:DEL1
3:R7=0
4:$
5:R7-1
6:R7=0 不等于0,返回2
7:R6-1
8:R6=0 不等于0,返回4
回主程序