arduino due changing frequency

Hello. I’m planning to make pulse generator with arduino due cause of frequency. (want to generate 1.5~1.7MHz)
I was making it with arduino uno and now, want to make it with due.

So I’ve been reading this post

but couldn’t achieve it.
I check voltage at D2 pin with multimeter connected to 1kohm and ground but it shows 0V(cause of PWML0 that mention at first line).
code is like this.

// Output a 40kHz 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 = 2100;                                  // Set the PWM frequency 84MHz/40kHz = 2100 
  REG_PWM_CDTY0 = 1050;                                  // Set the PWM duty cycle 50% (2100/2=1050)
  REG_PWM_ENA = PWM_ENA_CHID0;                          // Enable the PWM channel     
}

void loop() {}

Also want to know specific information.

I want to make different frequency at each pin like Pin 2 is 1.5MHz and Pin4 is 1.7MHz but didn’t know the method.
In Uno board, It can controlled by Timer0,1 and 2 but don’t know in Due board.

And one more things is making pulse at only 8 pulses.
It can be way to stopping by times like when Pin 2 is 1.5MHz, then stop it at (1/1500000)*8 seconds.
Just want to know another way that precise then this way.

Please help me…

To program a PWM, you need first the Graynomad pinout diagram, then you can use pwm_lib library from antodom, or program yourself PWM registers:

Firstly, power PWM peripheral with PMC_PCER1, set PWM pins to the PWM peripheral with PIO_PDR and PIO_ABSR.

Once you set the PWM clock (F_CPU (84 MHz) / DIVA/PREA), you set the PWM output frequency with CPRD:

Final frequency = PWM clock/CPRD Duty cycle = (CDTY/CPRD) * 100 %

If you intend to update PWM frequency or duty cycle in loop() once PWM is enabled, do that with CPRDUPD and CDTYUPD registers so that updates are made automatically right at the beginning of a new period.

An example code:

/******************************************************************************/
/*                         PWMH0 641  Hz                                      */
/******************************************************************************/

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  PMC->PMC_PCER1 |= PMC_PCER1_PID36;                   // PWM power ON

  PIOC->PIO_PDR |= PIO_PDR_P3;                         // Set PWM pin to a peripheral
  PIOC->PIO_ABSR |= PIO_PC3B_PWMH0;                    // Set PWM pin peripheral type B for PWMH0 (Arduino pin 35)

  PWM->PWM_CLK = PWM_CLK_PREB(0) | PWM_CLK_DIVB(2);    // select Frequency for clock B: Mck/2 = 42 MHz
  PWM->PWM_CH_NUM[0].PWM_CMR = PWM_CMR_CPRE_CLKB;      // The period is left aligned, clock source as CLKB on channel 0
  PWM->PWM_CH_NUM[0].PWM_CPRD = 65535;                 // Set the PWM frequency 42 MHz/PWM_CPRD = 641 Hz
  PWM->PWM_CH_NUM[0].PWM_CDTY = 32768;                 // Set the PWM duty cycle = (CPRD/CDTY) * 100 %

  PWM->PWM_IER1 = PWM_IER1_CHID0;                      // Interrupt on PWM Channel 0 counter
  NVIC_EnableIRQ(PWM_IRQn);                            // Enable interrupt

  PWM->PWM_ENA = PWM_ENA_CHID0;                        // Enable PWM channel 0
}

void loop() {
  // update PWM frequency with  PWM->PWM_CH_NUM[0].PWM_CPRDUPD = ....;
  // update PWM duty cycle with PWM->PWM_CH_NUM[0].PWM_CDTYUPD = ....;
}

void PWM_Handler() {
  static uint32_t Count;

  PWM->PWM_ISR1;      // Clear status register
  if (Count++ == 641)
  {
    PIOB->PIO_ODSR ^= PIO_ODSR_P27;
    Count = 0;
  }
}

Thank you . I succeed controlling pwm frequency with this post.

And now on, I'm planning to make 2 digital pin with different frequency. Is there any way to do this?