1 前言
本实验使用了板载电位计(USER POT)通过ADC外设通道读取电平值,可以手动调节电位计查看ADC通道的电平变化。通过本实验可以学习到简单外设的配置,包括rt-thread的外设配置和FSP的外设配置。
2 查看开发板用户手册
查看手册提取出需要注意的关键信息,电位计使用的ADC引脚是 P004,同时需要短接E31(开发板背面)才能连接到MCU。
3 FSP配置ADC
打开FSP,在Pins页找到 P004 引脚,并查看所属的ADC通道为ADC0通道
在Stacks中添加ADC外设,配置ADC0,在Input配置中打开通道4的输入功能
配置完成点击生成代码,此步必须操作否则配置不会添加到工程中。
4 打开ADC设备驱动
在 RT-Thread settings 中打开 ADC0,保存配置。
5 测试验证
添加测试代码到 hal_entry.c
#define ADC_DEV_NAME "adc0"
#define ADC_DEV_CHANNEL 4
#define REFER_VOLTAGE 330
#define CONVERT_BITS (1 << 12)
static void sensor_read_data(void)
{
rt_adc_device_t adc_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if (adc_dev == RT_NULL)
编译下载,输入测试命令 sensor_read_data 查看电位计此时的电平值,可以使用工具旋转电位
计,查看电平变化。
{
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);
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
vol = value * REFER_VOLTAGE / CONVERT_BITS;
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
}
MSH_CMD_EXPORT(sensor_read_data, sensor_read_data);
hal_entry.c完整代码
/*
- Copyright (c) 2006-2021, RT-Thread Development Team
- SPDX-License-Identifier: Apache-2.0
- Change Logs:
- Date Author Notes
- 2021-10-10 Sherman first version
- 2021-11-03 Sherman Add icu_sample
*/
#include <rtthread.h>
#include "hal_data.h"
#include <rtdevice.h>
#define LED3_PIN BSP_IO_PORT_01_PIN_06
#define USER_INPUT "P105"
void hal_entry(void)
{
rt_kprintf("\nHello RT-Thread!\n");
while (1)
{
rt_pin_write(LED3_PIN, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED3_PIN, PIN_LOW);
rt_thread_mdelay(500);
}
}
void irq_callback_test(void *args)
{
rt_kprintf("\n IRQ00 triggered \n");
}
void icu_sample(void)
{
rt_uint32_t pin = rt_pin_get(USER_INPUT);
rt_kprintf("\n pin number : 0x%04X \n", pin);
rt_err_t err = rt_pin_attach_irq(pin, PIN_IRQ_MODE_RISING, irq_callback_test, RT_NULL);
if(RT_EOK != err)
{
rt_kprintf("\n attach irq failed. \n");
}
err = rt_pin_irq_enable(pin, PIN_IRQ_ENABLE);
if(RT_EOK != err)
{
rt_kprintf("\n enable irq failed. \n");
}
}
MSH_CMD_EXPORT(icu_sample, icu sample);
#define ADC_DEV_NAME "adc0"
#define ADC_DEV_CHANNEL 4
#define REFER_VOLTAGE 330
#define CONVERT_BITS (1 << 12)
static void sensor_read_data(void)
{
rt_adc_device_t adc_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
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);
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
vol = value * REFER_VOLTAGE / CONVERT_BITS;
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
}
MSH_CMD_EXPORT(sensor_read_data, sensor_read_data);
编译下载,输入测试命令 ‘sensor_read_data’ 查看电位计此时的电平值,可以使用工具旋转电位
计,查看电平变化。
C:\Users\Asus>D:/RT-ThreadStudio/plugins/org.rt-thread.studio.serial.putty_1.0.5/putty/plink.exe -serial COM8 -sercfg 115200,8,n,1,N
\ | /
- RT - Thread Operating System
/ | \ 4.1.0 build Aug 15 2022 16:44:58
2006 - 2022 Copyright by RT-Thread team
Hello RT-Thread!
msh >sensor_read_data
the voltage is :1.73
msh >sensor_read_data
the voltage is :1.73
msh >sensor_read_data
the voltage is :1.97
msh >sensor_read_data
the voltage is :0.00
msh >sensor_read_data
the voltage is :0.00
msh >sensor_read_data
the voltage is :3.04
msh >
原作者:2345vor