| Xmega Application Note |

# ac_driver.h File Reference

## Detailed Description

XMEGA Analog Comparator driver header file.

This file contains the function prototypes and enumerator definitions for various configuration parameters for the XMEGA Analog Comparator driver.

The driver is not intended for size and/or speed critical code, since most functions are just a few lines of code, and the function call overhead would decrease code performance. The driver is intended for rapid prototyping and documentation purposes for getting started with the XMEGA Analog Comparator.

For size and/or speed critical code, it is recommended to copy the function contents directly into your application instead of making a function call.

Application note:
  AVR1302: Using the XMEGA Analog Comparator

Documentation
  For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html

Author:
  Atmel Corporation: http://www.atmel.com
  Support email: avr@atmel.com

Revision
  1569

Date
  2008-04-22 13:03:43 +0200 (ti, 22 apr 2008)

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file ac_driver.h.

`#include "avr_compiler.h"`

Include dependency graph for ac_driver.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Typedefs
typedef enum AC_COMP_ENUM | AC_COMP_t
## Enumerations
enum | AC_COMP_ENUM { ANALOG_COMPARATOR0 = 0, ANALOG_COMPARATOR1 = 1 }
## Functions
void | AC_ConfigHysteresis (AC_t *ac, AC_COMP_t comp, AC_HYSMODE_t hysteresisMode)
| Configure hysteresis settings for a comparator.
void | AC_ConfigInterrupt (AC_t *ac, AC_COMP_t comp, AC_INTMODE_t interruptMode, AC_INTLVL_t interruptLevel)
| Configure interrupt mode and level for a comparator.
void | AC_ConfigMUX (AC_t *ac, AC_COMP_t comp, AC_MUXPOS_t pos, AC_MUXNEG_t neg)
| Configure MUX input settings for a comparator.
void | AC_ConfigVoltageScaler (AC_t *ac, uint8_t scaleFactor)
| Configure the voltage scaler.
void | AC_Disable (AC_t *ac, AC_COMP_t comp)
| Disable a comparator.
void | AC_DisableComparator0_Output (AC_t *ac)
| This function disables routing of comparator 0 output to pin 0.
void | AC_DisableWindowMode (AC_t *ac)
| This function disables the window mode.
void | AC_Enable (AC_t *ac, AC_COMP_t comp, bool highSpeed)
| Enable a comparator.
void | AC_EnableComparator0_Output (AC_t *ac)
| This function enables routing of comparator 0 output to pin 0.
void | AC_EnableWindowMode (AC_t *ac, AC_WINTMODE_t interruptMode, AC_WINTLVL_t interruptLevel)
| Enable and configure window mode and associated interrupt settings.
uint8_t | AC_GetComparatorState (AC_t *ac, AC_COMP_t comp)
| Get current state of one comparator.
uint8_t | AC_GetWindowState (AC_t *ac)
| Get current state for window mode.
void | AC_WaitForComparator_Blocking (AC_t *ac, AC_COMP_t comp)
| Wait for event from a comparator.
void | AC_WaitForWindow_Blocking (AC_t *ac)
| Wait for window mode event.

## Typedef Documentation

typedef enum AC_COMP_ENUM AC_COMP_t

## Enumeration Type Documentation

enum AC_COMP_ENUM

Enumerator:

  ANALOG_COMPARATOR0 |
  ANALOG_COMPARATOR1 |

Definition at line 65 of file ac_driver.h.

    00065                          {
    00066         ANALOG_COMPARATOR0 = 0,
    00067         ANALOG_COMPARATOR1 = 1,
    00068 }AC_COMP_t;

## Function Documentation

void AC_ConfigHysteresis | ( | AC_t * | ac,
|  | AC_COMP_t | comp,
|  | AC_HYSMODE_t | hysteresisMode |
| ) |  |  |

Configure hysteresis settings for a comparator.

This function configures hysteresis settings for one of the two comparator submodules.

Parameters:

  | ac | Pointer to Analog Comparator module.
  | comp | Which submodule, 0 or 1.
  | hysteresisMode | Hysteresis setting (none, small, large).

Definition at line 137 of file ac_driver.c.

Referenced by main().

    00138 {
    00139         /* Access correct registers based on which comparator submodule indicated. */
    00140         if(comp == 0){
    00141                 ac->AC0CTRL = (ac->AC0CTRL & ~AC_HYSMODE_gm) | hysteresisMode;
    00142         }else{
    00143                 ac->AC1CTRL = (ac->AC1CTRL & ~AC_HYSMODE_gm) | hysteresisMode;
    00144         }
    00145 }

void AC_ConfigInterrupt | ( | AC_t * | ac,
|  | AC_COMP_t | comp,
|  | AC_INTMODE_t | interruptMode,
|  | AC_INTLVL_t | interruptLevel |
| ) |  |  |

