Hello All,
For a while I have been using the Arduino Due and have found a lot of information that was a great help on this forum. For this particular thing I could not find something.
Right now I am working on this library where by I want to control all the broken out hardware PWM pins on the Arduino Due.
I have got some working code with which I can control all those PWM pins, however there are a few things I can't solve.
With this post I am hoping to get some answers or pointers in the right direction.
The code:
#define PWM_MAX_DUTY 4096 //12 bits PWM resolution
void setup()
{
uint32_t dutyCycle = 2048; //50% duty cycle
uint8_t pwmPin[17] = { 6, 7, 8, 9, 29, 34, 35, 36, 37, 38, 39, 40, 41, 43, 44, 45, 53 }; //Arduino hardware PWM pins
uint8_t pwmCh[8] = { PWM_CH0, PWM_CH1, PWM_CH2, PWM_CH3, PWM_CH4, PWM_CH5, PWM_CH6, PWM_CH7 }; //PWM channels
//configure all hardware PWM pins to work as PWM
for (int i = 0; i < 18; i++) {
PIO_Configure(g_APinDescription[pwmPin[i]].pPort,
PIO_PERIPH_B,
g_APinDescription[pwmPin[i]].ulPin,
g_APinDescription[pwmPin[i]].ulPinConfiguration);
}
//configure and set duty cycle on all PWM channels
for (int i = 0; i < 8; i++) {
pmc_enable_periph_clk(PWM_INTERFACE_ID);
PWMC_ConfigureClocks(450 * PWM_MAX_DUTY, 0, VARIANT_MCK); //set to frequency @450hz @12bits
PWMC_ConfigureChannel(PWM_INTERFACE, pwmCh[i], PWM_CMR_CPRE_CLKA, 0, 0);
PWMC_SetPeriod(PWM_INTERFACE, pwmCh[i], PWM_MAX_DUTY);
PWMC_EnableChannel(PWM_INTERFACE, pwmCh[i]);
PWMC_SetDutyCycle(PWM_INTERFACE, pwmCh[i], dutyCycle);
}
}
void loop()
{
/* add main program code here */
}
The results:
D06 C24 PWM L7
D07 C23 PWM L6
D08 C22 PWM L5
D09 C21 PWM L4
D34 C2 PWM L0 22x FREQ
D35 C3 PWM H0 22x FREQ INVERTED
D36 C4 PWM L1
D37 C5 PWM H1 INVERTED
D38 C6 PWM L2
D39 C7 PWM H2 INVERTED
D40 C8 PWM L3
D41 C9 PWM H3 INVERTED
D42 A19 PWM H1 NOT WORKING
D43 A20 PWM L2
D44 C19 PWM H5 INVERTED
D45 C18 PWM H6 INVERTED
D53 B14 PWM H2 INVERTED
- On all of the above pins I get a PWM output except for pin 42.
Can anyone see why? - On pins 34 & 35 I get a frequency that's 22x higher than the other PWM outputs.
Is there a particular thing that needs to be configured for this PWM channel? - All the PWM H channels seem to be inverted in regards to PWM L channels.
Is there a particular thing that needs to be configured to get them in the same direction?
Thanks in advance for any feedback.

