Analog-to-Digital Converter (ADC) samples a analog signal and provides a digital value corresponding to it, which can be processed by a software.
ZDev provides APIs to read samples from ADC channels. The API supports
value scaling. If an 8-bit value (range 0x0 - 0xFF) is written using
the adc_write8()
API and the underlying ADC Hardware is 12-bit, the
value is scaled up to 12 bits. Similary if a 16-bit value (range 0x0 - 0xFFFF)
is written using the adc_write16()
API and the underlying ADC is
12-bit, the value is scaled down to 12 bits.
The ADC functions are declared in adc.h
. The no. of available ADC
channels is indicated by the constant ADC_NCHANNELS
. The following
example glows an LED if the voltage on the ADC channel 0, is greater
than Vref / 2.
Comparator.
#include <board.h>
#include <adc.h>
#define OFF 0
#define ON 1
int main()
{
uint8_t val;
board_init();
adc_init(); /* Initialize the ADC. */
adc_enable(0);
gpio_init();
gpio_enable_pin(LED1_GPIO);
gpio_direction_output(LED1_GPIO, OFF);
while (1) {
/* Read an 8-bit sample from the ADC channel 0. */
val = adc_read8(0);
if (val > 128) {
gpio_set_pin(LED1_GPIO, ON);
}
}
}