📄 timer.c
字号:
puiBaseAddr[__B_TIMER_PR << uiOffsetBase] = uiParam[MatPR]; /* 定时器分频系数设置 */
uiTime = (uint32)(((Fpclk / 1000000) * uiParam[MatTime]) / (uiParam[MatPR] + 1));
/* 匹配MRx 寄存器的数值 */
/*
* 捕获通道部分相关设置 & 匹配值设置
*/
if (uiParam[T_C] == 0) { /* 如果是内部定时方式 */
puiBaseAddr[__B_TIMER_CTCR << uiOffsetBase] = 0x00; /* 定时器初始化为定时方式 */
puiBaseAddr[(__B_TIMER_MR0 + uiParam[CountN]) << uiOffsetBase] = uiTime;
/* 设置对应通道的MRx 匹配值 */
} else { /* 如果是外部计数方式 */
puiBaseAddr[__B_TIMER_CTCR << uiOffsetBase] &= (~(0x03 << 0)
/* CTCR 寄存器捕获配置位清零 */
| (0x03 << 2));
/* 采样通道配置位清零 */
puiBaseAddr[__B_TIMER_CTCR << uiOffsetBase] |= ((uiParam[CountMode] + 1) << 0)
/* CTCR 寄存器采样边沿模式配置 */
| (uiParam[CountN] << 2);
/* CTCR 寄存器采样通道位配置 */
puiBaseAddr[__B_TIMER_CCR << uiOffsetBase] &= (~(0x03 << (uiParam[CountN] * 3)));
/* CCR 寄存器采样通道位清零 */
puiBaseAddr[(__B_TIMER_MR0 + uiParam[CountN]) << uiOffsetBase] = uiParam[MatTime];
/* 设置对应通道的MRx 匹配值 */
}
/*
* 匹配通道输出部分设置
*/
puiBaseAddr[__B_TIMER_MCR << uiOffsetBase] &= (~(0x07 << (uiParam[MatN] * 3)));
/* MCR 寄存器匹配控制位清零 */
puiBaseAddr[__B_TIMER_MCR << uiOffsetBase] |= (uiParam[MatCtrlMode] << ((uiParam[MatN] * 3) + 1))
/* 匹配产生时,复位/停止定时器 */
| (uiParam[MatInt] << (uiParam[MatN] * 3));
/* 匹配产生时,是否选择匹配中断 */
puiBaseAddr[__B_TIMER_EMR << uiOffsetBase] &= (~(0x03 << ((uiParam[MatN] * 2) + 4)));
/* EMR 寄存器匹配输出控制位清零*/
puiBaseAddr[__B_TIMER_EMR << uiOffsetBase] |= (uiParam[MatOut] << ((uiParam[MatN] * 2) + 4));
/* 匹配产生时,匹配引脚输出模式 */
} else {
return (uint32)(OPERATE_FAIL); /* 参数错误返回 */
}
#if __TIMER_MAT_GPIO > 0
/*
* 调用匹配引脚设置内部函数
*/
return (__timeMatGpioInit (GpTimerStruInfoData[uiID], uiParam[MatN],NULL));
#else
return (uint32)(OPERATE_SUCCESS);
#endif
}
/*********************************************************************************************************
** Function name: __timerCapGpioInit
**
** Descriptions: 定时器捕获引脚初始化
**
** input parameters: pTimerInfo -- 定时器设备描述符结构体指针
** uiCapN -- 捕获通道号.如0,1,2,3.
** pRsv -- 保留指针参数,可输入NULL.
** Returned value: OPERATE_SUCCESS 操作正确
** OPERATE_FAIL 操作错误
**
** NOTICE : 此函数由于涉及到具体不同ARM处理器芯片的硬件配置,不一定适用于所有LPC2000/2200系列.
** 用户须确认所用芯片为:LPC2290, LPC2294 才能确定使用此配置函数!!
*********************************************************************************************************/
#if __TIMER_CAP_GPIO > 0
static uint32 __timerCapGpioInit (__PTIMER_INFO pTimerInfo, uint32 uiCapN, void *pRsv)
{
volatile uint32 uiGpioOffset;
volatile uint32 *puiGpioAddr;
if (pTimerInfo == NULL) { /* 检查结构体指针是否有效 */
return (uint32)(OPERATE_FAIL);
}
/*
* 管脚功能选择寄存器基址设置
*/
puiGpioAddr =(uint32*) __GuiTPinBaseAddrTab[0]; /* 此处获取PINSEL寄存器的基址 */
uiGpioOffset = 0;
/*
* 选择定时器器件编号.
*/
if (pTimerInfo->uiTimerID == TIME_0) {
switch (uiCapN) {
case 0:
puiGpioAddr[__B_TIMER_PINSEL0 << uiGpioOffset] &= ~(0x03 << 4 );
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL0 << uiGpioOffset] |= (0x02 << 4 );
/* 设置P0_2 引脚为 捕获0 功能 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] &= ~(0x03 << 12);
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] |= (0x02 << 12);
/* 设置P0_22 引脚为 捕获0 功能 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] &= ~(0x03 << 28);
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] |= (0x03 << 28);
/* 设置P0_30 引脚为 捕获0 功能 */
break;
case 1:
puiGpioAddr[__B_TIMER_PINSEL0 << uiGpioOffset] &= ~(0x03 << 8 );
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL0 << uiGpioOffset] |= (0x02 << 8 );
/* 设置P0_4 引脚为 捕获1 功能 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] &= ~(0x03 << 22);
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] |= (0x02 << 22);
/* 设置P0_27 引脚为 捕获1 功能 */
break;
case 2:
puiGpioAddr[__B_TIMER_PINSEL0 << uiGpioOffset] &= ~(0x03 << 12);
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL0 << uiGpioOffset] |= (0x03 << 12);
/* 设置P0_6 引脚为 捕获2 功能 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] &= ~(0x03 << 0 );
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] |= (0x03 << 0 );
/* 设置P0_16 引脚为 捕获2 功能 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] &= ~(0x03 << 24);
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] |= (0x02 << 24);
/* 设置P0_28 引脚为 捕获2 功能 */
break;
case 3:
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] &= ~(0x03 << 26);
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] |= (0x02 << 26);
/* 设置P0_29 引脚为 捕获3 功能 */
break;
default:
{ return (uint32)(PIN_ERR); } /* 通道参数错误返回 */
}
} else {
switch (uiCapN) {
case 0:
puiGpioAddr[__B_TIMER_PINSEL0 << uiGpioOffset] &= ~(0x03 << 20);
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL0 << uiGpioOffset] |= (0x02 << 20);
/* 设置P0_10 引脚为 捕获0 功能 */
break;
case 1:
puiGpioAddr[__B_TIMER_PINSEL0 << uiGpioOffset] &= ~(0x03 << 22);
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL0 << uiGpioOffset] |= (0x02 << 22);
/* 设置P0_11 引脚为 捕获1 功能 */
break;
case 2:
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] &= ~(0x03 << 2 );
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] |= (0x02 << 2 );
/* 设置P0_17 引脚为 捕获2 功能 */
break;
case 3:
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] &= ~(0x03 << 4 );
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] |= (0x02 << 4 );
/* 设置P0_18 引脚为 捕获3 功能 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] &= ~(0x03 << 10);
/* 屏蔽相关的引脚设置位,位清零 */
puiGpioAddr[__B_TIMER_PINSEL1 << uiGpioOffset] |= (0x03 << 10);
/* 设置P0_21 引脚为 捕获3 功能 */
break;
default:
{ return (uint32)(PIN_ERR); } /* 通道参数错误返回 */
}
}
return (uint32)(OPERATE_SUCCESS);
}
#endif
/*********************************************************************************************************
** Function name: timerCapInit
** Descriptions: 定时器捕获功能初始化
**
** Input parameters: uiID -- 部件设备号:0-Time0;1-Time1
** pcArg-- 字符串输入指针参数,输入参数格式为:关键字0=值0[空格]关键字1=值1[空格]......
** 关键字 CapN: 捕获通道选择,0~3分别表示通道0至通道3
** CapMode:捕获控制模式,0表示上升沿捕获,
** 1表示下降沿捕获,
** 2表示上下沿都可以捕获
** CapInt: 中断使能,0表示捕获时不产生中断,1表示捕获时产生中断
** pRsv --保留指针参数,可输入NULL.
** Returned value: OPERATE_FAIL -- 操作失败
** OPERATE_SUCCESS -- 操作成功
** Using example: char pcArg[] = "CapN=2 CapMode=1 CapInt=0";
** timeCapInit( 0, pcArg, NULL );
** Notice: 设备操作失败的原因是输入不正确的定时器号或输入不正确的参数
**********************************************************************************************************/
uint32 timerCapInit (uint32 uiID,
char *pcArg,
void *pRsv)
{
volatile uint32 uiOffsetBase;
volatile uint32 *puiBaseAddr;
uint32 uiParam[3]={0,1,1};
if ((uiID >= __TIMER_MAX_NUM) | (pcArg == NULL)) { /* 检查参数有效性 */
return (uint32)(OPERATE_FAIL);
}
pRsv = pRsv ; /* 防止编译器警告 */
ImpCmd(Tab_CAP, pcArg, uiParam); /* 获取字符参数 */
/*
* 参数过滤处理
*/
if ((uiParam[CapN] < 4) &&
(uiParam[CapMode] < 3) &&
(uiParam[CapInt] < 2)) {
/*
* 初始化器件结构体
*/
__timerInit(uiID,pRsv);
IRQDisable(); /* 关IRQ中断 */
GpTimerStruInfoData[uiID]->uiTimerFun[__TIMER_CAP] = 1; /* 标志使用了捕获功能 */
uiOffsetBase = GpTimerStruInfoData[uiID]->uiOffsetBase; /* 必须在内部初始化后才赋地址值*/
puiBaseAddr = GpTimerStruInfoData[uiID]->puiAddrBase;
IRQEnable(); /* 开IRQ中断 */
/*
* 捕获功能设置
*/
puiBaseAddr[__B_TIMER_CCR << uiOffsetBase] &= (~(0x07 << uiParam[CapN]));
/* CCR对应通道参数设置位清零 */
puiBaseAddr[__B_TIMER_CCR << uiOffsetBase] |= (uiParam[CapInt] << ((uiParam[CapN] * 3) + 2));
/* 选择捕获中断 */
switch (uiParam[CapMode]) {
case __TIME_UP:
puiBaseAddr[__B_TIMER_CCR << uiOffsetBase] |= (0x01 << ((uiParam[CapN] * 3) + 0));
break;
case __TIME_DOWN:
puiBaseAddr[__B_TIMER_CCR << uiOffsetBase] |= (0x01 << ((uiParam[CapN] * 3) + 1));
break;
case __TIME_DOUBLE:
puiBaseAddr[__B_TIMER_CCR << uiOffsetBase] |= (0x03 << ((uiParam[CapN] * 3) + 0));
break;
default:
return (uint32)(OPERATE_FAIL); /* 返回参数错误 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -