Skip to content

Commit 3fc2698

Browse files
committed
增加adc、hwtimer、rtc等设备使用示例
1 parent d9e5847 commit 3fc2698

File tree

6 files changed

+230
-17
lines changed

6 files changed

+230
-17
lines changed

adc_vol_sample.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2018-11-29 misonyo first implementation.
9+
*/
10+
/*
11+
* 程序清单:这是一个 ADC 设备使用例程
12+
* 例程导出了 adc_sample 命令到控制终端
13+
* 命令调用格式:adc_sample
14+
* 程序功能:通过 ADC 设备采样电压值并转换为数值。
15+
*/
16+
17+
#include <rtthread.h>
18+
#include <rtdevice.h>
19+
20+
#define ADC_DEV_NAME "adc1"
21+
#define ADC_DEV_CHANNEL 5
22+
23+
static int adc_vol_sample(int argc, char *argv[])
24+
{
25+
rt_adc_device_t adc_dev;
26+
rt_uint32_t value, vol;
27+
rt_err_t ret = RT_EOK;
28+
29+
/* 查找设备 */
30+
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
31+
if (adc_dev == RT_NULL)
32+
{
33+
rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
34+
return RT_ERROR;
35+
}
36+
37+
/* 使能设备 */
38+
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
39+
40+
/* 读取数据 */
41+
value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
42+
rt_kprintf("the value is :%d \n", value);
43+
44+
/* 转换为对应电压值,3.3V对应12位最大值4096,数据精度乘以100保留2位小数 */
45+
vol = value * 330 / 4096;
46+
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
47+
48+
/* 关闭设备 */
49+
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
50+
51+
return ret;
52+
}
53+
/* 导出到 msh 命令列表中 */
54+
MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample);

hwtimer_sample.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2018-11-30 misonyo first implementation.
9+
*/
10+
/*
11+
* 程序清单:这是一个 hwtimer 设备使用例程
12+
* 例程导出了 hwtimer_sample 命令到控制终端
13+
* 命令调用格式:hwtimer_sample
14+
* 程序功能:硬件定时器周期性的打印当前tick值,2次tick值之差也就是定时时间。
15+
*/
16+
17+
#include <rtthread.h>
18+
#include <rtdevice.h>
19+
20+
#define HWTIMER_DEV_NAME "timer0"
21+
22+
/* 定时器超时回调函数 */
23+
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
24+
{
25+
rt_kprintf("this is hwtimer timeout callback fucntion!\n");
26+
rt_kprintf("tick is :%d !\n", rt_tick_get());
27+
28+
return 0;
29+
}
30+
31+
static int hwtimer_sample(int argc, char *argv[])
32+
{
33+
rt_err_t ret = RT_EOK;
34+
rt_hwtimerval_t timeout_s;
35+
rt_device_t hw_dev = RT_NULL;
36+
rt_hwtimer_mode_t mode; /* 定时器模式 */
37+
rt_uint32_t freq = 10000; /* 计数频率 */
38+
39+
/* 查找定时器设备 */
40+
hw_dev = rt_device_find(HWTIMER_DEV_NAME);
41+
if (hw_dev == RT_NULL)
42+
{
43+
rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
44+
return RT_ERROR;
45+
}
46+
47+
/* 打开设备 */
48+
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
49+
if (ret != RT_EOK)
50+
{
51+
rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
52+
return ret;
53+
}
54+
55+
/* 设置超时回调函数 */
56+
rt_device_set_rx_indicate(hw_dev, timeout_cb);
57+
58+
/* 设置计数频率(默认1Mhz或支持的最小计数频率) */
59+
ret = rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
60+
if (ret != RT_EOK)
61+
{
62+
rt_kprintf("set frequency failed! ret is :%d\n", ret);
63+
return ret;
64+
}
65+
66+
/* 设置模式为周期性定时器 */
67+
mode = HWTIMER_MODE_PERIOD;
68+
ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
69+
if (ret != RT_EOK)
70+
{
71+
rt_kprintf("set mode failed! ret is :%d\n", ret);
72+
return ret;
73+
}
74+
75+
/* 设置定时器超时值为5s并启动定时器 */
76+
timeout_s.sec = 5; /* 秒 */
77+
timeout_s.usec = 0; /* 微秒 */
78+
79+
if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
80+
{
81+
rt_kprintf("set timeout value failed\n");
82+
return RT_ERROR;
83+
}
84+
85+
/* 延时3500ms */
86+
rt_thread_mdelay(3500);
87+
88+
/* 读取定时器当前值 */
89+
rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
90+
rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
91+
92+
return ret;
93+
}
94+
/* 导出到 msh 命令列表中 */
95+
MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);

i2c_aht10_sample.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <rtdevice.h>
2020

2121
#define AHT10_I2C_BUS_NAME "i2c1" /* 传感器连接的I2C总线设备名称 */
22-
#define AHT10_ADDR 0x38
22+
#define AHT10_ADDR 0x38 /* 从机地址 */
2323
#define AHT10_CALIBRATION_CMD 0xE1 /* 校准命令 */
2424
#define AHT10_NORMAL_CMD 0xA8 /* 一般命令 */
2525
#define AHT10_GET_DATA 0xAC /* 获取数据命令 */
@@ -42,7 +42,7 @@ static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint
4242
msgs.buf = buf;
4343
msgs.len = 3;
4444

45-
/* 调用I2C设备接口发送数据 */
45+
/* 调用I2C设备接口传输数据 */
4646
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
4747
return RT_EOK;
4848
else
@@ -59,7 +59,7 @@ static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint
5959
msgs.buf = buf;
6060
msgs.len = len;
6161

