A library for interfacing with the analog comparator peripheral in the megaAVR-0 series MCUs. Developed by MCUdude. The megaAVR-0 has one comparator where four positive and three negative pins are available for use. An alternative for the negative pin is to use an internally generated reference voltage instead. More useful information about the analog comparator can be found in the Microchip Application Note TB3211 and in the megaAVR-0 family data sheet.
Class for interfacing with the built-in comparator. Use the predefined objects Comparator
or Comparator0
.
Variable for setting what input pin the positive input of the comparator should be connected to Accepted values:
comparator::in_p::in0; // Use positive input pin 0 (PD2) as input
comparator::in_p::in1; // Use positive input pin 1 (PD4) as input
comparator::in_p::in2; // Use positive input pin 2 (PD6) as input
comparator::in_p::in3; // Use positive input pin 3 (PD1) as input
Comparator.input_p = comparator::in_p::in0; // Connect positive input pin 0 to the positive pin of the comparator
Comparator.input_p
defaults to comparator::in_p::in0
if not specified in the user program.
Variable for setting what input pin the negative input of the comparator should be connected to Accepted values:
comparator::in_n::in0; // Use positive input pin 0 (PD3) as input
comparator::in_n::in1; // Use positive input pin 1 (PD5) as input
comparator::in_n::in2; // Use positive input pin 2 (PD7) as input
comparator::in_n::dacref; // Use DACREF as input
Comparator.input_n = comparator::in_n::in0; // Connect negative input pin 0 to the negative pin of the comparator
Comparator.input_n
defaults to comparator::in_n::in0
if not specified in the user program.
Variable for setting what reference voltage the DACREF should be derived from. This voltage is internally generated. Accepted values:
comparator::ref::disable; // Do not use any reference
comparator::ref::vref_0v55; // 0.55V internal voltage
comparator::ref::vref_1v1; // 1.1V internal voltage
comparator::ref::vref_1v5; // 1.5V internal voltage
comparator::ref::vref_2v5; // 2.5V internal voltage
comparator::ref::vref_4v3; // 4.3V internal voltage
comparator::ref::vref_avcc; // Use voltage on AVCC pin
Comparator.reference = comparator::ref::vref_2v5; // Use the internal 2.5V reference for the DACREF
Comparator.reference
defaults to comparator::ref::disable
if not specified in the user program.
Variable for setting the DACREF value. This voltage can be selected as the input for the negative side of the comparator. This is the formula for the DACREF output voltage:
Comparator.dacref = 127; // Divide the reference voltage by two
Comparator.dacref
defaults to 255
if not specified in the user program.
Variable for setting the comparator input hysteresis. Useful for eliminating "bouncing". Accepted values:
comparator::hyst::disable; // No hysteresis
comparator::hyst::small; // 10mV hysteresis (±5mV)
comparator::hyst::medium; // 25mV hysteresis (±12.5mV)
comparator::hyst::large; // 50mV hysteresis (±25mV)
Comparator.hysteresis = comparator::hyst::large; // Use 50V hysteresis
Comparator.hysteresis
defaults to comparator::hyst::disable
if not specified in the user program.
Variable for setting the comparator output, internally and/or externally. Accepted values:
comparator::out::disable; // No output pin, signal not inverted internally
comparator::out::disable_invert; // No output pin, signal inverted internally
comparator::out::enable; // Enable output pin (PA7), signal not inverted internally
comparator::out::invert; // Enable output pin (PA7), signal inverted internally
comparator::out::enable_invert; // Identical to out::invert
Comparator.output = comparator::out::enable; // Enable output pin (PA7)
Comparator.output
defaults to comparator::out::disable
if not specified in the user program.
Method for initializing the comparator.
Comparator.init(); // Initialize comparator
Method for starting the analog comparator.
Comparator.start(); // Start comparator
Method for stopping the analog comparator.
Comparator.stop(); // Stop comparator
Reads the state of the analog comparator output. Works also when the physical output pin is disabled.
bool comp_state = Comparator.read(); // Read comparator
Method for enabling analog comparator interrupt. The interrupt will trigger when the the comparator output changes.
Valid arguments for the third parameters are RISING
, FALLING
and CHANGE
.
Comparator.attachInterrupt(blinkLED, RISING); // Run the blinkLED function when the comparator output goes high
void blinkLED()
{
digitalWrite(myLedPin, CHANGE);
}
Method for disabling analog comparator interrupt.
Comparator.detachInterrupt(); // Disable interrupt