Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Analog Comparator (AC)

See Quick start guide for the XMEGA AC driver.

This is a driver for configuring, enabling, disabling and use of the on-chip analog comparator (AC).

Dependencies

The analog comparator depends on the following modules:

start guide

See Quick start guide for the XMEGA AC driver

Modules

 
 Quick Start Guide(s)
 In this section you can find a list of all Quick Start guides related to the Analog Comparator (AC).
 

Data Structures

struct  ac_config
 Analog comparator channel configuration struct. More...
 

Typedefs

typedef void(* ac_callback_t )(AC_t *ac, uint8_t channel, enum ac_status_t status)
 Interrupt event callback function type. More...
 

Enumerations

enum  ac_int_level_t {
  AC_INT_LVL_OFF = 0x00,
  AC_INT_LVL_LO = 0x01,
  AC_INT_LVL_MED = 0x02,
  AC_INT_LVL_HI = 0x03
}
 Analog comparator interrupt levels. More...
 
enum  ac_interrupt_mode_t {
  AC_INT_MODE_BOTH_EDGES = AC_INTMODE_BOTHEDGES_gc,
  AC_INT_MODE_FALLING_EDGE = AC_INTMODE_FALLING_gc,
  AC_INT_MODE_RISING_EDGE = AC_INTMODE_RISING_gc,
  AC_INT_MODE_ABOVE_WINDOW = AC_WINTMODE_ABOVE_gc,
  AC_INT_MODE_INSIDE_WINDOW = AC_WINTMODE_INSIDE_gc,
  AC_INT_MODE_BELOW_WINDOW = AC_WINTMODE_BELOW_gc,
  AC_INT_MODE_OUTSIDE_WINDOW = AC_WINTMODE_OUTSIDE_gc
}
 Analog comparator interrupt modes. More...
 
enum  ac_mode_t {
  AC_MODE_SINGLE,
  AC_MODE_WINDOW
}
 Analog comparator modes. More...
 
enum  ac_status_t {
  AC_STATUS_ABOVE = AC_WSTATE_ABOVE_gc,
  AC_STATUS_BELOW = AC_WSTATE_BELOW_gc,
  AC_STATUS_INSIDE = AC_WSTATE_INSIDE_gc
}
 Analog comparator status values. More...
 

Management functions

void ac_write_config (AC_t *ac, uint8_t channel, struct ac_config *config)
 Write analog comparator configuration to hardware. More...
 
void ac_read_config (AC_t *ac, uint8_t channel, struct ac_config *config)
 Read analog comparator configuration from hardware. More...
 
enum ac_status_t ac_get_status (AC_t *ac, uint8_t channel)
 Get analog compare channel status. More...
 
void ac_enable (AC_t *ac, uint8_t channel)
 Enable an analog comparator channel. More...
 
void ac_disable (AC_t *ac, uint8_t channel)
 Disable an analog comparator channel. More...
 

Interrupt management functions

void ac_set_interrupt_callback (AC_t *ac, ac_callback_t callback)
 Set interrupt callback function. More...
 
static void ac_set_interrupt_mode (struct ac_config *config, enum ac_interrupt_mode_t mode)
 Set analog comparator channel interrupt mode. More...
 
static void ac_set_interrupt_level (struct ac_config *config, enum ac_int_level_t level)
 Set analog comparator channel interrupt level. More...
 

Configuration helper functions

static void ac_set_mode (struct ac_config *config, enum ac_mode_t mode)
 Set analog comparator mode. More...
 
static void ac_set_positive_reference (struct ac_config *config, AC_MUXPOS_t reference)
 Set analog comparator reference for positive line. More...
 
static void ac_set_negative_reference (struct ac_config *config, AC_MUXNEG_t reference)
 Set analog comparator reference for negative line. More...
 
static void ac_set_hysteresis (struct ac_config *config, AC_HYSMODE_t hysteresis)
 Set analog comparator hysteresis. More...
 
static void ac_set_voltage_scaler (struct ac_config *config, uint8_t scale)
 Set analog comparator voltage scale. More...
 

typedef void(* ac_callback_t)(AC_t *ac, uint8_t channel, enum ac_status_t status)

Interrupt event callback function type.

The interrupt handler can be configured to do a function callback upon a compare interrupt, the callback function must match the ac_callback_t type.

Example:

void my_ac_callback(AC_t *ac, uint8_t channel, enum ac_status_t status) {
// Add desired functionality.
}

