專注差異化嵌入式產(chǎn)品解決方案 給智能產(chǎn)品定制注入靈魂給予生命
提供開(kāi)發(fā)工具、應(yīng)用測(cè)試 完善的開(kāi)發(fā)代碼案例庫(kù)分享
從全面的產(chǎn)品導(dǎo)入到強(qiáng)大技術(shù)支援服務(wù) 全程貼心伴隨服務(wù),創(chuàng)造無(wú)限潛能!
提供新的芯片及解決方案,提升客戶產(chǎn)品競(jìng)爭(zhēng)力
提供最新的單片機(jī)資訊,行業(yè)消息以及公司新聞動(dòng)態(tài)
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
點(diǎn)擊次數(shù):166次 更新時(shí)間: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_技術(shù)支持_內(nèi)容瀏覽}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