Friday, October 28, 2005

PWM example on 16F648A

Yep, I've got PWM working fine on my 16F648A...


title "Blame PWM test"
list p=16f648a,f=inhx32
#include

__config _INTOSC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF & _MCLRE_OFF & _LVP_OFF

org 00h
goto Start
org 0x005

Start
BANKSEL PCON
BSF PCON,3 ; clear bit 3 of pcon (OSCF) for 4 kHz operation.

MOVLW 0xFF ;PWM value
BANKSEL PR2
MOVWF PR2

BANKSEL CCPR1L
MOVLW 0x1F ; 50% duty cycle = 0x7F
MOVWF CCPR1L

;note, I'm ignoring the duty cycle LSBs at CCP1CON<5:4>

BANKSEL TRISB
BCF TRISB,3 ;make CCP1 an output
BANKSEL T2CON
BSF T2CON,2 ;enable timer 2
BSF T2CON,1 ;1:16 prescaler

BANKSEL CCP1CON
BSF CCP1CON,3
BSF CCP1CON,2 ;configure CCP1 for PWM

loop GOTO loop
end

No comments: