/* p5_7.c Toggle LED0 on SAMD21 Xplained Pro using TC7 * * This program uses TC7 to blink the LED0 in 16-bit mode. * * TC7 is configured to run continuously in 16-bit mode. TC7 counter * clock is derived from 1 MHz main clock. The prescaler divides the * GCLK_TC7 by 16. The TOP count is set at 31,250-1 in Match Frequency * operation. The counter overflows at 1 MHz / 16 / 31250 = 2.0 Hz. * * Tested with Atmel Studio 7 v7.0.1006 and Keil MDK-ARM v5.21a. */ #include "samd21.h" int main (void) { REG_GCLK_CLKCTRL = 0x401D; /* GCLK0 -> TC6/TC7 */ REG_PM_APBCMASK |= 0x00008000; /* enable TC7 Clock in PM */ REG_TC7_CTRLA = 1; /* reset TC7 before configuration */ while (REG_TC7_CTRLA & 1) {} /* wait till out of reset */ REG_TC7_CTRLA = 0x0420; /* prescaler /16, 16-bit mode, MFRQ */ REG_TC7_COUNT16_CC0 = 31250 - 1; /* set TOP count in MFRQ */ REG_TC7_CTRLA |= 2; /* enable TC7 */ REG_PORT_DIR1 |= 0x40000000; /* make PB30 output */ while(1) { if (REG_TC7_INTFLAG & 1) { /* wait until OVF is set */ REG_TC7_INTFLAG = 1; /* clear OVF flag */ REG_PORT_OUT1 ^= 0x40000000; /* toggle LED0 */ } } }