PWM
 
Component PWM
Pulse width modulation
Component Level: High
Typical Usage:
(Examples of a typical usage of the component in user code. For more information please see the page Component Code Typical Usage.)

Required component name is "PWM1".
There are the following four typical usage modes:

(1)
The easiest usage of the component is shown below on a simple output signal generation with predefined period and duty. It's possible to change duty using method SetRatio.

 MAIN.C

void main(void)
{
  unsigned int i, j;
  
  for(i = 0; i < 65535; ++i) {
    PWM1_SetRatio16(i);      //change duty
    for(j = 0; j < 65535; ++j);  //delay
  }
  for(;;);
}
Note: The component may be also used without any method and event - only for settings of the output signal period and duty.

(2)
Usually it's better to change the duty after the end of period, because this helps to avoid noises in the output signal. The property "Interrupt service/event" must be enabled for the support of OnEnd event.

 EVENTS.C

unsigned ratio = 0;
void PWM1_OnEnd(void)
{
  PWM1_SetRatio16(++ratio);  //change duty
}

(3)
There are other advanced methods for changing duty at runtime. It is possible to select one duty from the predefined list using SetDutyMode method or set the value of the interval using methods SetDutyTicks and SetDutyTime (please refer to Timing dialog box).
In the following example the duty is switched each 255-th period. The mode is set sequentially from three predefined values.

 EVENTS.C

int rpt_cntr = 255, mode = 0;

void PWM1_OnEnd(void)
{

  if(--rpt_cntr == 0) {  // If the event was generated 255times
    if(++mode == 3) mode = 0; // Select next mode of the period
    PWM1_SetDutyMode(mode);// Set new period mode
    rpt_cntr = 255; // Set repeat counter
  }
}

(4)
Component initial level of the output signal may be changed using SetValue and ClrValue methods. These methods may be used only if the component is disabled.

 MAIN.C

void main(void)
{
  /* output signal synchronization */
  PWM1_Disable();  /* disable the component */
  PWM1_ClrValue(); /* set output level to LOW */
  PWM1_Enable();   /* enable the component */
}