10 Appendix

Code examples for each use case in this technical brief.

Polling VDDIO2 Status

#define F_CPU 4000000ul

#include <avr/io.h>

int main(void)
{
    while (1)
    {
        /*Check if VDDIO2 is within acceptable range*/
        if(MVIO.STATUS & MVIO_VDDIO2S_bm)
        {
            /* Replace with your application code */
        }
    }
}

Interrupt on VDDIO2 Status

#define F_CPU 4000000ul

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

void LED0_init(void) {
    PORTF.DIRSET = PIN5_bm;
}
void LED0_toggle(void) {
    PORTF.OUTTGL = PIN5_bm;
}

int main(void)
{
    /*Enable the VDDIO2 interrupt*/
    MVIO.INTCTRL |= MVIO_VDDIO2IE_bm;

    /*Enable Global interrupts*/
    sei();
    LED0_init();
	
    /* Replace with your application code */
    while (1)
	{
	}
}

ISR(MVIO_MVIO_vect)
{
    /* The VDDIO2 interrupt has been triggered */
    LED0_toggle();
    /* Clear the CFD interrupt flag */
    MVIO.INTFLAGS |= MVIO_VDDIO2IF_bm;
}

Measuring VDDIO2

#define F_CPU 4000000ul
/*Initialize ADC0*/
void adc0_init(void)
{
    /*Select the 1.024V referance for ADC0*/
    VREF.ADC0REF = VREF_REFSEL_1V024_gc;	
	
    /* Enable 16 over samples*/
    ADC0.CTRLB = ADC_SAMPNUM_ACC16_gc;

    /*Enable ADC0*/
    ADC0.CTRLA = ADC_ENABLE_bm;
}

/*Measure VDDIO2 using the internal ADC*/
uint16_t read_VDDIO2()
{
    /*Select VDDIO2DIV10 as input for ADC0*/
    ADC0.MUXPOS = ADC_MUXPOS_VDDIO2DIV10_gc;

    /*Start an ADC conversion*/
    ADC0.COMMAND = ADC_STCONV_bm;

    /*Wait for the ADC conversion to be done*/
    while(!(ADC0.INTFLAGS & ADC_RESRDY_bm));
	
    /*Read the RES register and shift the result 4 bits to compensate for the 16 oversamples*/
    uint16_t result = (ADC0.RES>>4);
	
    return result;
}

int main(void)
{
    adc0_init();
    /* Replace with your application code */
    while (1)
    {
        /*Read the VDDIO2 voltage continiously*/
        read_VDDIO2();
    }
}

Blink a 3V LED with VDD at 1.8V

#define F_CPU 4000000ul

#include <avr/io.h>
#include <util/delay.h>

void LED_PC0_init(void)
{
    PORTC.DIRSET = PIN0_bm;
}

void LED_PC0_toggle(void)
{
    PORTC.OUTTGL=PIN0_bm;
} 



int main(void)
{
    LED_PC0_init();
    while (1) 
    {
        /*Check if VDDIO2 is within acceptable range*/
	 if(MVIO.STATUS & MVIO_VDDIO2S_bm)
	 {
	     /*Blink LED at PC0 forever*/
	     while (1)
	         {
		      LED_PC0_toggle();
		      _delay_ms(250);
		  }	
        }
    }
}