/* p11_5.c Comparison between Single-slope and Dual-slope PWM * * TCC1 is configured for single-slope PWM as reference. * TCC0 is configured for dual-slope PWM. See text for * detailed comparison. * * TCC0 output - PA08, TCC1 output - PA10 * * Tested with Atmel Studio 7 v7.0.1006 and Keil MDK-ARM v5.21a. */ #include "samd21.h" void TCC0_init(void); void TCC1_init(void); unsigned char* ARRAY_PORT_PINCFG0 = (unsigned char*)®_PORT_PINCFG0; unsigned char* ARRAY_PORT_PMUX0 = (unsigned char*)®_PORT_PMUX0; int main(void) { TCC0_init(); TCC1_init(); while (1) { } } /* TCC0 is used to generate dual-slope PWM. * The output is 100 Hz. * PA08 is used as output pin. */ void TCC0_init(void) { REG_GCLK_CLKCTRL = 0x401A; /* GCLK0 -> TCC0, TCC1 */ REG_PM_APBCMASK |= 0x00000100; /* enable TCC0 Clock in PM */ REG_TCC0_CTRLA = 1; /* reset */ while (REG_TCC0_CTRLA & 1) {} /* wait till out of reset */ REG_TCC0_WAVE = 7; /* dual-slope TOP PWM */ REG_TCC0_PER = 5000; /* period */ REG_TCC0_CC0 = 1500; /* pulse width */ REG_TCC0_CTRLA |= 2; /* enable */ ARRAY_PORT_PINCFG0[8] |= 1; /* make PA08 output for TCC0 */ ARRAY_PORT_PMUX0[4] = 0x04; /* PA08 = TCC0 */ } /* TCC1 is used to generate single-slope PWM. * The output is 200 Hz. * PA10 is used as output pin. */ void TCC1_init(void) { REG_GCLK_CLKCTRL = 0x401A; /* GCLK0 -> TCC0, TCC1 */ REG_PM_APBCMASK |= 0x00000200; /* enable TCC1 Clock in PM */ REG_TCC1_CTRLA = 1; /* reset */ while (REG_TCC1_CTRLA & 1) {} /* wait till out of reset */ REG_TCC1_WAVE = 2; /* Normal PWM */ REG_TCC1_PER = 5000 - 1; /* period */ REG_TCC1_CC0 = 1500 - 1; /* pulse width */ REG_TCC1_CTRLA |= 2; /* enable */ ARRAY_PORT_PINCFG0[10] |= 1; /* make PA10 output for TCC1 */ ARRAY_PORT_PMUX0[5] = 0x04; /* PA10 = TCC1 */ }