天堂草原最受欢迎的角色,天堂动漫,天堂在线,色天堂下载,天堂中文在线资源,亚洲男人天堂

技術熱線: 4007-888-234
設計開發

專注差異化嵌入式產品解決方案 給智能產品定制注入靈魂給予生命

開發工具

提供開發工具、應用測試 完善的開發代碼案例庫分享

技術支持

從全面的產品導入到強大技術支援服務 全程貼心伴隨服務,創造無限潛能!

新品推廣

提供新的芯片及解決方案,提升客戶產品競爭力

新聞中心

提供最新的單片機資訊,行業消息以及公司新聞動態

Using the PWM hardware

更新時間: 2019-03-25
閱讀量:1859

16F876 PWM example code
;
; Device 16F876
    LIST P=16F876, W=2, X=ON, R=DEC
    #INCLUDE P16F876.INC
    __CONFIG    0x393A

cblock 0x20 ;start of general purpose registers
count ;used in delay routine
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
temp ;temp storage
endc

RL Equ 0x00 ;pin for left motor reverse
FL Equ 0x03 ;pin for left motor forward
RR Equ 0x04 ;pin for right motor reverse
FR Equ 0x05 ;pin for right motor forward

;pins 1 and 2 are the 2 PWM channels



    ORG 0x0000
    NOP ;for bootloader compatibility
    NOP
    NOP
    GOTO START
    ORG 0x0010

START CALL Initialise

MainLoop:
MOVLW d'64
CALL SpeedL ;both half speed forwards
CALL SpeedR
CALL Long_Delay

MOVLW d'64
CALL SpeedL ;left half speed forwards

MOVLW d'192
CALL SpeedR ;right half speed reverse
CALL Long_Delay

MOVLW d'10
CALL SpeedL ;slow speed forwards
MOVLW d'228
CALL SpeedR ;fast speed reverse
CALL Long_Delay

MOVLW d'228
CALL SpeedL ;fast speed reverse
MOVLW d'10
CALL SpeedR ;slow speed forwards
CALL Long_Delay

GOTO MainLoop

Initialise:

 BANKSEL  ADCON1 ;turn off A2D
    MOVLW    0x06
    MOVWF    ADCON1
    BANKSEL  PORTA
    BANKSEL  TRISC
    MOVLW    0 ;set PORTC as all outputs
    MOVWF    TRISC
    BANKSEL  PORTC

   MOVF     CCP1CON,W ;set CCP1 as PWM
    ANDLW    0xF0
    IORLW    0x0C
    MOVWF    CCP1CON

    MOVF     CCP2CON,W ;set CCP2 as PWM
    ANDLW    0xF0
    IORLW    0x0C
    MOVWF    CCP2CON

    MOVLW    126 ;set highest PWM value
    BANKSEL  PR2 ;over this (127) is permanently on
    MOVWF    PR2
    BANKSEL  TMR2

    MOVF     T2CON,W ;set prescaler to 16

ANDLW    0xF8 ;PWM at 2500HZ
    IORLW    0x02
    MOVWF    T2CON

    MOVF     T2CON,W ;set postscaler to 1
    ANDLW    0x07
    IORLW    0x00
    MOVWF    T2CON
   
    CLRF CCPR1L ;set PWM to zero
    CLRF CCPR2L

    BSF      T2CON, TMR2ON ;and start the timer running
RETURN

SpeedL: ;use value in W to set speed (0-127)
    MOVWF temp
BTFSC temp, 7 ;if more than 128 set speed in reverse
CALL ReverseL ;so '1' is very slow forward
BTFSS temp, 7 ;and '129' is very slow reverse
CALL ForwardL

ANDLW 0x7F
    MOVWF   CCPR1L
RETURN

SpeedR:
    MOVWF temp
BTFSC temp, 7
CALL ReverseR
BTFSS temp, 7
CALL ForwardR
ANDLW 0x7F
    MOVWF   CCPR2L
RETURN

ReverseL:
BSF PORTC, RL ;set pins for reverse
BCF PORTC, FL
RETURN

ReverseR:
BSF PORTC, RR
BCF PORTC, FR
RETURN

ForwardL:
BCF PORTC, RL ;set pins for forward
BSF PORTC, FL
RETURN

ForwardR:
BCF PORTC, RR
BSF PORTC, FR
RETURN

;Delay routines

Long_Delay
movlw d'50' ;delay 5 seconds
call Delay100W
return

Delay100W movwf count ;delay W x 100mS
d2 call Delay100 ;maximum delay 25.5 seconds
decfsz count ,f
goto d2
return

Delay255 movlw 0xff ;delay 255 mS
goto d0
Delay100 movlw d'100' ;delay 100mS
goto d0
Delay50 movlw d'50' ;delay 50mS
goto d0
Delay20 movlw d'20' ;delay 20mS
goto d0
Delay10 movlw d'10' ;delay 10mS
goto d0
Delay1 movlw d'1' ;delay 1mS
goto d0
Delay5 movlw 0x05 ;delay 5.000 ms (4 MHz clock)
d0 movwf count1
d1 movlw 0xE7
movwf counta
movlw 0x04
movwf countb
Delay_0 decfsz counta, f
goto

Using the PWM hardware

點擊次數:166次        更新時間:2017-12-12