Configure interrupt mode and level for a comparator.

This function configures interrupt settings for one of the two comparator submodules.

Parameters:

  | ac | Pointer to Analog Comparator module.
  | comp | Which submodule, 0 or 1.
  | interruptMode | Interrupt mode setting (falling, rising, toggle).
  | interruptLevel | Interrupt level setting.

Definition at line 115 of file ac_driver.c.

    00117 {
    00118         /* Access correct registers based on which comparator submodule indicated. */
    00119         if(comp == 0){
    00120                 ac->AC0CTRL = (ac->AC0CTRL & ~(AC_INTMODE_gm | AC_INTLVL_gm))|
    00121                               ((uint8_t) interruptMode | interruptLevel);
    00122         }else{
    00123                 ac->AC1CTRL = (ac->AC1CTRL & ~(AC_INTMODE_gm | AC_INTLVL_gm)) |
    00124                                   ((uint8_t) interruptMode | interruptLevel);
    00125         }
    00126 }

void AC_ConfigMUX | ( | AC_t * | ac,
|  | AC_COMP_t | comp,
|  | AC_MUXPOS_t | pos,
|  | AC_MUXNEG_t | neg |
| ) |  |  |

Configure MUX input settings for a comparator.

This function configures MUX settings for one of the two comparator submodules.

Parameters:

  | ac | Pointer to Analog Comparator module.
  | comp | Which submodule, 0 or 1.
  | pos | Positive MUX input.
  | neg | Negative MUX input.

Definition at line 156 of file ac_driver.c.

Referenced by main().

    00157 {
    00158         /* Access correct registers based on which comparator submodule indicated. */
    00159         if(comp == 0){
    00160                 ac->AC0MUXCTRL = (ac->AC0MUXCTRL & ~(AC_MUXPOS_gm | AC_MUXNEG_gm)) |
    00161                                  ((uint8_t) pos | neg);
    00162         }else{
    00163                 ac->AC1MUXCTRL = (ac->AC1MUXCTRL & ~(AC_MUXPOS_gm | AC_MUXNEG_gm)) |
    00164                                      ((uint8_t) pos | neg);
    00165         }
    00166 }

void AC_ConfigVoltageScaler | ( | AC_t * | ac,
|  | uint8_t | scaleFactor |
| ) |  |  |

Configure the voltage scaler.

This function configures the internal voltage scaler. The voltage scaler input is VCC and it can scale the voltage in 1/64 steps. The output from the voltage scaler can be used as input.

Parameters:

  | ac | Pointer to Analog Comparator module.
  | scaleFactor | Scale factor in number of 1/64ths.

Definition at line 178 of file ac_driver.c.

    00179 {
    00180         /* Scale factor gets truncated to bit field size. */
    00181         ac->CTRLB = (ac->CTRLB & ~AC_SCALEFAC_gm) |
    00182                     ((scaleFactor << AC_SCALEFAC_bp) & AC_SCALEFAC_gm);
    00183 }

void AC_Disable | ( | AC_t * | ac,
|  | AC_COMP_t | comp |
| ) |  |  |

Disable a comparator.

This function disables one of the two comparator submodules.

Parameters:

  | ac | Pointer to Analog Comparator module.
  | comp | Which submodule, 0 or 1.

Definition at line 95 of file ac_driver.c.

Referenced by main().

    00096 {
    00097         /* Access correct registers based on which comparator submodule indicated. */
    00098         if(comp == 0){
    00099                 ac->AC0CTRL &= ~AC_ENABLE_bm;
    00100         }else{
    00101                 ac->AC1CTRL &= ~AC_ENABLE_bm;
    00102         }
    00103 }

void AC_DisableComparator0_Output | ( | AC_t * | ac | ) |

This function disables routing of comparator 0 output to pin 0.

Parameters:

  | ac | Pointer to Analog Comparator module.

Definition at line 230 of file ac_driver.c.

    00231 {
    00232         ac->CTRLA &= ~AC_AC0OUT_bm;
    00233 }

void AC_DisableWindowMode | ( | AC_t * | ac | ) |

This function disables the window mode.

Parameters:

  | ac | Pointer to Analog Comparator module.

Definition at line 210 of file ac_driver.c.

    00211 {
    00212         ac->WINCTRL = ~AC_WEN_bm;
    00213 }

void AC_Enable | ( | AC_t * | ac,
|  | AC_COMP_t | comp,
|  | bool | highSpeed |
| ) |  |  |

Enable a comparator.

This function enables one of the two comparator submodules.

Parameters:

  | ac | Pointer to Analog Comparator module.
  | comp | Which submodule, 0 or 1.
  | highSpeed | Set to false for low-power, true for high-speed.

Definition at line 76 of file ac_driver.c.

