adafruit / Adafruit_ZeroTimer

happy wrappers for TC modules 3,4,5 on SAMD21
MIT License
32 stars 25 forks source link

Example on how to set up PWM #13

Closed Knochi closed 4 years ago

Knochi commented 4 years ago

Hi,

I know that is for internal only but it looks very useful. I already managed to set up a proper PWM signal by using callbacks. Now I'm just curious about the PWMout(boolean pwmout, uint8_t channum, uint8_t pin) function. I'm trying to set up a custom PWM at a custom board with ATSAMD21E15 with custom variant files. Pin PA15 (This is on TC3/WO[1]) is mapped to Arduino-Pin 8

This code, which i modified from the example, produces a 10Hz wit 10% duty cycle on the desired pin. Later i want to change the duty cycle on the fly in predefined steps.

//[...]

// the timer 3 callbacks
void Timer3Callback0()
{
  digitalWrite(8, LOW);
}

void Timer3Callback1()
{
  digitalWrite(8, HIGH);
}

void setup() {
  pinMode(13, OUTPUT);
  pinMode(8, OUTPUT);

  /********************* Timer #3, 16 bit, two PWM outs, period = 65535 */
  //uses 48MHz clock as source
  //
  zt3.configure(TC_CLOCK_PRESCALER_DIV256, // prescaler divide by 1,2,4,8,16,64,256,1024 
                TC_COUNTER_SIZE_16BIT,   // bit width of timer/counter
                TC_WAVE_GENERATION_MATCH_PWM // frequency or PWM mode
                );

  zt3.setCompare(0, 18740); //period, e.g. 100ms = 10Hz, 
  zt3.setCompare(1, 0xFFFF/35); //match

  zt3.setCallback(true, TC_CALLBACK_CC_CHANNEL0, Timer3Callback1);  // this one sets pin low
  zt3.setCallback(true, TC_CALLBACK_CC_CHANNEL1, Timer3Callback0);  // this one sets pin high
  zt3.enable(true);
}

I tried this just because i can, but didn't work as expected, there is no signal on the pin:

void setup() {
  pinMode(13, OUTPUT);
  pinMode(8, OUTPUT);

  /********************* Timer #3, 16 bit, two PWM outs, period = 65535 */
  //uses 48MHz clock as source
  //
  zt3.configure(TC_CLOCK_PRESCALER_DIV256, // prescaler divide by 1,2,4,8,16,64,256,1024 
                TC_COUNTER_SIZE_16BIT,   // bit width of timer/counter
                TC_WAVE_GENERATION_MATCH_PWM // frequency or PWM mode
                );

  zt3.setCompare(0, 18740); //period, e.g. 100ms = 10Hz, 
  zt3.setCompare(1, 0xFFFF/35); //match
  //PWMout(boolean pwmout, uint8_t channum, uint8_t pin)
  zt3.PWMout(true, 0, 8);
  //zt3.setCallback(true, TC_CALLBACK_CC_CHANNEL0, Timer3Callback1);  // this one sets pin low
  //zt3.setCallback(true, TC_CALLBACK_CC_CHANNEL1, Timer3Callback0);  // this one sets pin high
  zt3.enable(true);
}
Knochi commented 4 years ago

sorry.. didn't see that there is already a pwm example