Dear LEUNG LAUREN KWAN-KIT,
I know how you feal, I read Microchip application note many times until I
finally understod what they wanted to say. Here is a working
intiailization routines that will help you use the PWM on TIMER2. It is
used on the 16C74 running at 16MHz.
to use it do:
call InitCCP1
call InitTimer2
Chaipi
;--------------------------------------------------------------------
; Routine : InitTimer2 - Initialize Timer 2 to work as a PWM counter.
; in a frequency of 34KHz - 40KHz.
;
; Input : none
; Output : to timers control register
;
; called by InitTimers
; calling :
;
; Revision date change
; --------------------------
; 29/10/95 first version
;
;--------------------------------------------------------------------
InitTimer2
LowBank
;--------------------------------------------------------------------
; Clear Timer2 interrupt flag - no interrupts are needed from this timer
;--------------------------------------------------------------------
bcf PIR1, _TMR2IF
;--------------------------------------------------------------------
; Disable Timer2 interrupts - no interrupts are needed from this timer
;--------------------------------------------------------------------
HighBank
bcf PIE1 & 0x7f, _TMR2IE ; Disable interrupts
LowBank
;--------------------------------------------------------------------
; Set timer2 Prescale to 1
;--------------------------------------------------------------------
bcf T2CON, _T2CKPS0
bcf T2CON, _T2CKPS1
;--------------------------------------------------------------------
; Set timer2 Postscale to 1
;--------------------------------------------------------------------
bcf T2CON, _TOUTPS0
bcf T2CON, _TOUTPS1
bcf T2CON, _TOUTPS2
bcf T2CON, _TOUTPS3
;--------------------------------------------------------------------
; Turn timer2 on
;--------------------------------------------------------------------
bsf T2CON, _TMR2ON
return
;--------------------------------------------------------------------
; Routine : InitCCP1 - Initialize Capture/Compare/PWM module 1
; to PWM model 10 bits
;
; CCP1 module works in PWM mode. A frequency is loaded to PR2
; register and a duty cycle of 50% is loaded into CCPR1L (PR2/2).
; When timer2 is enabled, a frequncy is outputed on pin RC2/CCP1
;
; Input : none
; Output : to CCP1 control register
;
; called by : InitTimers
; calling : none
;
; Revision date change
; --------------------------
; 02/12/95 first version
;
;--------------------------------------------------------------------
InitCCP1
LowBank
;--------------------------------------------------------------------
; Set CCP1 module in PWM mode
;--------------------------------------------------------------------
bcf CCP1CON, _CCP1M0
bcf CCP1CON, _CCP1M1
bsf CCP1CON, _CCP1M2
bsf CCP1CON, _CCP1M3
;--------------------------------------------------------------------
; Set 10-bit resolution
;--------------------------------------------------------------------
bsf CCP1CON, _CCP1Y
bsf CCP1CON, _CCP1X
;--------------------------------------------------------------------
; Load an initial PWM period
;--------------------------------------------------------------------
HighBank
movlw 0x80
movwf PR2 & 0x7f
LowBank
;--------------------------------------------------------------------
; Load an initial PWM duty cycle
;--------------------------------------------------------------------
movlw 0x40
movwf CCPR1L
return
\\\|///
\\ ~ ~ //
( @ @ )
----------------------------oOOo-(_)-oOOo--------------------------------------
! !
! Chaipi Wijnbergen !
! Electronics/Computer Eng. M.Sc. Tel : +972-8-9343079 !
! Optical Imaging Laboratory Fax : +972-8-9344129 !
! Brain Research Center Email : spam_OUTchaipiTakeThisOuT
tohu0.weizmann.ac.il !
! Weizmann Institute of Science URL : http://www.weizmann.ac.il/~chaipi !
! Rehovot 76100 ISRAEL IPhone : chaipi !
! !
------------------------------------Oooo.--------------------------------------
.oooO ( )
( ) ) /
\ ( (_/
\_)
On Fri, 24 May 1996, LEUNG LAUREN KWAN-KIT wrote:
{Quote hidden}> Hi, all,
>
> I'm currently working on 16C74, and I'd like to know how the parameters
> for the PWM work? I'd like to know what values to set to the PERIOD and
> the DUTY CYCLE in order for it to make sense..... can anyone give me a
> sample in what values to set to CCPR1L/CCPR1H, PR2, or whatsoever reg to
> be set.... I have read thru many appnotes but still find it hard to
> understand.
>
> Thanks in advance.
>
> - LL.
>