0

I'm trying to generate a PWM signal from TCC1. I have a personnal board built with SAMD21E18A. I want to use pin 27 PA30, in my variant.cpp I have this line:

{ PORTA, 30, PIO_PWM, PIN_ATTR_DIGITAL|PIO_SERCOM, No_ADC_Channel, NOT_ON_PWM, TCC1_CH0, EXTERNAL_INT_10 }, // SWCLK

I understand I can use TCC1_CH0 on this pin with PIO_TIMER. Here is what I've tried at the end of my setup:

  pinPeripheral(27, PIO_TIMER);
  pinMode(27, OUTPUT);

  ....

  Tcc* TC = (Tcc*)GetTC(TCC1_CH0);
  TC->CTRLA.reg &= ~TCC_CTRLA_ENABLE; 
  TC->CTRLA.reg |= TCC_CTRLA_PRESCALER_DIV1; 
  TC->WAVE.reg |= TCC_WAVE_WAVEGEN_NPWM; 
  TC->PER.reg = 255; 
  TC->CC[0].reg = 128; 
  TC->CTRLA.reg |= TCC_CTRLA_ENABLE; 

But it's not working: I have nothing on my pin.

Edit: I've also tried this in my setup:

  GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID(TCC1_GCLK_ID) |
                     GCLK_CLKCTRL_GEN_GCLK0 |
                     GCLK_CLKCTRL_CLKEN;
  while (GCLK->STATUS.bit.SYNCBUSY);

  Tcc* TC = (Tcc*) TCC1;
  TC->CTRLA.reg &= ~TCC_CTRLA_ENABLE; 

  TC->WAVE.reg = TCC_WAVE_WAVEGEN_NPWM;

  TC->CTRLA.reg |= TCC_CTRLA_PRESCALER_DIV1 | 
                   TCC_CTRLA_RUNSTDBY;        

  PORT->Group[PORTA].PINCFG[30].bit.PMUXEN = 1; 
  PORT->Group[PORTA].PMUX[15].reg = PORT_PMUX_PMUXE_E; 

  TC->CC[0].reg = 1000;     
  TC->CC[1].reg = 4000;  

  // Activer le timer
  TC->CTRLA.reg |= TCC_CTRLA_ENABLE;
  while (TC->SYNCBUSY.bit.ENABLE);

I have something on my oscilloscope, but the signal seems not to be good, HIGH signal is very very quick and low signal very long.

1 Answer 1

1

For example, settings for ATSAMR21G18A (it is similar to you MCU), PA08 PWM:

Pin init:

// GPIO PA08 PWM (DO)
PORT->Group[0].PINCFG[8].reg = PORT_PINCFG_PMUXEN;
PORT->Group[0].PMUX[8/2].bit.PMUXE = PORT_PMUX_PMUXE_E_Val; // TCC0/WO[0]

Timer init:

void timer0_init()
{   
    GCLK->CLKCTRL.reg =   GCLK_CLKCTRL_ID_TCC0_TCC1 // Gen Clk Sel ID TCC0_TCC1
                | GCLK_CLKCTRL_GEN_GCLK0    // Generic clock generator 0
                | GCLK_CLKCTRL_CLKEN;       // Clock Enable
    
    PM->APBCSEL.reg = PM_APBCSEL_APBCDIV_DIV1;      // Divide by 1
    PM->APBCMASK.reg |= PM_APBCMASK_TCC0;           // TCC0 APB Clock Enable
    
    TCC0->WAVE.reg = TCC_WAVEB_WAVEGENB_NPWM;

    TCC0->PER.reg = 80; //TCC0/WO[0] PWM 100kHz DT=50%
    TCC0->CC->reg = 40;
    
    TCC0->CTRLA.reg = TCC_CTRLA_CPTEN0 | TCC_CTRLA_ENABLE;  // Enable                   
}
1
  • I have tried something similar @Dimitri but it's doesn't work my doubt come from this line in my variant.cpp{ PORTA, 30, PIO_PWM, PIN_ATTR_DIGITAL|PIO_SERCOM, No_ADC_Channel, NOT_ON_PWM, TCC1_CH0, EXTERNAL_INT_10 }, // SWCLKI'm not sure about NOT_ON_PWM` can I use TCC1_CH0for PWM??
    – simon
    Nov 8, 2023 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.