/* p5_8.c using TC4 Compare Waveform Output * * This program uses TC4 to generate periodic waveform outputs. * The GCLK_TC4 is running at 1 MHz. Prescaler is not used so * the timer counter clock is 1 MHz as well. * TC4 is set up to run 16-bit Match Frequency mode, in * which the counter keeps counting but the output toggles * when the counter matches one of the CC register content. * The counter counts up and wraps around to 0 when the counter * value matches CC0. * CC0 is set to 5,000-1 which gives the output of channel 0 * 1,000,000 / 5,000 / 2 = 100 Hz. (Each period has two toggles.) * CC1 is set to half of the value of CC0 so that the outputs of * channel 0 and channel 1 have a 90 degree phase. * PB12 is used for TC4/WO[0] and PB13 is used for TC4/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 |= 0x00001000; /* Enable TC4 Clock in PM */ REG_TC4_CTRLA = 1; /* Software Reset */ while (REG_TC4_CTRLA & 1) {} /* wait till out of reset */ REG_TC4_CTRLA = 0x0020; /* no prescaler, 16-bit mode, MFRQ */ REG_TC4_COUNT16_CC0 = 5000 - 1; /* match value 0 */ REG_TC4_COUNT16_CC1 = 2500 - 1; /* match value 1 */ REG_TC4_CTRLA |= 2; /* enable */ ARRAY_PORT_PINCFG1[12] |= 1; /* make PB12, PB13 output for TC4 */ ARRAY_PORT_PINCFG1[13] |= 1; ARRAY_PORT_PMUX1[6] = 0x44; /* PB12 = TC4, PB13 = TC4*/ while(1) { } }