Then add this callback function to the analog comparator interrupt routine by setting it in the driver.

ac_set_interrupt_callback(ACA, 0, my_ac_callback);

Analog comparator interrupt levels.

Enumerator
AC_INT_LVL_OFF 
AC_INT_LVL_LO 
AC_INT_LVL_MED 
AC_INT_LVL_HI 

Analog comparator interrupt modes.

Enumerator
AC_INT_MODE_BOTH_EDGES 

Interrupt on both edges.

AC_INT_MODE_FALLING_EDGE 

Interrupt on falling edge.

AC_INT_MODE_RISING_EDGE 

Interrupt on rising edge.

AC_INT_MODE_ABOVE_WINDOW 

Interrupt when above window.

AC_INT_MODE_INSIDE_WINDOW 

Interrupt when inside window.

AC_INT_MODE_BELOW_WINDOW 

Interrupt when below window.

AC_INT_MODE_OUTSIDE_WINDOW 

Interrupt when outside the window.

enum ac_mode_t

Analog comparator modes.

Enumerator
AC_MODE_SINGLE 

Analog comparator in normal single trigger value mode.

AC_MODE_WINDOW 

Analog comparator in window trigger mode.

Analog comparator status values.

Enumerator
AC_STATUS_ABOVE 

Compare value is above window or reference value.

AC_STATUS_BELOW 

Compare value is below window or reference value.

AC_STATUS_INSIDE 

Compare value is inside window.

void ac_disable ( AC_t *  ac,
uint8_t  channel 
)

Disable an analog comparator channel.

Parameters
acPointer to the analog comparator (AC) base address
channelNumber of analog comparator (AC) channel to disable

References ac_aca_opened, ac_acb_opened, cpu_irq_restore(), cpu_irq_save(), SLEEPMGR_IDLE, sleepmgr_unlock_mode(), SYSCLK_AC, sysclk_disable_module(), SYSCLK_PORT_A, and SYSCLK_PORT_B.

Referenced by example_acb_interrupt_callback().

void ac_enable ( AC_t *  ac,
uint8_t  channel 
)

Enable an analog comparator channel.

Parameters
acPointer to the analog comparator (AC) base address
channelNumber of analog comparator (AC) channel to enable

References ac_aca_opened, ac_acb_opened, cpu_irq_restore(), cpu_irq_save(), SLEEPMGR_IDLE, sleepmgr_lock_mode(), SYSCLK_AC, sysclk_enable_module(), SYSCLK_PORT_A, and SYSCLK_PORT_B.

Referenced by example_acb_interrupt_callback(), and main().

enum ac_status_t ac_get_status ( AC_t *  ac,
uint8_t  channel 
)

Get analog compare channel status.

This function will get the current analog compare status from the given channel.

Parameters
acPointer to the analog comparator (AC) base address
channelNumber of analog comparator (AC) channel to get status from
Return values
Analogcomparator status given by a ac_status_t value

References AC_STATUS_ABOVE, and AC_STATUS_BELOW.

Referenced by main().

void ac_read_config ( AC_t *  ac,
uint8_t  channel,
struct ac_config config 
)

Read analog comparator configuration from hardware.

This function will read the hardware configuration and put it into the config argument.

Parameters
acPointer to the analog comparator (AC) base address
channelNumber of analog comparator (AC) channel
configPointer to a ac_config variable

References ac_config::acctrl, ac_config::acmuxctrl, cpu_irq_restore(), cpu_irq_save(), ac_config::ctrlb, and ac_config::winctrl.

Referenced by main().

static void ac_set_hysteresis ( struct ac_config config,
AC_HYSMODE_t  hysteresis 
)
inlinestatic

Set analog comparator hysteresis.

This function helps the caller setting the analog comparator hysteresis for a channel configuration. The hysteresis are defined in the toolchain header files in the form of AC_HYSMODE_*_gc, where * represents NONE, SMALL and LARGE.

Parameters
configPointer to an ac_config variable
hysteresisAnalog comparator hysteresis config

References ac_config::acctrl.

Referenced by main().

void ac_set_interrupt_callback ( AC_t *  ac,
ac_callback_t  callback 
)

Set interrupt callback function.

This function allows the caller to set and change the interrupt callback function. Without setting a callback function the interrupt handler in the driver will only clear the interrupt flags.

Parameters
acPointer to the analog comparator (AC) base address
callbackReference to a callback function

