200ms调一次脉宽 一次变动0.4ms 周期为20ms list p=16F54 #include <p16f5x.inc>
__CONFIG _CP_OFF & _WDT_OFF & _HS_OSC
WIDE EQU 11H DO_200M EQU 12H TURN EQU 13H MID EQU 14H ORG 0x1FF ; processor reset vector GOTO START ORG 0x000
;初始化 START NOP CLRF 10H MOVFW 10H TRIS PORTB CLRF PORTB BSF PORTB,3 MOVLW 02H OPTION ;8分频 MOVLW .206 MOVWF TMR0 ;定时400us MOVLW .50 MOVWF WIDE ;脉宽20ms MOVLW .10 MOVWF DO_200M ;200ms调整一次占空比 MOVLW .50 MOVWF TURN ;初值为全占空 MOVWF MID LOOP ;0.4ms MOVFW TMR0 BTFSS STATUS,Z GOTO LOOP LOOP1 MOVLW .206 MOVWF TMR0 ;灭 DECFSZ TURN,F ; GOTO $+4 BSF PORTB,2 MOVFW MID MOVWF TURN ;判断是否为20ms DECFSZ WIDE,F GOTO LOOP ;亮 BCF PORTB,2 MOVLW .50 MOVWF WIDE MOVFW MID MOVWF TURN DECFSZ DO_200M,F GOTO LOOP ;递减脉宽 MOVLW .10 MOVWF DO_200M DECFSZ MID,F GOTO LOOP BSF PORTB,2 BCF PORTB,3 GOTO $ |