Go Down

Topic: Changing Arduino Zero PWM Frequency (Read 71009 times) previous topic - next topic

dev_000

Dear MartinL,

Great work with this topic. I have learnt quite a lot reading through. I have some troubles understanding a few things.


I am using an Adafruit M0 Feather board and I am trying to get a 100kHz waveform on D10, D12. I am having trouble understanding the section of the code where you connect TCC timer to the ports. I am not sure how to go about doing that for any board(with a custom mapping) for any particular pin. Will you please help explain that or point to where in the Atsamd21 datasheet, I should be reading for clarification.

I am attaching the variant.cpp file for pin Mapping for Adafruit M0 if that helps.

MartinL

#211
May 31, 2019, 02:27 pm Last Edit: Jun 02, 2019, 05:18 pm by MartinL
Hi dev_000,

Looking at the Adafruit Feather M0 schematic: https://cdn-learn.adafruit.com/assets/assets/000/040/553/original/arduino_schem.png?1490994398, it can be seen that digital pin D10 is connected to SAMD21's port pin PA18 and D12 to PA19.

By default the microcontroller's pins are set to General Purpose Input Output (GPIO), however the pins can also be switched over to act as peripheral inputs and outputs. Switching over from GPIO to peripheral IO is performed by setting the PMUXEN (Peripheral Multiplexer Enable) bit in a given pin's CONFIG (Configuration) register:

This can be set either for an Arduino style digtial pin:

Code: [Select]
// Set PMUXEN for digital pin D10
PORT->Group[g_APinDescription[10].ulPort].PINCFG[g_APinDescription[10].ulPin].bit.PMUXEN = 1;

...or using the SAMD21's port pin:

Code: [Select]
PORT->Group[PORTA].PINCFG[18].bit.PMUXEN = 1;    // Set PMUXEN for port pin PA18
The SAMD21's datasheet contains a table, called "Table 6-1 PORT Function Multiplexing". It lists each of the microcontroller's pins against peripherals labelled A to H. Going down the table we can see that port pin PA18 (D10) has a timer TCC0/WO[2] on peripheral F. WO[2] indicates that this pin uses TCC0 channel 2. Port pin PA19 (D12) has timer TCC0/WO[3], also on peripheral F.

On the SAMD21 the port pins a paired as odd and even, starting PA00 and PA01, PA02 and PA03 and so on... Port pin PA18 on D10, happens to be paired with PA19 on D12. Using the pins' PMUX register we can activate both peripheral outputs. Each PMUX register is 8-bits wide with the upper nibble (4-bits) determining the ODD port peripheral IO: A through to H and the lower nibble determining the EVEN port peripheral IO, again: A through to H.

Therefore to activate the even PA18 (D10) and the odd PA19 (D12) as PWM outputs, it's necessary to set both the even and odd nibbles to peripheral F.

Again, this can be set either for an Arduino style digtial pins:

Code: [Select]
PORT->Group[g_APinDescription[10].ulPort].PMUX[g_APinDescription[10].ulPin >> 1].reg = PORT_PMUX_PMUXO_F | PORT_PMUX_PMUXE_F;
...or using the SAMD21's port pins:

Code: [Select]
PORT->Group[PORTA].PMUX[18 >> 1].reg = PORT_PMUX_PMUXO_F | PORT_PMUX_PMUXE_F;
Each port has 32 pins (PA00-PA31) (although not all port pins are available on some SAMD21 variants), this means there are 32 port pin CONFIG registers (one for each pin), but only 16 PMUX registers (one for each port pair), the logical shift right ">>" essentially divides the value by to 2, to account for half the number of PMUX registers.

dev_000

Thanks a lot for taking the time to explain that beautifully. Got it working fine at 100kHz.

Just a small correction. In the newer datasheet, its Table 7-1. I had searched for Table 6-1 and couldn't find it. For anyone searching for it in the future, search for "PORT Function Multiplexing" in the datasheet.

