# ADC Principles

<figure><img src="/files/sTgBFZ7swhLrcfOQzcOA" alt=""><figcaption></figcaption></figure>

มีหลายเทคนิคที่ถูกนำมาใช้ในการแปลงสัญญาณอนาล็อก เพื่อเป็นข้อมูลค่าดิจิตอล ที่นิยมกันตัวอย่างเช่น successive approximation conversion, parallel-encoded conversion (หรือ flash conversion), delta-sigma processing และ pulse-code modulation (PCM) เป็นต้น&#x20;

ซึ่งหนึ่งในเทคนิคที่ถูกใช้มากที่สุดคือ วิธีการประมาณค่าต่อเนื่อง (successive approximation conversion) โดยเทคนิคนี้จะทำการแปลงแต่ละบิตของผลลัพธ์แบบไบนารีจะถูกค้นหา หนึ่งบิตต่อครั้ง—เริ่มจาก [MSB bit](https://www.youtube.com/watch?v=_VmD99A69TY) ซึ่งวิธีนี้จะทำการแปลงที่ค่อนข้างเร็ว (ในช่วงเวลาประมาณ 10 ถึง 300 μs) ด้วยจำนวนวงจรที่จำกัด ดังตัวอย่างรูปข้างล่าง เป็นการแปลงค่าประมาณต่อเนื่องขนาด 8-bit แบบง่าย&#x20;

<figure><img src="/files/sCDMrop1xEIuPSLCH7Fq" alt=""><figcaption><p>Successive Approximation Conversion Technique</p></figcaption></figure>

ใน  PSoCᵀᴹ ไมโครคอลโทรเลอร์ จะมีตัว ADC เป็นส่วนประกอบฮาร์ดแวร์ที่สามารถอ่านแรงดันจากขาอนาล็อก (โหมดแบบเดี่ยว - <mark style="color:blue;">single ended mode</mark>) หรือคู่ขาอนาล็อก (โหมดเชิงเส้น - <mark style="color:blue;">differential mode</mark>) แล้วแปลงเป็นค่าดิจิตอลที่ PSoCᵀᴹ ไมโครคอลโทรเลอร์ สามารถตีความได้ โดยในโหมดแบบเดี่ยว ADC จะอ่านแรงดันที่ขาอนาล็อกที่ระบุเมื่อเทียบกับดินหรือค่าอ้างอิงอื่นๆ <mark style="color:red;">ในขณะที่</mark> ในโหมดเชิงเส้น ADC จะอ่านแรงดันระหว่างขาอนาล็อกสองขาที่ระบุ จำนวนบิตในผลลัพธ์ที่ ADC สร้างขึ้นจะกำหนดความละเอียดของ ADC ตัวอย่างเช่น ผลลัพธ์ที่ 12-bit ADC สามารถสร้างขึ้นจะอยู่ในช่วง

* `0 → 2¹²` = `0 → 4096` สำหรับการวัดในโหมดแบบเดี่ยว&#x20;
* `-2¹¹ → 2¹¹` = `-2048 → +2048` สำหรับการวัดในโหมดแบบเชิงเส้น

ในช่วงแรงดันอินพุตของ PSoCᵀᴹ ADC สามารถปรับค่าได้และแตกต่างกันตามอุปกรณ์ โดยทั่วไปแรงดันที่สามารถวัดได้คือ `0 → 2*Vref` สำหรับการวัดแบบเดี่ยวและ `Vx ± Vref` สำหรับการวัดแบบเชิงเส้น โดยที่ Vref เป็นแรงดันอ้างอิงที่สร้างขึ้นในชิป และ Vx เป็นอินพุตลบ สำหรับการวัดแบบเชิงเส้น&#x20;

ผลลัพธ์ที่ ADC สร้างขึ้นมีหน่วยว่า "counts" โดยแต่ละ "count" แทนส่วนหนึ่งของช่วงแรงดันอินพุตทั้งหมด&#x20;

> **ตัวอย่างเช่น**
>
> &#x20;ถ้าช่วงอินพุตคือ ±5V (ทั้งหมด 10V) และ ADC มีความละเอียด 12 บิต (12-bit หรือ 2¹²) ดังนั้นแต่ละ count จะเท่ากับ

$$
count = {10v  \over 2^{12}-1} \approx 0.00242 V/count
$$

หรือเท่ากับ

$$
\= 409.5 counts/V
$$

{% hint style="info" %} <mark style="color:red;">**คำถาม:**</mark> ถ้า input voltage ที่เข้าไปเท่ากับ 2V ตัว ADC จะนับจำนวน count ออกมาได้เท่าไหร่

<mark style="color:green;">**คำตอบ:**</mark> counts = 409.5 \* 2 = 819 counts
{% endhint %}

PSoCᵀᴹ devices มี ADC อยู่สองแบบคือ

1. Successive Approximation Register (SAR)
2. Sigma-Delta or Delta-Sigma&#x20;

ในการเขียนโปรแกรมสำหรับบอร์ด PSoC6 จะมีการเรียกใช้ฟังก์ชัน [cyhal\_adc\_init](https://infineon.github.io/mtb-hal-cat1/html/group__group__hal__adc.html#ga7618b26a0677cb3f8cd477e3731da510) เพื่อกำหนดการทำงานเริ่มต้นให้กับ PSoCᵀᴹ ADC ผ่านตัวแปร `obj` ของ ADC object, ตัวแปร `pin ของ` input pin และตัวแปร `clk` ของ clock&#x20;

นอกจากนั้นจะเรียกใช้ฟังก์ชัน [cyhal\_adc\_channel\_init\_diff](https://infineon.github.io/mtb-hal-cat1/html/group__group__hal__adc.html#gaba21a0c4d83266951823ea5d9eba39b9) เพื่อกำหนดจำนวนช่องสัญญาณที่จะรับเข้ามา (channel) และฟังก์ชัน [cyhal\_adc\_read](https://infineon.github.io/mtb-hal-cat1/html/group__group__hal__adc.html#ga7207c3d089c48255e382cfe159a04ed0) สำหรับการอ่านผลลัพธ์จากการแปลงสัญญาณอนาล็อกเป็นดิจิตอลต่อไป

{% hint style="info" %}
*ตัวอย่างโปรแกรมการอ่านค่าสัญญาณอนาล็อกแบบ 1 channel* &#x20;
{% endhint %}

```c
cy_rslt_t           rslt;
cyhal_adc_t         adc_obj;
cyhal_adc_channel_t adc_chan_0_obj;
// ADC conversion result.
int32_t adc_out;
 
// Initialize ADC. The ADC block which can connect to pin ADC_VPLUS1 is selected
rslt = cyhal_adc_init(&adc_obj, ADC_VPLUS1, NULL);
 
// Initialize ADC channel, allocate channel number 0 to pin ADC_VPLUS1 as this is the first
// channel initialized
const cyhal_adc_channel_config_t channel_config =
    { .enable_averaging = false, .min_acquisition_ns = 220, .enabled = true };
rslt = cyhal_adc_channel_init_diff(&adc_chan_0_obj, &adc_obj, ADC_VPLUS1, CYHAL_ADC_VNEG,
                                 &channel_config);
 
// Read the ADC conversion result for corresponding ADC channel. Repeat as necessary.
adc_out = cyhal_adc_read(&adc_chan_0_obj);
 
// Release ADC and channel objects when no longer needed
cyhal_adc_channel_free(&adc_chan_0_obj);
cyhal_adc_free(&adc_obj);
```

รายละเอียดเพิ่มเติมทางเทคนิค

* [PSoCᵀᴹ 6 SAR ADC Datasheet](https://www.infineon.com/dgdl/Infineon-PSoC_6_Scanning_SAR_ADC_\(Scan_ADC\)_2.0-Software+Module+Datasheets-v03_01-EN.pdf?fileId=8ac78c8c7d0d8da4017d0eb28a4b2b24\&utm_source=cypress\&utm_medium=referral\&utm_campaign=202110_globe_en_all_integration-files\&redirId=File_3_2120)
* [Cypress Delta-Sigma ADC Datasheet](https://www.infineon.com/dgdl/Infineon-Component_Delta_Sigma_ADC_\(ADC_DelSig\)_V3.30-Software+Module+Datasheets-v03_03-EN.pdf?fileId=8ac78c8c7d0d8da4017d0e7ce9430ebe\&utm_source=cypress\&utm_medium=referral\&utm_campaign=202110_globe_en_all_integration-files\&redirId=File_4_0_18)
* [SAR ADC Architecture](https://www.analog.com/media/en/training-seminars/tutorials/MT-021.pdf)
* [Sigma-Delta ADC Interactive Illustration](https://www.analog.com/en/design-center/interactive-design-tools/sigma-delta-adc-tutorial.html)
* [SAR and delta-sigma: Basic operation](https://www.youtube.com/watch?v=U_Yv69IGAfQ)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/biil_psoc6/basic-mcu-interfacing/psoc-tm-6s2-peripherals-interfacing-adc-pwm/psoc-tm-6s2-sar-adc/adc-principles.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
