8 Appendix

Using Periodic Interrupt Mode Full Code Example

#define PERIOD_EXAMPLE_VALUE    (0x0CB6)

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

void TCA0_init(void);
void PORT_init(void);

void TCA0_init(void)
{
    /* enable overflow interrupt */
    TCA0.SINGLE.INTCTRL = TCA_SINGLE_OVF_bm;
    
    /* set Normal mode */
    TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_NORMAL_gc;
    
    /* disable event counting */
    TCA0.SINGLE.EVCTRL &= ~(TCA_SINGLE_CNTEI_bm);
    
    /* set the period */
    TCA0.SINGLE.PER = PERIOD_EXAMPLE_VALUE;  
    
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV256_gc         /* set clock source (sys_clk/256) */
                      | TCA_SINGLE_ENABLE_bm;               /* start timer */
}
void PORT_init(void)
{
    /* set pin 0 of PORT A as output */
    PORTA.DIR |= PIN0_bm;
}

ISR(TCA0_OVF_vect)
{
    /* Toggle PIN 0 of PORT A */
    PORTA.OUTTGL = PIN0_bm;
    
    /* The interrupt flag has to be cleared manually */
    TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm;
}

int main(void)
{
    PORT_init();
    
    TCA0_init();
    
    /* enable global interrupts */
    sei();
    
    while (1) 
    {
        ;
    }
}

Generating a Dual-Slope PWM Signal Full Code Example

#define PERIOD_EXAMPLE_VALUE        (0x01A0)
#define DUTY_CYCLE_EXAMPLE_VALUE    (0x00D0)

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

void TCA0_init(void);
void PORT_init(void);

void TCA0_init(void)
{
    /* set waveform output on PORT A */
    PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTA_gc;
    
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm             /* enable compare channel 0 */
                      | TCA_SINGLE_WGMODE_DSBOTTOM_gc;   /* set dual-slope PWM mode */
    
    /* disable event counting */
    TCA0.SINGLE.EVCTRL &= ~(TCA_SINGLE_CNTEI_bm); 
    
    /* set PWM frequency and duty cycle (50%) */
    TCA0.SINGLE.PERBUF  = PERIOD_EXAMPLE_VALUE;       
    TCA0.SINGLE.CMP0BUF = DUTY_CYCLE_EXAMPLE_VALUE;  
    
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV4_gc        /* set clock source (sys_clk/4) */
                      | TCA_SINGLE_ENABLE_bm;            /* start timer */
}

void PORT_init(void)
{    
    /* set pin 0 of PORT A as output */    
    PORTA.DIR |= PIN0_bm;
}

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

Generating Two PWM Signals in Split Mode Full Code Example

#define PERIOD_EXAMPLE_VALUE_L        (0xCF)
#define PERIOD_EXAMPLE_VALUE_H        (0x44)
#define DUTY_CYCLE_EXAMPLE_VALUE_L    (0x68)
#define DUTY_CYCLE_EXAMPLE_VALUE_H    (0x11)

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

void TCA0_init(void);
void PIN_init(void);
void TCA0_hardReset(void);

void TCA0_init(void)
{
    /* set waveform output on PORT A */
    PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTA_gc;
    
    /* enable split mode */
    TCA0.SPLIT.CTRLD = TCA_SPLIT_SPLITM_bm;                 
    
    TCA0.SPLIT.CTRLB = TCA_SPLIT_HCMP0EN_bm         /* enable compare channel 0 for the higher byte */
                     | TCA_SPLIT_LCMP0EN_bm;        /* enable compare channel 0 for the lower byte */

    /* set the PWM frequencies and duty cycles */
    TCA0.SPLIT.LPER = PERIOD_EXAMPLE_VALUE_L;
    TCA0.SPLIT.LCMP0 = DUTY_CYCLE_EXAMPLE_VALUE_L;
    TCA0.SPLIT.HPER = PERIOD_EXAMPLE_VALUE_H;
    TCA0.SPLIT.HCMP0 = DUTY_CYCLE_EXAMPLE_VALUE_H;

    TCA0.SPLIT.CTRLA = TCA_SPLIT_CLKSEL_DIV16_gc    /* set clock source (sys_clk/16) */
                     | TCA_SPLIT_ENABLE_bm;         /* start timer */
}

void PIN_init(void)
{
    PORTA.DIR |= PIN0_bm    /* set pin 0 of PORT A as output */
              | PIN3_bm;    /* set pin 3 of PORT A as output */
}

/* must be used when switching from single mode to split mode */
void TCA0_hardReset(void)
{
    /* stop timer */
    TCA0.SINGLE.CTRLA &= ~(TCA_SINGLE_ENABLE_bm);  
    
    /* force a hard reset */
    TCA0.SINGLE.CTRLESET = TCA_SINGLE_CMD_RESET_gc; 
}

int main(void)
{
    PIN_init();
    
    TCA0_init();
    
    while (1) 
    {
        ;
    }
}