Referenced by main().

    00077 {
    00078         /* Access correct registers based on which comparator submodule indicated. */
    00079         if(comp == 0){
    00080                 ac->AC0CTRL = (ac->AC0CTRL & ~AC_HSMODE_bm)|
    00081                               (AC_ENABLE_bm | (highSpeed ? AC_HSMODE_bm : 0));
    00082         }else{
    00083                 ac->AC1CTRL = (ac->AC1CTRL & ~AC_HSMODE_bm)|
    00084                                   (AC_ENABLE_bm | (highSpeed ? AC_HSMODE_bm : 0));
    00085         }
    00086 }

void AC_EnableComparator0_Output | ( | AC_t * | ac | ) |

This function enables routing of comparator 0 output to pin 0.

Parameters:

  | ac | Pointer to Analog Comparator module.

Definition at line 220 of file ac_driver.c.

    00221 {
    00222         ac->CTRLA |= AC_AC0OUT_bm;
    00223 }

void AC_EnableWindowMode | ( | AC_t * | ac,
|  | AC_WINTMODE_t | interruptMode,
|  | AC_WINTLVL_t | interruptLevel |
| ) |  |  |

Enable and configure window mode and associated interrupt settings.

This function enables and configures the window mode and the window interrupt level.

Parameters:

  | ac | Pointer to Analog Comparator module.
  | interruptMode | Interrupt mode setting (above, below, inside, outside).
  | interruptLevel | Interrupt level setting.

Note:
  Both submodules in the Analog Comparator must be enabled to make the window mode work. This is done by using the AC_enable function.

Definition at line 198 of file ac_driver.c.

Referenced by ISR(), and main().

    00200 {
    00201         ac->WINCTRL = (ac->WINCTRL & ~(AC_WINTMODE_gm | AC_WINTLVL_gm)) |
    00202                       (AC_WEN_bm | interruptMode | interruptLevel);
    00203 }

uint8_t AC_GetComparatorState | ( | AC_t * | ac,
|  | AC_COMP_t | comp |
| ) |  |  |

Get current state of one comparator.

This function returns the current state of one of the two comparator submodule.

Parameters:

  | ac | Pointer to Analog Comparator module.
  | comp | Which submodule, 0 or 1.

Returns:
  0 if positive input is below negative, non-zero otherwise.

Definition at line 245 of file ac_driver.c.

    00246 {
    00247         uint8_t state = 0;
    00248
    00249         /* Access correct bits based on which comparator submodule indicated. */
    00250         if(comp == 0){
    00251                 state = ac->STATUS & AC_AC0STATE_bm;
    00252         }else if(comp == 1){
    00253                 state = ac->STATUS & AC_AC1STATE_bm;
    00254         }else{
    00255
    00256         }
    00257
    00258         return state;
    00259 }

uint8_t AC_GetWindowState | ( | AC_t * | ac | ) |

Get current state for window mode.

This function returns the current window state. The return value from this function can be compared with the enumerator values from AC_WSTATE_t.

Parameters:

  | ac | Pointer to Analog Comparator module.

Returns:
  Window state.

Definition at line 272 of file ac_driver.c.

Referenced by main().

    00273 {
    00274         return (ac->STATUS & AC_WSTATE_gm);
    00275 }

void AC_WaitForComparator_Blocking | ( | AC_t * | ac,
|  | AC_COMP_t | comp |
| ) |  |  |

Wait for event from a comparator.

This function waits for a comparator event, clears the flag and returns. Which event to wait for is decided by the interrupt mode setting.

Parameters:

  | ac | Pointer to Analog Comparator module.
  | comp | Which submodule, 0 or 1.

Definition at line 286 of file ac_driver.c.

Referenced by main().

    00287 {
    00288         /* Access correct bits based on which comparator submodule indicated.*/
    00289         if(comp == 0){
    00290                 /* Wait for interrupt flag to be set and clear it. */
    00291                 do {} while ((ac->STATUS & AC_AC0IF_bm) == 0);
    00292                 ac->STATUS = AC_AC0IF_bm;
    00293         }else{
    00294                 /* Wait for interrupt flag to be set and clear it. */
    00295                 do {} while ((ac->STATUS & AC_AC1IF_bm) == 0);
    00296                 ac->STATUS = AC_AC1IF_bm;
    00297         }
    00298 }

void AC_WaitForWindow_Blocking | ( | AC_t * | ac | ) |

Wait for window mode event.

This function waits for a window mode event, clears the flag and returns. Which event to wait for is decided by the window interrupt mode setting.

Parameters:

  | ac | Pointer to Analog Comparator module.

Definition at line 308 of file ac_driver.c.

    00309 {
    00310         /* Wait for interrupt flag to be set and clear it. */
    00311         do {} while ((ac->STATUS & AC_WIF_bm) == 0);
    00312         ac->STATUS = AC_WIF_bm;
    00313 }

@DOC_TITLE@