ATTiny817 | How do I enable PWM on PC0 and PC1? TCD0?

That's done in the TCD0_FAULTCTRL register

I made a small demonstration of setting up TCD0 to output 50Hz on WOA and/or WOC

// =================================================================
// Demonstration of setting up TCD0 to output 50Hz on WOA and/or WOC
// Hans Meijdam, March 2024
// =================================================================

void setup() {
  takeOverTCD0();// this will give control of the timer.
  CCP = CCP_IOREG_gc; // enable write to protected IO registers (timed sequence)
/*********  uncomment one of these 3 outputs to demonstrate output on WOA and/or WOC *******/
//  TCD0_FAULTCTRL |= TCD_CMPAEN_bm;// // enable Waveform Output A (WOA) from TCD0 to PA6
//  TCD0_FAULTCTRL |= TCD_CMPCEN_bm;// // enable Waveform Output C (WOC) from TCD0 to PC0
  TCD0_FAULTCTRL |= TCD_CMPAEN_bm |TCD_CMPCEN_bm;// or on both at the same time
  
  TCD0_CTRLB |= TCD_WGMODE_TWORAMP_gc; // Set timer to one ramp mode (=default)
  TCD0_CTRLA |= TCD_CLKSEL_SYSCLK_gc; // Internal 16/20 MHz Oscillator (OSC20M)
  TCD0_CTRLA |= TCD_CNTPRES_DIV32_gc; // Division factor  (1, 4 or 32 possible)
  TCD0_CTRLA |= TCD_SYNCPRES_DIV2_gc; // Division factor  (1, 2, 4 or 8 possible)
  TCD0_CTRLA |= TCD_ENABLE_bm; // enable TCD0
  while (!(TCD0.STATUS & TCD_CMDRDY_bm));//wait for ready to receive a command in double buffer
  TCD0_CMPASET = 4095 - 623;  //310 gives 1000us pulse. 623 gives 2000us pulse. change the set value to define pulse
  TCD0_CMPACLR = 4095; // do not change top value or frequency of ramp one will change
  TCD0_CMPBCLR = 2095; // 2nd ramp lowering TOP from 4095 to 2095 gives 50Hz
  TCD0_CTRLE |= TCD_SYNC_bm; // send synchronization command to load double buffer to TCD0
}

void loop() {}
1 Like