7 Appendix

ZCD Free-Running Code Example

#include <avr/io.h>

void ZCD0_Init(void);
void PORT_Init(void);

/* Initialization of the Zero Cross Detector */
void ZCD0_Init(void)
{ 
    ZCD0.CTRLA = ZCD_ENABLE_bm       /* Enable the ZCD */
	           | ZCD_OUTEN_bm;    /* Enable the output of the ZCD */
}

void PORT_Init(void)
{
    /* Disable digital input buffer and pull-up resistor on PD1*/
    PORTD.PIN1CTRL |= PORT_ISC_INPUT_DISABLE_gc;
    
    /* Enable PA7 to become a gateway for an internal signal */
    PORTA.DIRSET |= PIN7_bm;
    PORTA.OUTSET |= PIN7_bm;
}

int main(void)
{
    ZCD0_Init();
    PORT_Init();
	
    while(1) 
    {
        ;
    }
}

ZCD Frequency Detection Code Example

#include <avr/io.h>
#include <avr/interrupt.h>

uint16_t signal_frequency = 0;

void CLK_Init (void);
void PORT_Init (void);
void ZCD0_Init (void);
void TCB0_Init (void);
void EVSYS_Init (void);

void CLK_Init(void)
{
    /* Select internal HF oscillator */
    _PROTECTED_WRITE (CLKCTRL.MCLKCTRLA, CLKCTRL_CLKSEL_OSCHF_gc);
    /* Set internal clock to 4 Mhz */
    _PROTECTED_WRITE (CLKCTRL.OSCHFCTRLA, CLKCTRL_FREQSEL_4M_gc);
}

void PORT_Init(void)
{
    /* Disable digital input buffer and pull-up resistor on PD1*/
    PORTD.PIN1CTRL = PORT_ISC_INPUT_DISABLE_gc;
		
    /* Enable PA7 to become a gateway for an internal signal */
    PORTA.OUTSET |= PIN7_bm;
    PORTA.DIRSET |= PIN7_bm;	
}

/* Initialization of the Zero Cross Detector */
void ZCD0_Init(void)
{
    ZCD0.CTRLA = ZCD_ENABLE_bm    /* Enable the ZCD */
	        | ZCD_OUTEN_bm;    /* Enable the output of the ZCD */
}

/* Initialize the TCB in pulse width-frequency measurement mode,
   input from ZCD through Event System */
void TCB0_Init(void)
{
    TCB0.CTRLB = TCB_CNTMODE_FRQ_gc;    /* Input Capture Frequency */
    TCB0.EVCTRL = TCB_CAPTEI_bm;        /* Event Input Enable: enabled */	
    TCB0.INTCTRL = TCB_CAPT_bm;         /* Capture or Timeout: enabled */

    TCB0.CTRLA = TCB_CLKSEL_DIV2_gc     /* CLK_PER/2 (From Prescaler) - This is needed to be
                                              able to count to 40 Hz with a 4 Mhz system clock */
	        | TCB_ENABLE_bm	   /* Enable: enabled */
	        | TCB_RUNSTDBY_bm;	/* Run Standby: enabled */
}

/* Enable event generation from ZCD to TCB */
void EVSYS_Init(void)
{
    /* Zero-cross detector 0 out linked to event channel 0 */
    EVSYS.CHANNEL0 = EVSYS_CHANNEL0_ZCD0_gc;
    /* TCB uses event channel 0 */
    EVSYS.USERTCB0CAPT = EVSYS_CHANNEL00_bm;
}

int main(void)
{
    CLK_Init();
    TCB0_Init();
    EVSYS_Init();
    ZCD0_Init();
    PORT_Init();

    while(1)
    {
        ;
    }
}

ISR(TCB0_INT_vect)
{
    /* The frequency is stored in the CCMP register
       An equation can be used to transform the value in Hz */
    signal_frequency = TCB0.CCMP;
}

ZCD Active Bridge Code Example

#include <avr/io.h>

void ZCD0_Init(void);
void ZCD1_Init(void);
void PORT_Init(void);
void EVSYS_Init(void);

/* Initialization of the IO Ports */
void PORT_Init(void)
{
    /* Disable digital input buffer and pull-up resistor on PE3*/
    PORTE.PIN3CTRL = PORT_ISC_INPUT_DISABLE_gc;

    /* Disable digital input buffer and pull-up resistor on PD1*/
    PORTD.PIN1CTRL = PORT_ISC_INPUT_DISABLE_gc;
	
    /* SET PB2 as an output*/
    PORTB.OUTSET |= PIN2_bm;
    PORTB.DIRSET |= PIN2_bm;
	
    /* SET PC2 as an output*/
    PORTC.OUTSET |= PIN2_bm;
    PORTC.DIRSET |= PIN2_bm;
}

/* Initialization of the Zero Cross Detector */
void ZCD0_Init(void)
{	
    ZCD0.CTRLA = ZCD_ENABLE_bm;    /* Enable the ZCD0 */
}

void ZCD1_Init(void)
{
    ZCD1.CTRLA = ZCD_ENABLE_bm     /* Enable the ZCD1 */
	        | ZCD_INVERT_bm;    /* Invert the ZCD1 OUTPUT */
}

/* Enable event generation from ZCD to Event system OUTPUT I/O pins */
void EVSYS_Init(void)
{
    /* Zero-cross detector 0 out linked to event channel 0 */
    EVSYS.CHANNEL0 = EVSYS_CHANNEL0_ZCD0_gc;
    /* Event system output B (PINB2) uses event channel 0 */
    EVSYS.USEREVSYSEVOUTB = EVSYS_CHANNEL00_bm;
	
    /* Zero-cross detector 1 out linked to event channel 1 */
    EVSYS.CHANNEL1 = EVSYS_CHANNEL1_ZCD1_gc;
    /* Event system output C (PINC2) uses event channel 1 */
    EVSYS.USEREVSYSEVOUTC = EVSYS_CHANNEL01_bm;
}

int main(void)
{
    EVSYS_Init();
    ZCD0_Init();
    ZCD1_Init();
    PORT_Init();
	
    while(1)
    {
        ;
    }
}