Browse Source

增加adc、hwtimer、rtc等设备使用示例

misonyo 7 years ago
parent
commit
3fc2698713
6 changed files with 230 additions and 17 deletions
  1. 54 0
      adc_vol_sample.c
  2. 95 0
      hwtimer_sample.c
  3. 3 3
      i2c_aht10_sample.c
  4. 2 2
      iwdg_sample.c
  5. 25 12
      pwm_led_sample.c
  6. 51 0
      rtc_sample.c

+ 54 - 0
adc_vol_sample.c

@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2018-11-29     misonyo      first implementation.
+ */
+/*
+ * 程序清单:这是一个 ADC 设备使用例程
+ * 例程导出了 adc_sample 命令到控制终端
+ * 命令调用格式:adc_sample
+ * 程序功能:通过 ADC 设备采样电压值并转换为数值。
+*/
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+#define ADC_DEV_NAME        "adc1"
+#define ADC_DEV_CHANNEL     5
+
+static int adc_vol_sample(int argc, char *argv[])
+{
+    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);
+    rt_kprintf("the value is :%d \n", value);
+
+    /* 转换为对应电压值,3.3V对应12位最大值4096,数据精度乘以100保留2位小数 */
+    vol = value * 330 / 4096;
+    rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
+
+    /* 关闭设备 */
+    ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
+
+    return ret;
+}
+/* 导出到 msh 命令列表中 */
+MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample);

+ 95 - 0
hwtimer_sample.c

@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2018-11-30     misonyo      first implementation.
+ */
+/*
+ * 程序清单:这是一个 hwtimer 设备使用例程
+ * 例程导出了 hwtimer_sample 命令到控制终端
+ * 命令调用格式:hwtimer_sample
+ * 程序功能:硬件定时器周期性的打印当前tick值,2次tick值之差也就是定时时间。
+*/
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+#define HWTIMER_DEV_NAME   "timer0"
+
+/* 定时器超时回调函数 */
+static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
+{
+    rt_kprintf("this is hwtimer timeout callback fucntion!\n");
+    rt_kprintf("tick is :%d !\n", rt_tick_get());
+
+    return 0;
+}
+
+static int hwtimer_sample(int argc, char *argv[])
+{
+    rt_err_t ret = RT_EOK;
+    rt_hwtimerval_t timeout_s;
+    rt_device_t hw_dev = RT_NULL;
+    rt_hwtimer_mode_t mode;         /* 定时器模式 */
+    rt_uint32_t freq = 10000;       /* 计数频率 */
+
+    /* 查找定时器设备 */
+    hw_dev = rt_device_find(HWTIMER_DEV_NAME);
+    if (hw_dev == RT_NULL)
+    {
+        rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
+        return RT_ERROR;
+    }
+
+    /* 打开设备 */
+    ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
+    if (ret != RT_EOK)
+    {
+        rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
+        return ret;
+    }
+
+    /* 设置超时回调函数 */
+    rt_device_set_rx_indicate(hw_dev, timeout_cb);
+
+    /* 设置计数频率(默认1Mhz或支持的最小计数频率) */
+    ret = rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
+    if (ret != RT_EOK)
+    {
+        rt_kprintf("set frequency failed! ret is :%d\n", ret);
+        return ret;
+    }
+
+    /* 设置模式为周期性定时器 */
+    mode = HWTIMER_MODE_PERIOD;
+    ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
+    if (ret != RT_EOK)
+    {
+        rt_kprintf("set mode failed! ret is :%d\n", ret);
+        return ret;
+    }
+
+    /* 设置定时器超时值为5s并启动定时器 */
+    timeout_s.sec = 5;      /* 秒 */
+    timeout_s.usec = 0;     /* 微秒 */
+
+    if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
+    {
+        rt_kprintf("set timeout value failed\n");
+        return RT_ERROR;
+    }
+
+    /* 延时3500ms */
+    rt_thread_mdelay(3500);
+
+    /* 读取定时器当前值 */
+    rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
+    rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
+
+    return ret;
+}
+/* 导出到 msh 命令列表中 */
+MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);

+ 3 - 3
i2c_aht10_sample.c

@@ -19,7 +19,7 @@
 #include <rtdevice.h>
 
 #define AHT10_I2C_BUS_NAME          "i2c1"  /* 传感器连接的I2C总线设备名称 */
-#define AHT10_ADDR                  0x38
+#define AHT10_ADDR                  0x38    /* 从机地址 */
 #define AHT10_CALIBRATION_CMD       0xE1    /* 校准命令 */
 #define AHT10_NORMAL_CMD            0xA8    /* 一般命令 */
 #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
     msgs.buf = buf;
     msgs.len = 3;
 
-    /* 调用I2C设备接口发送数据 */
+    /* 调用I2C设备接口传输数据 */
     if (rt_i2c_transfer(bus, &msgs, 1) == 1)
         return RT_EOK;
     else
@@ -59,7 +59,7 @@ static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint
     msgs.buf = buf;
     msgs.len = len;
 
-    /* 调用I2C设备接口接收数据 */
+    /* 调用I2C设备接口传输数据 */
     if (rt_i2c_transfer(bus, &msgs, 1) == 1)
         return RT_EOK;
     else

+ 2 - 2
iwdg_sample.c

@@ -11,7 +11,7 @@
  * 程序清单:这是一个独立看门狗设备使用例程
  * 例程导出了 iwdg_sample 命令到控制终端
  * 命令调用格式:iwdg_sample iwg
- * 命令解释:命令第二个参数是要使用的看门狗设备名称,为空则使用例程默认的看门狗设备
+ * 命令解释:命令第二个参数是要使用的看门狗设备名称,为空则使用例程默认的看门狗设备
  * 程序功能:程序通过设备名称查找看门狗设备,然后初始化设备并设置看门狗设备溢出时间。
  *           然后设置空闲线程回调函数,在回调函数里会喂狗。
 */
@@ -32,7 +32,7 @@ static void idle_hook(void)
 static int iwdg_sample(int argc, char *argv[])
 {
     rt_err_t result = RT_EOK;
-    rt_uint32_t timeout = 1000;    /* 超时时间为1000ms*/
+    rt_uint32_t timeout = 1000;    /* 溢出时间 */
     char device_name[RT_NAME_MAX];
 
     /* 判断命令行参数是否给定了设备名称 */

+ 25 - 12
pwm_led_sample.c

@@ -22,22 +22,16 @@
 #define PWM_DEV_NAME        "pwm3"
 #define PWM_DEV_CHANNEL     4
 
+struct rt_device_pwm *pwm_dev;
+
 static void pwm_led_entry(void *parameter)
 {
     rt_uint32_t period, pulse, dir;
-    struct rt_device_pwm *pwm_dev;
-    /* 设置LED引脚脚模式为输出 */
-    rt_pin_mode(LED_PIN_NUM, PIN_MODE_OUTPUT);
-    /* 拉高LED引脚 */
-    rt_pin_write(LED_PIN_NUM, PIN_HIGH);
 
-    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
-
-    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
-
-    period = 500000;    /* 0.5ms周期,单位为纳秒ns */
+    period = 500000;    /* 周期为0.5ms,单位为纳秒ns */
     dir = 1;            /* PWM脉冲宽度值的增减方向 */
     pulse = 0;          /* PWM脉冲宽度值,单位为纳秒ns */
+
     while (1)
     {
         rt_thread_mdelay(50);
@@ -49,7 +43,7 @@ static void pwm_led_entry(void *parameter)
         {
             pulse -= 5000;      /* 从最大值开始每次减少5000ns */
         }
-        if (pulse >= 500000)
+        if (pulse >= period)
         {
             dir = 0;
         }
@@ -58,14 +52,31 @@ static void pwm_led_entry(void *parameter)
             dir = 1;
         }
 
+        /* 设置PWM周期和脉冲宽度 */
         rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
     }
 }
 
-static void pwm_led_sample(int argc, char *argv[])
+static int pwm_led_sample(int argc, char *argv[])
 {
     rt_thread_t tid;
 
+    /* 设置LED引脚脚模式为输出 */
+    rt_pin_mode(LED_PIN_NUM, PIN_MODE_OUTPUT);
+    /* 拉高LED引脚 */
+    rt_pin_write(LED_PIN_NUM, PIN_HIGH);
+
+    /* 查找设备 */
+    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
+    if (pwm_dev == RT_NULL)
+    {
+        rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
+        return RT_ERROR;
+    }
+
+    /* 使能设备 */
+    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
+
     tid = rt_thread_create("pwm",
                            pwm_led_entry,
                            RT_NULL,
@@ -76,6 +87,8 @@ static void pwm_led_sample(int argc, char *argv[])
     {
         rt_thread_startup(tid);
     }
+
+    return 0;
 }
 /* 导出到 msh 命令列表中 */
 MSH_CMD_EXPORT(pwm_led_sample, pwm sample);

+ 51 - 0
rtc_sample.c

@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2018-11-30     misonyo      first implementation.
+ */
+/*
+ * 程序清单:这是一个 RTC 设备使用例程
+ * 例程导出了 rtc_sample 命令到控制终端
+ * 命令调用格式:rtc_sample
+ * 程序功能:设置RTC设备的日期和时间,延时一段时间后获取当前时间并打印显示。
+*/
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+static int rtc_sample(int argc, char *argv[])
+{
+    rt_err_t ret = RT_EOK;
+    time_t now;
+
+    /* 设置日期 */
+    ret = set_date(2018, 12, 3);
+    if (ret != RT_EOK)
+    {
+        rt_kprintf("set RTC date failed\n");
+        return ret;
+    }
+
+    /* 设置时间 */
+    ret = set_time(11, 15, 50);
+    if (ret != RT_EOK)
+    {
+        rt_kprintf("set RTC time failed\n");
+        return ret;
+    }
+
+    /* 延时3秒 */
+    rt_thread_mdelay(3000);
+
+    /* 获取时间 */
+    now = time(RT_NULL);
+    rt_kprintf("%s\n", ctime(&now));
+
+    return ret;
+}
+/* 导出到 msh 命令列表中 */
+MSH_CMD_EXPORT(rtc_sample, rtc sample);