62-
/* 调用I2C设备接口接收数据 */
62+
/* 调用I2C设备接口传输数据 */
6363
if (rt_i2c_transfer(bus, &msgs, 1) == 1)
6464
return RT_EOK;
6565
else

iwdg_sample.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* 程序清单:这是一个独立看门狗设备使用例程
1212
* 例程导出了 iwdg_sample 命令到控制终端
1313
* 命令调用格式:iwdg_sample iwg
14-
* 命令解释:命令第二个参数是要使用的看门狗设备名称,为空则使用例程默认的看门狗设备
14+
* 命令解释:命令第二个参数是要使用的看门狗设备名称,为空则使用例程默认的看门狗设备
1515
* 程序功能:程序通过设备名称查找看门狗设备,然后初始化设备并设置看门狗设备溢出时间。
1616
* 然后设置空闲线程回调函数,在回调函数里会喂狗。
1717
*/
@@ -32,7 +32,7 @@ static void idle_hook(void)
3232
static int iwdg_sample(int argc, char *argv[])
3333
{
3434
rt_err_t result = RT_EOK;
35-
rt_uint32_t timeout = 1000; /* 超时时间为1000ms*/
35+
rt_uint32_t timeout = 1000; /* 溢出时间 */
3636
char device_name[RT_NAME_MAX];
3737

3838
/* 判断命令行参数是否给定了设备名称 */

pwm_led_sample.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,16 @@
2222
#define PWM_DEV_NAME "pwm3"
2323
#define PWM_DEV_CHANNEL 4
2424

25+
struct rt_device_pwm *pwm_dev;
26+
2527
static void pwm_led_entry(void *parameter)
2628
{
2729
rt_uint32_t period, pulse, dir;
28-
struct rt_device_pwm *pwm_dev;
29-
/* 设置LED引脚脚模式为输出 */
30-
rt_pin_mode(LED_PIN_NUM, PIN_MODE_OUTPUT);
31-
/* 拉高LED引脚 */
32-
rt_pin_write(LED_PIN_NUM, PIN_HIGH);
3330

34-
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
35-
36-
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
37-
38-
period = 500000; /* 0.5ms周期,单位为纳秒ns */
31+
period = 500000; /* 周期为0.5ms,单位为纳秒ns */
3932
dir = 1; /* PWM脉冲宽度值的增减方向 */
4033
pulse = 0; /* PWM脉冲宽度值,单位为纳秒ns */
34+
4135
while (1)
4236
{
4337
rt_thread_mdelay(50);
@@ -49,7 +43,7 @@ static void pwm_led_entry(void *parameter)
4943
{
5044
pulse -= 5000; /* 从最大值开始每次减少5000ns */
5145
}
52-
if (pulse >= 500000)
46+
if (pulse >= period)
5347
{
5448
dir = 0;
5549
}
@@ -58,14 +52,31 @@ static void pwm_led_entry(void *parameter)
5852
dir = 1;
5953
}
6054

55+
/* 设置PWM周期和脉冲宽度 */
6156
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
6257
}
6358
}
6459

65-
static void pwm_led_sample(int argc, char *argv[])
60+
static int pwm_led_sample(int argc, char *argv[])
6661
{
6762
rt_thread_t tid;
6863

64+
/* 设置LED引脚脚模式为输出 */
65+
rt_pin_mode(LED_PIN_NUM, PIN_MODE_OUTPUT);
66+
/* 拉高LED引脚 */
67+
rt_pin_write(LED_PIN_NUM, PIN_HIGH);
68+
69+
/* 查找设备 */
70+
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
71+
if (pwm_dev == RT_NULL)
72+
{
73+
rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
74+
return RT_ERROR;
75+
}
76+
77+
/* 使能设备 */
78+
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
79+
6980
tid = rt_thread_create("pwm",
7081
pwm_led_entry,
7182
RT_NULL,
@@ -76,6 +87,8 @@ static void pwm_led_sample(int argc, char *argv[])
7687
{
7788
rt_thread_startup(tid);
7889
}
90+
91+
return 0;
7992
}
8093
/* 导出到 msh 命令列表中 */
8194
MSH_CMD_EXPORT(pwm_led_sample, pwm sample);

rtc_sample.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2018-11-30 misonyo first implementation.
9+
*/
10+
/*
11+
* 程序清单:这是一个 RTC 设备使用例程
12+
* 例程导出了 rtc_sample 命令到控制终端
13+
* 命令调用格式:rtc_sample
14+
* 程序功能:设置RTC设备的日期和时间,延时一段时间后获取当前时间并打印显示。
15+
*/
16+
17+
#include <rtthread.h>
18+
#include <rtdevice.h>
19+
20+
static int rtc_sample(int argc, char *argv[])
21+
{
22+
rt_err_t ret = RT_EOK;
23+
time_t now;
24+
25+
/* 设置日期 */
26+
ret = set_date(2018, 12, 3);
27+
if (ret != RT_EOK)
28+
{
29+
rt_kprintf("set RTC date failed\n");
30+
return ret;
31+
}
32+
33+
/* 设置时间 */
34+
ret = set_time(11, 15, 50);
35+
if (ret != RT_EOK)
36+
{
37+
rt_kprintf("set RTC time failed\n");
38+
return ret;
39+
}
40+
41+
/* 延时3秒 */
42+
rt_thread_mdelay(3000);
43+
44+
/* 获取时间 */
45+
now = time(RT_NULL);
46+
rt_kprintf("%s\n", ctime(&now));
47+
48+
return ret;
49+
}
50+
/* 导出到 msh 命令列表中 */
51+
MSH_CMD_EXPORT(rtc_sample, rtc sample);

0 commit comments

Comments
 (0)