Analogue
Constants
Pin Definitions
Constant | Type | Standard App Pin Name |
---|---|---|
uint8_t PIN_ANALOGUE::A1 = 0 | ADC1 Pin | AI1 |
uint8_t PIN_ANALOGUE::A2 = 1 | ADC2 Pin | AI3 |
uint8_t PIN_ANALOGUE::A3 = 2``uint8_t PIN_ANALOGUE::D0 = 2 | ADC3 Pin (DIO0) *1 | AI2 |
uint8_t PIN_ANALOGUE::A4 = 3``uint8_t PIN_ANALOGUE::D1 = 3 | ADC4 Pin (DIO1) *1 | AI4 |
uint8_t PIN_ANALOGUE::VCC = 4 | Vcc Power Supply Voltage |
*1 The ADC2/ADC3 pins shared by digital and analog have usage procedures and restrictions.
Before starting ADC, set the pins to be used to no pull-up. If this is not done, the ADC will always observe the pull-up voltage.
pinMode(PIN_DIGITAL::DIO0, PIN_MODE::INPUT);
pinMode(PIN_DIGITAL::DIO1, PIN_MODE::INPUT);
In a normal circuit configuration, current leakage occurs during sleep. This cannot be avoided by software alone.
To avoid current leakage during sleep, disconnect the GND of the analog circuit part with a FET switch or similar, leaving it floating during sleep. Also, before sleep, set the pins to input with pull-up enabled.
Methods
setup()
void setup(
bool bWaitInit = false,
uint8_t kick_ev = E_AHI_DEVICE_TICK_TIMER,
void (*fp_on_finish)() = nullptr)
Initializes the ADC. In setup(), it starts the internal semiconductor regulator, specifies the timer device for periodic execution, and specifies a callback function called when all specified ADC channels have finished.
Parameter | Description |
---|---|
bWaitInit | If true , waits for the internal semiconductor regulator initialization. |
kick_ev | Specifies the timer device for periodic execution. The following five devices can be specified. Except for the first time, ADC starts in the interrupt handler. E_AHI_DEVICE_TICK_TIMER (TickTimer)``E_AHI_DEVICE_TIMER0 .. 4 (Timer0 .. 4) |
fp_on_finish | Callback function called from the interrupt handler after all specified ports’ ADCs finish. Useful for separately storing ADC measurement values in FIFO queues, etc. |
begin()
void begin(uint8_t bmPorts, uint8_t capt_tick = 1)
The first parameter specifies the ports for ADC. The port specification is a bitmap with bits set corresponding to the port numbers defined in the pin definitions. For example, to get values of PIN_ANALOGUE::A2
and PIN_ANALOGUE::VCC
, specify (1 <<PIN_ANALOGUE::A1 | 1<<PIN_ANALOGUE::VCC )
. You can also write pack_bits(PIN_ANALOGUE::A1,PIN_ANALOGUE::VCC)
using pack_bits
.
After calling begin()
, the first ADC process starts promptly, and after its completion interrupt, the next pin process starts. When all processes finish, the callback function (if specified) is called. The next ADC process starts after waiting for the next timer interrupt.
The second parameter specifies the number of timer interrupts before starting ADC. For example, TickTimer
is called every 1ms, and specifying 16
means processing every 16ms.
void begin()
Starts ADC processing with default ADC pins (PIN_ANALOGUE::A1
,PIN_ANALOGUE::A2
). end()
resumes interrupted ADC processing.
end()
void end()
Stops ADC processing and stops the internal semiconductor regulator.
available()
inline bool available()
Returns true
after ADC values are obtained. After checking with this function, it returns false
until the next ADC completion.
read()
, read_raw()
inline int16_t read(uint8_t s)
inline int16_t read_raw(uint8_t s)
Reads ADC values. The parameter specifies the ADC pin to read. read()
returns the value converted to mV, and read_raw()
returns the ADC value (0..1023).
read()
. To convert the read_raw()
value to mV, a special conversion formula must be applied.loop()
processing.ADC Interrupt Handler
The ADC interrupt handler is set to periph_analogue::ADC_handler()
when setup()
is called.
Specifying a handler separately in the semiconductor peripheral library will cause malfunction.
Behavior During Sleep
If ADC is in periodic execution state by begin()
, ADC processing resumes after wake-up from sleep.
Since setup(true)
is automatically called on wake-up from sleep, when using timers other than E_AHI_DEVICE_TICK_TIMER
, explicitly reinitialize in wakeup()
.
For example, inserting the following code reinitializes with E_AHI_DEVICE_TIMER0
on wake-up.
void wakeup()
{
Analogue.setup(true, E_AHI_DEVICE_TIMER0, adc_handler);
Analogue.begin(pack_bits(PIN_ANALOGUE::A1), 1);
}