/* p11_1.c Using TC5 for PWM Output * * This program uses TC5 to generate PWM waveform outputs. * The GCLK_TC5 is running at 1 MHz. Prescaler is not used so * the timer counter clock is 1 MHz as well. * TC5 is set up to run 16-bit Match PWM mode, in * which the counter counts up to CC0 as the TOP value and wraps * around. The channel 1 output is set when on wraparound * and clear on match of CC1. * CC0 is set to 16,666-1 which gives the output frequency * 1,000,000 / 16,666 = 60 Hz. * CC1 is set to 1/3 of the value of CC0 so that the outputs of * channel 1 has 33.3% duty cycle. * PB11 is used for TC5 output channel 1 (TC5/WO[1]). * * Tested with Atmel Studio 7 v7.0.1006 and Keil MDK-ARM v5.21a. */ #include "samd21.h" unsigned char* ARRAY_PORT_PINCFG1 = (unsigned char*)®_PORT_PINCFG1; unsigned char* ARRAY_PORT_PMUX1 = (unsigned char*)®_PORT_PMUX1; int main (void) { REG_GCLK_CLKCTRL = 0x401C; /* GCLK0 -> TC4/TC5 */ REG_PM_APBCMASK |= 0x00002000; /* Enable TC5 Clock in PM */ REG_TC5_CTRLA = 1; /* Software Reset */ while (REG_TC5_CTRLA & 1) {} /* wait till out of reset */ REG_TC5_CTRLA = 0x0060; /* no prescaler, 16-bit, MPWM */ REG_TC5_COUNT16_CC0 = 16666 - 1; /* match value 0 */ REG_TC5_COUNT16_CC1 = 5555 - 1; /* match value 1 */ REG_TC5_CTRLA |= 2; /* enable */ ARRAY_PORT_PINCFG1[11] |= 1; /* make PB11 output for TC5 */ ARRAY_PORT_PMUX1[5] = 0x40; /* PB11 = TC5*/ while(1) { } }