NicoCaldo

#213
Jun 18, 2019, 10:40 am Last Edit: Jun 18, 2019, 10:41 am by NicoCaldo
Hi MartinL,
your post has been super useful to me to understand how PWM on Arduino Zero works.

I'm trying to make a LED (pin 8 ) blink with PWM, changing the frequency to value visible to the eyes. Is it possible in some ways?

MartinL

Hi Nico,

Quote
I'm trying to make a LED (pin 8 ) blink with PWM, changing the frequency to value visible to the eyes. Is it possible in some ways?
If your intention is to simply blink an LED, then it's possible to run the TC or TCC timers at a lower frequency.

Here's some code that blinks an LED at 0.5Hz, 1 second on, 1 second off on digital pin D8. It uses the TCC1 timer in Normal Frequency (NFRQ) mode, this mode simply toggles the D8 output each time the timer overflows (every second):

Code: [Select]
// Toggle LED output 1 second on, 1 second off (0.5Hz) on digital pin D8 using timer TCC1
void setup()
{
  // Feed GCLK0 to TCC0 and TCC1
  GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN |         // Enable GCLK0
                      GCLK_CLKCTRL_GEN_GCLK0 |     // Select GCLK0
                      GCLK_CLKCTRL_ID_TCC0_TCC1;   // Feed GCLK0 to TCC0 and TCC1
  while (GCLK->STATUS.bit.SYNCBUSY);               // Wait for synchronization

  // Enable the port multiplexer for the TCC1 PWM channel 0 (digital pin D8), SAMD21 pin PA06
  PORT->Group[g_APinDescription[8].ulPort].PINCFG[g_APinDescription[8].ulPin].bit.PMUXEN = 1;
 
  // Connect the TCC0 timer to the port outputs - port pins are paired odd PMUO and even PMUXE
  // F & E specify the timers: TCC0, TCC1 and TCC2
  PORT->Group[g_APinDescription[8].ulPort].PMUX[g_APinDescription[8].ulPin >> 1].reg |= PORT_PMUX_PMUXE_E;
 
  TCC1->PER.reg = 46874;                          // Set TCC1 timer to overflow every 1 second: 48MHz / (1024 * 46874 + 1) = 1Hz
  while(TCC0->SYNCBUSY.bit.PER);                  // Wait for synchronization
 
  TCC1->CTRLA.reg |= TCC_CTRLA_PRESCALER_DIV1024; // Set TCC1 prescaler to 1024
  TCC1->CTRLA.bit.ENABLE = 1;                     // Enable the TCC1 output
  while (TCC1->SYNCBUSY.bit.ENABLE);              // Wait for synchronization
}

void loop() {}

NicoCaldo

Amazing, thanks a lot.
And then I simply use the AnalogWrite for PWM as always right?

Last question; if I would like to use the digital pins 2-3 for the same purpose, do I need to change the value to another timer or is the code the same?

Thanks again

MartinL

#216
Jun 19, 2019, 09:21 pm Last Edit: Jun 19, 2019, 09:22 pm by MartinL
Hi Nico,

Quote
And then I simply use the AnalogWrite for PWM as always right?
Unfortunately the issue with the analogWrite() function, is that it will set up a predefined timer and frequency on a given pin, this will interfere with code above.

Quote
if I would like to use the digital pins 2-3 for the same purpose, do I need to change the value to another timer or is the code the same?
That depends if you need to change the signal's frequency (period) or duty-cycle. Changing the frequency affects the whole timer and all of its output channels, while the duty-cycle can be set for each individual channel.

What would you like the LED outputs to do? What frequency (range) and duty-cycle do you intend to use?

NicoCaldo

Quote
Unfortunately the issue with the analogWrite() function, is that it will set up a predefined timer and frequency on a given pin, this will interfere with code above.
I've tried the analogWrite(8,x) with your code and it works fine. Changing the value of x between 0 and 255 change the time which the led stay on in a period of one second.

