ADC

Last Updated on : 2025-04-27 06:28:26download

Overview

An analog-to-digital converter (ADC) is an electronic component that converts a continuous-time and continuous-amplitude analog signal to a discrete-time and discrete-amplitude digital signal.

A/D conversion generally goes through four processes: sampling, holding, quantization, and encoding. In actual circuits, some of these processes are combined. For example, sampling and holding, as well as quantization and encoding, are often implemented simultaneously during the conversion process.

Type

The following types are available:

  • Successive-approximation ADC
  • Flash ADC, also known as a parallel comparator ADC
  • Integrating ADC

Technical indicators

  • Resolution: Theoretically, the ADC’s capability to resolve input signals is generally 8 bits, 10 bits, 12 bits, or 16 bits.

  • Accuracy: Indicates the difference between the actual analog voltage and the sampled voltage. The maximum difference value is the absolute accuracy, while the percentage of the maximum difference relative to the full-scale analog voltage is called the relative error.

  • Conversion time: Indicates the time required for each conversion, characterizing the ADC’s conversion speed. It depends on the ADC’s clock frequency, sampling cycle, and conversion cycle.

  • Data output: Data is output through parallel ports and serial ports.

  • Operating voltage: Pay attention to the operating voltage range of the ADC and whether it can directly measure negative voltage.

API description

tkl_adc_init

OPERATE_RET tkl_adc_init(TUYA_ADC_NUM_E port_num, TUYA_ADC_BASE_CFG_T *cfg);
  • Features:

    • Initialize the ADC.
  • Parameters:

    • port_num: The ADC port number. Each port corresponds to one physical ADC device (typically with multiple channels). Valid values are as follows:

      Name Definition
      TUYA_ADC_NUM_0 ADC port 0
      TUYA_ADC_NUM_1 ADC port 1
      TUYA_ADC_NUM_2 ADC port 2
      TUYA_ADC_NUM_3 ADC port 3
      TUYA_ADC_NUM_4 ADC port 4
      TUYA_ADC_NUM_5 ADC port 5
    • cfg: Basic ADC configuration. Valid values are as follows:

      typedef struct {
          TUYA_AD_DA_CH_LIST_U  ch_list;  // The list of ADC channels
          UINT8_T ch_nums;        // The number of ADC channels to sample
          UINT8_T  width;         // Resolution (bit width)
          UINT32_T freq;          // Sampling frequency
          TUYA_ADC_TYPE_E type;   // ADC sampling type
          TUYA_ADC_MODE_E mode;   // ADC sampling mode
          UINT16_T   conv_cnt;    // ADC sampling count
          UINT32_T   ref_vol;     // ADC reference voltage (unit: mV)
      } TUYA_ADC_BASE_CFG_T;
      
      • TUYA_AD_DA_CH_LIST_U:

           typedef union {
               TUYA_AD_DA_CH_LIST_BIT_T bits;
               UINT32_T data;
           }TUYA_AD_DA_CH_LIST_U;
        
           typedef struct {
               UINT32_T ch_0             : 1;
               UINT32_T ch_1             : 1;
               UINT32_T ch_2             : 1;
               UINT32_T ch_3             : 1;
               UINT32_T ch_4             : 1;
               UINT32_T ch_5             : 1;
               UINT32_T ch_6             : 1;
               UINT32_T ch_7             : 1;
               UINT32_T ch_8             : 1;
               UINT32_T ch_9             : 1;
               UINT32_T ch_10            : 1;
               UINT32_T ch_11            : 1;
               UINT32_T ch_12            : 1;
               UINT32_T ch_13            : 1;
               UINT32_T ch_14            : 1;
               UINT32_T ch_15            : 1;
               UINT32_T rsv              :16;
           }TUYA_AD_DA_CH_LIST_BIT_T;
        
        • The channel list can be configured using either bitwise operations or direct data assignment, with identical functional results.

        • ch_nums: The number of channels to sample.

      • TUYA_ADC_TYPE_E:

        Name Definition
        TUYA_ADC_INNER_SAMPLE_VOL Internal voltage sampled by the ADC (such as power supply voltage)
        TUYA_ADC_EXTERNAL_SAMPLE_VOL External voltage sampled by the ADC (such as external pin voltage)
      • TUYA_ADC_MODE_E:

         typedef enum {
              TUYA_ADC_SINGLE = 0,       ///< Single conversion mode
              TUYA_ADC_CONTINUOUS,       ///< Continuous conversion mode
              TUYA_ADC_SCAN,             ///< Scan mode
         } TUYA_ADC_MODE_E;
        
        Name Definition Remarks
        TUYA_ADC_SINGLE Single channel sampling -
        TUYA_ADC_CONTINUOUS Continuous sampling in a single channel -
        TUYA_ADC_SCAN Sampling in scanning mode Sampling can be implemented in multiple channels

        conv_cnt: The number of sampling in the specified sampling mode.

  • Return value:

    • OPRT_OK: Success.
    • For more information, see the definitions of OS_ADAPTER_ADC in the file tuya_error_code.h.

