c. Bridging ADC

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorail 20 - CleO Bridging >

c. Bridging ADC

This tutorial shows how to use bridging commands to access ADC interface on CleO50. IR Distance click module is connected to CleO50 click 2 header interface. Upon executing the tutorial,the device outputs the voltage corresponding to the detected distance. The voltage output is inversely proportional to the distance, this means that when the distance increases, the output voltage decreases.

 

 

Command Prototype

 

int16_t DeviceOpen(uint8_t Slot,  uint8_t Interface,  int16_t bytestowrite,  uint8_t* Buffer)

 

Parameters

Description

Slot

Currently only one Slot is supported. Slot value should be equal to 0.

Interface

Value of this parameter should be BRIDGE_TYPE_ADC for ADC interface access.

bytestowrite

Value of this parameter is the total number of bytes to be written to CleO through this function which in this case is sizeof adc_options structure.

Buffer

Value of this parameter should be a pointer to adc_options structure type (as shown below) in Bridge.h.

 

typedef struct PACK {

   union PACK {

       struct PACK {

           uint8_t Mode; /* single shot or continuous */

           uint8_t Channel;

       };

       uint8_t b[2];

   };

}adc_options;

 

Structure member description:

 

Mode: This parameter will take the values of enum defined in Bridge.h

 

typedef enum

{

   adc_mode_single,        /*!< One analogue reading will be taken and then the ADC stopped */

   adc_mode_continuous  /*!< The ADC will continuously aquire analogue readings */

} adc_mode_t;

 

Channel: This parameter should be CLICK1_ADC_CHANNEL for click 1 ADC and CLICK2_ADC_CHANNEL for click 2.

 

 

Return Value

Description

int16_t

On success, the function returns int16_t value which is the handle that can be used in subsequent Device*() calls. On failure the function returns a negative value of error code with blue screen.

 

 

int16_t DeviceRead(uint8_t DevHandle,  int16_t bytestowrite,  int16_t bytestoread,  uint8_t* Buffer,  uint8_t* rBuffer)

 

Parameters

Description

DevHandle

An ADC handle number that is obtained using DeviceOpen() call.

bytestowrite

Value of this parameter is the total number of bytes to be written to CleO. In this case this parameter value will be 0.

bytestoread

Value of this parameter is the total number of bytes intend to read from ADC. Each sample read is 2 bytes value as CleO ADC supports 10 bit resolution.

Buffer

Value of this parameter should be a pointer to type uint8_t.

This buffer holds the data to be written to CleO. "bytestowrite" parameter refers to number of bytes in this Buffer. When "bytestowrite" is 0, this parameter should be NULL..

rBuffer

Value of this parameter should be read buffer pointer to which read result will be updated when the function returns..

 

Return Value

Description

int16_t

On success the function returns the number of bytes read from ADC.

On failure the function returns a negative value of error code with blue screen.

 

 

Code

 

 

int16_t HandleADC = -1;

 

void setup()

{    

 CleO.begin();

 Serial.begin(115200);

 

 Bridge::adc_options adc_opts;

 adc_opts.Mode = Bridge::adc_mode_continuous;

 adc_opts.Channel = CLICK2_ADC_CHANNEL;

 

/* Open ADC device using bridging commands */

 HandleADC = CleO.DeviceOpen(0, BRIDGE_TYPE_ADC, sizeof(adc_opts), (uint8_t*)&adc_opts);

 if(HandleADC < 0)

 {

  /* Failed to open a handle */

   Serial.print("DeviceOpen failed");

 }  

}

 

void loop()

{

 uint16_t AdcReadVal;

 if(HandleADC >= 0)

 {

  /* Read from ADC channel using bridging command.

      CleO ADC supports 10-bit resolution output

    */        

   CleO.DeviceRead(HandleADC, 0,2 , NULL, (uint8_t *)&AdcReadVal);                                

   Serial.println(AdcReadVal);

 }

}

 

 

Description

 

IR Distance click module is connected to CleO50 click 2 header interface. DeviceOpen() is called on  CLICK2_ADC_CHANNEL in continuous mode. ADC value is read using the DeviceRead() in loop function.

 

 

Output

 

ADC value read is displayed in the serial monitor.