N32G457 ADC采样只有3.3v 和0 这两个值
#include <rtthread.h>
#include "board.h"
#include <rtdevice.h>
#include "protocol.h"
#define ADC_DEV_NAME "adc2" /* ADC 设备名称 /
#define ADC_DEV_CHANNEL 4
#define REFER_VOLTAGE 330 / 参考电压 3.3V,数据精度乘以100保留2位小数*/
#define CONVERT_BITS 4095 /* 转换位数为12位 */
static int adc_thread_entry(int argc, char argv[])
{
rt_adc_device_t adc_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
while(1){
/ 查找设备 /
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if (adc_dev == RT_NULL)
{
rt_kprintf("adc sample run failed! 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("the value is :%d \n", value);
/ 转换为对应电压值 /
vol = value * REFER_VOLTAGE / CONVERT_BITS;
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
/
if(value<800)
{
mcu_dp_enum_update(DPID_COVER_STATE,0); //当前蜂箱盖状态枚举型数据上报;
}
else {
mcu_dp_enum_update(DPID_COVER_STATE,1); //当前蜂箱盖状态枚举型数据上报;
}
/
rt_thread_mdelay(500);
/ 关闭通道 /
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
}
return ret;
}
int rt_thread_adc(void)
{
rt_thread_t ret=rt_thread_create("read_adc",adc_thread_entry,RT_NULL,512,10,10);
if(ret!=RT_NULL)
{
rt_kprintf("rt_thread_read_adc succeed....\n");
rt_thread_startup(ret);
}
else {
rt_kprintf("rt_thread_read_adc err....\n");
}
}
INIT_APP_EXPORT(rt_thread_adc);
/ 导出到 msh 命令列表中 */
//MSH_CMD_EXPORT(rt_thread_adc, adc7_thread_entry);
更多回帖