ADC (JSAPI_ADC)

ADC (Analog Digital Converter) converts an input analog signal to a digital value (0..255 for an 8-bit value, and 0..65535 for a 16-bit value). Usually PortA pins of an ST MCU can act as ADCs, and each of the 16 pins may be set up individually to perform analog-to-digital conversion. Prior to performing a conversion, the pin must be set up as an ADC input using the port<x>.MakeAnalog function call.

To demonstrate the ADC features, you need to attach additional hardware to the ST-Nucleo. The example assumes a light sensor is attached to PortA.1, such as the one in the MicroBox Shield.

Object

extern JSAPI_ADC adc1;

STM32F0xx Series

Perform a single conversion

unsigned Convert(

    int channel_no,     // ADC channel

    unsigned nsamples, // number of samples to take

    unsigned *high,     // [out] highest sampled value

    unsigned *low       // [out] lowest sampled value

);

Perform self-calibration

void Calibrate(void);

STM32F3xx Series

Perform a single conversion

unsigned Convert(

    unsigned nsamples,

    unsigned *high,

    unsigned *low

);

STM32F4xx/STM32F7xx Series

Perform a single conversion

unsigned Convert(

    int channel_no,

    unsigned nsamples,

    unsigned *high,

    unsigned *low

);

Start a DMA conversion

int DMAConvert(

    int channel_no,

    unsigned nsamples,

    unsigned short results[],

    bool *status

);

Stop the DMA conversion

int DMAStop(void);

Generic Common Routines

Set sample time

void SetSampleTime(int sample_time);

Set resolution

void SetResolution(unsigned res);

Disable ADC (minimize power usage)

void Disable(void);

 

How to Use

To use the JumpStart ADC API:

1.       With a JSAPI_GPIO object (usually porta), call its method function MakeAnalog to specify which pin is to be used for ADC conversion.

2.       To perform a conversion, call the function Convert, which takes nsamples samples and writes the high and low values of the samples to the high and low arguments. The return value is the average value of the samples.

3.       Currently DMA support is available only for the STM32F4xx and STM32F7xx series. To perform conversion with DMA enabled, set up the DMA Stream (see here) prior to calling DMAConvert.

Example

This example takes 5x readings of 20 ADC samples at PortA.1.

    porta.MakeAnalog(1);

 

    for (int i = 0; i < 5; i++)

        {

        unsigned high, low;

        unsigned val = adc1.Convert(1, 20, &high, &low);

  printf("ADC light sensor reading %d high %d low %d\n", val,

          high, low);

        printf("pausing for one second\n");

        DelaySeconds(1);

        }