【匠芯创D133CBS】 ADC测试
<p><span style="font-size:16px;">通过ADC按键来测试ADC输入。</span></p><p> </p>
<p><span style="font-size:16px;"><strong>一、硬件电路</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">ADC按键部分电路</span></p>
<p><span style="font-size:16px;"></span></p>
<p><span style="font-size:16px;">PA2对应GPADC2功能</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;"><strong>二、程序部分</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">adc.c</span></p>
<pre>
<code>//adc.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/time.h>
#include <rtthread.h>
#include "rtdevice.h"
#include <aic_core.h>
#include "hal_adcim.h"
#include "aic_log.h"
#include "hal_gpai.h"
/* Global macro and variables */
#define AIC_GPAI_NAME "gpai"
/* The default voltages are set to D21x->3.0V, D31x、D12x->2.5V */
#define AIC_GPAI_DEFAULT_VOLTAGE 3.3
#define AIC_GPAI_VOLTAGE_ACCURACY 10000
#define AIC_GPAI_FIFO_MAX_DEPTH 32
static rt_adc_device_t gpai_dev;
static int g_sample_num = -1;
static u32 g_cal_param;
struct aic_gpai_ch_info ch_info = {0};
float def_voltage = AIC_GPAI_DEFAULT_VOLTAGE;
static int test_adc_hdl(void)
{
int c;
int ret;
u32 ch = 0;
int adc_val;
int voltage = -1;
int scale = AIC_GPAI_VOLTAGE_ACCURACY;
ch_info.chan_id = 2; //采样通道2
ch_info.mode = AIC_GPAI_MODE_PERIOD; //单次采样
def_voltage = 2.5; //参考电压2.5V
g_sample_num = 1000;
gpai_dev = (rt_adc_device_t)rt_device_find(AIC_GPAI_NAME);
if (!gpai_dev) {
rt_kprintf("Failed to open %s device\n", AIC_GPAI_NAME);
return -RT_ERROR;
}
ret = rt_adc_control(gpai_dev, RT_ADC_CMD_GET_MODE, (void *)&ch_info);
if (ret) {
rt_kprintf("Failed to get GPAI mode\n");
return -RT_ERROR;
}
g_cal_param = hal_adcim_auto_calibration();
rt_adc_enable(gpai_dev, ch_info.chan_id);
rt_adc_control(gpai_dev, RT_ADC_CMD_GET_CH_INFO, (void *)&ch_info);
aicos_msleep(10);
adc_val = ch_info.adc_values;
if (adc_val) {
voltage = hal_adcim_adc2voltage(adc_val, g_cal_param,
AIC_GPAI_VOLTAGE_ACCURACY,
def_voltage);
rt_kprintf("GPAI voltage:%d.%04d v\n", voltage / scale,
voltage % scale);
}
return voltage;
}
static void adc_read_hdl(void *parmeter)
{
while (1)
{
test_adc_hdl();
rt_thread_mdelay(100);
}
}
inttest_adc_sample(void)
{
rt_thread_t adc_thread;
adc_thread = rt_thread_create(
"test_adc",
adc_read_hdl,
"adc2",
2048,
RT_THREAD_PRIORITY_MAX/2,
25);
if(adc_thread != RT_NULL)
{
rt_thread_startup(adc_thread);
}
return RT_EOK;
}
MSH_CMD_EXPORT_ALIAS(test_adc_sample, test_adc_sample, gpai device sample);
</code></pre>
<p> </p>
<p><span style="font-size:16px;"><strong>三、配置ADC</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">3.1、打开ADC2</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;">3.2、打开添加的测试例程</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;"><strong>四、执行结果</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">下载程序后运行</span></p>
<p><span style="font-size:16px;">4.1、按下UP按键,ADC电压值</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;">4.2、按下DOWN按键,ADC电压值</span></p>
<p><span style="font-size:16px;"></span></p>
<p><span style="font-size:16px;">4.3、按下LEFT按键,ADC电压值</span></p>
<p><span style="font-size:16px;"></span></p>
<p><span style="font-size:16px;">4.4、按下RIGHT按键,ADC电压值</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p>加油加油加油加油加油加油加油加油</p>
页:
[1]