2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 文件名称: SWM320_timr.c
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 功能说明: SWM320单片机的计数器/定时器功能驱动库
|
|
|
|
|
* 技术支持: http://www.synwit.com.cn/e/tool/gbook/?bid=1
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 注意事项:
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 版本日期: V1.1.0 2017年10月25日
|
|
|
|
|
* 升级记录:
|
2021-02-18 13:29:12 +08:00
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*******************************************************************************************************************************************
|
|
|
|
|
* @attention
|
|
|
|
|
*
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
|
|
|
|
|
* REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
|
2021-02-18 13:29:12 +08:00
|
|
|
|
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
|
|
|
|
|
* -ECTION WITH THEIR PRODUCTS.
|
|
|
|
|
*
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* COPYRIGHT 2012 Synwit Technology
|
2021-02-18 13:29:12 +08:00
|
|
|
|
*******************************************************************************************************************************************/
|
|
|
|
|
#include "SWM320.h"
|
|
|
|
|
#include "SWM320_timr.h"
|
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 函数名称: TIMR_Init()
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 功能说明: TIMR定时器/计数器初始化
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,有效值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
|
|
|
|
* uint32_t mode TIMR_MODE_TIMER 定时器模式 TIMR_MODE_COUNTER 计数器模式
|
|
|
|
|
* uint32_t period 定时/计数周期
|
|
|
|
|
* uint32_t int_en 中断使能
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
void TIMR_Init(TIMR_TypeDef * TIMRx, uint32_t mode, uint32_t period, uint32_t int_en)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2021-05-06 10:42:41 +08:00
|
|
|
|
SYS->CLKEN |= (0x01 << SYS_CLKEN_TIMR_Pos);
|
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
TIMR_Stop(TIMRx); //一些关键寄存器只能在定时器停止时设置
|
2021-05-06 10:42:41 +08:00
|
|
|
|
|
|
|
|
|
TIMRx->CTRL &= ~TIMR_CTRL_CLKSRC_Msk;
|
|
|
|
|
TIMRx->CTRL |= mode << TIMR_CTRL_CLKSRC_Pos;
|
|
|
|
|
|
|
|
|
|
TIMRx->LDVAL = period;
|
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
switch((uint32_t)TIMRx)
|
2021-05-06 10:42:41 +08:00
|
|
|
|
{
|
|
|
|
|
case ((uint32_t)TIMR0):
|
2022-08-17 00:43:24 +08:00
|
|
|
|
TIMRG->IF = (1 << TIMRG_IF_TIMR0_Pos); //使能中断前清除中断标志
|
2021-05-06 10:42:41 +08:00
|
|
|
|
TIMRG->IE &= ~TIMRG_IE_TIMR0_Msk;
|
|
|
|
|
TIMRG->IE |= (int_en << TIMRG_IE_TIMR0_Pos);
|
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
if(int_en) NVIC_EnableIRQ(TIMR0_IRQn);
|
2021-05-06 10:42:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR1):
|
|
|
|
|
TIMRG->IF = (1 << TIMRG_IF_TIMR1_Pos);
|
|
|
|
|
TIMRG->IE &= ~TIMRG_IE_TIMR1_Msk;
|
|
|
|
|
TIMRG->IE |= (int_en << TIMRG_IE_TIMR1_Pos);
|
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
if(int_en) NVIC_EnableIRQ(TIMR1_IRQn);
|
2021-05-06 10:42:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR2):
|
|
|
|
|
TIMRG->IF = (1 << TIMRG_IF_TIMR2_Pos);
|
|
|
|
|
TIMRG->IE &= ~TIMRG_IE_TIMR2_Msk;
|
|
|
|
|
TIMRG->IE |= (int_en << TIMRG_IE_TIMR2_Pos);
|
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
if(int_en) NVIC_EnableIRQ(TIMR2_IRQn);
|
2021-05-06 10:42:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR3):
|
|
|
|
|
TIMRG->IF = (1 << TIMRG_IF_TIMR3_Pos);
|
|
|
|
|
TIMRG->IE &= ~TIMRG_IE_TIMR3_Msk;
|
|
|
|
|
TIMRG->IE |= (int_en << TIMRG_IE_TIMR3_Pos);
|
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
if(int_en) NVIC_EnableIRQ(TIMR3_IRQn);
|
2021-05-06 10:42:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR4):
|
|
|
|
|
TIMRG->IF = (1 << TIMRG_IF_TIMR4_Pos);
|
|
|
|
|
TIMRG->IE &= ~TIMRG_IE_TIMR4_Msk;
|
|
|
|
|
TIMRG->IE |= (int_en << TIMRG_IE_TIMR4_Pos);
|
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
if(int_en) NVIC_EnableIRQ(TIMR4_IRQn);
|
2021-05-06 10:42:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR5):
|
|
|
|
|
TIMRG->IF = (1 << TIMRG_IF_TIMR5_Pos);
|
|
|
|
|
TIMRG->IE &= ~TIMRG_IE_TIMR5_Msk;
|
|
|
|
|
TIMRG->IE |= (int_en << TIMRG_IE_TIMR5_Pos);
|
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
if(int_en) NVIC_EnableIRQ(TIMR5_IRQn);
|
2021-05-06 10:42:41 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
|
|
|
|
* 函数名称: TIMR_Start()
|
|
|
|
|
* 功能说明: 启动定时器,从初始值开始计时/计数
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
void TIMR_Start(TIMR_TypeDef * TIMRx)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2021-05-06 10:42:41 +08:00
|
|
|
|
TIMRx->CTRL |= TIMR_CTRL_EN_Msk;
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
|
|
|
|
* 函数名称: TIMR_Stop()
|
|
|
|
|
* 功能说明: 停止定时器
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
void TIMR_Stop(TIMR_TypeDef * TIMRx)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2021-05-06 10:42:41 +08:00
|
|
|
|
TIMRx->CTRL &= ~TIMR_CTRL_EN_Msk;
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
|
|
|
|
* 函数名称: TIMR_Halt()
|
|
|
|
|
* 功能说明: 暂停定时器,计数值保持不变
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
void TIMR_Halt(TIMR_TypeDef * TIMRx)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2022-08-17 00:43:24 +08:00
|
|
|
|
switch((uint32_t)TIMRx)
|
2021-05-06 10:42:41 +08:00
|
|
|
|
{
|
|
|
|
|
case ((uint32_t)TIMR0):
|
|
|
|
|
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR0_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR1):
|
|
|
|
|
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR1_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR2):
|
|
|
|
|
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR2_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR3):
|
|
|
|
|
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR3_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR4):
|
|
|
|
|
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR4_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR5):
|
|
|
|
|
TIMRG->HALT |= (0x01 << TIMRG_HALT_TIMR5_Pos);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
|
|
|
|
* 函数名称: TIMR_Resume()
|
|
|
|
|
* 功能说明: 恢复定时器,从暂停处继续计数
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
void TIMR_Resume(TIMR_TypeDef * TIMRx)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2022-08-17 00:43:24 +08:00
|
|
|
|
switch((uint32_t)TIMRx)
|
2021-05-06 10:42:41 +08:00
|
|
|
|
{
|
|
|
|
|
case ((uint32_t)TIMR0):
|
|
|
|
|
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR0_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR1):
|
|
|
|
|
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR1_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR2):
|
|
|
|
|
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR2_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR3):
|
|
|
|
|
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR3_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR4):
|
|
|
|
|
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR4_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR5):
|
|
|
|
|
TIMRG->HALT &= ~(0x01 << TIMRG_HALT_TIMR5_Pos);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 函数名称: TIMR_SetPeriod()
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 功能说明: 设置定时/计数周期
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
|
|
|
|
* uint32_t period 定时/计数周期
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
void TIMR_SetPeriod(TIMR_TypeDef * TIMRx, uint32_t period)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2021-05-06 10:42:41 +08:00
|
|
|
|
TIMRx->LDVAL = period;
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 函数名称: TIMR_GetPeriod()
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 功能说明: 获取定时/计数周期
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
|
|
|
|
* 输 出: uint32_t 当前定时/计数周期
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
uint32_t TIMR_GetPeriod(TIMR_TypeDef * TIMRx)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2021-05-06 10:42:41 +08:00
|
|
|
|
return TIMRx->LDVAL;
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
|
|
|
|
* 函数名称: TIMR_GetCurValue()
|
|
|
|
|
* 功能说明: 获取当前计数值
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
|
|
|
|
* 输 出: uint32_t 当前计数值
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
uint32_t TIMR_GetCurValue(TIMR_TypeDef * TIMRx)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2021-05-06 10:42:41 +08:00
|
|
|
|
return TIMRx->CVAL;
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
|
|
|
|
* 函数名称: TIMR_INTEn()
|
|
|
|
|
* 功能说明: 使能中断
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
void TIMR_INTEn(TIMR_TypeDef * TIMRx)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2022-08-17 00:43:24 +08:00
|
|
|
|
switch((uint32_t)TIMRx)
|
2021-05-06 10:42:41 +08:00
|
|
|
|
{
|
|
|
|
|
case ((uint32_t)TIMR0):
|
|
|
|
|
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR0_Pos);
|
|
|
|
|
NVIC_EnableIRQ(TIMR0_IRQn);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR1):
|
|
|
|
|
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR1_Pos);
|
|
|
|
|
NVIC_EnableIRQ(TIMR1_IRQn);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR2):
|
|
|
|
|
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR2_Pos);
|
|
|
|
|
NVIC_EnableIRQ(TIMR2_IRQn);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR3):
|
|
|
|
|
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR3_Pos);
|
|
|
|
|
NVIC_EnableIRQ(TIMR3_IRQn);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR4):
|
|
|
|
|
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR4_Pos);
|
|
|
|
|
NVIC_EnableIRQ(TIMR4_IRQn);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR5):
|
|
|
|
|
TIMRG->IE |= (0x01 << TIMRG_IE_TIMR5_Pos);
|
|
|
|
|
NVIC_EnableIRQ(TIMR5_IRQn);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 函数名称: TIMR_INTDis()
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 功能说明: 禁能中断
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
void TIMR_INTDis(TIMR_TypeDef * TIMRx)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2022-08-17 00:43:24 +08:00
|
|
|
|
switch((uint32_t)TIMRx)
|
2021-05-06 10:42:41 +08:00
|
|
|
|
{
|
|
|
|
|
case ((uint32_t)TIMR0):
|
|
|
|
|
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR0_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR1):
|
|
|
|
|
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR1_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR2):
|
|
|
|
|
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR2_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR3):
|
|
|
|
|
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR3_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR4):
|
|
|
|
|
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR4_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR5):
|
|
|
|
|
TIMRG->IE &= ~(0x01 << TIMRG_IE_TIMR5_Pos);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
|
|
|
|
* 函数名称: TIMR_INTClr()
|
|
|
|
|
* 功能说明: 清除中断标志
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
void TIMR_INTClr(TIMR_TypeDef * TIMRx)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2022-08-17 00:43:24 +08:00
|
|
|
|
switch((uint32_t)TIMRx)
|
2021-05-06 10:42:41 +08:00
|
|
|
|
{
|
|
|
|
|
case ((uint32_t)TIMR0):
|
|
|
|
|
TIMRG->IF = (0x01 << TIMRG_IF_TIMR0_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR1):
|
|
|
|
|
TIMRG->IF = (0x01 << TIMRG_IF_TIMR1_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR2):
|
|
|
|
|
TIMRG->IF = (0x01 << TIMRG_IF_TIMR2_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR3):
|
|
|
|
|
TIMRG->IF = (0x01 << TIMRG_IF_TIMR3_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR4):
|
|
|
|
|
TIMRG->IF = (0x01 << TIMRG_IF_TIMR4_Pos);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR5):
|
|
|
|
|
TIMRG->IF = (0x01 << TIMRG_IF_TIMR5_Pos);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 函数名称: TIMR_INTStat()
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 功能说明: 获取中断状态
|
|
|
|
|
* 输 入: TIMR_TypeDef * TIMRx 指定要被设置的定时器,可取值包括TIMR0、TIMR1、TIMR2、TIMR3、TIMR4、TIMR5
|
|
|
|
|
* 输 出: uint32_t 0 TIMRx未产生中断 1 TIMRx产生了中断
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
2022-08-17 00:43:24 +08:00
|
|
|
|
uint32_t TIMR_INTStat(TIMR_TypeDef * TIMRx)
|
2021-02-18 13:29:12 +08:00
|
|
|
|
{
|
2022-08-17 00:43:24 +08:00
|
|
|
|
switch((uint32_t)TIMRx)
|
2021-05-06 10:42:41 +08:00
|
|
|
|
{
|
|
|
|
|
case ((uint32_t)TIMR0):
|
|
|
|
|
return (TIMRG->IF & TIMRG_IF_TIMR0_Msk) ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR1):
|
|
|
|
|
return (TIMRG->IF & TIMRG_IF_TIMR1_Msk) ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR2):
|
|
|
|
|
return (TIMRG->IF & TIMRG_IF_TIMR2_Msk) ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR3):
|
|
|
|
|
return (TIMRG->IF & TIMRG_IF_TIMR3_Msk) ? 1 : 0;
|
2021-02-18 13:29:12 +08:00
|
|
|
|
|
2021-05-06 10:42:41 +08:00
|
|
|
|
case ((uint32_t)TIMR4):
|
|
|
|
|
return (TIMRG->IF & TIMRG_IF_TIMR4_Msk) ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
case ((uint32_t)TIMR5):
|
|
|
|
|
return (TIMRG->IF & TIMRG_IF_TIMR5_Msk) ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-02-18 13:29:12 +08:00
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 函数名称: Pulse_Init()
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 功能说明: 脉宽测量功能初始化
|
|
|
|
|
* 输 入: uint32_t pulse PULSE_LOW 测量低脉冲宽度 PULSE_HIGH 测量高脉冲宽度
|
|
|
|
|
* uint32_t int_en 是否使能脉冲测量完成中断
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
|
|
|
|
void Pulse_Init(uint32_t pulse, uint32_t int_en)
|
2021-05-06 10:42:41 +08:00
|
|
|
|
{
|
|
|
|
|
SYS->CLKEN |= (0x01 << SYS_CLKEN_TIMR_Pos);
|
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
TIMRG->PCTRL = (0 << TIMRG_PCTRL_CLKSRC_Pos) | // 系统时钟作为时钟源
|
|
|
|
|
(pulse << TIMRG_PCTRL_HIGH_Pos) |
|
|
|
|
|
(0 << TIMRG_PCTRL_EN_Pos);
|
2021-05-06 10:42:41 +08:00
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
TIMRG->IE |= (1 << TIMRG_IE_PULSE_Pos); //使能才能查询中断标志
|
2021-05-06 10:42:41 +08:00
|
|
|
|
|
2022-08-17 00:43:24 +08:00
|
|
|
|
if(int_en) NVIC_EnableIRQ(PULSE_IRQn);
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 函数名称: Pulse_Start()
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 功能说明: 脉宽测量功能启动,测量到脉宽后会自动关闭测量功能
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 入: 无
|
|
|
|
|
* 输 出: 无
|
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
|
|
|
|
void Pulse_Start(void)
|
2021-05-06 10:42:41 +08:00
|
|
|
|
{
|
|
|
|
|
TIMRG->PCTRL |= (1 << TIMRG_PCTRL_EN_Pos);
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 21:04:42 +08:00
|
|
|
|
/******************************************************************************************************************************************
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 函数名称: Pulse_Done()
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 功能说明: 脉宽测量是否完成
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 输 入: 无
|
2021-09-13 21:04:42 +08:00
|
|
|
|
* 输 出: uint32_t 1 测量已完成 0 测量未完成
|
2021-05-06 10:10:29 +08:00
|
|
|
|
* 注意事项: 无
|
2021-02-18 13:29:12 +08:00
|
|
|
|
******************************************************************************************************************************************/
|
|
|
|
|
uint32_t Pulse_Done(void)
|
|
|
|
|
{
|
2022-08-17 00:43:24 +08:00
|
|
|
|
if(TIMRG->IF & TIMRG_IF_PULSE_Msk)
|
2021-05-06 10:42:41 +08:00
|
|
|
|
{
|
2022-08-17 00:43:24 +08:00
|
|
|
|
TIMRG->IF = TIMRG_IF_PULSE_Msk; // 清除中断标志
|
2021-05-06 10:42:41 +08:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-02-18 13:29:12 +08:00
|
|
|
|
}
|