SAMD21 basic pwm not working (no ASF)

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to setup PWM on certain pin as basic test. I downloaded this example to setup correct clock (getting 48MHz on clk gen 0 as input for PWM timer): DFLL48M 48 MHz Initialization

 

Then I call:

void init_pulse(void)
 {
     PM->APBCMASK.reg |= PM_APBCMASK_EVSYS;           // Switch on the event system peripheral
     
     ////////////////////////////////////////////////////////////////////////////////////////
     // Genric Clock Initialisation
     ////////////////////////////////////////////////////////////////////////////////////////
     
     GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN |         // Enable the generic clock...
     GCLK_CLKCTRL_GEN_GCLK0 |                          // ....on GCLK0 at 48MHz
     GCLK_CLKCTRL_ID_TCC2_TC3;                          // Feed the GCLK0 to TCC2 and TC3
     while (GCLK->STATUS.bit.SYNCBUSY);               // Wait for synchronization

     ////////////////////////////////////////////////////////////////////////////////////////
     // TCC2 Initialisation - generate pulse every 100us (10kHz) on port pin PA16
     ////////////////////////////////////////////////////////////////////////////////////////

     // Enable the port multiplexer on port pin PA16
     PORT->Group[PORTA].PINCFG[16].bit.PMUXEN = 1;
     // Set-up the pin as a TCC2/WO[0] peripheral on PA16
     PORT->Group[PORTA].PMUX[16 >> 1].reg |= PORT_PMUX_PMUXE_E;

     TCC2->WAVE.reg = TCC_WAVE_WAVEGEN_NPWM;            // Set the TCC2 timer to normal PWM mode (NFRQ)
     while(TCC2->SYNCBUSY.bit.WAVE);                    // Wait for synchronization
     
     TCC2->PER.reg = 4799;                              // Set the period (PER) register for a PWM frequency of 10kHz (100us)
     while(TCC2->SYNCBUSY.bit.PER);                     // Wait for synchronization

     TCC2->CC[0].reg = 2400;                            // Set the counter compare 0 (CC0) register for a PWM duty-cycle of 50%
     while(TCC2->SYNCBUSY.bit.CC0);                     // Wait for synchronization

     TCC2->CTRLA.bit.ENABLE = 1;                        // Enable TCC2
     while (TCC2->SYNCBUSY.bit.ENABLE);                 // Wait for synchronization

 }

 

But nothing happens at PA16? 

This topic has a solution.
Last Edited: Mon. Oct 19, 2020 - 11:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you get an ASF example to work ?

 

If so, use that as a basis to see what you're missing ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi awneil, just discovered that I forgot to switch on TCC2:

PM->APBCMASK.reg |= PM_APBCMASK_EVSYS | PM_APBCMASK_TCC2;

Now it works.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MarcelH wrote:
Now it works.

Excellent. Now please mark the solution - see Tip #5 in my signature, below, for instructions:

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...