Quote
What would you like the LED outputs to do? What frequency (range) and duty-cycle do you intend to use?
The purpose is the same as the pin 8. I need to make two led connected on pin 2 and 3 blinks with the PWM so, it would be amazing if I can set the frequency to 0.5Hz as on the previous example and then I make it blink slower or faster changing the duty cycle with analogWrite(2,x) and analogWrite(3,x)

Does the changing of the PWM frequency on pins 2 and 3 will change the running time of my whole firmware (like will change delays) or is it unlinked from the actual clock speed of the board?

MartinL

#218
Jun 21, 2019, 09:20 am Last Edit: Jun 21, 2019, 11:32 am by MartinL
Hi Nico,

Quote
I've tried the analogWrite(8,x) with your code and it works fine. Changing the value of x between 0 and 255 change the time which the led stay on in a period of one second.
The analogWrite() function sort of works in this instance, because it's using the same timer (TCC1). However, behind the scenes it does change the timer's configuration and should now be giving you an on/off time of around 1.4s (0.7Hz).

If you require control over the duty-cycle of the signal on digital pin D8 then it's better to set up TCC1 for Normal PWM (NPWM) operation. Changes to the duty-cycle are made by loading the Counter Compare Buffered Channel 0 (CCB0) register with a value between 0 (0%) and the Period (PER) register (100%).

Here's the code to do that:

Code: [Select]
// Set up timer TCC1 to output a 0.5Hz signal on digtal pin D8
void setup()
{
  // Feed GCLK0 to TCC0 and TCC1
  GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN |         // Enable GCLK0
                      GCLK_CLKCTRL_GEN_GCLK0 |     // Select GCLK0
                      GCLK_CLKCTRL_ID_TCC0_TCC1;   // Feed GCLK0 to TCC0 and TCC1
  while (GCLK->STATUS.bit.SYNCBUSY);               // Wait for synchronization

  // Enable the port multiplexer for the TCC1 PWM channel 0 (digital pin D8), SAMD21 pin PA06
  PORT->Group[g_APinDescription[8].ulPort].PINCFG[g_APinDescription[8].ulPin].bit.PMUXEN = 1;
 
  // Connect the TCC0 timer to the port outputs - port pins are paired odd PMUO and even PMUXE
  // F & E specify the timers: TCC0, TCC1 and TCC2
  PORT->Group[g_APinDescription[8].ulPort].PMUX[g_APinDescription[8].ulPin >> 1].reg |= PORT_PMUX_PMUXE_E;

  TCC1->WAVE.reg |= TCC_WAVE_WAVEGEN_NPWM;        // Setup single slope PWM on TCC1
  while (TCC1->SYNCBUSY.bit.WAVE);                // Wait for synchronization
 
  TCC1->PER.reg = 93749;                          // Set the TCC1 timer to overflow every 1 second: 48MHz / (1024 * 93749 + 1) = 0.5Hz
  while(TCC0->SYNCBUSY.bit.PER);                  // Wait for synchronization

  TCC1->CC[0].reg = 46875;                        // Set the TCC1 duty cycle to 50% (PER / 2)
  while(TCC0->SYNCBUSY.bit.CC0);                  // Wait for synchronization
 
  TCC1->CTRLA.reg |= TCC_CTRLA_PRESCALER_DIV1024; // Set TCC1 prescaler to 1024
  TCC1->CTRLA.bit.ENABLE = 1;                     // Enable the TCC1 output
  while (TCC1->SYNCBUSY.bit.ENABLE);              // Wait for synchronization
}

void loop()
{
  TCC1->CCB[0].reg = 23437;                       // Set the TCC1 duty cycle to 25% (PER / 4)
  while(TCC0->SYNCBUSY.bit.CCB0);                 // Wait for synchronization 
  delay(4000);                                    // Wait for 4 seconds
  TCC1->CCB[0].reg = 70312;                       // Set the TCC1 duty cycle to 75% (PER * 3 / 4)
  while(TCC0->SYNCBUSY.bit.CCB0);                 // Wait for synchronization 
  delay(4000);                                    // Wait for 4 seconds
}

