PWM Updating Speed

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DaMc_1553781
Level 1
Level 1
First like given

Hi,

I'm having problems changing the value of my period and compare of my PWM quickly. I'm using two interrupts, one running at a 10ms tick which calculates a new pwm period and compare (compare is always half the period to maintain duty cycle) which is speeding up and slowing down the PWM between a max and a min.

The other interrupt is for the PWM that updates the compare and period on the TC interrupt.

The PWM block is running at 10MHz and i'm trying to get a frequency of around 150k from the PWM. Running at this speed the PWM 'skips' and jitters around.

Questions is, When should the PWM be updated and is it even possible at higher speeds?

i.e.

CY_ISR(tick)

{

     pwmPeriod     = newValue;

     pwmCompare = newValue / 2

}

CY_ISR(pwmTC)

{

    PWM_ClearInterrupt(PWM_INTR_MASK_TC);

    PWM_WritePeriod(pwmPeriod);

    PWM_WriteCompare(pwmCompare);

}

0 Likes
1 Solution
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

DaMc,

There are two solutions.

First: PWM. In your example you don't need the second interrupt.Write calculated values for period and compare in first Tick isr. The way PWM works, it waits until the period expires, and then uploads new values. So there will be a delay of one period, but it is quite fast in your case. When PWM period is long, to update values it is necessary to stop PWM, write new values and start PWM again.

Second: DDS. Since you need exactly 50% duty cycle, it is possible to use a DDS component to generate square wave. You can find latest version of the custom component (DDS32)

Re: DDS24: 24-bit DDS arbitrary frequency generator component

And PSoC4 example.

Re: Need a programmable frequency generator

And here

Re: How can I handle a PSoC 5 community library DDS24 on PSoC 4 BLE device ? 

To save  hardware resources you can  use 24 or 16-bit resolution.

The difference between PWM and DDS is that DDS output frequency can be changed on the fly, that is during the period, while PWM needs completion of the full period to update working registers.

/odissey1

DDS32_P4_basic.png

View solution in original post

5 Replies
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

DaMc,

There are two solutions.

First: PWM. In your example you don't need the second interrupt.Write calculated values for period and compare in first Tick isr. The way PWM works, it waits until the period expires, and then uploads new values. So there will be a delay of one period, but it is quite fast in your case. When PWM period is long, to update values it is necessary to stop PWM, write new values and start PWM again.

Second: DDS. Since you need exactly 50% duty cycle, it is possible to use a DDS component to generate square wave. You can find latest version of the custom component (DDS32)

Re: DDS24: 24-bit DDS arbitrary frequency generator component

And PSoC4 example.

Re: Need a programmable frequency generator

And here

Re: How can I handle a PSoC 5 community library DDS24 on PSoC 4 BLE device ? 

To save  hardware resources you can  use 24 or 16-bit resolution.

The difference between PWM and DDS is that DDS output frequency can be changed on the fly, that is during the period, while PWM needs completion of the full period to update working registers.

/odissey1

DDS32_P4_basic.png

Hi BoTa,

Thanks for the advice, I will give this a go and if not will give the DDS ago!

D

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

When you change compare value, you need to write counter 0 or PWM will run up to its max value.

So it will be look like

===================

PWM_WriteCompare(compare) ;

PWM_WriteCounter(0) ;

===================

And if you want to change the period, you need to stop PWM once

So usually I do

===================

void ConfigPWM(uint16_t period, uint16_t compare)

{

    PWM_Stop() ;

    PWM_WritePeriod(period) ;

    PWM_WriteCompare(compare) ;

    PWM_WriteCounter(0) ;

    PWM_Start() ;

}

===================

moto

Thanks MoToo,

I tried resetting the counter but instead of glitching, it jumped around quite a bit! The glitching went but seems to turn the PWM to be a bit random in frequency.

D

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi D-san,

May be in your case, swap function of PWM may be useful.

Recently I saw a post of CE below

PWM swap for PSoC4S

moto

P.S. I also had an interesting discussion in the topic below

Re: TCPWM Compare Registers Not Swapping on PSoC 4200