Q群:
电话:
邮箱:
地址:
本开发指南给用户提供了Pulse Encoder的基本配置,工程构建编译、操作API介绍以及使用示例展示。
如果在提供的OneOS源码中已经有适合的工程DEMO工程,则可以直接使用;如果没有请参照快速上手中的操作指南,新建一个合适的工程。 STM32的脉冲编码器硬件接口依赖于定时器实现,在配置时需要配置好定时器的脉冲编码器功能。定时器配置为脉冲编码器功能后,将只做脉冲编码器,不再支持通用定时器功能。
打开oneos\projects\xxxxx(project文件夹)\board\CubeMX_Config下的CUBE工程文件;
在CUBE工程中进行Pulse Encoder配置,如下图所示:
注意事项: i. 请根据实际的编码器类型进行正确的配置,保证计数准确性。 ii. 编码器使用时必须开启外定时器的更新中断,否则编码器计数无法累计。
(Top) → Drivers→ MISC
OneOS Configuration
[*] Using push button device drivers
[*] Using led device drivers
[ ] Using buzzer device drivers
[ ] Using ADC device drivers
[ ] Using DAC device drivers
[ ] Using PWM device drivers
[ ] Using input capture device drivers
[*] Using pulse encoder device drivers
函数 | 说明 |
---|---|
os_device_find() | 查找脉冲编码器设备 |
os_device_control() | 控制脉冲编码器设备,可以清空计数值、设置计数周期、开关设备 |
os_pulse_encoder_read() | 读取编码器计数值 |
os_pulse_encoder_clear() | 清除编码器计数值 |
os_pulse_encoder_set_period() | 设置脉冲编码器计数周期 |
os_pulse_encoder_enable() | 使能脉冲编码器计数 |
os_pulse_encoder_disable() | 失能脉冲编码器计数 |
该函数根据脉冲编码器设备名查找对应的脉冲编码器设备,函数原型如下:
os_device_t *os_device_find(const char *name);
参数 | 说明 |
---|---|
name | 脉冲编码器设备名称 |
返回 | 说明 |
脉冲编码器设备指针 | 查找到对应设备将返回相应的设备指针 |
OS_NULL | 没有找到设备 |
该函数用于控制脉冲编码器设备参数,函数原型如下:
os_err_t os_device_control(os_device_t *dev, int cmd, void *arg);
参数 | 说明 |
---|---|
dev | 设备指针 |
cmd | 命令控制字 |
arg | 控制的参数 |
返回 | 说明 |
OS_EOK | 函数执行成功 |
OS_ENOSYS | 失败,设备操作方法为空 |
其他错误码 | 失败 |
脉冲编码器支持的命令控制字如下所示:
控制字 | 说明 |
---|---|
PULSE_ENCODER_CMD_ENABLE | 启动脉冲编码器设备 |
PULSE_ENCODER_CMD_DISABLE | 停止脉冲编码器设备 |
PULSE_ENCODER_CMD_SET_PERIOD | 设置编码器的计数周期 |
PULSE_ENCODER_CMD_CLEAR_COUNT | 清除脉冲编码器计数 |
该函数用于获取脉冲编码器计数值,函数原型如下:
os_err_t os_pulse_encoder_read(struct os_pulse_encoder_device *pulse_encoder, os_int32_t *buffer);
参数 | 说明 |
---|---|
pulse_encoder | 脉冲编码器设备指针 |
buffer | 输出参数,指向存储脉冲编码器计数值变量的指针 |
返回 | 说明 |
OS_EOK | 读取成功 |
其他错误码 | 读取失败 |
该函数用于清除脉冲编码器计数,函数原型如下:
os_err_t os_pulse_encoder_clear(struct os_pulse_encoder_device *pulse_encoder);
参数 | 说明 |
---|---|
pulse_encoder | 脉冲编码器设备指针 |
返回 | 说明 |
OS_EOK | 清除计数成功 |
其他错误码 | 清除计数失败 |
该函数用于设定编码器计数脉冲编码器周期,函数原型如下:
os_err_t os_pulse_encoder_set_period(struct os_pulse_encoder_device *pulse_encoder, os_uint32_t period);
参数 | 说明 |
---|---|
pulse_encoder | 脉冲编码器设备指针 |
period | 脉冲编码器设置周期值 |
返回 | 说明 |
OS_EOK | 设置成功 |
其他错误码 | 设置失败 |
该函数用于使能启动编码器计数,函数原型如下:
os_err_t os_pulse_encoder_enable(struct os_pulse_encoder_device *pulse_encoder);
参数 | 说明 |
---|---|
pulse_encoder | 脉冲编码器设备指针 |
返回 | 说明 |
OS_EOK | 启动成功 |
其它错误码 | 启动失败 |
该函数用于停止编码器计数,函数原型如下:
os_err_t os_pulse_encoder_disable(struct os_pulse_encoder_device *pulse_encoder);
参数 | 说明 |
---|---|
pulse_encoder | 脉冲编码器设备指针 |
返回 | 说明 |
OS_EOK | 停止成功 |
其它错误码 | 停止失败 |
脉冲编码器设备的具体使用方式可以参考如下示例代码,示例代码的主要步骤如下:
#include <os_device.h>
#include "drv_gpio.h"
#include <os_errno.h>
#include <os_idle.h>
#ifdef OS_USING_SHELL
#include <shell.h>
#endif
int pulse_encoder_sample(int argc, char **argv)
{
os_err_t ret = OS_EOK;
os_int32_t count = 0;
char *dev_name;
os_pulse_encoder_device_t *pulse_encoder_dev = OS_NULL;
if (argc != 2)
{
os_kprintf("usage: pulse_encoder_sample <dev>\r\n");
os_kprintf(" pulse_encoder_sample encoder_tim1\r\n");
return -1;
}
dev_name = argv[1];
pulse_encoder_dev = (os_pulse_encoder_device_t *)os_device_find(dev_name);
if (pulse_encoder_dev == OS_NULL)
{
os_kprintf("pulse encoder sample run failed! can't find %s device!\r\n", dev_name);
return OS_ERROR;
}
ret = os_pulse_encoder_enable(pulse_encoder_dev);
for (int i = 0; i <= 10; i++)
{
os_task_msleep(500);
ret = os_pulse_encoder_read(pulse_encoder_dev, &count);
os_kprintf("get count %d\r\n", count);
}
return ret;
}
/* Export to msh command list */
SH_CMD_EXPORT(pulse_encoder_sample, pulse_encoder_sample, "pulse_encoder_sample");
运行结果如下:
sh />pulse_encoder_sample encoder_tim1
get count 101
get count 203
get count 304
get count 202