MartinL

#219
Jun 21, 2019, 09:36 am Last Edit: Jun 21, 2019, 11:32 am by MartinL
Regarding obtaining the same output on D2 and D3:

On D2 change the pin output configuration to:

Code: [Select]
PORT->Group[g_APinDescription[2].ulPort].PINCFG[g_APinDescription[2].ulPin].bit.PMUXEN = 1;
PORT->Group[g_APinDescription[2].ulPort].PMUX[g_APinDescription[2].ulPin >> 1].reg |= PORT_PMUX_PMUXE_F;

...and the timer and timer channel to TCC0, channel 0, for example:

Code: [Select]
TCC0->CCB[0].reg = 23437;                       // Set the TCC0 duty cycle to 25% (PER / 4)
while(TCC0->SYNCBUSY.bit.CCB0);                 // Wait for synchronization

On D3 change the pin output configuration to:

Code: [Select]
PORT->Group[g_APinDescription[3].ulPort].PINCFG[g_APinDescription[3].ulPin].bit.PMUXEN = 1;
PORT->Group[g_APinDescription[3].ulPort].PMUX[g_APinDescription[3].ulPin >> 1].reg |= PORT_PMUX_PMUXO_F;

... and the timer and timer channel to TCC1, channel 1, for example:

Code: [Select]
TCC1->CCB[1].reg = 23437;                       // Set the TCC1 duty cycle to 25% (PER / 4)
while(TCC1->SYNCBUSY.bit.CCB1);                 // Wait for synchronization

Making changes to the TCCx timers won't affect the timing delay(), delayMicroseconds(), millis() or micros() functions, as they're generated using the SAMD21's Systick (System Tick) timer.

NicoCaldo


Nilab

#221
Jun 24, 2019, 07:18 pm Last Edit: Jun 24, 2019, 07:55 pm by Nilab
Hi MartinL
How to set d3 and d4 pin to 20kHz or 30khz  (arduino M0)

and change duty cycle in loop to control the motor with high resolution (10 bit) for example
can i connect to it to tcc0 ? or i need tcc0 to d4 and tcc1 to d3 ?


MartinL

Hi Nilab,

Here's an example of that generates 30kHz PWM on digital pins D3 and D4, it provides 10-bit resolution (log(1600)/log(2)) on both outputs.

