7 Appendix

Generating Complementary Driving Samples Source Code

#define SIGNAL_PERIOD_EXAMPLE_VALUE            (0xC8)
#define SIGNAL_DUTY_CYCLE_EXAMPLE_VALUE        (0x64)

#include <avr/io.h>
/*Using default clock 3.3 MHz */

void TCD0_init(void);
void TCD0_enableOutputChannels(void);
void PORT_init(void);

void TCD0_init(void)
{    
    /* set the waveform mode */
    TCD0.CTRLB = TCD_WGMODE_DS_gc;
    
    /* set the signal period */
    TCD0.CMPBCLR = SIGNAL_PERIOD_EXAMPLE_VALUE;    
    
    /* the signals are alternatively active and a small symmetric dead time is needed */
    TCD0.CMPBSET = SIGNAL_DUTY_CYCLE_EXAMPLE_VALUE + 1;   
    TCD0.CMPASET = SIGNAL_DUTY_CYCLE_EXAMPLE_VALUE - 1;
    
    /* ensure the ENRDY bit is set */
    while(!(TCD0.STATUS & TCD_ENRDY_bm))
    {
        ;
    }
    
    TCD0.CTRLA = TCD_CLKSEL_20MHZ_gc        /* choose the timer’s clock */
               | TCD_CNTPRES_DIV1_gc        /* choose the prescaler */
               | TCD_ENABLE_bm;             /* enable the timer */
}

void TCD0_enableOutputChannels(void)
{
    /* enable write-protected register */
    CPU_CCP = CCP_IOREG_gc;    
    
    TCD0.FAULTCTRL = TCD_CMPAEN_bm          /* enable channel A */
                   | TCD_CMPBEN_bm;         /* enable channel B */
}

void PORT_init(void)
{
    PORTA.DIR |= PIN4_bm                    /* set pin 4 as output */
              | PIN5_bm;                    /* set pin 5 as output */
}

int main(void)
{
    PORT_init();
    
    TCD0_enableOutputChannels();
    
    TCD0_init();
    
    /* Replace with your application code */
    while (1) 
    {
        ;
    }
}

Controlling Synchronous Signals Using Input Events Source Code

#define SETTLING_TIME_EXAMPLE_VALUE        (0x02)
#define DUTY_CYCLE_EXAMPLE_VALUE           (0xF6)

#include <avr/io.h>
/*Using default clock 3.3 MHz */

void TCD0_init(void);
void TCD0_enableOutputChannels(void);
void EVENT_SYSTEM_init(void);
void PORT_init(void);

void TCD0_init(void)
{
    /* set the waveform mode */
    TCD0.CTRLB = TCD_WGMODE_FOURRAMP_gc;
    
    /* set channel D to match channel B */
    TCD0.CTRLC = TCD_CMPDSEL_bm;        
    
    /* set the settling time and duty cycle for the signals*/    
    TCD0.CMPASET = SETTLING_TIME_EXAMPLE_VALUE;    
    TCD0.CMPACLR = DUTY_CYCLE_EXAMPLE_VALUE;                    
    TCD0.CMPBSET = SETTLING_TIME_EXAMPLE_VALUE;                    
    TCD0.CMPBCLR = DUTY_CYCLE_EXAMPLE_VALUE;                
    
    TCD0.EVCTRLA = TCD_CFG_FILTER_gc            /* set the anti-spike filter */
                 | TCD_EDGE_FALL_LOW_gc         /* set the ‘fault’ state */
                 | TCD_TRIGEI_bm;               /* enable input channel A */
    
    /* set the input mode */             
    TCD0.INPUTCTRLA = TCD_INPUTMODE_WAIT_gc;
    
    /* ensure the ENRDY bit is set */
    while(!(TCD0.STATUS & TCD_ENRDY_bm))
    {
        ;
    }
    
    TCD0.CTRLA = TCD_CLKSEL_20MHZ_gc            /* choose the timer’s clock */
               | TCD_CNTPRES_DIV4_gc            /* choose the prescaler */
               | TCD_ENABLE_bm;                 /* enable the timer */
}

void TCD0_enableOutputChannels(void)
{
    /* enable write-protected register */
    CPU_CCP = CCP_IOREG_gc;
    
    TCD0.FAULTCTRL = TCD_CMPAEN_bm              /* enable channel A */
                   | TCD_CMPBEN_bm              /* enable channel B */
                   | TCD_CMPCEN_bm              /* enable channel C */
                   | TCD_CMPDEN_bm;             /* enable channel D */
} 
    
void EVENT_SYSTEM_init(void)
{
    EVSYS.ASYNCCH2 = EVSYS_ASYNCCH2_PORTC_PIN5_gc;
    
    EVSYS.ASYNCUSER6 = EVSYS_ASYNCUSER6_ASYNCCH2_gc;
}

void PORT_init(void)
{
    /* set pin 4 and pin 5 of port A as output */
    PORTA.DIR |= PIN4_bm                        
              | PIN5_bm;                      
                
    /* set pin 0 and pin 1 of port C as output */
    PORTC.DIR |= PIN0_bm                        
              | PIN1_bm;
                
    /* set pin 5 of port C as input */
    PORTC.DIR &= ~PIN5_bm;                      
    
    /* enable pull-up resistor for pin 5 of port C */    
    PORTC.PIN5CTRL = PORT_PULLUPEN_bm;        
}

int main(void)
{    
    PORT_init();    
    
    EVENT_SYSTEM_init();
    
    TCD0_enableOutputChannels();
    
    TCD0_init();
    
    /* Replace with your application code */
    while (1) 
    {
        ;
    }
}