DIY晤最ん厙

梓枙: PIC16F84莉汜謗郪PWM怀堤腔妗瞰埭最唗 [湖荂掛珜]

釬氪: liyf    奀潔: 2011-4-30 11:05
梓枙: PIC16F84莉汜謗郪PWM怀堤腔妗瞰埭最唗
PIC16F84莉汜謗郪PWM怀堤腔妗瞰埭最唗
;**********************************************************************
list p=16F84 ; list directive to define processor
ㄒi nclude  ; processor specific variable definitions
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_OFF & _RC_OSC
; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

;********************** CONST DEFINITIONS******************************
FULL_WORK = 10 ; 最???周期預設?
DEF_WORK1 = 2 ; PWM1??周期預設?
DEF_WORK2 = 8 ; PWM2??周期預設?
PORT_OUT = PORTB ; PWM輸?埠
PIN_PWM1 = 0 ; PWM1輸?腳?
PIN_PWM2 = 1 ; PWM2輸?腳?
;********************** VARIABLE DEFINITIONS***************************
CBLOCK 0x0C
w_temp ; variable used for context saving
status_temp ; variable used for context saving
PWM_FULL ; 最???周期計數器(?數)
PWM_WORK_CFG1 ; PWM1 ??周期設定暫?器
PWM_WORK_CFG2 ; PWM2 ??周期設定暫?器
PWM_WORK_CNT1 ; PWM1 ??周期計數器(?數)
PWM_WORK_CNT2 ; PWM2 ??周期計數器(?數)
ENDC
;**********************************************************************
;****************** 晶??置?程式進?點 ***********************
;**********************************************************************
ORG 0x000 ; processor reset vector
ResetStart
goto main ; go to beginning of program
;**********************************************************************
;****************** 中斷服務程式進?點 *************************
;**********************************************************************
ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
bsf STATUS,RP0
bcf INTCON,T0IF ; 清除計?器中斷旗號
bcf STATUS,RP0
decfsz PWM_FULL,F ; 檢查是?完成??周期
goto ISR_PWM1
bsf PORT_OUT,PIN_PWM1 ; PWM1/PWM2?復High電?
bsf PORT_OUT,PIN_PWM2
movlw FULL_WORK ; ?新設定???周期計數器
movwf PWM_FULL
movf PWM_WORK_CFG1,W
movwf PWM_WORK_CNT1
movf PWM_WORK_CFG2,W
movwf PWM_WORK_CNT2
goto ISR_EXIT
ISR_PWM1
decfsz PWM_WORK_CNT1,F ; 檢查PWM1??周期
goto ISR_PWM2
bcf PORT_OUT,PIN_PWM1 ; ??周期結??將輸?設?Low電?
ISR_PWM2
decfsz PWM_WORK_CNT2,F ; 檢查PWM2??周期
goto ISR_EXIT
bcf PORT_OUT,PIN_PWM2 ; ??周期結??將輸?設?Low電?
ISR_EXIT
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
;**********************************************************************
;****************** ?始化PWM *******************************
;**********************************************************************
InitPWM
bsf STATUS,RP0 ; 設定PWM1/PWM2腳?輸?
bcf PORT_OUT,PIN_PWM1
bcf PORT_OUT,PIN_PWM2
bcf STATUS,RP0
bsf PORT_OUT,PIN_PWM1 ; 預設PWM1/PWM2輸?高電?
bsf PORT_OUT,PIN_PWM2
movlw FULL_WORK ; 設定?暫?器之預設?
movwf PWM_FULL
movlw DEF_WORK1
movwf PWM_WORK_CFG1
movwf PWM_WORK_CNT1
movlw DEF_WORK2
movwf PWM_WORK_CFG2
movwf PWM_WORK_CNT2
retlw 0
;**********************************************************************
;****************** ?始化計?器 *******************************
;**********************************************************************
InitTimer
bsf STATUS,RP0
bsf OPTION_REG,T0CS ; 停止?盪器?波輸?
bsf OPTION_REG,PSA ; 設定不使用?波預除器
bsf INTCON,GIE ; 打開?域中斷開關(中斷總開關)
bcf INTCON,T0IF ; 清除計?器中斷旗號
bcf STATUS,RP0
retlw 0
;**********************************************************************
;****************** 啟動計?器 *******************************
;**********************************************************************
StartTimer
bsf STATUS,RP0
bcf OPTION_REG,T0CS ; 設定?盪器?波輸?(1/4 osc)
bsf INTCON,T0IE ; 打開計?器中斷
bcf STATUS,RP0
retlw 0
;**********************************************************************
;****************** 停止計?器 *******************************
;**********************************************************************
StopTimer
bsf STATUS,RP0
bsf OPTION_REG,T0CS ; 停止?盪器?波輸?
bcf INTCON,T0IE ; 關?計?器中斷
bcf STATUS,RP0
retlw 0
;**********************************************************************
;****************** ?程式進?點 *******************************
;**********************************************************************
main
bcf STATUS,RP0 ; select Page 0 for Port Access
call InitPWM
call InitTimer
call StartTimer
goto $
;**********************************************************************
END ; directive 'end of program'




辣茩嫖還 DIY晤最ん厙 (http://diybcq.com/) Powered by Discuz! X3.2