COSA
An Object-Oriented Platform for Arduino Programming
AnalogPin_static.cpp
Go to the documentation of this file.
1 
21 #include "Cosa/AnalogPin.hh"
22 
24 
25 void
26 AnalogPin::prescale(uint8_t factor)
27 {
28  const uint8_t MASK = (_BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0));
29  bit_field_set(ADCSRA, MASK, factor);
30 }
31 
32 uint16_t
33 AnalogPin::bandgap(uint16_t vref)
34 {
35  loop_until_bit_is_clear(ADCSRA, ADSC);
36  if (UNLIKELY(sampling_pin != NULL)) return (UINT16_MAX);
38 #if defined(MUX5)
39  bit_clear(ADCSRB, MUX5);
40 #endif
41  DELAY(500);
42  bit_set(ADCSRA, ADSC);
43  loop_until_bit_is_clear(ADCSRA, ADSC);
44  uint16_t sample;
45  synchronized sample = ADCW;
46  return ((vref * 1024L) / sample);
47 }
48 
49 uint16_t
51 {
52  loop_until_bit_is_clear(ADCSRA, ADSC);
53  if (UNLIKELY(sampling_pin != NULL)) return (UINT16_MAX);
54  ADMUX = (ref | (pin & 0x1f));
55 #if defined(MUX5)
56  bit_write(pin & 0x20, ADCSRB, MUX5);
57 #endif
58  bit_set(ADCSRA, ADSC);
59  loop_until_bit_is_clear(ADCSRA, ADSC);
60  return (ADCW);
61 }
Board::AnalogPin pin() const
Definition: AnalogPin.hh:69
static void prescale(uint8_t factor)
#define bit_set(p, b)
Definition: Bits.h:35
#define NULL
Definition: Types.h:101
#define bit_clear(p, b)
Definition: Bits.h:36
#define DELAY(us)
Definition: Types.h:280
#define UINT16_MAX
Definition: Types.h:71
#define bit_write(c, p, b)
Definition: Bits.h:38
static uint16_t bandgap(uint16_t vref=1100)
#define bit_field_set(p, m, v)
Definition: Bits.h:40
static AnalogPin * sampling_pin
Current sampling pin if any.
Definition: AnalogPin.hh:201
uint16_t sample()
Definition: AnalogPin.hh:135
#define UNLIKELY(x)
Definition: Types.h:153
#define ADCW
Definition: Leonardo.hh:304