tkl_adc_deinit

OPERATE_RET tkl_adc_deinit(TUYA_ADC_NUM_E port_num);
  • Features:
    • Deinitialize the ADC.
  • Parameters:
    • port_num: The port number.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OS_ADAPTER_ADC in the file tuya_error_code.h.

tkl_adc_width_get

UINT8_T tkl_adc_width_get(TUYA_ADC_NUM_E port_num);
  • Features:
    • Query the resolution (bit width).
  • Parameters:
    • port_num: The port number.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OS_ADAPTER_ADC in the file tuya_error_code.h.

tkl_adc_ref_voltage_get

UINT32_T tkl_adc_ref_voltage_get(TUYA_ADC_NUM_E port_num);
  • Features:
    • Query the reference voltage.
  • Parameters:
    • port_num: The port number.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OS_ADAPTER_ADC in the file tuya_error_code.h.

tkl_adc_temperature_get

INT32_T tkl_adc_temperature_get(VOID_T);
  • Features:
    • Query the temperature of the chip.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OS_ADAPTER_ADC in the file tuya_error_code.h.

tkl_adc_read_data

OPERATE_RET tkl_adc_read_data(TUYA_ADC_NUM_E port_num, INT32_T *buff, UINT16_T len);
  • Features:
    • Read data from the ADC.
  • Parameters:
    • port_num: The port number.
    • buff: The data buffer.
    • len: The length of the data buffer.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OS_ADAPTER_ADC in the file tuya_error_code.h.

tkl_adc_read_single_channel

OPERATE_RET tkl_adc_read_single_channel(TUYA_ADC_NUM_E port_num, UINT8_T ch_id, INT32_T *data);
  • Features:
    • Read data from the ADC (a single channel).
  • Parameters:
    • port_num: The port number.
    • ch_id: The channel ID.
    • data: The data buffer.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OS_ADAPTER_ADC in the file tuya_error_code.h.

tkl_adc_read_voltage

OPERATE_RET tkl_adc_read_voltage(TUYA_ADC_NUM_E port_num, INT32_T *buff, UINT16_T len);
  • Features:
    • Read voltage from the ADC.
  • Parameters:
    • port_num: The port number.
    • buff: The voltage buffer.
    • len: The length of the voltage buffer.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OS_ADAPTER_ADC in the file tuya_error_code.h.

Example

Example 1

VOID_T tuya_adc_single_channel_test(VOID_T)
{
    OPERATE_RET ret;
    TUYA_ADC_BASE_CFG_T adc_cfg;
    UINT32_T adc_value = 0;
    UINT16_T channel = 0;

    adc_cfg.ch_list.data = 1; // or adc_cfg.ch_list.bits.ch_0 = 1;
    adc_cfg.ch_nums = 1;
    adc_cfg.type = TUYA_ADC_INNER_SAMPLE_VOL;
    adc_cfg.mode = TUYA_ADC_SINGLE;
    adc_cfg.width = 10;
    adc_cfg.conv_cnt = 1;

    ret = tkl_adc_init(TUYA_ADC_NUM_0, &adc_cfg);
    if(ret != OPRT_OK) {
        // Failed
        return;
    }

    ret = tkl_adc_read_single_channel(TUYA_ADC_NUM_0, channel, &adc_value);
    if(ret != OPRT_OK) {
        // Failed
        return;
    }

    // Output the value of adc_value

    ret = tkl_adc_deinit(TUYA_ADC_NUM_0);
    if(ret != OPRT_OK) {
        // Failed
        return;
    }
}

Example 2

#define ADC_CHANNEL_NUM 3

VOID_T tuya_adc_multi_channel_test(VOID_T)
{
    OPERATE_RET ret;
    TUYA_ADC_BASE_CFG_T adc_cfg;
    UINT32_T adc_value[ADC_CHANNEL_NUM] = {0};

    adc_cfg.ch_list.bits.ch_0 = 1;
    adc_cfg.ch_list.bits.ch_1 = 1;
    adc_cfg.ch_list.bits.ch_2 = 1;
    adc_cfg.ch_nums = ADC_CHANNEL_NUM;
    adc_cfg.type = TUYA_ADC_INNER_SAMPLE_VOL;
    adc_cfg.mode = TUYA_ADC_SCAN;
    adc_cfg.width = 10;
    adc_cfg.conv_cnt = 1;

    ret = tkl_adc_init(TUYA_ADC_NUM_0, &adc_cfg);
    if(ret != OPRT_OK) {
        // Failed
        return;
    }

    ret = tkl_adc_read_data(TUYA_ADC_NUM_0, adc_value, ADC_CHANNEL_NUM);
    if(ret != OPRT_OK) {
        // Failed
        return;
    }

    // Output the value of adc_value[ADC_CHANNEL_NUM]

    ret = tkl_adc_deinit(TUYA_ADC_NUM_0);
    if(ret != OPRT_OK) {
        // Failed
        return;
    }
}