Software PWM on Due

I have a lamp matrix where using the analogWrite() function is too slow to properly maintain the strobing without flicker, but using the direct digital call works. The lamp matrix is strobed on a 1ms timer, so a full cycle of the full matrix is 8ms.

void lampMatrix() {
  lampColumnLast = lampColumn;
  lampColumn++;                                                                     // Increment lamp column

  if (lampColumn > maxLampColumn) {
    lampColumn = 1;

  g_APinDescription[lampColPin[lampColumnLast]].pPort->PIO_CODR = g_APinDescription[lampColPin[lampColumnLast]].ulPin;              // Turn off previous lamp column

  for (int i = 1; i <= maxLampRow; i++) {                                // Cycle through rows on that column
    if (lampState[lampColumn][i].on) {
      g_APinDescription[lampRowPin[i]].pPort->PIO_SODR = g_APinDescription[lampRowPin[i]].ulPin;
    } else {
      g_APinDescription[lampRowPin[i]].pPort->PIO_CODR = g_APinDescription[lampRowPin[i]].ulPin;

  g_APinDescription[lampColPin[lampColumn]].pPort->PIO_SODR = g_APinDescription[lampColPin[lampColumn]].ulPin;      // Turn on next lamp column

The issue is I’d like to use the PWM functionality of the pins in question, so is there a method to implement a software-based PWM function in the above routine?

I thought I might use the Timer0 value but I can’t figure out how to access that on the Due.

The PWM frequency is set by a #define in variants.cpp I believe - there are other postings here on changing the Due PWM parameters, a search should throw something up.

The Due has completely different hardware from the Uno/Mega AVR-based controllers.

There are probably some good PWM libraries for Due out there too.

Hopefully future Arduino releases will add a PWM frequency setting function, as this is commonly desired.