#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 initRTC (void) { // Initialize RTC // RTC.CLKSEL = 0; // internal 32kHz, Periodendauer 30,518µs RTC.PER = 32; // set period -->> 1000ms / 31,25ms RTC.CMP = 16; //RTC.INTCTRL = RTC_CMP_bm | RTC_OVF_bm; uint8_t temp = 0; temp |= RTC_PRESCALER_DIV1024_gc; // Prescaler 1024 -->> 30,518µs * 1024 = 31,250ms temp |= RTC_RUNSTDBY_bm; temp |= RTC_RTCEN_bm; // enabled RTC.CTRLA = temp; // set Config while (RTC.STATUS > 0) { ; } // wait for all register to be synchronized } ISR(RTC_CNT_vect) { PORTA_OUTTGL = PIN3_bm; RTC.INTFLAGS = RTC_CMP_bm | RTC_OVF_bm; } int main(void) { initInternal20MHzOscillator(); initRTC(); PORTA_DIR = PIN3_bm; while (1) { if (RTC.INTFLAGS > 0) { PORTA_OUTTGL = PIN3_bm; RTC.INTFLAGS = RTC_CMP_bm | RTC_OVF_bm; } } }