void setup_clocks(void) { // Enable the 32 MHz internal oscillator. CLKSYS_Enable(OSC_RC32MEN_bm); // Set the multiplication factor and clock reference for the PLL. // The USB clock runs at 48 MHz. // 32 MHz / 4 = 4 MHz, 48 MHz / 4 MHz = 6 (factor). OSC.PLLCTRL = (OSC_PLLSRC_RC32M_gc | (6 << OSC_PLLFAC_gp)); // Wait for the 32 MHz clock to be ready. do { } while (CLKSYS_IsReady(OSC_RC32MRDY_bm) == 0); // Switch to the 32 MHz clock for the main system clock. // XXX Check the return value! CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_RC32M_gc); // Enable the PLL. CLKSYS_Enable(OSC_PLLEN_bm | OSC_RC32MEN_bm); // Turn off all the other clocks. CLKSYS_Disable(OSC_RC32KEN_bm | OSC_RC2MEN_bm | OSC_XOSCEN_bm); // Wait for the PLL to be ready. do { } while (CLKSYS_IsReady(OSC_PLLRDY_bm) == 0); }
char system_clocks_init(void) { /****************************************************************** * System Clock 32MHz (XOSC Quarz 16MHz, PLL Faktor 2) ******************************************************************/ /* Nach dem Reset ist die Quelle des Systemtaktes der interne 2MHz RC-Oszillator (System Clock Selection: RC2MHz) */ // Oszillator XOSC konfigurieren (12..16MHz, 256 clocks startup time) CLKSYS_XOSC_Config( OSC_FRQRANGE_12TO16_gc, false, OSC_XOSCSEL_XTAL_256CLK_gc ); // Oszillator XOSC enable CLKSYS_Enable( OSC_XOSCEN_bm ); // Warten bis der Oszillator bereit ist do {} while ( CLKSYS_IsReady( OSC_XOSCRDY_bm ) == 0 ); // PLL source ist XOSC, Multiplikator x2 CLKSYS_PLL_Config( OSC_PLLSRC_XOSC_gc, 2 ); // Enable PLL CLKSYS_Enable( OSC_PLLEN_bm ); // Prescalers konfigurieren CLKSYS_Prescalers_Config(CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc ); // Warten bis PLL locked do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); // Main Clock Source ist Ausgang von PLL CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); // Nun ist der System Clock 32MHz ! /* Hinweis: 32kHz TOSC kann nicht in Verbindung mit PLL genutzt werden, da die minimale Eingangsfrequenz des PLLs 400kHz beträgt. */ return(1); } // Sets System Clock to 32MHz
void clk_init(void){ OSC.XOSCCTRL=0b11001011;//datasheet p89 OSC.CTRL=0x08; do {} while ( CLKSYS_IsReady( OSC_XOSCRDY_bm ) == 0 ); //CCP=0x9D; CCP=0xD8; CLK.CTRL=0x03; }
void configClock (void) { // enable 32mhz oscillator CLKSYS_Enable( OSC_RC32MEN_bm ); do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 ); // configure pll source = 32mhz oscillator/4 * 16 = 4*32mhz = 128mhz output CLKSYS_PLL_Config( OSC_PLLSRC_RC32M_gc, 16 ); // enable pll CLKSYS_Enable( OSC_PLLEN_bm ); do {} while ( CLKSYS_IsReady( OSC_PLLEN_bm ) == 0 ); // enable prescale by 2 and 2 again to generate 2x and 4x clocks CCP = CCP_IOREG_gc; CLK.PSCTRL = CLK_PSBCDIV_2_2_gc; // select main clock source as pll output CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); }
void clockInit(void) { /* Enable internal 32 MHz ring oscillator and wait until it's stable. Divide clock by two with the prescaler C and set the 32 MHz ring oscillator as the main clock source. */ CLKSYS_Enable( OSC_RC32MEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_2_gc ); do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc ); }
/** \brief Initialize and set cpu and periheral clocks. * * CPU clock frequencies set are: * -CPU: 32HMZ * -Peripheral Prescaling: NONE */ void setup_clocks(void) { // set 32MHZ oscillator as CPU clock source CLKSYS_Enable(OSC_RC32MEN_bm); // enable do { nop(); } while (!CLKSYS_IsReady(OSC_RC32MRDY_bm)); // wait til stable CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_RC32M_gc); // select for CPU // disable all presacalers, until we decide otherwise CLKSYS_Prescalers_Config(CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc); // set up external 32KHz oscillator (NOTE: first param is ignored) CLKSYS_XOSC_Config(OSC_FRQRANGE_04TO2_gc, false, OSC_XOSCSEL_32KHz_gc); // set internal 32KHz oscillator as source for DFLL and autocalibrate 32MHz CLKSYS_Enable(OSC_XOSCEN_bm); //enable do { nop(); } while (!CLKSYS_IsReady(OSC_XOSCRDY_bm)); // wait til stable CLKSYS_AutoCalibration_Enable(OSC_RC32MCREF_bm, true); // true == ext 32KHz // disable unused oscillators (internal 2MHz and 32KHz oscillators) CLKSYS_Disable(OSC_RC2MEN_bm | OSC_RC32KEN_bm); }
static void InitMainClockPll(void) { CLKSYS_XOSC_Config( OSC_FRQRANGE_2TO9_gc, 0, OSC_XOSCSEL_XTAL_16KCLK_gc ); CLKSYS_Enable( OSC_XOSCEN_bm ); do {} while ( CLKSYS_IsReady( OSC_XOSCRDY_bm ) == 0 ); PORTE.OUTSET = PIN0_bm; CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_XOSC_gc ); CLKSYS_Disable( OSC_RC2MEN_bm ); CLKSYS_Disable( OSC_RC32MEN_bm ); }
/*! \brief This function enables the internal 32MHz oscillator and the * prescalers needed by the HiRes extension. * * \note The optimization of the compiler must be set above low to ensure * that the setting of the CLK register is set within 4 clock cylcles * after the CCP register is set. */ void ConfigClockSystem( void ) { /* Enable internal 32 MHz ring oscillator and wait until it's * stable. */ CLKSYS_Enable( OSC_RC32MEN_bm ); do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc ); /* Configure PLL with the 32 MHz ring oscillator/4 as source and * multiply by 16 to get 128 MHz PLL clock and enable it. Wait * for it to be stable and set prescalers B and C to divide by four * to set the CPU clock to 32 MHz. */ CLKSYS_PLL_Config( OSC_PLLSRC_RC32M_gc, 16 ); CLKSYS_Enable( OSC_PLLEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_2_2_gc ); do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); }
void InitClock () { /* Clock Setup */ /* Enable for external 2-9 MHz crystal with quick startup time * (256CLK). Check if it's stable and set it as the PLL input. */ CLKSYS_XOSC_Config( OSC_FRQRANGE_2TO9_gc, false, OSC_XOSCSEL_EXTCLK_gc ); CLKSYS_Enable( OSC_XOSCEN_bm ); do {} while ( CLKSYS_IsReady( OSC_XOSCRDY_bm ) == 0 ); /* Configure PLL with the 8 MHz external clock as source and * multiply by 4 to get 32 MHz PLL clock and enable it. Wait * for it to be stable and set prescaler C to divide by two * to set the CPU clock to 16 MHz. */ CLKSYS_PLL_Config(OSC_PLLSRC_XOSC_gc, 4 ); CLKSYS_Enable( OSC_PLLEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_2_gc ); do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); }
/*! \brief Initalizes TWI slave driver structure. * * Initialize the instance of the TWI Slave and set the appropriate values. * * \param twi The TWI_Slave_t struct instance. * \param module Pointer to the TWI module. * \param processDataFunction Pointer to the function that handles incoming data. */ void TWI_SlaveInitializeDriver(TWI_Slave_t *twi, TWI_t *module, void (*processDataFunction) (void)) { twi->interface = module; twi->Process_Data = processDataFunction; twi->bytesReceived = 0; twi->bytesSent = 0; twi->status = TWIS_STATUS_READY; twi->result = TWIS_RESULT_UNKNOWN; twi->abort = false; CLKSYS_Enable( OSC_RC32MEN_bm ); do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 ); /* 32MHz Internal RC*/ CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc ); CLKSYS_Disable( OSC_RC2MEN_bm | OSC_RC32KEN_bm ); }
/** * Set up all the processor clock inputs. */ void setup_processor_clocks(void) { // Enable the 32 MHz internal oscillator. CLKSYS_Enable(OSC_RC32MEN_bm); // Wait for the 32 MHz clock to be ready. do { } while (CLKSYS_IsReady(OSC_RC32MRDY_bm) == 0); // Switch to the 32 MHz clock for the main system clock. // XXX Check the return value! CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_RC32M_gc); // Turn off all the other clocks. // XXX What about the external clock? CLKSYS_Disable(OSC_PLLEN_bm | OSC_RC32KEN_bm | OSC_RC2MEN_bm); // Enable automatic calibration of the 32MHz clock. OSC.DFLLCTRL = OSC_RC32MCREF_XOSC32K_gc; DFLLRC32M.CTRL = DFLL_ENABLE_bm; }
int main(void) { ADDR_T address = 0; unsigned int temp_int=0; unsigned char val; /* Initialization */ void (*funcptr)( void ) = 0x0000; // Set up function pointer to RESET vector. PMIC_SetVectorLocationToBoot(); // Configure 32KHz OSC CLKSYS_Enable( OSC_RC32KEN_bm ); do {} while ( CLKSYS_IsReady( OSC_RC32KEN_bm ) == 0 ); CLKSYS_Enable( OSC_RC32MEN_bm ); do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc ); CLKSYS_Disable( OSC_RC2MEN_bm); CLKSYS_AutoCalibration_Enable( OSC_RC32MCREF0_bm, true ); eeprom_disable_mapping(); PROGPORT |= (1<<PROG_NO); // Enable pull-up on PROG_NO line on PROGPORT. /* Branch to bootloader or application code? */ if( /*!(PROGPIN & (1<<PROG_NO))*/1 ) // If PROGPIN is pulled low, enter programming mode. { initbootuart(); // Initialize UART. /* Main loop */ for(;;) { val = recchar(); // Wait for command character. // Check autoincrement status. if(val=='a') { sendchar('Y'); // Yes, we do autoincrement. } // Set address (2 bytes). else if(val == 'A') { // NOTE: Flash addresses are given in words, not bytes. address = recchar(); address <<= 8; address |= recchar(); // Read address high and low byte. sendchar('\r'); // Send OK back. } // Set extended address (3 bytes). else if(val == 'H') { // NOTE: Flash addresses are given in words, not bytes. address = (uint32_t)recchar() << 16; address |= (uint16_t)recchar() << 8; address |= recchar(); sendchar('\r'); // Send OK back. } // Chip erase. else if(val=='e') { for(address = 0; address < APP_END; address += PAGESIZE) { // NOTE: Here we use address as a byte-address, not word-address, for convenience. nvm_wait_until_ready(); #ifdef __ICCAVR__ #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr. #endif EraseApplicationPage( address ); #ifdef __ICCAVR__ #pragma diag_default=Pe1053 // Back to default. #endif } nvm_eeprom_erase_all(); sendchar('\r'); // Send OK back. } #ifndef REMOVE_BLOCK_SUPPORT // Check block load support. else if(val=='b') { sendchar('Y'); // Report block load supported. sendchar((BLOCKSIZE>>8) & 0xFF); // MSB first. sendchar(BLOCKSIZE&0xFF); // Report BLOCKSIZE (bytes). } // Start block load. else if(val=='B')
int main(void) { // Configure switches PORTCFG.MPCMASK = 0xFF; // Configure several PINxCTRL registers at the same time SWITCHPORTL.PIN0CTRL = (SWITCHPORTL.PIN0CTRL & ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc; //Enable pull-up to get a defined level on the switches SWITCHPORTL.DIRCLR = 0xFF; // Set port as input // Configure LEDs PORTCFG.MPCMASK = 0xFF; // Configure several PINxCTRL registers at the same time LEDPORT.PIN0CTRL = PORT_INVEN_bm; // Invert input to turn STK600 leds on when port value is 1 LEDPORT.DIRSET = 0xFF; // Set port as output LEDPORT.OUT = 0xA0; // Set initial value unsigned int counter = 0; while (1) { /* Change the clock source to the internal 2MHz. Disable PLL. * Configure PLL with the 2 MHz RC oscillator as source and * multiply by 15 to get 30 MHz PLL clock and enable it. Wait * for it to be stable and set prescaler C to divide by two * to set the CPU clock to 15 MHz */ if ( (SWITCHPORTL.IN & PIN0_bm) == 0 ) { CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC2M_gc ); CLKSYS_Disable( OSC_PLLEN_bm ); CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 15 ); CLKSYS_Enable( OSC_PLLEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_2_gc ); do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); LEDPORT.OUTCLR = 0x1F; LEDPORT.OUTSET = PIN0_bm; } /* Configure PLL with the 2 MHz RC oscillator as source and * multiply by 15 to get 30 MHz PLL clock and enable it. Wait * for it to be stable and set prescaler B and C to divide by two * to set the CPU clock to 7.5 MHz */ if ( (SWITCHPORTL.IN & PIN1_bm) == 0 ) { CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC2M_gc ); CLKSYS_Disable( OSC_PLLEN_bm ); CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 15 ); CLKSYS_Enable( OSC_PLLEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_2_2_gc ); do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); LEDPORT.OUTCLR = 0x1F; LEDPORT.OUTSET = PIN1_bm; } /* Configure PLL with the 2 MHz RC oscillator as source and * multiply by 6 to get 12 MHz PLL clock and enable it. Wait * for it to be stable and set prescaler B and C to divide by two * to set the CPU clock to 3 MHz */ if ( (SWITCHPORTL.IN & PIN2_bm) == 0 ) { CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC2M_gc ); CLKSYS_Disable( OSC_PLLEN_bm ); CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 6 ); CLKSYS_Enable( OSC_PLLEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_2_2_gc ); do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); LEDPORT.OUTCLR = 0x1F; LEDPORT.OUTSET = PIN2_bm; } /* Dynamically change prescaler: * Set prescaler A to divide by 2, prescaler B with no division, * and prescaler C to divide by 2. This will give the CPU a freqency * of 1/4th of the input freqency. Input freqency will depend on the * previous switch that has been pressed. */ if ( (SWITCHPORTL.IN & PIN3_bm) == 0 ) { CLKSYS_Prescalers_Config( CLK_PSADIV_2_gc, CLK_PSBCDIV_1_2_gc ); LEDPORT.OUTSET = PIN3_bm; } /* * Add code here for generating 62 MHz to clkper4 and 15.5 MHz to the CPU */ if ( (SWITCHPORTL.IN & PIN4_bm) == 0 ) { CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC2M_gc ); CLKSYS_Disable( OSC_PLLEN_bm ); CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 31 ); CLKSYS_Enable( OSC_PLLEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_2_2_gc ); do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); LEDPORT.OUTCLR = 0x1F; LEDPORT.OUTSET = PIN4_bm; } // Blink LEDS after the loop has been run several times // This gives us visual feedback on the MCU speed counter++; if (counter > 20000 ) { LEDPORT.OUTTGL = 0xE0; counter=0; } } }
/** * Main is used to: * Initialize drivers * Create threads * Pass drivers structures to threads */ int main( void ) { /* * Enable internal 32 MHz ring oscillator and wait until it's * stable. Set the 32 MHz ring oscillator as the main clock source. */ CLKSYS_Enable( OSC_RC32MEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc ); do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc ); //Enable watchdog timer, which will be reset by timer WDT_EnableAndSetTimeout( WDT_PER_1KCLK_gc ); /* * Do all configuration and create all tasks and queues before scheduler is started. * It is possible to put initialization of peripherals like displays into task functions * (which will be executed after scheduler has started) if fast startup is needed. * Interrupts are not enabled until the call of vTaskStartScheduler(); */ // Enable the Round-Robin Scheduling scheme.Round-Robin scheme ensures that no low-level // interrupts are “starved”, as the priority changes continuously PMIC_EnableRoundRobin(); //Create and start the timer, which will reset Watch Dog Timer xTimerStart(xTimerCreate((signed char*)"WDT",500, pdTRUE, 0, watchdogTimerCallback), 0); //---------Use USART on PORTC---------------------------- Serial usartFTDI = Serial(&USARTE0, BAUD9600, 128, 10); // Initialize SPI slave on port D SpiSlave spiSlave = SpiSlave(&SPIC,false,SPI_MODE_1_gc,64); // Initialize SPI master on port C SpiMaster spiMaster = SpiMaster(&SPID, false, SPI_MODE_1_gc, false, SPI_PRESCALER_DIV4_gc); SpiDevice spiDevice = SpiDevice(&spiMaster, &PORTD, SPI_SS_bm); //---------Start LED task for testing purposes----------- ledRGB = LedGroup(3); ledRGB.add(&PORTF, 0x04,1 );//R ledRGB.add(&PORTF, 0x08,1 );//G ledRGB.add(&PORTF, 0x02,1 );//B ledRGB.set(BLUE); LedProcessorThread ledRGBEThread = LedProcessorThread(&ledRGB, GREEN, 500, "RGB", 64, configLOW_PRIORITY); LedGroup ledString = LedGroup(7); ledString.add(&PORTA, 0x02, 0); ledString.add(&PORTA, 0x04, 0); ledString.add(&PORTA, 0x08, 0); ledString.add(&PORTA, 0x10, 0); ledString.add(&PORTA, 0x20, 0); ledString.add(&PORTA, 0x40, 0); ledString.add(&PORTA, 0x80, 0); LedProcessorThread ledStringThread = LedProcessorThread(&ledString, "STR", 64, configLOW_PRIORITY); // ***** Start main Looper Looper looper = Looper(10, "LPR", 750, configNORMAL_PRIORITY); //XXX why it is not working if on the heap not on the stack? //ExampleHandler *exampleHandler = (ExampleHandler*) pvPortMalloc(sizeof(ExampleHandler)); //*exampleHandler = ExampleHandler(looper, spiDevice, ledStringQueue, usartFTDI); ExampleHandler exampleHandler = ExampleHandler(&looper, &spiDevice, &ledStringThread, &usartFTDI); // ****** Register commands for the interpreter CommandInterpreter interpreter = CommandInterpreter(); interpreter.registerCommand(Strings_SpiExampleCmd, Strings_SpiExampleCmdDesc, &exampleHandler, EVENT_RUN_SPI_TEST); interpreter.registerCommand(Strings_BlinkCmd, Strings_BlinkCmdDesc, &exampleHandler, EVENT_BLINK); CommandInterpreterThread cmdIntThreadFTDI = CommandInterpreterThread(&interpreter, 32, &usartFTDI, "I12", 128, configNORMAL_PRIORITY); PinChangeController pinChangeController = PinChangeController(); pinChangeController.registerOnPinChangeListener(&exampleHandler, &PORTD, PIN2_bm, PORT_OPC_TOTEM_gc, PORT_ISC_RISING_gc); // ****** Start stand-alone tasks SpiSlaveThread spiSlaveThread = SpiSlaveThread(&spiSlave, &usartFTDI, "SLV", 128, configLOW_PRIORITY); /* Start scheduler. Creates idle task and returns if failed to create it. * vTaskStartScheduler never returns during normal operation. It is unlikely that XMEGA port will need to * dynamically create tasks or queues. To ensure stable work, create ALL tasks and ALL queues before * vTaskStartScheduler call. * Interrupts would be enabled by calling PMIC_EnableLowLevel();*/ vTaskStartScheduler(); /* Should never get here, stop execution and report error */ while(true) ledRGB.set(PINK); return 0; }
void init_clock(void) { ////////////////////////////////////////////////////////// /* system clock setup */ /* start the 32MHz ring oscillator ticking */ /* ticks at 32MHz */ CLKSYS_Enable(OSC_RC32MEN_bm); /* set the clock prescaler to divide by 1 */ /* ticks at 32MHz */ CLKSYS_Prescalers_Config(CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc); /* enable mid-priority interrupts */ PMIC.CTRL |= PMIC_MEDLVLEN_bm; PMIC.CTRL |= PMIC_LOLVLEN_bm; ////////////////////////////////////////////////////////// /* clock 1 setup */ /* we use clock 1 for control loops because the avr-gcc headers * only have the registers for PWMing TC0 */ /* medium-priority interrupt */ TCC1.INTCTRLA = (TCC1.INTCTRLA & ~TC1_OVFINTLVL_gm) | TC_OVFINTLVL_MED_gc; /* divide main clock source by 2 */ /* ticks at 16MHz */ TCC1.CTRLA = (TCC1.CTRLA & ~TC1_CLKSEL_gm) | TC_CLKSEL_DIV2_gc; /* count to 1600 before looping. */ /* ticks at 10kHz */ /* we use this for handling control loops */ TCC1.PER = 1600; ////////////////////////////////////////////////////////// /* clock 0 setup */ TCD0.CTRLB = 0x00; /* low-priority interrupt */ TCD0.INTCTRLA = (TCD0.INTCTRLA & ~TC0_OVFINTLVL_gm) | TC_OVFINTLVL_LO_gc; /* divide main clock source by 2 */ /* ticks at 16MHz */ TCD0.CTRLA = (TCD0.CTRLA & ~TC0_CLKSEL_gm) | TC_CLKSEL_DIV2_gc; /* count to 65536 before looping. */ /* ticks at about 250Hz */ /* we use its A and B comparators for motor PWM */ TCD0.PER = PWM_PERIOD; /* single-slope PWM with both CCA and CCB enabled */ TCD0.CTRLB = (TCD0.CTRLB & ~TC0_WGMODE_gm) | TC_WGMODE_SS_gc | TC0_CCAEN_bm | TC0_CCBEN_bm; ////////////////////////////////////////////////////////// /* start things ticking */ /* wait until the 32MHz oscillator is stable */ do {nop();} while (CLKSYS_IsReady(OSC_RC32MRDY_bm) == 0); /* and select it */ CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_RC32M_gc); }