⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wdg.c

📁 arm(str710)usb功能的实现
💻 C
字号:
/****************************************Copyright (c)**************************************************
**                                      
**                                          STR710 development team
**                                          
**
**                                 http://www.appchip.com
**
**--------------文件信息--------------------------------------------------------------------------------
** 文 件 名: wdg.c
** 创 建 人: lhl
** 创建日期: 2006年5月10日
** 描    述: 该文件包含了WDG相关功能函数
**
**--------------历史版本--------------------------------------------------------------------------------
** 创 建 人: lhl
** 版    本: V1.0
** 日   期: 2006年5月10日
** 描   述: 原始版本
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/

#include "wdg.h"


#if EN_ARM_WDG > 0		//决定是否编译该文件

#ifndef abs
	#define abs(x) ((x)>0 ? (x) : -(x))
#endif

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

/******************************************* 局部函数声明 ***********************************************/

/*********************************************************************************************************
;** 函数名称: WDG_Enable
;** 功能描述: Enable the Watchdog Mode
;** 
;** 参    数: 无
;**
;** 返 回 值: 无
;**         
;** 作   者: lhl
;** 日   期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人: 
;** 日   期: 
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void WDG_Enable(void)
{
	WDG_FeedDogConfig(WDG_PRESCALE_RELOAD_CONVERT_VALUE);
  	WDG_CR = 0x01;
}
/*********************************************************************************************************
;** 函数名称: WDG_TimerStart
;** 功能描述: 使用WDG的定时器功能,根据装载的计数值立即启动定时器
;** 
;** 参    数: 无
;**
;** 返 回 值: 无
;**         
;** 作   者: lhl
;** 日   期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人: 
;** 日   期: 
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void WDG_TMStart(void)
{  
                                  //set prescale and reload counter value
	//WDG_PeriodValueConfig(TIMER_PRESCALE_RELOAD_CONVERT_VALUE);
	WDG_PrescalerConfig(0x0FF);    //根据具体应用设置值
	WDG_CounterReloadUpdate(0xFFE0);
	WDG_ECFlagClear();			  //clear the status of wdg_timer interrupt  ,
	WDG_ECITConfig(ENABLE);		  //enable the interrupt
	WDG_CounterfConfig(ENABLE);	  //start timer,disable WDG.
}

void WDG_TMStop()
{  	
	WDG_PrescalerConfig(0x0000);    //根据具体应用设置值
	WDG_CounterReloadUpdate(0x00ff);//set the wdg unit prescale ,0xff means the max value;
	WDG_ECFlagClear();			    //clear the status of wdg_timer interrupt  ,
	WDG_ECITConfig(DISABLE);		//Disable the interrupt
	WDG_CounterfConfig(DISABLE);	//stop timer,disable WDG.
	
}

/*********************************************************************************************************
;** 函数名称: WDG_FindFactors
;** 功能描述: Search for the best (a,b) values that fit n = a*b with the following constraints: 1<=a<=256, 1<=b<=65536 
;** 
;** 参    数: n: the number to decompose
;**           a: a pointer to the first factor
;**           b: a pointer to the second factor
;**
;** 返 回 值: 无
;**         
;** 作   者: lhl
;** 日   期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人: 
;** 日   期: 
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
static void WDG_FindFactors(UWORD32 n, UWORD16 *a, UWORD32 *b)
{
	UWORD32 b0;
	UWORD16 a0;
	
	WORD32 err, err_min=n;

	*a = a0 = ((n-1)/65536ul) + 1;
	*b = b0 = n / *a;

	for (; *a <= 256; (*a)++)
	{
		*b = n / *a;
		err = (WORD32)*a * (WORD32)*b - (WORD32)n;
		if (abs(err) > (*a / 2))
		{
			(*b)++;
			err = (WORD32)*a * (WORD32)*b - (WORD32)n;
		}
		if (abs(err) < abs(err_min))
		{
			err_min = err;
			a0 = *a;
			b0 = *b;
			if (err == 0) break;
		}
	}

	*a = a0;
	*b = b0;
}

/*********************************************************************************************************
;** 函数名称: WDG_PeriodValueConfig
;** 功能描述: 根据定时时间设置prescaler和counter reload 值
;** 
;** 参    数: time:Amount of time (us) needed
;**
;** 返 回 值: 无
;**         
;** 作   者: lhl
;** 日   期: 2006年5月10日
;**-------------------------------------------------------------------------------------------------------
;** 修 改 人: 
;** 日   期: 
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
void WDG_FeedDogConfig (UWORD32 time)
{
	UWORD16 a;
	UWORD32 n, b;

	n = time * (RCCU_AnyClockObtain(RCCU_PCLK) / 1000000);
	WDG_FindFactors(n, &a, &b);
	
    WDG_PR = a - 1;
    WDG_VR = b - 1;
}

#endif  //EN_ARM_WDG > 0

/****************************************end of file*******************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -