This is the quick start guide for the AC Driver, with step-by-step instructions on how to configure and use the driver for specific use cases.
The section described below can be compiled into e.g. the main application loop or any other function that might use the AC functionality.
In the basic use case, the Analog Comparator driver is set up to compare an input voltage to the voltage VCC/2. If the voltage is below VCC/2, LED0 is lit up. If it is above VCC/2, LED 1 is lit.
This basic example uses the XMEGA interrupt functionality, and therefore the Programmable Multilevel Interrupt Controller PMIC driver must be included. The General Purpose Input/Output driver must also be included to be able to control the LEDs.
You must also enable the AC pins, this is done by defining:
in your source code.
The memset() function used to zero out our configuration structure is located in string.h, so we must include it:
Add to main()
:
We define how much we wish to scale the voltage:
We create a structure to store the configuration data for the AC:
We zero out the aca_config struct:
First, we set the AC mode for the ACA comparator to single operation.
We set the reference voltage to VCC/2 (scaler 32/64):
We enable hysteresis, to avoid flickering due to disturbances:
We set the negative reference to be our scaled voltage, and the positive reference to be the positive reference:
We set the interrupt mode to call the interrupt function on both edges, the interrupt level to medium, and set the callback function:
We write the configuration to the AC A channel 0, and enable it:
Finally, we must enable interrupts:
The example itself is handled by the interrupt callback, which switches the LEDS according to the AC status:
We switch the LEDS off:
We turn on LEDS according to where the AC reads our PIN to be in relation to the reference.