PDA

View Full Version : Is it possible to change HPWM duty cycle without restarting the PWM generator?



CuriousOne
- 9th December 2015, 06:28
Hello.

I'm doing some experiments with music synthesis, using hardware pwm as tone source, and it's rectified duty cycle voltage as envelope generator. The issue is that for example, when I want to change PWM duty cycle, HPWM statement "restarts" PWM generator each time, which causes undesirable sound effects. At certain point, it can be used for sound effect modulation, but I'd like to get rid of it. Here's example of it's proper usage: https://www.youtube.com/watch?v=E164zNFB378&feature=youtu.be


So, say I have a code like this:



FOR A=1 TO 100
HPWM 1,A,1000
NEXT


HPWM statement on each cycle momentarily shuts off PWM generator and re-launches it with new parameters, which causes "click" in sound. Is it possible to alter duty cycle of already running generator, without stopping it?

pedja089
- 9th December 2015, 08:02
Load duty cycle registers directly, without using HPWM,

CuriousOne
- 9th December 2015, 11:39
Sounds simple, but hard to do:

24.3.2 SETUP FOR PWM OPERATION
The following steps should be taken when configuring
the CCP1 module for standard PWM operation:
1. Disable the CCP1 pin output driver by setting
the associated TRIS bit.
2. Load the PR2 register with the PWM period
value.
3. Configure the CCP1 module for the PWM mode
by loading the CCP1CON register with the
appropriate values.
4. Load the CCPR1L register and the DC1B1 bits
of the CCP1CON register, with the PWM duty
cycle value.
5. Configure and start Timer2:
• Clear the TMR2IF interrupt flag bit of the
PIR1 register. See Note below.
• Configure the T2CKPS bits of the T2CON
register with the Timer prescale value.
• Enable the Timer by setting the TMR2ON
bit of the T2CON register.
6. Enable PWM output pin:
• Wait until the Timer overflows and the
TMR2IF bit of the PIR1 register is set. See
Note below.
• Enable the CCP1 pin output driver by clearing the associated TRIS bit.

I guess, I need to alter step #4 each time, but how to do that in PBP is beyond my understanding...

HenrikOlsson
- 9th December 2015, 19:26
Hi,
It's really not that complicated and there's plenty of code floating around the forum showing how it's done and discussions about how it works and how the values interact.
If you really can not find the info please enlighten us regarding the device used, clock frequency and desired PWM frequency?

/Henrik.