PIC16F84产生两组PWM输出的实例源程序
PIC16F84产生两组PWM输出的实例源程序
;**********************************************************************
list p=16F84 ; list directive to define processor
#i nclude p16F84.inc> ; 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 ; 程???P戳w]?
DEF_WORK1 = 2 ; PWM1??P戳w]?
DEF_WORK2 = 8 ; PWM2??P戳w]?
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 ; 程???P戳p计竟(?计)
PWM_WORK_CFG1 ; PWM1 ??P戳]w既?竟
PWM_WORK_CFG2 ; PWM2 ??P戳]w既?竟
PWM_WORK_CNT1 ; PWM1 ??P戳p计竟(?计)
PWM_WORK_CNT2 ; PWM2 ??P戳p计竟(?计)
ENDC
;**********************************************************************
;****************** 垂??m?{Αi?I ***********************
;**********************************************************************
ORG 0x000 ; processor reset vector
ResetStart
goto main ; go to beginning of program
;**********************************************************************
;****************** い_A叭{Αi?I *************************
;**********************************************************************
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 ; M埃p?竟い_X腹
bcf STATUS,RP0
decfsz PWM_FULL,F ; 浪dO?ЧΘ??P戳
goto ISR_PWM1
bsf PORT_OUT,PIN_PWM1 ; PWM1/PWM2?_Highq?
bsf PORT_OUT,PIN_PWM2
movlw FULL_WORK ; ?s]w???P戳p计竟
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 ; 浪dPWM1??P戳
goto ISR_PWM2
bcf PORT_OUT,PIN_PWM1 ; ??P戳挡??N块?]?Lowq?
ISR_PWM2
decfsz PWM_WORK_CNT2,F ; 浪dPWM2??P戳
goto ISR_EXIT
bcf PORT_OUT,PIN_PWM2 ; ??P戳挡??N块?]?Lowq?
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
;**********************************************************************
;****************** ?lてPWM *******************************
;**********************************************************************
InitPWM
bsf STATUS,RP0 ; ]wPWM1/PWM2}?块?
bcf PORT_OUT,PIN_PWM1
bcf PORT_OUT,PIN_PWM2
bcf STATUS,RP0
bsf PORT_OUT,PIN_PWM1 ; w]PWM1/PWM2块?蔼q?
bsf PORT_OUT,PIN_PWM2
movlw FULL_WORK ; ]w?既?竟ぇw]?
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
;**********************************************************************
;****************** ?lてp?竟 *******************************
;**********************************************************************
InitTimer
bsf STATUS,RP0
bsf OPTION_REG,T0CS ; 氨ゎ?历竟?i块?
bsf OPTION_REG,PSA ; ]wぃㄏノ?iw埃竟
bsf INTCON,GIE ; ゴ}?办い_}闽(い_`}闽)
bcf INTCON,T0IF ; M埃p?竟い_X腹
bcf STATUS,RP0
retlw 0
;**********************************************************************
;****************** 币笆p?竟 *******************************
;**********************************************************************
StartTimer
bsf STATUS,RP0
bcf OPTION_REG,T0CS ; ]w?历竟?i块?(1/4 osc)
bsf INTCON,T0IE ; ゴ}p?竟い_
bcf STATUS,RP0
retlw 0
;**********************************************************************
;****************** 氨ゎp?竟 *******************************
;**********************************************************************
StopTimer
bsf STATUS,RP0
bsf OPTION_REG,T0CS ; 氨ゎ?历竟?i块?
bcf INTCON,T0IE ; 闽?p?竟い_
bcf STATUS,RP0
retlw 0
;**********************************************************************
;****************** ?{Αi?I *******************************
;**********************************************************************
main
bcf STATUS,RP0 ; select Page 0 for Port Access
call InitPWM
call InitTimer
call StartTimer
goto $
;**********************************************************************
END ; directive 'end of program'
实例 源程序 输出 PWM 产生 PIC16F84 相关文章:
- 国外现场总线应用实例两则(12-21)
- 三菱PLC编程技巧及实例图解(12-19)
- 单片机的七个创意设计实例(从易到难),附电路图、源代码、演示视频(06-11)
- PCI驱动程序开发实例(02-21)
- 51单片机做信号发生器实例编程(01-16)
- 简单的单片机I/O口扩展实例(05-18)