一、M487 ECAP简介
6.15.1 概述
M480 系列提供了多达两组的增强型输入捕捉定时器/计数器,以用于检测输入通道边沿信号的改变。每
个单元含有三路输入通道。定时器/计数器有向上计数、重载及比较匹配的功能。
6.15.2 特性
多达两组输入捕捉定时器/计数器单元,CAP0和CAP1
每个单元都有3个输入通道
每个单元都有独立的中断向量
每个输入通道都有对应的捕捉计数器保持寄存器
24位的输入捕捉向上计数定时器/计数器
在输入端末前附有噪声过滤
三种边沿检测:
– 上升沿检测
– 下降沿检测
– 双边沿检测
支持用被捕捉的事情复位/重载捕捉计数器
支持比较匹配功能
二、软件测试环境
KEIL MDK v5.26
Env RT-Thread 开发辅助工具。
终端联机工具Tera Term V4.99
RT-Thread 标准版系统及bsp包
三、硬件环境
NuMaker-IoT-M487境
1.在官网下载Env RT-Thread 开发辅助工具包,包含了终端联机工具Tera Term;
2.根据NuMaker-IoT-M487 上手指南
下载更新RT-Thread 标准版系统及bsp包
3.在ENV环境下,输入cd rt-thread>> cd bsp/nuvoton/numaker-iot-m487>>menuconfig打开系统配置界面,Hardware Drivers Config>>On-Chip Peripheral Drivers >>Enable Enhance Input Capture timer>>Enable ECAP0>> 0X01(Specify Channel Mark for ECAP0 Channel);
如图:
最后输入scons —target=mdk5 进行编译;
打开KEIL编译通过后下载到板上;通过终端联机工具Tera Term 输入list_device查询
如图
出现ecap0i0接口对象, 说明项目配置成功;
4.找出M487 ECAP0 对应的IO PA10,如图
在nutool_pincfg.c 中的nutool_pincfg_init 和nutool_pincfg_deinit中分别加入: nutool_pincfg_init_ecap0(), nutool_pincfg_deinit_ecap0();
void nutool_pincfg_init_ecap0(void)
{
/* Set PA.10 for ECAP0_IC0*/
SYS->GPA_MFPH = (SYS->GPA_MFPH & ~SYS_GPA_MFPH_PA10MFP_Msk) |SYS_GPA_MFPH_PA10MFP_ECAP0_IC0;
}
void nutool_pincfg_deinit_ecap0(void)
{
/* Set PA.10 for ECAP0_IC0*/
SYS->GPA_MFPH = (SYS->GPA_MFPH & ~SYS_GPA_MFPH_PA10MFP_Msk);
}
如图
5.找出NuMaker-IoT-M487 对应的测试端子;
然后将杜邦线连接:
6.开始代码测试
用keil 打开 rt-threadsp
uvoton
umaker-iot-m487 目录下project项目;在 applications 下新增 ECAP0_test.c;
#include "rtthread.h"
#include "rtdevice.h"
#include "NuMicro.h"
#include
#define Input_Push NU_GET_PININDEX(NU_PA, 0)
//#define B0 NU_GET_PININDEX(NU_PA, 10)
rt_size_t g_size;
typedef struct _ecap_dev
{
ECAP_T *ecap_base;
float fUsPerTick;
} nu_ecap_dev_t;
typedef struct _ecap
{
struct rt_inputcapture_device parent;
nu_ecap_dev_t *ecap_dev;
uint8_t u8Channel;
rt_bool_t bfirstData;
uint32_t u32CurrentCnt;
uint32_t u32LastCnt;
rt_bool_t input_data_level;
} nu_capture_t;
static rt_err_t ecap0_rx_callback(rt_device_t dev, rt_size_t size)
{
rt_kprintf("%d
",size);
g_size = size;
return 0;
}
void ecap_thread_entry(void *arc)
{
rt_int32_t s32_Count = 0;
rt_device_t ecap_ops=rt_device_find("ecap0i0");
if(ecap_ops == RT_NULL)
{
rt_kprintf("not find device ecap0i0
");
return ;
}
rt_kprintf("find device ecap0i0
");
rt_device_open(ecap_ops, RT_NULL); //
rt_device_set_rx_indicate(ecap_ops, ecap0_rx_callback);
while(1)
{
if( s32_Count < 100 )
{
rt_pin_write(Input_Push, PIN_HIGH);
rt_thread_delay(2);
if( g_size > 50 ) rt_kprintf(" high level pulse, rx_Callback %d, Count:%d, capture level:%d
", g_size, s32_Count,((nu_capture_t*)ecap_ops)->input_data_level );
s32_Count++;
rt_pin_write(Input_Push, PIN_LOW);
rt_thread_delay(2);
if( g_size > 50 ) rt_kprintf(" low level pulse,rx_Callback %d, Count:%d, capture level:%d
", g_size, s32_Count,((nu_capture_t*)ecap_ops)->input_data_level );
s32_Count++;
}
else
rt_thread_delay(2);
}
}
int run_ecap0(void)
{
rt_pin_mode(Input_Push, PIN_MODE_OUTPUT);
rt_pin_write(Input_Push, PIN_LOW);
rt_thread_t tid = RT_NULL;
tid = rt_thread_create("ecap_test_thd", ecap_thread_entry, RT_NULL, 2048, 12, 10);
if (tid != RT_NULL)
{
rt_thread_startup(tid);
}
else
{
return RT_ERROR;
}
return RT_EOK;
}
//INIT_APP_EXPORT(run_qei);
MSH_CMD_EXPORT(run_ecap0 , ecap0 test );
编译通过后下载到板上;
打开
终端联机工具Tera Term 输入run_ecap0;
如图
实现了通过中断函数调用CallBack函数返回捕捉到的输入电平状态;
五、测评总结
1、ECAP模块 作为 “Miscellaneous Device” 个人感觉对ECAP还不熟悉;
2、发现了一个BUG:Dev_ecap.c ->void ECAP0_IRQHandler(void) 以下是修改过来 if (NU_ECAP_GET_LEVEL(u32Status, 0)) { nu_ecap0_capture[0]->input_data_level = RT_TRUE; } else { nu_ecap0_capture[0]->input_data_level = RT_FALSE; }
原作者:颜旭涛
|