/* p6_6.c Toggle LED0 using TC3 Overflow Interrupt * * 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, an interrupt is triggered. In the interrupt handler, * 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) { __disable_irq(); REG_GCLK_CLKCTRL = 0x401B; /* GCLK0 -> TCC2/TC3 */ REG_PM_APBCMASK |= 0x00000800; /* enable TC3 Clock in PM */ REG_TC3_CTRLA = 1; /* reset TC3 before configuration */ 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; /* TOP count value in 8-bit mode */ REG_TC3_CTRLA |= 2; /* enable TC3 */ REG_TC3_INTENSET = 1; /* enable overflow interrupt */ NVIC_EnableIRQ(TC3_IRQn); /* enable TC3 interrupt in NVIC */ __enable_irq(); /* enable interrupt globally */ REG_PORT_DIRSET1 = 0x40000000; /* make PB30 output */ while(1) { } } void TC3_Handler(void) { REG_PORT_OUTTGL1 = 0x40000000; /* toggle LED0 */ REG_TC3_INTFLAG = 1; /* clear OVF flag */ }