/* p5_3.c Toggle LED0 on SAMD21 Xplained Pro at 1 Hz using SysTick * This program uses SysTick to generate multiples of millisecond delay. * System clock is running at 1 MHz. SysTick is configure * to count down from 999 to zero to give a 1 ms delay. * A for loop counts how many millisecond the delay should be. * When 1000 is used for loop count, the delay is 1000 ms or 1 second. * The LED0 is connected to PB30. * * Tested with Atmel Studio 7 v7.0.1006 and Keil MDK-ARM v5.21a. */ #include "samd21.h" void delayMs(int n); int main (void) { // make PB30 output for LED REG_PORT_DIRSET1 = 0x40000000; /* make PB30 output for LED */ while (1) { delayMs(1000); /* delay 1000 ms */ REG_PORT_OUTTGL1 = 0x40000000; /* toggle red LED */ } } void delayMs(int n) { int i; /* Configure SysTick */ SysTick->LOAD = 1000 - 1; /* reload with number of clocks per millisecond */ SysTick->VAL = 0; /* clear current value register */ SysTick->CTRL = 0x5; /* Enable the timer */ for(i = 0; i < n; i++) { while((SysTick->CTRL & 0x10000) == 0) /* wait until the COUNTFLAG is set */ { } } SysTick->CTRL = 0; /* Stop the timer (Enable = 0) */ }