/* p5_4.c Toggle LED0 on SAMD21 Xplained Pro using TC3 * * This program uses TC3 to toggle PB30 at 4 Hz. The LED0 on board * will blink at 2 Hz. * TC3 is configured to run continuously in 8-bit mode. TC3 counter * clock is derived from 1 MHz main clock. The prescaler divides the * GCLK_TC3 by 1024. The TOP count is set to 244-1. The counter * overflows at 1 MHz / 1024 / 244 = 4. Every time the counter * overflows, PB30 is toggled. The yellow LED0 on board is connected * to PB30. * * Tested with Atmel Studio 7 v7.0.1006 and Keil MDK-ARM v5.21a. */ #include "samd21.h" int main (void) { REG_GCLK_CLKCTRL = 0x401B; /* GCLK0 -> TCC2/TC3 */ REG_PM_APBCMASK |= 0x00000800; /* enable TC3 Clock in PM */ REG_TC3_CTRLA = 1; /* reset */ while (REG_TC3_CTRLA & 1) {} /* wait till out of reset */ REG_TC3_CTRLA = 0x0704; /* prescaler /1024, 8-bit mode, NFRQ */ REG_TC3_COUNT8_PER = 244 - 1; /* maximum count value in 8-bit mode */ REG_TC3_CTRLA |= 2; /* enable TC3 */ REG_PORT_DIR1 |= 0x40000000; /* make PB30 output */ while(1) { if (REG_TC3_INTFLAG & 1) { /* wait until OVF is set */ REG_TC3_INTFLAG = 1; /* clear OVF flag */ REG_PORT_OUT1 ^= 0x40000000; /* toggle LED0 */ } } }