標(biāo)題:
stm8051F3獨(dú)立看門狗的使用源程序
[打印本頁(yè)]
作者:
jijiahui555
時(shí)間:
2019-5-13 16:12
標(biāo)題:
stm8051F3獨(dú)立看門狗的使用源程序
stm8051F3獨(dú)立看門狗的使用,對(duì)初學(xué)者很實(shí)用。
單片機(jī)源程序如下:
#include "stm8l15x.h"//STM8L051/151等系列共用庫(kù)函數(shù)
//定義LED端口
#define LED1_PORT GPIOD
#define LED1_PINS GPIO_Pin_0
#define LED2_PORT GPIOC
#define LED2_PINS GPIO_Pin_4
#define LED3_PORT GPIOB
#define LED3_PINS GPIO_Pin_2
#define KEY1_PORT GPIOB
#define KEY1_PINS GPIO_Pin_1
#define KEY2_PORT GPIOA
#define KEY2_PINS GPIO_Pin_2
#define KEY3_PORT GPIOB
#define KEY3_PINS GPIO_Pin_3
#define RELOAD_VALUE 255
/*******************************************************************************
****入口參數(shù):無(wú)
****出口參數(shù):無(wú)
****函數(shù)備注:不精確延時(shí)函數(shù)
****版權(quán)信息:藍(lán)旗嵌入式系統(tǒng)
*******************************************************************************/
void Delay(__IO uint16_t nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
static void IWDG_Config(void)
{
//使能IWDG
IWDG_Enable();
//解除寫保護(hù)
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
//LSI驅(qū)動(dòng)IWDG,LSI 256分頻=38000/256
IWDG_SetPrescaler(IWDG_Prescaler_256);
/* IWDG timeout = (RELOAD_VALUE + 1) * Prescaler / LSI
= (255 + 1) * 256 / 38 000
= 1723.63 ms */
IWDG_SetReload((uint8_t)RELOAD_VALUE);
/* Reload IWDG counter */
IWDG_ReloadCounter();
}
/*******************************************************************************
****函數(shù)說(shuō)明:主函數(shù)
****入口參數(shù):無(wú)
****出口參數(shù):無(wú)
****函數(shù)備注: 主函數(shù),軟件獨(dú)立看門狗
按鍵觸發(fā)中斷,中斷服務(wù)程序里面調(diào)用軟件中斷TRAP,TRAP里面是while(1),
這樣就不會(huì)喂狗,從而導(dǎo)致IWDG計(jì)數(shù)器計(jì)數(shù)到0,引發(fā)復(fù)位。復(fù)位后程序判斷
復(fù)位標(biāo)志是不是IWDG引起的復(fù)位,如果是,則點(diǎn)亮LED。
********************************************************************************/
void main(void)
{
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
GPIO_Init(LED1_PORT,LED1_PINS,GPIO_Mode_Out_PP_High_Slow);//初始化LED端口
GPIO_Init(KEY1_PORT, KEY1_PINS, GPIO_Mode_In_PU_IT);//初始化按鍵,上拉輸入,帶中斷
EXTI_DeInit (); //恢復(fù)中斷的所有設(shè)置
EXTI_SetPinSensitivity (EXTI_Pin_1,EXTI_Trigger_Falling);//外部中斷1,下降沿觸發(fā),向量號(hào)9
enableInterrupts();//使能中斷
if(RST_GetFlagStatus(RST_FLAG_IWDGF) != RESET)//判斷IWDG復(fù)位有沒(méi)有發(fā)生
{
GPIO_ResetBits(LED1_PORT, LED1_PINS);//點(diǎn)亮LED
//清掉復(fù)位標(biāo)志
RST_ClearFlag(RST_FLAG_IWDGF);
}
else //如果不是IWDG引起的復(fù)位
{
GPIO_SetBits(LED1_PORT, LED1_PINS);
}
//配置IWDG
IWDG_Config();
while (1)
{
IWDG_ReloadCounter(); //喂狗
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
復(fù)制代碼
所有資料51hei提供下載:
stn8.IWDG-獨(dú)立看門狗.7z
(5.88 MB, 下載次數(shù): 4)
2019-5-14 04:41 上傳
點(diǎn)擊文件名下載附件
stm8051F3獨(dú)立看門狗
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1