[学习笔记]STM8L05x基于BasicTimer(TIM4)实现1ms滴答定时器

目的:因为STM8L系列没有滴答定时器,所以不能够利用滴答定时器来实现delay处理。

           但是STM8L系列有basci timer,可以利用它来实现滴答定时器,进而实现delay处理接口。

具体实现接口如下:

drv_timer.c

/*****************************************************************************************
 * Confidential and Proprietary Information of xxx Corporation
 * (C) 2019 ,xxx Corporation . All rights reserved.
 * file :    drv_timer.c
 * brief :  .c files
 * History:    Author        Version          ChangeContent            Date
 *               xxx                           NewFile             2019.04.24
 *****************************************************************************************/

/**********************************************
* 头文件引用
**********************************************/
#include "drv_timer.h"


/**********************************************
 * 静态全局变量定义
**********************************************/
static u16 m_baseTimerCnt = 0u;


/**********************************************
 *全局变量定义
**********************************************/



/**********************************************
 * 内部函数声明
 **********************************************/ 


/**********************************************
 * 全局函数实现体
 **********************************************/ 
 /****************************************************************************************
 * FunctionName   : drvTimer4_init
 * Abstract       : Init base time.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2019/04/28 , xxx New
 * History        :
****************************************************************************************/
void drvTimer4_init(void)
{
	//-------打开TIM4外设时钟-------
	//打开定时器4时钟
	CLK_PCKENR1_PCKEN12 = 1u;	
	
	//----禁用预装载寄存器----
	//不经过缓存,分频值和重装值立即被写入.
	//除非需要频繁的在定时器运行时, 
	//改变分频值和重装值,否则没必要使用预装载寄存器
	TIM4_CR1_ARPE = 0u;

	//------设置TIM4时钟分频值------
	//分频值  2M/2^3=2M/8 = 250000Hz
	TIM4_PSCR_PSC = 3u;
  

	//-重装值,TIM4从0计数到此值,发生溢出-
	//自动重装值    250000Hz/250=1000HZ
	TIM4_ARR = 250u;

	//TIM4定时器每隔1ms进入一次中断

	//仅当计数器溢出时才发生中断请求
	TIM4_CR1_URS = 1u;
	//允许更新中断
	TIM4_CR1_UDIS = 0u;

	//开启计数器
	TIM4_CR1_CEN = 1u;
	
	// update interrupt enable
	TIM4_IER_UIE = 1u;
}

/****************************************************************************************
 * FunctionName   : drvBaseTimerGet
 * Abstract       : Get current base time.
 * Argument1(in)  : void
 * Argument2(out) : 
 * Return Value   : u16
 * Remarks        :
 * Create         : 2019/04/28 , xxx New
 * History        :
****************************************************************************************/
u16 drvBaseTimerGet(void)
{
	return m_baseTimerCnt;
}


#pragma vector = TIM4_UIF_vector
__interrupt void TIM4_UIF_ISR (void)
{
   /* User operation */
	m_baseTimerCnt++;
   
	//清除中断标志位
	TIM4_SR1_UIF = 0;
}

drv_timer.h

/*****************************************************************************************
 * Confidential and Proprietary Information of xxx Corporation
 * (C) 2019 ,xxx Corporation . All rights reserved.
 * file :    drv_timer.h
 * brief :  .h files
 * History:    Author        Version          ChangeContent            Date
 *               xxx                             NewFile             2019.04.28
 *****************************************************************************************/

#ifndef __DRV_TIMER_H__
#define __DRV_TIMER_H__

/**********************************************
* 头文件引用
**********************************************/
#include "../typedef.h"

/**********************************************
 * 宏定义
**********************************************/



/**********************************************
 * 枚举型定义
**********************************************/


/**********************************************
 * 全局函数声明
**********************************************/
extern void drvTimer4_init(void);
extern u16 drvBaseTimerGet(void);

#endif

sys_delay.c

/*****************************************************************************************
 * Confidential and Proprietary Information of xxx  Corporation
 * (C) 2019 ,xxx Corporation . All rights reserved.
 * file :    sys_delay.c
 * brief :  .c files
 * History:    Author        Version          ChangeContent            Date
 *               xx                              NewFile             2019.04.28
 *****************************************************************************************/

/**********************************************
* 头文件引用
**********************************************/
#include "sys_delay.h"
#include "./drivers/drv_timer.h"


/**********************************************
 * 静态全局变量定义
**********************************************/



/**********************************************
 *全局变量定义
**********************************************/



/**********************************************
 * 内部函数声明
 **********************************************/ 


/**********************************************
 * 全局函数实现体
 **********************************************/ 
 /****************************************************************************************
 * FunctionName   : sys_delay
 * Abstract       : system delay function.
 * Argument1(in)  : u16
 * Argument2(out) : 
 * Return Value   : void
 * Remarks        :
 * Create         : 2019/04/28 , xxx  New
 * History        :
****************************************************************************************/
void sys_delay( u16 delay_ms )
{
	u16 cur_baseTime = 0;

	cur_baseTime = drvBaseTimerGet();

	while(1){
		if( drvBaseTimerGet() < cur_baseTime ){
			if( (drvBaseTimerGet() + 65535 - cur_baseTime) >= delay_ms )
				break;
		}else{
			if( (drvBaseTimerGet() - cur_baseTime) >= delay_ms )
				break;
		}
	}
}

sys_delay.h

/*****************************************************************************************
 * Confidential and Proprietary Information of xxx Corporation
 * (C) 2019 ,xxx Corporation . All rights reserved.
 * file :    drv_timer.h
 * brief :  .h files
 * History:    Author        Version          ChangeContent            Date
 *               xxx                              NewFile             2019.04.28
 *****************************************************************************************/

#ifndef __SYS_DELAY_H__
#define __SYS_DELAY_H__

/**********************************************
* 头文件引用
**********************************************/
#include "typedef.h"

/**********************************************
 * 宏定义
**********************************************/



/**********************************************
 * 枚举型定义
**********************************************/


/**********************************************
 * 全局函数声明
**********************************************/
extern  void sys_delay( u16 delay_ms );

#endif

猜你喜欢

转载自blog.csdn.net/zgp2917/article/details/89638460
今日推荐