i am using 18f4520, Fosc=4Mhz and the led is connected to RB3. The led is working fine, i have checke that. I am not getting pwm output.
here is my code:
Any help would be appreciated.
here is my code:
Code:
;********************************************************************
;* FileName: pwm.asm
;* Processor: PIC18F4520
;* Compiler: MPLAB C18 v.3.06
;*
;* (description)
;* Using pwm to control the brightness of a LED
;*
;* 4khz pwm frequency, Fosc = 4Mhz
;*
;* PWM period = 1 / 4khz = 0.25 msec
;*
;* 0.25 ms = [(PR2) + 1] * 4 * 1 / 4Mhz * 1
;* 0.25 ms = [(PR2) + 1] 1 / 1Mhz
;* 250 = PR2 + 1
;* PR2 = 250 or 0xFA (1111 1010)
;*
;*PWM RESOLUTION: pwm resolution = (log (Fosc/Fpwm)) / (log(2)) bits
;* = log (4Mhz/4Khz) / log(2)
;* = 9
;*that means 0 <= (CCPRXL:CCPXCON<5:4>) <= 511
;*
;*PWM Duty Cycle = (CCPRXL:CCPXCON<5:4>)* Tosc * (TMR2 Prescale Value)
;*For 50% duty cycle: PWM period / 2 = 0.125 msec
;* 0.125 msec = (CCPRXL:CCPXCON<5:4>) * 1 / Fosc * 1
;* (CCPRXL:CCPXCON<5:4>) = 500 (0111 1101 00)
;* CCPRXL = 0x7D
;* CCPXCON<5:4> = 00
;*
;*
;*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
LIST P=18F4520 ;directive to define processor
#include <P18F4520.INC>
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Configuration bits
CONFIG OSC = EC ;external 4MHz oscillator
CONFIG WDT = OFF ;watch dog timer off
CONFIG LVP = OFF
CONFIG BOREN = OFF
CCP2MX = PORTB ;use RB3 for pwm output
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Variables
pr2_val EQU 0xFA ;needed for 4khz pwm frequency
ccprxl_val EQU 0x7D ;needed for duty cycle
ccp1con_val EQU 0x0C ;includes 2 lsb's for duty cycle
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Reset Vector
ORG 0x0000
goto Main ;goto start of main code
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Main starts here
Main:
call INIT_PORT
call INIT_TMR2
pwm_settings:
;sets our duty cycle
movlw ccp1con_val
movwf CCP2CON
movlw ccprxl_val
movwf CCPR2L
here: bra here
INIT_PORT:
clrf PORTB
movlw 0x0F
movwf ADCON1
movlw 0x00
movwf TRISB ;make portb output
return
INIT_TMR2:
bsf PIE1,1 ;timer2 interrupt enables
bcf T2CON,1 ;1:1 prescalar
bcf T2CON,0 ;
movlw pr2_val
movwf PR2
bsf T2CON,2 ;start timer2
bra pwm_settings
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;End of program
END
Any help would be appreciated.