;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Copyright (c) 2013 Manolis Agkopian ;
;See the file LICENCE for copying permission. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; THE FOLLOWING STEPS SHOULD BE TAKEN WHEN CONFIGURING THE CCP MODULE FOR PWM OPERATION: ;
; ;
; 1. SET THE PWM PERIOD BY WRITING TO THE PR2 REGISTER. ;
; 2. SET THE PWM DUTY CYCLE BY WRITING TO THE CCPR1L REGISTER AND CCP1CON<5:4> BITS. ;
; 3. MAKE THE CCP1 PIN AN OUTPUT BY CLEARING THE TRISC<2> BIT. ;
; 4. SET THE TMR2 PRESCALE VALUE AND ENABLE TIMER2 BY WRITING TO T2CON. ;
; 5. CONFIGURE THE CCP1 MODULE FOR PWM OPERATION. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; PROCESSOR '16F876A'
INCLUDE <P16F876A.INC>
__CONFIG _XT_OSC & _WDT_OFF & _PWRTE_OFF & _CP_OFF & _LVP_OFF & _BODEN_OFF
CBLOCK 0x20
D1
D2
FADE_STATE ;IF = 0x00 INCREMENT CCPR1L ELSE DECREMENT CCPR1L
ENDC
ORG 0x0000
INIT:
;PWM PERIOD = [(PR2)+1] * 4 * TOSC * (TMR2 PRESCALE VALUE) ;PR2 = TMR2 PERIOD REGISTER, TOSC = PIC CLOCK PERIOD (FOSC = 1 / TOSC)
;PWM DUTY CYCLE = (CCPR1L:CCP1CON<5:4>) * TOSC * (TMR2 PRESCALE VALUE)
;;;SET PWM FREQUENCY;;;
BSF STATUS, RP0 ;SELECT BANK 01
MOVLW D'128' ;SET PR2 TO 128 DECIMAL SO THE PWM PERIOD = 2064uS => PWM FREQUENCY = 484Hz
MOVWF PR2
BCF STATUS, RP0 ;SELECT BANK 00
;;;SET PWM STARTING DUTY CYCLE;;;
CLRF CCPR1L
MOVLW B'00001100' ;SET PWM MODE, BITS 5 AND 4 ARE THE TWO LSBs OF THE 10BIT DUTY CYCLE REGISTER (CCPR1L:CCP1CON<5:4>)
MOVWF CCP1CON
;;;SET PWM PIN TO OUTPUT MODE;;;
BSF STATUS, RP0 ;SELECT BANK 01
BCF TRISC, 2 ;SET RC2 AS OUTPUT, TO USE FOR PWM
BCF STATUS, RP0 ;SELECT BANK 00
;;;SET TIMER 2 PRESCALE VALUE;;;
;PRESCALE = 16 SO THE PWM PERIOD = 2064uS => PWM FREQUENCY = 484Hz
MOVLW B'00000010'
MOVWF T2CON
;;;CLEAR TIMER 2 MODULE;;;
CLRF TMR2
;;;ENABLE TIMER 2 MODULE;;;
BSF T2CON, TMR2ON
CLRF FADE_STATE
MAIN:
CALL DELAY
MOVLW 0x00
IORWF FADE_STATE, W
BTFSS STATUS, Z ;IF FADE_STATE == 0 GOTO INC_CCPR1L
GOTO DEC_CCPR1L ;ELSE GOTO DEC_CCPR1L
INC_CCPR1L:
INCFSZ CCPR1L ;INCREMENT CCPR1L
GOTO MAIN
GOTO CHANGE_STATE_0 ;IF WE HAVE AN OVERFLOW GOTO CHANGE_STATE
DEC_CCPR1L:
DECFSZ CCPR1L ;DECREMENT CCPR1L
GOTO MAIN
;IF WE HAVE AN OVERFLOW GOTO CHANGE_STATE
CHANGE_STATE:
COMF FADE_STATE, F ;TOGLE FADE_STATE BITS
INCFSZ CCPR1L
GOTO MAIN
CHANGE_STATE_0:
COMF FADE_STATE, F ;TOGLE FADE_STATE BITS
DECFSZ CCPR1L
GOTO MAIN
DELAY
;9993 CYCLES
MOVLW 0xCE
MOVWF D1
MOVLW 0x08
MOVWF D2
DELAY_0
DECFSZ D1, F
GOTO $ + 2
DECFSZ D2, F
GOTO DELAY_0
;3 CYCLES
GOTO $ + 1
NOP
;4 CYCLES (INCLUDING CALL)
RETURN
END
hello
please can i use 20mhz instead off 16mhz
Yes you can use 20 mhz if you need output with 60 hz.
For 50 hz output with 20mhz crystal you also need to modify value in timer register.
How can we modify the frequency to 20kHz on inverter mode?
How can we modify the frequency to 20kHz on inverter mode?
- - - Updated - - -
Which part of the code can we change So as the pwm will be 20kHz?
You will need to change the value of the period register (PR2). Use the formular above to calculate PR2.
Lookout for this App. MistrE PICMulticalc it will save you a lot of headache in punching calculations. plus you get other freebies from it like UART, PWM and timer calculation.
Hope it helps
Tunde.
- - - Updated - - -
You will need to change the value of the period register (PR2). Use the formular above to calculate PR2.
Lookout for this App. MistrE PICMulticalc it will save you a lot of headache in punching calculations. plus you get other freebies from it like UART, PWM and timer calculation.
Hope it helps
Tunde.
Which part of the code needs to be altered So as the frequency of the pwm will increase to 20khz
something strange i notice in this schematic diagram the bootstrap capacitor used in this design was 220uf which i believe is too high and the bootstrap diode also instead of high freq diode to be used 1n4007 was used i am asking members here do you think this values were ok?
the bootstrap capacitor values in determine by the switching frequency according to the datasheets but with this inverter switching frequency the capacitor value shouldn't be high like this...
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?