您现在的位置是:首页 > 单片机

PIC16F84产生两组PWM输出的实例源程序

2020-03-21 02:15:13

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  ; 程???㏄戳箇砞?
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'