References ac_aca_callback, and ac_acb_callback.

Referenced by main().

static void ac_set_interrupt_level ( struct ac_config config,
enum ac_int_level_t  level 
)
inlinestatic

Set analog comparator channel interrupt level.

This function allows the caller to set the analog comparator channel interrupt level. The interrupt levels are defined by the

See Also
enum ac_int_level_t defines.
Precondition
ac_set_mode() must be called before this function.
Parameters
configPointer to an ac_config variable
levelAnalog comparator interrupt level

References ac_config::acctrl, and ac_config::winctrl.

Referenced by main().

static void ac_set_interrupt_mode ( struct ac_config config,
enum ac_interrupt_mode_t  mode 
)
inlinestatic

Set analog comparator channel interrupt mode.

This function allows the caller to set which analog comparator channel interrupt mode should cause an interrupt to trigger.

Parameters
configPointer to an ac_config variable
modeInterrupt mode given by an ac_interrupt_mode_t value

References ac_config::acctrl, and ac_config::winctrl.

Referenced by example_acb_interrupt_callback(), and main().

static void ac_set_mode ( struct ac_config config,
enum ac_mode_t  mode 
)
inlinestatic

Set analog comparator mode.

This function helps the caller setting the analog comparator mode for a channel configuration. For AVR XMEGA this can be normal where it will trigger at a defined value, or window mode, where two analog comparators are combined to make two limits and three operating areas. The modes are defined in the ac_mode_t type.

Note
This function must be called before any other analog comparator channel or window configuration. This is due to some of the set functions depends on which mode the channels should be configured for.
Parameters
configPointer to an ac_config variable
modeAnalog comparator mode given by a ac_mode_t value

References AC_MODE_SINGLE, and ac_config::winctrl.

Referenced by main().

static void ac_set_negative_reference ( struct ac_config config,
AC_MUXNEG_t  reference 
)
inlinestatic

Set analog comparator reference for negative line.

This function helps the caller setting the analog comparator reference source for the negative line, for a given channel configuration. The references are defined in the toolchain header files in the form of AC_MUXNEG_*_gc, where * represents the various available references.

Note
In window mode the negative reference is the compare levels, analog comparator channel 0 hold the upper limit of the window, while channel 1 hold the lower limit of the window.
See Also
Chip-specific datasheet or header file for available mux configuration.
Parameters
configPointer to an ac_config variable
referenceAnalog comparator negative reference

References ac_config::acmuxctrl.

Referenced by main().

static void ac_set_positive_reference ( struct ac_config config,
AC_MUXPOS_t  reference 
)
inlinestatic

Set analog comparator reference for positive line.

This function helps the caller setting the analog comparator reference source for the positive line, for a given channel configuration. The references are defined in the toolchain header files in the form of AC_MUXPOS_*_gc, where * represents the various available references.

Note
In window mode the positive reference is the input signal.
See Also
Chip-specific datasheet or header file for available mux configuration.
Parameters
configPointer to an ac_config variable
referenceAnalog comparator positive reference

References ac_config::acmuxctrl.

Referenced by main().

static void ac_set_voltage_scaler ( struct ac_config config,
uint8_t  scale 
)
inlinestatic

Set analog comparator voltage scale.

This function helps the caller setting the analog comparator voltage scaler for a channel configuration, the voltage scale is a numeric value.

Note
The analog comparator has one voltage scaler shared between the channels. If both channels use the output from the voltage scaler they must both share the same configuration.
Parameters
configPointer to an ac_config variable
scaleNumeric value of the voltage scale

References Assert, and ac_config::ctrlb.

Referenced by main().

void ac_write_config ( AC_t *  ac,
uint8_t  channel,
struct ac_config config 
)

Write analog comparator configuration to hardware.

This function will write a ac_config struct to the selected analog comparator hardware and channel.

Parameters
acPointer to the analog comparator (AC) base address
channelNumber of analog comparator (AC) channel
configPointer to a ac_config variable

References ac_aca_opened, ac_acb_opened, ac_config::acctrl, ac_config::acmuxctrl, cpu_irq_restore(), cpu_irq_save(), ac_config::ctrlb, SYSCLK_AC, sysclk_disable_module(), sysclk_enable_module(), SYSCLK_PORT_A, SYSCLK_PORT_B, and ac_config::winctrl.

Referenced by example_acb_interrupt_callback(), and main().