> For the complete documentation index, see [llms.txt](https://sdk.twelite.info/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sdk.twelite.info/en/hw-api-ref/perifuraru/adc/adc.c.md).

# adc.c

`adc.c` is included as `common/Source/adc.c` in much of the sample code.

The following code is relevant:

| Filename         | Remark                                          |
| ---------------- | ----------------------------------------------- |
| sensor\_driver.h | Abstraction of sensor processing (header files) |
| sensor\_driver.c | Sensor processing using state transitions       |
| adc.h            | ADC processing section (header file)            |
| adc.c            | ADC processing                                  |

{% hint style="warning" %}
There may be differences in the code for each sample.
{% endhint %}

### Usage

See the comments in the code below.

```c
#include "sensor_driver.h"
#include "adc.h"

tsObjData_ADC sObjADC; // ADC management structure (data part)
tsSnsObj sADC; // ADC Management Structure (Control Unit)
int16 a1,a2,ab; // For storing results

...
	// ADC initialisation
	vSnsObj_Init(&sADC);
	vADC_Init(&sObjADC, &sADC, TRUE);
	vADC_WaitInit(); // Waiting for hardware initialisation

...
	// Specify the port on which you want to make ADC measurements 
	//(supply voltage and ADC1,2 in the following)
	sObjADC.u8SourceMask = TEH_ADC_SRC_VOLT
    	| TEH_ADC_SRC_ADC_1 | TEH_ADC_SRC_ADC_2;
    
	// start ADC
	vSnsObj_Process(&sADC, E_ORDER_KICK); // Kick measurement

// Wait for the ADC to finish processing one channel 
//(=E_AHI_DEVICE_ANALOGUE interrupt).
// vSnsObj_Process() is called sequentially.
void cbToCoNet_vHwEvent(uint32 u32DeviceId, uint32 u32ItemBitmap) {
	switch (u32DeviceId) {
	case E_AHI_DEVICE_ANALOGUE:
		// ADC completion interrupt
		vSnsObj_Process(&sADC, E_ORDER_KICK);
		if (bSnsObj_isComplete(&sADC)) {
			// All channels have been processed.
			// Values are stored in:
			a1=sObjADC.ai16Result[TEH_ADC_IDX_ADC_1]; // ADC1[mV]
			a2=sObjADC.ai16Result[TEH_ADC_IDX_ADC_2]; // ADC2[mV]
			ab=sObjADC.ai16Result[TEH_ADC_IDX_VOLT]; // VCC[mV]
			
			// Restore the initial state before the ADC started
			vSnsObj_Process(&sADC, E_ORDER_KICK);
			
			// E_ORDER_KICK again for successive runs
			vSnsObj_Process(&sADC, E_ORDER_KICK);
		}
		break;

	default:
		break;
	}
}
```

### functions

#### void vSnsObj\_Init(tsSnsObj \_\*\_pSnsObj)

Initializes the sensor management structure. Called just before `vADC_Init()`.

#### void vADC\_Init(tsObjData\_ADC \*pData,\_ \_tsSnsObj \_\*\_pSnsObj, bool\_t bInitAPR)

This function initializes ADC. The `tsObjData` structure (for storing results) and the `tsSnsObj` structure (for managing ADC) are prepared in advance.

If `bInitAPR` is `TRUE`, the ADC hardware is initialized. Hardware initialization takes some time, so be sure to run `vADC_WaitInit()` and wait for the initialization.

#### void vSnsObj\_Process(tsSnsObj \*pObj, teEvent eEv)

This process is called every time the conversion of one ADC port is completed. To wait for the completion of ADC conversion, `E_AHI_DEVICE_ANALOGUE` interrupt is used. This can be done simply after a certain period of time (approximately 100 usec) has elapsed.

In this process, the acquisition of ADC value and the operation to mV value are performed, and the value is stored in the `tsSnsObj` structure.

This process is an event processing for the state transition managed by the `tsSnsObj` structure. Immediately after this processing, `bSnsObj_isComplete()` is called to check whether the processing is completed. To restore the initial state again, this process is executed again with `E_ORDER_KICK` as an argument (that is, to execute ADC again, `E_ORDER_KICK` is executed twice after completion).

#### tsObjData\_ADC structure

| Members          | Remark                                                                                                                                                                                                                       |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| u8SourceMask     | <p>A bitmap that specifies the port to be ADCed. The specified port becomes the ADC target.</p><ul><li><code>TEH\_ADC\_SRC\_VOLT</code> : supply voltage</li><li><code>TEH\_ADC\_SRC\_ADC\_1-4</code> : ADC1,2,3,4</li></ul> |
| u8InputRangeMask | Specify the range (0-Vref or 0-2Vref) of the ADC target port. The specified port becomes 0-Vref. If not specified, it becomes 0-2Vref.                                                                                       |
| ai16Result\[]    | <p>Structure for storing ADC values. The result is stored as mV value.</p><ul><li><code>TEH\_ADC\_IDX\_VOLT</code>: supply voltage.</li><li><code>TEH\_ADC\_IDX\_ADC\_1-4</code>: ADC1,2,3,4</li></ul>                       |

####
