RT-Thread RTOS
An open source embedded real-time operating system
|
An ADC (analog-to-digital converter) is a hardware device that converts continuously changing analog signals to discrete digital signals. Usually, these analog signals include temperature, pressure, sound, video and many other types of signals. Converting them is important, as digital signals are easier to store, process, and transmit. This conversion can be achieved by using an ADC device which is commonly integrated in various platforms. Historically, ADCs were first used to convert received wireless signals to digital signals, for example, television signals, or signals from long-short broadcast stations.
As shown in the figure below, the analog-to-digital conversion generally involves steps of sampling, holding, quantifying, and encoding. In actual circuits, some processes are combined, such as sampling and holding, while quantization and encoding are implemented simultaneously in the conversion process.
Sampling is the conversion of analog signals that changes continuously over time into time-discrete analog signals. It takes a certain amount of time for the analog signals obtained by sampling to be converted into digital signals. In order to provide a stable value for the subsequent quantization coding process, it is required to keep the sampling analog signals for a period of time after the sampling circuit.
The process of converting a numerically continuous analog quantity into a digital quantity is called quantization. Digital signals are discrete numerically. The output voltage of the sample-and-hold circuit also needs to be naturalized to a corresponding discrete level in a similar way, and any digital quantity can only be an integer multiple of a certain minimum quantity unit. The quantized value also requires the encoding process, which is the digital output of the A/D converter.
Resolution is represented as binary (or decimal) numbers. Generally, it comes in 8 bits, 10 bits, 12 bits, 16 bits, etc. A larger resolution, in bits, means more accuracy in the conversion of analog to digital signals.
Precision is the maximum error value between analog signals and real ADC device numerical points’ values.An ADC with a high resolution might have a low precision, meaning that factors like noise can affect the numerical ADC reading more than small changes in the input signal.
The conversion rate is the reciprocal of time taken for an ADC device to complete conversion from an analog to a digital signal. For example, an ADC device with a conversion rate of 1MHz means that the ADC conversion time is 1 microsecond.
The application accesses the ADC hardware through the ADC device management interface provided by RT-Thread. The relevant interfaces are as follows:
Function | Description |
---|---|
rt_device_find() | Find device handles based on ADC device name |
rt_adc_enable() | Enable ADC devices |
rt_adc_read() | Read ADC device data |
rt_adc_disable() | Close the ADC device |
The application gets the device handler based on the ADC device name to operate the ADC device. Following is the interface function to find the devices:
Parameter | Description |
---|---|
name | The name of the ADC device |
Return | —— |
Device handle | Finding the corresponding device will return to the corresponding device handle |
RT_NULL | No device found |
|
In a nutshell, the names of the ADC devices are registered as adc0, adc1, and so on. What follows is an example on how to use it
It is required to enable the ADC device with the following interface function before reading and operating the ADC device.
Parameter | Description |
---|---|
dev | ADC device handle |
channel | ADC channel |
Return | —— |
RT_EOK | Succeed |
-RT_ENOSYS | Failed, the device operation method is empty |
Other error code | Failed |
An usage example is as follows:
Reading the ADC channel sample values can be done by the following function:
Parameter | Description |
---|---|
dev | ADC device handle |
channel | ADC channel |
Return | —— |
Read values |
An example of using the ADC sampled voltage value is as follows:
The calculation formula of the actual voltage value is: sampling value * reference voltage/(1 << resolution digit)
. In the above example, variable vol was enlarged 100 times, so finally the integer part of voltage is obtained through vol / 100, and the decimal part of voltage is obtained through vol % 100.
An ADC channel can be disabled through the following function:
Parameter | Description |
---|---|
dev | ADC device handle |
channel | ADC channel |
Return | —— |
RT_EOK | Succeed |
-RT_ENOSYS | Failed, the device operation method is empty |
Other error code | Failed |
An example:
To find out the registered device, you can use the command adc probe followed by the registered ADC device name as following,
A channel of the enabled device can use the command adc enable
followed by the channel number.
To read data from a channel of an ADC device, you can use the command adc read
followed by the channel number.
To close a channel of an ADC device, you can use the command adc disable
followed by the channel number.
The specific usage of the ADC device can refer to the following sample code. The main steps of the sample code are as follows:
Running result: Print the raw and converted data which actually read , and print the calculated actual voltage value.