100kHz PWM

Hi gamer1,

If you're aiming to change the duty cycle every PWM timer period? Then you could try enabling either the PWM compare match or compare update interrupts, (update interrupt is commented out in the code below).

The compare match interrupt occurs whenever the counter matches the duty cycle register's value, while the compare update interrupt occurs whenever the counter matches the period register (at the end of the cycle). The interrupts call the PWM controller's PWM_Handler() interrupt service routine (ISR).

// Output a 100kHz PWM waveform at a resolution of 11-bits on pin DAC1 (PWML0)
void setup() {
  // PWM Set-up on pin: DAC1
  REG_PMC_PCER1 |= PMC_PCER1_PID36;                     // Enable PWM
  REG_PIOB_ABSR |= PIO_ABSR_P16;                        // Set PWM pin perhipheral type A or B, in this case B
  REG_PIOB_PDR |= PIO_PDR_P16;                          // Set PWM pin to an output
  REG_PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(1);      // Set the PWM clock rate to 84MHz (84MHz/1)
  REG_PWM_CMR0 = PWM_CMR_CPRE_CLKA;                     // Enable single slope PWM and set the clock source as CLKA
  REG_PWM_CPRD0 = 840;                                  // Set the PWM frequency 84MHz/100kHz = 840
  REG_PWM_CDTY0 = 0;                                    // Set the PWM duty cycle 0%
  REG_PWM_IER2 = PWM_IER2_CMPM0;                        // Enable interrupt for comparison match channel 0
  //REG_PWM_IER2 = PWM_IER2_CMPU0;                        // Enable interrupt for comparison update channel 0
  REG_PWM_ENA = PWM_ENA_CHID0;                          // Enable the PWM channel     
}

loop (){}

void PWM_Handler()                                      // ISR Handler for the PWM controller
{
  if (REG_PWM_ISR2 & PWM_ISR2_CMPM0)                    // Check if the comparison match channel 0 interrupt has been triggered
  {
     // Add your code here...
  }
  /*if (REG_PWM_ISR2 & PWM_ISR2_CMPU0)                    // Check if the comparison update channel 0 interrupt has been triggered
  {
     // Add your code here...
  }*/
}