/* Frequency Divider using CCL - see http://www.technoblogy.com/show?3HWB David Johnson-Davies - www.technoblogy.com - 3rd March 2021 AVR128DA28 @ 16 MHz (internal oscillator) CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ // TCA0 flashes LED ********************************************** void TCA0Setup () { PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTA_gc; // WO outputs on PORTA PORTA.DIRSET = PIN0_bm; // Make WO0 an output TCA0.SINGLE.CTRLD = 0; // Single mode // Set Frequency mode: TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_WGMODE_FRQ_gc; TCA0.SINGLE.CMP0 = 15624; // Set period 0.5 Hz // Prescale 1024 and enable: TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1024_gc | TCA_SINGLE_ENABLE_bm; } // Set up CCL ********************************************** void CCLSetup () { // Set up Configurable Custom Logic LUT0 and LUT1 CCL.LUT0CTRLA = CCL_CLKSRC_IN2_gc; // Flip-Flop clocked by IN2 CCL.LUT0CTRLB = CCL_INSEL0_FEEDBACK_gc; // Feed Flip-Flop output to IN0 CCL.LUT0CTRLC = CCL_INSEL2_IN2_gc; // Input IN2 from pin PA2 // Sequential logic 0 CCL.SEQCTRL0 = CCL_SEQSEL_DFF_gc; // Use D-type Flip-Flop // Truth tables CCL.TRUTH0 = 0b01010101; // Truth table for LUT0 CCL.TRUTH1 = 0b11111111; // Truth table for LUT1 // Exactly the same for LUT2 and LUT3 CCL.LUT2CTRLA = CCL_CLKSRC_IN2_gc; // Flip-Flop clocked by IN2 CCL.LUT2CTRLB = CCL_INSEL2_FEEDBACK_gc; // Feed Flip-Flop output to IN0 CCL.LUT2CTRLC = CCL_INSEL2_IN2_gc; // Input IN2 from pin PA2 // Sequential logic 1 CCL.SEQCTRL1 = CCL_SEQSEL_DFF_gc; // Use D-type Flip-Flop // Truth tables CCL.TRUTH2 = 0b01010101; // Truth table for LUT2 CCL.TRUTH3 = 0b11111111; // Truth table for LUT3 // Now enable everything CCL.LUT0CTRLA |= CCL_ENABLE_bm | CCL_OUTEN_bm; // LUT0 and output to PA3 CCL.LUT1CTRLA |= CCL_ENABLE_bm; // LUT1 enabled CCL.LUT2CTRLA |= CCL_ENABLE_bm | CCL_OUTEN_bm; // LUT2 and output to PD3 CCL.LUT3CTRLA |= CCL_ENABLE_bm; // LUT3 enabled CCL.CTRLA = CCL_ENABLE_bm; // Enable CCL } void setup () { TCA0Setup(); CCLSetup(); } void loop() { }