[Zhongke Bluexun AB32VG1 RISC-V board "run into" RTT evaluation] Part 3: Use of ADC peripherals
[Copy link]
This post was last edited by Digital Leaf on 2021-4-18 20:50
For ADC, RT-Thread provides four ADC device management interface functions to access ADC hardware
It looks the same as the previous GPIO button, thanks to RT-Thread providing a simple I/O device model framework. " The I/O device management layer implements the encapsulation of the device driver. The application accesses the underlying device through the standard interface provided by the I/O device layer, and the upgrade and replacement of the device driver will not affect the upper-level application. This method allows the device's hardware operation-related code to exist independently of the application. Both parties only need to focus on their own functional implementation, thereby reducing the coupling and complexity of the code and improving the reliability of the system. The device driver framework layer is an abstraction of the same type of hardware device drivers. It extracts the same parts from the same type of hardware device drivers from different manufacturers, and leaves the different parts with interfaces to be implemented by the driver. "
For adc, rt_device_find generally returns values of adc0 and adc1. Before using, you need to confirm whether the adc device has been registered in the system.
#define ADC_DEV_NAME "adc0"
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if (adc_dev == RT_NULL)
{
rt_kprintf("can't find %s device!\n", ADC_DEV_NAME);
return RT_ERROR;
}
Create a new adc_a0 project here
Because it is a template project, ADC is not turned on. If you use the above rt_device_find directly, it will definitely return RT_ERROR, so you need to turn on the ADC device first.
That's it. Let's test whether ADC works properly. Add the operation directly in the main thread.
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if (adc_dev == RT_NULL)
{
rt_kprintf("can't find %s device!\n", ADC_DEV_NAME);
return RT_ERROR;
}
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
rt_kprintf("\nthe value is :%d \n", value);
vol = value * REFER_VOLTAGE / CONVERT_BITS;
rt_kprintf("the vol is :%d \n", vol);
rt_kprintf("the voltage is :%d.%03d \n", vol / 1000, vol % 1000);
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
First use 3v3 and GND to test the measurement results
After the download is complete, the test results can be seen through the serial port interface
It looks very stable. Let's try to make the result more accurate, because the accuracy should be able to reach 0.003
The result is quite stable. Replace the input source 3v3 and GND, and add voltage division to test the dynamic voltage.
It can be seen that when measuring the voltage divider, the jump in accuracy can be shown.
If the ADC test is OK, then this part can be written into a separate thread, and multiple measurements can be taken and averaged, and the maximum and minimum values will not be removed for the time being.
void adc_a0_entry(void *parameter)
{
rt_err_t ret = RT_EOK;
rt_uint32_t value[10], value_sum,vol,vol_ave,ii;
rt_adc_device_t adc_dev;
while(1)
{
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if (adc_dev == RT_NULL)
{
rt_kprintf("can't find %s device!\n", ADC_DEV_NAME);
return ;
}
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
for(ii=0;ii<10;ii++)
{
value[ii] = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
}
for(ii=0;ii<10;ii++)
{
rt_kprintf("%d;",value[ii]);
}
for(ii=0;ii<10;ii++)
{
value_sum += value[ii];
}
vol_ave =value_sum /10;
vol=vol_ave* REFER_VOLTAGE / CONVERT_BITS;
rt_kprintf("the vol is :%d \n", vol);
rt_kprintf("the voltage is :%d.%03d \n", vol / 1000, vol % 1000);
value_sum=0;
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
rt_thread_mdelay(1000);
}
}
After several operations, the only function of AB32VG1_Register and AB32VG1_DataSheet is to let you know how many pins there are and what each pin does. If rt_thread had not encapsulated the bottom layer, it would be impossible to perform any operation. AB32VG1_Register has 32 pages and AB32VG1_DataSheet has 13 pages. It is impossible to operate according to the manual.
|