/* p6_7.c Toggle LED0 using TC6 and TC7 Match Interrupts * * TC6 and 7 are configured to run continuously in 16-bit mode. TC6/7 * counter clock is derived from 1 MHz main clock. The prescaler divides the * GCLK by 16. The TOP count is set at 31,250-1 for TC6 and 30,000-1 for TC7 * in Match Frequency operation. * The counter overflows at 1 MHz / 16 / 31,250 = 2.0 Hz for TC6 and * 1 MHz / 16 / 30,000 = 2.08 Hz for TC7. * * Everytime the counter matches the CC register, an interrupt is triggered. * In the interrupt handler, PB30 is turned on by TC6 and turned off by TC7. * The yellow LED0 on board is connected to PB30. It is on from TC6 * interrupt to TC7 interrupt and off from TC7 interrupt to TC6 interrupt. * Because these two interrupts have different frequency, the on time of the * LED0 changes. * * 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 = 0x401D; /* GCLK0 -> TC6/TC7 */ REG_PM_APBCMASK |= 0x0000C000; /* enable TC7/6 Clock in PM */ REG_TC6_CTRLA = 1; /* reset TC6 before configuration */ while (REG_TC6_CTRLA & 1) {} /* wait till out of reset */ REG_TC6_CTRLA = 0x0420; /* prescaler /16, 16-bit mode, MFRQ */ REG_TC6_COUNT16_CC0 = 31250 - 1; /* set TOP count in MFRQ */ REG_TC6_CTRLA |= 2; /* enable TC6 */ REG_TC6_INTENSET = 0x10; /* enable match interrupt */ NVIC_EnableIRQ(TC6_IRQn); /* enable TC6 interrupt in NVIC */ 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 = 30000 - 1; /* set TOP count in MFRQ */ REG_TC7_CTRLA |= 2; /* enable TC7 */ REG_TC7_INTENSET = 0x10; /* enable match interrupt */ NVIC_EnableIRQ(TC7_IRQn); /* enable TC7 interrupt in NVIC */ __enable_irq(); /* enable interrupt globally */ REG_PORT_DIRSET1 = 0x40000000; /* make PB30 output */ while(1) { } } void TC6_Handler(void) { REG_PORT_OUTCLR1 = 0x40000000; /* turn on LED0 */ REG_TC6_INTFLAG = 0x10; /* clear MC0 flag */ } void TC7_Handler(void) { REG_PORT_OUTSET1 = 0x40000000; /* turn off LED0 */ REG_TC7_INTFLAG = 0x10; /* clear MC0 flag */ }