Note that on the Arduino M0 board's D2 and D4 pins are reversed with respect to the Arduino Zero's, (I've accounted for that in the code):

Code: [Select]
// Output 30kHz PWM on digital pins D3 and D4 on Arduino.org M0 board
// Note: D2 and D4 are reversed on Arduino MO with respect to Arduino Zero
void setup()
{
  // Feed GCLK0 to TCC0 and TCC1
  GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN |         // Enable GCLK0 as a clock source
                      GCLK_CLKCTRL_GEN_GCLK0 |     // Select GCLK0
                      GCLK_CLKCTRL_ID_TCC0_TCC1;   // Feed GCLK0 to TCC0 and TCC1
  while (GCLK->STATUS.bit.SYNCBUSY);               // Wait for synchronization

  // Enable the port multiplexer for pins D3 and D4
  PORT->Group[g_APinDescription[3].ulPort].PINCFG[g_APinDescription[3].ulPin].bit.PMUXEN = 1;
  PORT->Group[g_APinDescription[4].ulPort].PINCFG[g_APinDescription[4].ulPin].bit.PMUXEN = 1;

  // D3 is on ODD port pin PA09 and TCC0/WO[1] channel 1 is on peripheral E
  // D4 is on EVEN port pin PA14 and TCCO/WO[4] channel 0 is on peripheral F
  PORT->Group[g_APinDescription[3].ulPort].PMUX[g_APinDescription[3].ulPin >> 1].reg |= PORT_PMUX_PMUXO_E;
  PORT->Group[g_APinDescription[4].ulPort].PMUX[g_APinDescription[4].ulPin >> 1].reg |= PORT_PMUX_PMUXE_F;

  // Normal (single slope) PWM operation: timer countinuouslys count up to PER register value and then is reset to 0
  TCC0->WAVE.reg |= TCC_WAVE_WAVEGEN_NPWM;         // Setup single slope PWM on TCC1
  while (TCC0->SYNCBUSY.bit.WAVE);                 // Wait for synchronization
 
  TCC0->PER.reg = 1599;                            // Set the frequency of the PWM on TCC0 to 30kHz: 48MHz / (1599 + 1) = 30kHz
  while (TCC0->SYNCBUSY.bit.PER);                  // Wait for synchronization
 
  // The CCx register value corresponds to the pulsewidth in microseconds (us)
  TCC0->CC[1].reg = 800;                          // TCC0 CC1 - 50% duty cycle on D3
  while (TCC0->SYNCBUSY.bit.CC1);                 // Wait for synchronization
  TCC0->CC[0].reg = 800;                          // TCC0 CC0 - 50% duty cycle on D4
  while (TCC0->SYNCBUSY.bit.CC0);                 // Wait for synchronization
 
  TCC0->CTRLA.bit.ENABLE = 1;                     // Enable the TCC0 counter
  while (TCC0->SYNCBUSY.bit.ENABLE);              // Wait for synchronization
}

void loop()
{
  // Using buffered counter compare registers (CCBx)
  TCC0->CCB[1].reg = 400;                         // TCC0 CCB1 - 25% duty cycle on D3
  while (TCC0->SYNCBUSY.bit.CCB1);                // Wait for synchrnoization
  TCC0->CCB[0].reg = 400;                         // TCC0 CCB1 - 25% duty cycle on D4
  while (TCC0->SYNCBUSY.bit.CCB0);                // Wait for synchronization
  delay(1000);                                    // Wait for 1 second
  TCC0->CCB[1].reg = 1200;                        // TCC0 CCB1 - 75% duty cycle on D3
  while (TCC0->SYNCBUSY.bit.CCB1);                // Wait for synchrnoization
  TCC0->CCB[0].reg = 1200;                        // TCC0 CCB1 - 75% duty cycle on D4
  while (TCC0->SYNCBUSY.bit.CCB0);                // Wait for synchronization
  delay(1000);                                    // Wait for 1 second
}

Nilab

#223
Jun 25, 2019, 11:08 am Last Edit: Jun 25, 2019, 11:22 am by Nilab
Sorry MartinL ! I am not to said! I use robotdyn m0-mini  robotdyn_arduino_zero_arduino_m0

in attachement Schematic
d3 connected to "pa09"
d4 connected to "pa08"
Is that means my d3 and d4 pin on peripheral E ? for timer tcc0?

MartinL

#224
Jun 25, 2019, 01:25 pm Last Edit: Jun 25, 2019, 01:26 pm by MartinL
Hi Nilab

Quote
in attachement Schematic
d3 connected to "pa09"
d4 connected to "pa08"
Is that means my d3 and d4 pin on peripheral E ? for timer tcc0?
Yes that's right. In this instance the TCC0 channels 0 and 1 are now on peripheral E for pins D4 and D3 respectively, the same as the Arduino Zero.

Just replace the Peripheral Multiplexing (PMUX) lines in the above example with the following line:

Code: [Select]
// D3 is on ODD port pin PA09 and TCC0/WO[1] channel 1 is on peripheral E
// D4 is on EVEN port pin PA08 and TCCO/WO[0] channel 0 is on peripheral E
PORT->Group[g_APinDescription[3].ulPort].PMUX[g_APinDescription[3].ulPin >> 1].reg = PORT_PMUX_PMUXO_E | PORT_PMUX_PMUXE_E;

Go Up