8 Appendix
Level Crossing Detector Source Code
#include <avr/io.h> #include <avr/interrupt.h> /* set DACREF to 0.8 Volts for Vref = 1.5Volts */ #define DACREF_VALUE (0.8 * 256 / 1.5) void PORT0_init (void); void AC0_init(void); ISR(AC0_AC_vect) { /* Insert AC interrupt handling code here */ /* The interrupt flag has to be cleared manually */ AC0.STATUS = AC_CMP_bm; } void PORT0_init (void) { /* Positive Input - Disable digital input buffer */ PORTD.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc; /*Enable output buffer on PA7*/ PORTA |= PIN7_bm; } void AC0_init(void) { /* Negative input uses internal reference - voltage reference should be enabled */ VREF.CTRLA = VREF_AC0REFSEL_1V5_gc; /* Voltage reference at 1.5V */ VREF.CTRLB = VREF_AC0REFEN_bm; /* AC0 DACREF reference enable: enabled */ AC0.DACREF = DACREF_VALUE; /* Set DAC voltage reference */ /*Select proper inputs for comparator*/ AC0.MUXCTRLA = AC_MUXPOS_PIN0_gc /* Positive Input - Analog Positive Pin 0 */ | AC_MUXNEG_DACREF_gc; /* Negative Input - DAC Voltage Reference */ AC0.CTRLA = AC_ENABLE_bm /* Enable analog comparator */ | AC_OUTEN_bm; /* Output Buffer Enable: enabled */ AC0.INTCTRL = AC_CMP_bm; /* Analog Comparator 0 Interrupt enabled */ } int main(void) { PORT0_init(); AC0_init(); sei(); /* Global interrupts enabled */ while (1) { ; } }
Preventing False Spike Detection Source Code
#include <avr/io.h> #include <avr/interrupt.h> /* set DACREF to 0.8 Volts for Vref = 1.5Volts */ #define DACREF_VALUE (0.8 * 256 / 1.5) void PORT0_init (void); void AC0_init (void); ISR(AC0_AC_vect) { /* Insert AC interrupt handling code here */ /* The interrupt flag has to be cleared manually */ AC0.STATUS = AC_CMP_bm; } void PORT0_init (void) { /* Positive Input - Disable digital input buffer */ PORTD.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc; /*Enable output buffer on PA7*/ PORTA |= PIN7_bm; } void AC0_init (void) { /* Negative input uses internal reference - voltage reference should be enabled */ VREF.CTRLA = VREF_AC0REFSEL_1V5_gc; /* Voltage reference at 1.5V */ VREF.CTRLB = VREF_AC0REFEN_bm; /* AC0 DACREF reference enable: enabled */ AC0.DACREF = DACREF_VALUE; /* Set DAC voltage reference */ /*Select proper inputs for comparator*/ AC0.MUXCTRLA = AC_MUXPOS_PIN0_gc /* Positive Input - Analog Positive Pin 0 */ | AC_MUXNEG_DACREF_gc; /* Negative Input - DAC Voltage Reference */ AC0.CTRLA = AC_ENABLE_bm /* Enable analog comparator */ | AC_HYSMODE_25mV_gc /* Enable hysteresis @25mV */ | AC_OUTEN_bm; /* Output Buffer Enable: enabled */ AC0.INTCTRL = AC_CMP_bm; /* Analog Comparator 0 Interrupt enabled */ } int main(void) { PORT0_init(); AC0_init(); sei(); /*Global interrupts enabled */ while (1) { ; } }
Analog Signal Pulse Duration/Frequency Measurement Source Code
#include <avr/io.h> /* set DACREF to 0.8 Volts for Vref = 1.5Volts */ #define DACREF_VALUE (0.8 * 256 / 1.5) void PORT0_init (void); void EVENT_SYSTEM_init (void); void AC0_init (void); void TIMER0_init (void); void PORT0_init (void) { /* Positive Input - Disable digital input buffer */ PORTD.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc; /*Enable output buffer on PA7*/ PORTA |= PIN7_bm; } void AC0_init (void) { /* Negative input uses internal reference - voltage reference should be enabled */ VREF.CTRLA = VREF_AC0REFSEL_1V5_gc; /* Voltage reference at 1.5V */ VREF.CTRLB = VREF_AC0REFEN_bm; /* AC0 DACREF reference enable: enabled */ AC0.DACREF = DACREF_VALUE; /* Set DAC voltage reference */ /*Select proper inputs for comparator*/ AC0.MUXCTRLA = AC_MUXPOS_PIN0_gc /* Positive Input - Pin 0 */ | AC_MUXNEG_DACREF_gc; /* Negative Input - DAC Voltage Reference */ AC0.CTRLA = AC_ENABLE_bm /* Enable analog comparator */ | AC_OUTEN_bm; /* Output Buffer Enable: enabled */ AC0.INTCTRL = 0; /* Analog Comparator 0 Interrupt disabled */ } /*Init TCB in pulse width-frequency measurement mode, input from Analog Comparator through Event System */ void TIMER0_init (void) { TCB0.CTRLB = TCB_CNTMODE_FRQPW_gc; /* Input Capture Frequency and Pulse-Width measurement */ TCB0.EVCTRL = TCB_CAPTEI_bm; /* Event Input Enable: enabled */ TCB0.INTCTRL = TCB_CAPT_bm; /* Capture or Timeout: enabled */ TCB0.CTRLA = TCB_CLKSEL_CLKDIV2_gc /* CLK_PER/2 (From Prescaler) */ | TCB_ENABLE_bm /* Enable: enabled */ | TCB_RUNSTDBY_bm; /* Run Standby: enabled */ } /* Enable event generation from Analog comparator to TCB */ void EVENT_SYSTEM_init (void) { EVSYS.CHANNEL0 = EVSYS_GENERATOR_AC0_OUT_gc; /* Analog Comparator 0 out linked to Event Channel 0 */ EVSYS.USERTCB0 = EVSYS_CHANNEL_CHANNEL0_gc; /* TCB uses Event Channel 0 */ } int main(void) { uint16_t signal_pulse = 0, signal_period = 0; PORT0_init(); AC0_init(); EVENT_SYSTEM_init(); TIMER0_init(); while(1) { if (TCB0.INTFLAGS) { /** * First read the CNT register * The interrupt flag is cleared by writing 1 to it, or when the Capture register * is read in Capture mode */ signal_period = TCB0.CNT; signal_pulse = TCB0.CCMP; } } }