COSA
An Object-Oriented Platform for Arduino Programming
AnalogComparator.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_ANALOG_COMPARATOR_HH
22 #define COSA_ANALOG_COMPARATOR_HH
23 
24 #include "Cosa/Event.hh"
25 #include "Cosa/Interrupt.hh"
26 
33 public:
34  enum Mode {
36  ON_FALLING_MODE = _BV(ACIS1),
37  ON_RISING_MODE = (_BV(ACIS1) | _BV(ACIS0)),
38  } __attribute__((packed));
39 
45  AnalogComparator(Mode mode = ON_TOGGLE_MODE, bool bandgap = false) :
46  m_mode(mode),
47  m_pin(bandgap ? VBG : AIN1)
48  {}
49 
57  m_mode(mode),
58  m_pin((uint8_t) (pin - Board::A0))
59  {}
60 
66  virtual void enable()
67  {
68  synchronized {
69  s_comparator = this;
70  ADCSRB = (m_pin >= AIN1) ? 0 : (_BV(ACME) | m_pin);
71  if (m_pin == VBG)
72  ACSR = _BV(ACBG) | _BV(ACIE) | m_mode;
73  else
74  ACSR = _BV(ACIE) | m_mode;
75  }
76  }
77 
83  virtual void disable()
84  {
85  synchronized {
86  ACSR = _BV(ACD);
88  }
89  }
90 
96  virtual void on_interrupt(uint16_t arg = 0);
97 
98 protected:
100  static const uint8_t AIN1 = 254;
101  static const uint8_t VBG = 255;
103  uint8_t m_pin;
104 
106  friend void ANALOG_COMP_vect(void);
107 };
108 
109 #endif
static const uint8_t VBG
Bandgap voltage reference.
#define ACSR
Definition: ATtinyX61.hh:252
Mode m_mode
Compare mode.
#define NULL
Definition: Types.h:101
uint8_t m_pin
Analog channel.
virtual void disable()
static const uint8_t AIN1
Default reference voltage.
virtual void enable()
virtual void on_interrupt(uint16_t arg=0)
static AnalogComparator * s_comparator
Current comparator.
friend void ANALOG_COMP_vect(void)
AnalogComparator(Board::AnalogPin pin, Mode mode=ON_TOGGLE_MODE)
AnalogComparator(Mode mode=ON_TOGGLE_MODE, bool bandgap=false)