cancel
Showing results for 
Search instead for 
Did you mean: 

How to SYNC PWM?

PChia.2237
Senior

Hello,

i have now this working code on SPC560P without sync flag.

	   pwm_lld_start(&PWMD1, &pwm_config_pwm_cfg0);
	   pwm_lld_start(&PWMD2, &pwm_config_pwm_cfg0);
	   pwm_lld_start(&PWMD3, &pwm_config_pwm_cfg0);
	   pwm_lld_start(&PWMD4, &pwm_config_pwm_cfg0);
	   pwm_lld_enable_channel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
	   pwm_lld_enable_channel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));
 
	   pwm_lld_enable_channel(&PWMD1, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
          pwm_lld_enable_channel(&PWMD2, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));
	   pwm_lld_enable_channel(&PWMD3, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD3, 5000));
	   pwm_lld_enable_channel(&PWMD3, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD3, 5000));
	   pwm_lld_enable_channel(&PWMD4, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD3, 5000));

But of course output is not in phase (syncronized).

If i enable flag "Synchronized FlexPWM0" and i use this code :

	   pwm_lld_start(&PWMD1, &pwm_config_pwm_cfg0);
	   pwm_lld_enable_channel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  	   pwm_lld_enable_channel(&PWMD1, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
 
	   pwm_lld_enable_channel(&PWMD1, 2, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  	   pwm_lld_enable_channel(&PWMD1, 3, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
 
	   pwm_lld_enable_channel(&PWMD1, 4, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  	   pwm_lld_enable_channel(&PWMD1, 5, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
 
	   pwm_lld_enable_channel(&PWMD1, 6, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  	   pwm_lld_enable_channel(&PWMD1, 7, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));

Only CH0 and CH1 output is working in phased.

No output on CH2,CH3,CH4,CH5,CH6,CH7.

What i have to do?

Thank you in advantages

1 ACCEPTED SOLUTION

Accepted Solutions
PChia.2237
Senior

Sorry my mistake.

I have to enable also other channel...

View solution in original post

10 REPLIES 10
PChia.2237
Senior

Sorry my mistake.

I have to enable also other channel...

MRay.1
Associate

To configure the PWM module to generate a right-aligned output, the MODE bits of the PWM Configuration registers are used to select the output alignment mode and the P1 or P2 parameter registers are used to determine the resulting period of the output signal.

0693W00000SuPsHQAV.pngMore you can learn at Pulse-Width Modulation (PWM) Tips and Tricks (microchip.com)

Angle
Associate II

Hi,

i have question regarding PWM, if i want to use the push button as input and led as a output PWM. In pinmap do i have to set as PWM or eirq for push button?

PChia.2237
Senior

Output LED ->PWM

Push button:

->if you read in polling just set as digital input.

->if you want to have interrupt set as eirq

if i want push button as a interrupt for led pwm, How to change the duty according to the push button? May i have any c code to refer ? i get confuse since in example pwm , it set the duty cycle automatically ?

PChia.2237
Senior

1->Set input following example RLA EIRQ Test Application

2->Set your output PWM following example RLA FlexPWM

In your code change duty-cicle every time push button is pressed

Okay then how can i know what percentage duty cycle need to set to follow the push button when i press it?

Hi,

Sorry again, now my project is not looping. When i pressed the button, led only will ON on time, after that it will turn off permanently even i press the button, Here my c code

0693W00000Suf40QAB.png

Hi.

I need to understand what yuo mean for reading push button duty cycle.

Have you a standard frequency?How much is it?

Because if frequency is low you can easy read digital input with pal library in polling every systick (1ms) then you can set your pwm.

Example:

uint32_t TickScanDigital=0;
uint16_t dConter=0;
uint16_t period=10000;
uint16_t duty=0;
bool d1OldState=false;
int main(void) {
	
  /* Initialization of all the imported components in the order specified in
     the application wizard. The function is generated automatically.*/
  componentsInit();
 
  /* Uncomment the below routine to Enable Interrupts. */
  irqIsrEnable();
 
 
           pwm_lld_start(&PWMD2, &pwm_config_pwm_cfg0);//config SM2
	   pwm_lld_enable_channel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 0));
	   pwm_lld_enable_channel(&PWMD2, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 0));
 
	if((osalThreadGetMilliseconds()-TickScanDigital)>1){
		TickScanDigital=osalThreadGetMilliseconds();
               dConter++;
 
               if(pal_readpad(PORT_D1,D1)==PAL_HIGH){
                      if(d1OldState==false){//trigger high
                            period=dConter;
                            dConter=0;
                    }
                    d1OldState=true;
                }else{
                     if(d1OldState==true){
                        duty=dConter*10000/period;
                        pwm_lld_enable_channel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, duty));
	               pwm_lld_enable_channel(&PWMD2, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, duty));
                     }
                   d1OldState=false;
                }
 
 
 
}