FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
analog_comparator.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef ANALOGCOMPARATOR_HH
22#define ANALOGCOMPARATOR_HH
23
24#include "boards/board_traits.h"
25#include "interrupts.h"
26#include "utilities.h"
27
37#define REGISTER_ANALOG_COMPARE_ISR_FUNCTION(CALLBACK) \
38 ISR(ANALOG_COMP_vect) \
39 { \
40 CALLBACK(); \
41 }
42
53#define REGISTER_ANALOG_COMPARE_ISR_METHOD(HANDLER, CALLBACK) \
54 ISR(ANALOG_COMP_vect) \
55 { \
56 interrupt::CallbackHandler<void (HANDLER::*)(), CALLBACK>::call(); \
57 }
58
67#define REGISTER_ANALOG_COMPARE_ISR_EMPTY() EMPTY_INTERRUPT(ANALOG_COMP_vect)
68
75#define DECL_ANALOG_COMPARE_ISR_HANDLERS_FRIEND friend void ::ANALOG_COMP_vect(void);
76
77namespace analog
78{
83 enum class ComparatorInterrupt : uint8_t
84 {
86 NONE = bits::BV8(ACI),
88 TOGGLE = bits::BV8(ACI, ACIE),
90 FALLING_EDGE = bits::BV8(ACI, ACIE, ACIS1),
92 RISING_EDGE = bits::BV8(ACI, ACIE, ACIS1, ACIS0)
93 };
94
117 {
118 private:
119 static constexpr board_traits::REG8 ACSR_{ACSR};
120 using GLOBAL_TRAIT = board_traits::GlobalAnalogPin_trait;
121
122 public:
123 AnalogComparator() = default;
124 AnalogComparator(const AnalogComparator&) = delete;
125 AnalogComparator& operator=(const AnalogComparator&) = delete;
126
161 template<board::AnalogPin INPUT1 = board::AnalogPin::NONE, bool INPUT0_BANDGAP = false>
162 void begin(ComparatorInterrupt mode = ComparatorInterrupt::NONE, bool trigger_icp = false)
163 {
164 synchronized begin_<INPUT1, INPUT0_BANDGAP>(mode, trigger_icp);
165 }
166
179 void end()
180 {
181 synchronized end_();
182 }
183
192 bool output() const
193 {
194 return ACSR_ & bits::BV8(ACO);
195 }
196
231 template<board::AnalogPin INPUT1 = board::AnalogPin::NONE, bool INPUT0_BANDGAP = false>
232 void begin_(ComparatorInterrupt mode = ComparatorInterrupt::NONE, bool trigger_icp = false)
233 {
234 using ATRAIT = board_traits::AnalogPin_trait<INPUT1>;
235 static_assert(ATRAIT::IS_ANALOG_PIN || (INPUT1 == board::AnalogPin::NONE), "INPUT must not be TEMP!");
236 static_assert((INPUT1 != board::AnalogPin::NONE) || GLOBAL_TRAIT::HAS_AIN1, "Target has no AIN1 pin!");
237 static_assert(GLOBAL_TRAIT::HAS_AIN0 || INPUT0_BANDGAP, "Target has no AIN0 hence INPUT0_BANDGAP must be true");
238
239 GLOBAL_TRAIT::ADCSRB_ = ((INPUT1 == board::AnalogPin::NONE) ? 0 : bits::BV8(ACME));
240 GLOBAL_TRAIT::ADCSRA_ = ATRAIT::MUX_MASK2;
241 GLOBAL_TRAIT::ADMUX_ = ATRAIT::MUX_MASK1;
242 ACSR_ = (INPUT0_BANDGAP ? bits::BV8(ACBG) : 0U) |
243 uint8_t(mode) |
244 (trigger_icp ? GLOBAL_TRAIT::ICP_TRIGGER : 0U);
245 }
246
259 void end_()
260 {
261 GLOBAL_TRAIT::ADCSRB_ = 0;
262 GLOBAL_TRAIT::ADCSRA_ = 0;
263 GLOBAL_TRAIT::ADMUX_ = 0;
264 ACSR_ = 0;
265 }
266 };
267}
268
269#endif /* ANALOGCOMPARATOR_HH */
Handler of the Analog Comparator feature.
void begin(ComparatorInterrupt mode=ComparatorInterrupt::NONE, bool trigger_icp=false)
Start operations of the Analog Comparator.
void begin_(ComparatorInterrupt mode=ComparatorInterrupt::NONE, bool trigger_icp=false)
Start operations of the Analog Comparator.
bool output() const
Get current output of Analog Comparator.
void end()
Stop operations of the Analog Comparator.
void end_()
Stop operations of the Analog Comparator.
General API for handling AVR interrupt vectors.
Defines all API to manipulate analog input/output.
ComparatorInterrupt
Kind of change that will trigger an Analog Comparator Interrupt.
@ TOGGLE
An interrupt is generated everytime the Analog Comparator output changes.
@ FALLING_EDGE
An interrupt is generated everytime the Analog Comparator output changes from 1 to 0.
@ NONE
No interrupt will be generated by the Anolog Comparator.
@ RISING_EDGE
An interrupt is generated everytime the Analog Comparator output changes from 0 to 1.
static constexpr uint8_t BV8(uint8_t bit)
Create a uint8_t bitmask for the given bit number.
Definition: bits.h:41
General utilities API that have broad application in programs.