#include #include #include #include void initInternal20MHzOscillator (void) { // 20MHz mittels Fuse ausgewählt // CPU_CCP = CCP_IOREG_gc; CLKCTRL.OSCHFCTRLA = CLKCTRL_RUNSTDBY_bm | CLKCTRL_AUTOTUNE_0_bm; // Main Clock Prescaler CPU_CCP = CCP_IOREG_gc; CLKCTRL.MCLKCTRLB = 0; // disable Prescaler, Default 6 // wait internal oscillator is stable while(!(CLKCTRL.MCLKSTATUS & CLKCTRL_OSCHFS_bm)) { ; } // Select Internal Clock Source and enable Pin Clock Out CPU_CCP = CCP_IOREG_gc; CLKCTRL.MCLKCTRLA = (CLKCTRL_CLKSEL_OSCHF_gc | CLKCTRL_CLKOUT_bm); // wait for system oscillator changing to finish while (CLKCTRL.MCLKSTATUS & CLKCTRL_SOSC_bm) { ; } } void initRTCPIT (void) { // Initialize RTC // RTC.CLKSEL = 0; // internal 32kHz, Periodendauer 30,518µs //RTC.PITINTCTRL = RTC_PI_bm; uint8_t temp = 0; temp |= RTC_PERIOD_3_bm; // Bit 6 temp |= RTC_PERIOD_2_bm; // Bit 5 temp |= RTC_PERIOD_0_bm; // Bit 3 temp |= RTC_PITEN_bm; RTC.PITCTRLA = temp; while (RTC.PITSTATUS > 0) { ; } // wait for all register to be synchronized } ISR(RTC_PIT_vect) { PORTA_OUTTGL = PIN3_bm; RTC.PITINTFLAGS = RTC_PI_bm; } int main(void) { initInternal20MHzOscillator(); initRTCPIT(); PORTA_DIR = PIN3_bm; PORTA_OUTTGL = PIN3_bm; _delay_ms(250); PORTA_OUTTGL = PIN3_bm; _delay_ms(250); PORTA_OUTTGL = PIN3_bm; _delay_ms(250); PORTA_OUTTGL = PIN3_bm; _delay_ms(250); while (1) { if (RTC.PITINTFLAGS > 0) { PORTA_OUTTGL = PIN3_bm; RTC.PITINTFLAGS = RTC_PI_bm; } } }