File tree Expand file tree Collapse file tree 1 file changed +5
-3
lines changed
Expand file tree Collapse file tree 1 file changed +5
-3
lines changed Original file line number Diff line number Diff line change 1212 * 例程导出了 adc_sample 命令到控制终端
1313 * 命令调用格式:adc_sample
1414 * 程序功能:通过 ADC 设备采样电压值并转换为数值。
15- * 示例代码参考电压为3.3V,分辨率为12位 。
15+ * 示例代码参考电压为3.3V,转换位数为12位 。
1616*/
1717
1818#include <rtthread.h>
1919#include <rtdevice.h>
2020
2121#define ADC_DEV_NAME "adc1" /* ADC 设备名称 */
2222#define ADC_DEV_CHANNEL 5 /* ADC 通道 */
23+ #define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
24+ #define CONVERT_BITS (1 << 12) /* 转换位数为12位 */
2325
2426static int adc_vol_sample (int argc , char * argv [])
2527{
@@ -42,8 +44,8 @@ static int adc_vol_sample(int argc, char *argv[])
4244 value = rt_adc_read (adc_dev , ADC_DEV_CHANNEL );
4345 rt_kprintf ("the value is :%d \n" , value );
4446
45- /* 转换为对应电压值,3.3V对应12位最大值4096,数据精度乘以100保留2位小数 */
46- vol = value * 330 / 4096 ;
47+ /* 转换为对应电压值 */
48+ vol = value * REFER_VOLTAGE / CONVERT_BITS ;
4749 rt_kprintf ("the voltage is :%d.%02d \n" , vol / 100 , vol % 100 );
4850
4951 /* 关闭通道 */
You can’t perform that action at this time.
0 commit comments