标题:
stm8051F3独立看门狗的使用源程序
[打印本页]
作者:
jijiahui555
时间:
2019-5-13 16:12
标题:
stm8051F3独立看门狗的使用源程序
stm8051F3独立看门狗的使用,对初学者很实用。
单片机源程序如下:
#include "stm8l15x.h"//STM8L051/151等系列共用库函数
//定义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
/*******************************************************************************
****入口参数:无
****出口参数:无
****函数备注:不精确延时函数
****版权信息:蓝旗嵌入式系统
*******************************************************************************/
void Delay(__IO uint16_t nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
static void IWDG_Config(void)
{
//使能IWDG
IWDG_Enable();
//解除写保护
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
//LSI驱动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();
}
/*******************************************************************************
****函数说明:主函数
****入口参数:无
****出口参数:无
****函数备注: 主函数,软件独立看门狗
按键触发中断,中断服务程序里面调用软件中断TRAP,TRAP里面是while(1),
这样就不会喂狗,从而导致IWDG计数器计数到0,引发复位。复位后程序判断
复位标志是不是IWDG引起的复位,如果是,则点亮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 (); //恢复中断的所有设置
EXTI_SetPinSensitivity (EXTI_Pin_1,EXTI_Trigger_Falling);//外部中断1,下降沿触发,向量号9
enableInterrupts();//使能中断
if(RST_GetFlagStatus(RST_FLAG_IWDGF) != RESET)//判断IWDG复位有没有发生
{
GPIO_ResetBits(LED1_PORT, LED1_PINS);//点亮LED
//清掉复位标志
RST_ClearFlag(RST_FLAG_IWDGF);
}
else //如果不是IWDG引起的复位
{
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****/
复制代码
所有资料51hei提供下载:
stn8.IWDG-独立看门狗.7z
(5.88 MB, 下载次数: 4)
2019-5-14 04:41 上传
点击文件名下载附件
stm8051F3独立看门狗
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1