16F876 PWM example code
;
; Device 16F876
    LIST P=16F876, W=2, X=ON, R=DEC
    #INCLUDE P16F876.INC
    __CONFIG    0x393A

cblock 0x20 ;start of general purpose registers
count ;used in delay routine
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
temp ;temp storage
endc

RL Equ 0x00 ;pin for left motor reverse
FL Equ 0x03 ;pin for left motor forward
RR Equ 0x04 ;pin for right motor reverse
FR Equ 0x05 ;pin for right motor forward

;pins 1 and 2 are the 2 PWM channels



    ORG 0x0000
    NOP ;for bootloader compatibility
    NOP
    NOP
    GOTO START
    ORG 0x0010

START CALL Initialise

MainLoop:
MOVLW d'64
CALL SpeedL ;both half speed forwards
CALL SpeedR
CALL Long_Delay

MOVLW d'64
CALL SpeedL ;left half speed forwards

MOVLW d'192
CALL SpeedR ;right half speed reverse
CALL Long_Delay

MOVLW d'10
CALL SpeedL ;slow speed forwards
MOVLW d'228
CALL SpeedR ;fast speed reverse
CALL Long_Delay

MOVLW d'228
CALL SpeedL ;fast speed reverse
MOVLW d'10
CALL SpeedR ;slow speed forwards
CALL Long_Delay

GOTO MainLoop

Initialise:

 BANKSEL  ADCON1 ;turn off A2D
    MOVLW    0x06
    MOVWF    ADCON1
    BANKSEL  PORTA
    BANKSEL  TRISC
    MOVLW    0 ;set PORTC as all outputs
    MOVWF    TRISC
    BANKSEL  PORTC

   MOVF     CCP1CON,W ;set CCP1 as PWM
    ANDLW    0xF0
    IORLW    0x0C
    MOVWF    CCP1CON

    MOVF     CCP2CON,W ;set CCP2 as PWM
    ANDLW    0xF0
    IORLW    0x0C
    MOVWF    CCP2CON

    MOVLW    126 ;set highest PWM value
    BANKSEL  PR2 ;over this (127) is permanently on
    MOVWF    PR2
    BANKSEL  TMR2

    MOVF     T2CON,W ;set prescaler to 16

ANDLW    0xF8 ;PWM at 2500HZ
    IORLW    0x02
    MOVWF    T2CON

    MOVF     T2CON,W ;set postscaler to 1
    ANDLW    0x07
    IORLW    0x00
    MOVWF    T2CON
   
    CLRF CCPR1L ;set PWM to zero
    CLRF CCPR2L

    BSF      T2CON, TMR2ON ;and start the timer running
RETURN

SpeedL: ;use value in W to set speed (0-127)
    MOVWF temp
BTFSC temp, 7 ;if more than 128 set speed in reverse
CALL ReverseL ;so '1' is very slow forward
BTFSS temp, 7 ;and '129' is very slow reverse
CALL ForwardL

ANDLW 0x7F
    MOVWF   CCPR1L
RETURN

SpeedR:
    MOVWF temp
BTFSC temp, 7
CALL ReverseR
BTFSS temp, 7
CALL ForwardR
ANDLW 0x7F
    MOVWF   CCPR2L
RETURN

ReverseL:
BSF PORTC, RL ;set pins for reverse
BCF PORTC, FL
RETURN

ReverseR:
BSF PORTC, RR
BCF PORTC, FR
RETURN

ForwardL:
BCF PORTC, RL ;set pins for forward
BSF PORTC, FL
RETURN

ForwardR:
BCF PORTC, RR
BSF PORTC, FR
RETURN

;Delay routines

Long_Delay
movlw d'50' ;delay 5 seconds
call Delay100W
return

Delay100W movwf count ;delay W x 100mS
d2 call Delay100 ;maximum delay 25.5 seconds
decfsz count ,f
goto d2
return

Delay255 movlw 0xff ;delay 255 mS
goto d0
Delay100 movlw d'100' ;delay 100mS
goto d0
Delay50 movlw d'50' ;delay 50mS
goto d0
Delay20 movlw d'20' ;delay 20mS
goto d0
Delay10 movlw d'10' ;delay 10mS
goto d0
Delay1 movlw d'1' ;delay 1mS
goto d0
Delay5 movlw 0x05 ;delay 5.000 ms (4 MHz clock)
d0 movwf count1
d1 movlw 0xE7
movwf counta
movlw 0x04
movwf countb
Delay_0 decfsz counta, f
goto {D_L_技術支持_內容瀏覽}2

decfsz countb, f
goto Delay_0

decfsz count1 ,f
goto d1
return

;end of Delay routines

    END


2

decfsz countb, f
goto Delay_0

decfsz count1 ,f
goto d1
return

;end of Delay routines

    END


联系我们: 宜宾市| 东丰县| 和平区| 久治县| 宁安市| 霍州市| 汝州市| 双柏县| 田林县| 武鸣县| 昂仁县| 上犹县| 靖边县| 林州市| 军事| 马龙县| 永兴县| 九龙县| 涟水县| 都江堰市| 靖西县| 县级市| 合阳县| 金昌市| 长顺县| 临夏县| 五华县| 遂昌县| 满洲里市| 临海市| 石林| 谢通门县| 阿拉善左旗| 遂平县| 集贤县| 泗阳县| 三江| 澄城县| 开平市| 吴忠市| 工布江达县|