ADC Single Conversion Code Example
#define RTC_PERIOD (511)
#include <avr/io.h>
#include <avr/interrupt.h>
uint16_t adcVal;
void ADC0_init(void);
uint16_t ADC0_read(void);
void ADC0_init(void)
{
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
ADC0.CTRLC = ADC_PRESC_DIV4_gc
| ADC_REFSEL_INTREF_gc;
ADC0.CTRLA = ADC_ENABLE_bm
| ADC_RESSEL_10BIT_gc;
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
}
uint16_t ADC0_read(void)
{
ADC0.COMMAND = ADC_STCONV_bm;
while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) )
{
;
}
ADC0.INTFLAGS = ADC_RESRDY_bm;
return ADC0.RES;
}
int main(void)
{
ADC0_init();
adcVal = ADC0_read();
while (1)
{
;
}
}
ADC Free-Running Code
Example
#include <avr/io.h>
#include <stdbool.h>
uint16_t adcVal;
void ADC0_init(void);
uint16_t ADC0_read(void);
void ADC0_start(void);
bool ADC0_conersionDone(void);
void ADC0_init(void)
{
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
ADC0.CTRLC = ADC_PRESC_DIV4_gc
| ADC_REFSEL_INTREF_gc;
ADC0.CTRLA = ADC_ENABLE_bm
| ADC_RESSEL_10BIT_gc;
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
ADC0.CTRLA |= ADC_FREERUN_bm;
}
uint16_t ADC0_read(void)
{
ADC0.INTFLAGS = ADC_RESRDY_bm;
return ADC0.RES;
}
void ADC0_start(void)
{
ADC0.COMMAND = ADC_STCONV_bm;
}
bool ADC0_conersionDone(void)
{
return (ADC0.INTFLAGS & ADC_RESRDY_bm);
}
int main(void)
{
ADC0_init();
ADC0_start();
while(1)
{
if (ADC0_conersionDone())
{
adcVal = ADC0_read();
}
}
}
ADC Sample Accumulator Code Example
#define ADC_SHIFT_DIV64 (6)
#include <avr/io.h>
uint16_t adcVal;
void ADC0_init(void);
uint16_t ADC0_read(void);
void ADC0_init(void)
{
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
ADC0.CTRLC = ADC_PRESC_DIV4_gc
| ADC_REFSEL_INTREF_gc;
ADC0.CTRLA = ADC_ENABLE_bm
| ADC_RESSEL_10BIT_gc;
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
ADC0.CTRLB = ADC_SAMPNUM_ACC64_gc;
}
uint16_t ADC0_read(void)
{
ADC0.COMMAND = ADC_STCONV_bm;
while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) )
{
;
}
ADC0.INTFLAGS = ADC_RESRDY_bm;
return ADC0.RES;
}
int main(void)
{
ADC0_init();
while (1)
{
adcVal = ADC0_read();
adcVal = adcVal >> ADC_SHIFT_DIV64;
}
}
ADC Window Comparator Code Example
#define WINDOW_CMP_LOW_TH_EXAMPLE (0x100)
#include <avr/io.h>
#include <stdbool.h>
uint16_t adcVal;
void ADC0_init(void);
uint16_t ADC0_read(void);
void ADC0_start(void);
bool ADC0_conersionDone(void);
bool ADC0_resultBelowTreshold(void);
void ADC0_clearWindowCmpIntFlag(void);
void LED0_init(void);
void LED0_on(void);
void LED0_off(void);
void ADC0_init(void)
{
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
ADC0.CTRLC = ADC_PRESC_DIV4_gc
| ADC_REFSEL_INTREF_gc;
ADC0.CTRLA = ADC_ENABLE_bm
| ADC_RESSEL_10BIT_gc;
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
ADC0.WINLT = WINDOW_CMP_LOW_TH_EXAMPLE;
ADC0.CTRLE = ADC_WINCM_BELOW_gc;
ADC0.CTRLA |= ADC_FREERUN_bm;
}
uint16_t ADC0_read(void)
{
ADC0.INTFLAGS = ADC_RESRDY_bm;
return ADC0.RES;
}
void ADC0_start(void)
{
ADC0.COMMAND = ADC_STCONV_bm;
}
bool ADC0_conersionDone(void)
{
return (ADC0.INTFLAGS & ADC_RESRDY_bm);
}
bool ADC0_resultBelowTreshold(void)
{
return (ADC0.INTFLAGS & ADC_WCMP_bm);
}
void ADC0_clearWindowCmpIntFlag(void)
{
ADC0.INTFLAGS = ADC_WCMP_bm;
}
void LED0_init(void)
{
PORTB.OUT |= PIN5_bm;
PORTB.DIR |= PIN5_bm;
}
void LED0_on(void)
{
PORTB.OUT &= ~PIN5_bm;
}
void LED0_off(void)
{
PORTB.OUT |= PIN5_bm;
}
int main(void)
{
ADC0_init();
LED0_init();
ADC0_start();
while(1)
{
if (ADC0_conersionDone())
{
if(ADC0_resultBelowTreshold())
{
LED0_on();
ADC0_clearWindowCmpIntFlag();
}
else
{
LED0_off();
}
adcVal = ADC0_read();
}
}
}
ADC Event Triggered Code Example
#define RTC_PERIOD (511)
#include <avr/io.h>
#include <avr/interrupt.h>
volatile uint16_t adcVal;
void ADC0_init(void);
void LED0_init(void);
void LED0_toggle(void);
void RTC_init(void);
void EVSYS_init(void);
void ADC0_init(void)
{
PORTD.PIN6CTRL &= ~PORT_ISC_gm;
PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
ADC0.CTRLC = ADC_PRESC_DIV4_gc
| ADC_REFSEL_INTREF_gc;
ADC0.CTRLA = ADC_ENABLE_bm
| ADC_RESSEL_10BIT_gc;
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
ADC0.INTCTRL |= ADC_RESRDY_bm;
ADC0.EVCTRL |= ADC_STARTEI_bm;
}
void LED0_init(void)
{
PORTB.OUT |= PIN5_bm;
PORTB.DIR |= PIN5_bm;
}
void LED0_toggle(void)
{
PORTB.IN |= PIN5_bm;
}
ISR(ADC0_RESRDY_vect)
{
ADC0.INTFLAGS = ADC_RESRDY_bm;
adcVal = ADC0.RES;
LED0_toggle();
}
void RTC_init(void)
{
uint8_t temp;
temp = CLKCTRL.XOSC32KCTRLA;
temp &= ~CLKCTRL_ENABLE_bm;
CPU_CCP = CCP_IOREG_gc;
CLKCTRL.XOSC32KCTRLA = temp;
while(CLKCTRL.MCLKSTATUS & CLKCTRL_XOSC32KS_bm)
{
;
}
temp = CLKCTRL.XOSC32KCTRLA;
temp &= ~CLKCTRL_SEL_bm;
CPU_CCP = CCP_IOREG_gc;
CLKCTRL.XOSC32KCTRLA = temp;
temp = CLKCTRL.XOSC32KCTRLA;
temp |= CLKCTRL_ENABLE_bm;
CPU_CCP = CCP_IOREG_gc;
CLKCTRL.XOSC32KCTRLA = temp;
while (RTC.STATUS > 0)
{
;
}
RTC.CTRLA = RTC_PRESCALER_DIV32_gc
| RTC_RTCEN_bm
| RTC_RUNSTDBY_bm;
RTC.PER = RTC_PERIOD;
RTC.CLKSEL = RTC_CLKSEL_TOSC32K_gc;
RTC.DBGCTRL |= RTC_DBGRUN_bm;
}
void EVSYS_init(void)
{
EVSYS.CHANNEL0 = EVSYS_GENERATOR_RTC_OVF_gc;
EVSYS.USERADC0 = EVSYS_CHANNEL_CHANNEL0_gc;
}
int main(void)
{
ADC0_init();
LED0_init();
RTC_init();
EVSYS_init();
sei();
while (1)
{
;
}
}