# XMEGA Ring with Event System Here I'm using the XMEGA's Event System to run a ring oscillator. It runs at 6MHz - the XMEGA can be made to do the same thing at 5.2MHz through the CPU. ![osc](xmega-ring-evsys.jpg) ```C void clock_init(void){ OSC.XOSCCTRL = OSC_XOSCSEL_XTAL_256CLK_gc | OSC_FRQRANGE_12TO16_gc; // select external source OSC.CTRL = OSC_XOSCEN_bm; // enable external source while(!(OSC.STATUS & OSC_XOSCRDY_bm)); // wait for external OSC.PLLCTRL = OSC_PLLSRC_XOSC_gc | 3; // select external osc for pll, do pll = source * 3 OSC.CTRL |= OSC_PLLEN_bm; // enable PLL while (!(OSC.STATUS & OSC_PLLRDY_bm)); // wait for PLL to be ready CCP = CCP_IOREG_gc; // enable protected register change CLK.CTRL = CLK_SCLKSEL_PLL_gc; // switch to PLL for main clock /* OSC.PLLCTRL = OSC_PLLFAC4_bm | OSC_PLLFAC3_bm; // 2 MHz * 24 = 48 MHz OSC.CTRL = OSC_PLLEN_bm; // enable PLL while (!(OSC.STATUS & OSC_PLLRDY_bm)); // wait for PLL to be ready CCP = CCP_IOREG_gc; // enable protected register change CLK.CTRL = CLK_SCLKSEL_PLL_gc; // switch to PLL */ } void ring_evsys(void){ // set pin4 as input, fires interrupt / event on either edge PORTF.DIRCLR = PIN4_bm; PORTF.PIN4CTRL = PORT_ISC_BOTHEDGES_gc; // event channel 0 on events from F4 EVSYS_CH0MUX = EVSYS_CHMUX_PORTF_PIN4_gc; //EVSYS_CH0CTRL = EVSYS_DIGFILT_2SAMPLES_gc; // we can't write an event directly to a pin, // instead, setup a counter with EVCH0 as source and count to 0, wavegen on that TCF0.CTRLA = TC_CLKSEL_EVCH0_gc; TCF0.CTRLB = TC_WGMODE_FRQ_gc | (1 << 4); // frequency wave mode, and enable CCA channel output // need to set CCA, TCF0.CCABUFL = 0; TCF0.CCABUFH = 0; // we have to kick this to get it going EVSYS.STROBE = 1; } int main(void){ clock_init(); ring_evsys(); while(1){ // } } ```