找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1416|回复: 0
打印 上一主题 下一主题
收起左侧

stm8051F3独立看门狗的使用源程序

[复制链接]
跳转到指定楼层
楼主
ID:537075 发表于 2019-5-13 16:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
stm8051F3独立看门狗的使用,对初学者很实用。

单片机源程序如下:
  1. #include "stm8l15x.h"//STM8L051/151等系列共用库函数

  2. //定义LED端口
  3. #define LED1_PORT  GPIOD
  4. #define LED1_PINS  GPIO_Pin_0
  5. #define LED2_PORT  GPIOC
  6. #define LED2_PINS  GPIO_Pin_4
  7. #define LED3_PORT  GPIOB
  8. #define LED3_PINS  GPIO_Pin_2
  9. #define KEY1_PORT  GPIOB
  10. #define KEY1_PINS  GPIO_Pin_1
  11. #define KEY2_PORT  GPIOA
  12. #define KEY2_PINS  GPIO_Pin_2
  13. #define KEY3_PORT  GPIOB
  14. #define KEY3_PINS  GPIO_Pin_3

  15. #define RELOAD_VALUE   255
  16. /*******************************************************************************
  17. ****入口参数:无
  18. ****出口参数:无
  19. ****函数备注:不精确延时函数
  20. ****版权信息:蓝旗嵌入式系统
  21. *******************************************************************************/
  22. void Delay(__IO uint16_t nCount)
  23. {
  24.     /* Decrement nCount value */
  25.     while (nCount != 0)
  26.     {
  27.         nCount--;
  28.     }
  29. }

  30. static void IWDG_Config(void)
  31. {
  32.   //使能IWDG
  33.   IWDG_Enable();
  34.   //解除写保护  
  35.   IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  36.   //LSI驱动IWDG,LSI 256分频=38000/256
  37.   IWDG_SetPrescaler(IWDG_Prescaler_256);
  38.   
  39.   /* IWDG timeout = (RELOAD_VALUE + 1) * Prescaler / LSI
  40.                   = (255 + 1) * 256 / 38 000
  41.                   = 1723.63 ms */
  42.   IWDG_SetReload((uint8_t)RELOAD_VALUE);
  43.   
  44.   /* Reload IWDG counter */
  45.   IWDG_ReloadCounter();
  46. }
  47. /*******************************************************************************
  48. ****函数说明:主函数
  49. ****入口参数:无
  50. ****出口参数:无
  51. ****函数备注: 主函数,软件独立看门狗
  52.               按键触发中断,中断服务程序里面调用软件中断TRAP,TRAP里面是while(1),
  53.               这样就不会喂狗,从而导致IWDG计数器计数到0,引发复位。复位后程序判断
  54.               复位标志是不是IWDG引起的复位,如果是,则点亮LED。
  55. ********************************************************************************/
  56. void main(void)
  57. {  
  58.   CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
  59.   
  60.   GPIO_Init(LED1_PORT,LED1_PINS,GPIO_Mode_Out_PP_High_Slow);//初始化LED端口

  61.   GPIO_Init(KEY1_PORT, KEY1_PINS, GPIO_Mode_In_PU_IT);//初始化按键,上拉输入,带中断

  62.   EXTI_DeInit (); //恢复中断的所有设置
  63.   EXTI_SetPinSensitivity (EXTI_Pin_1,EXTI_Trigger_Falling);//外部中断1,下降沿触发,向量号9
  64.   
  65.   enableInterrupts();//使能中断
  66.   
  67.   
  68.   if(RST_GetFlagStatus(RST_FLAG_IWDGF) != RESET)//判断IWDG复位有没有发生
  69.      {     
  70.         GPIO_ResetBits(LED1_PORT, LED1_PINS);//点亮LED
  71.         //清掉复位标志
  72.         RST_ClearFlag(RST_FLAG_IWDGF);
  73.       }
  74.   else                                         //如果不是IWDG引起的复位
  75.      {
  76.         GPIO_SetBits(LED1_PORT, LED1_PINS);
  77.       }
  78.   
  79.     //配置IWDG
  80.   IWDG_Config();
  81.   
  82.   while (1)
  83.   {  
  84.     IWDG_ReloadCounter(); //喂狗
  85.   }
  86. }








  87. #ifdef  USE_FULL_ASSERT

  88. /**
  89.   * @brief  Reports the name of the source file and the source line number
  90.   *   where the assert_param error has occurred.
  91.   * @param  file: pointer to the source file name
  92.   * @param  line: assert_param error line source number
  93.   * @retval None
  94.   */
  95. void assert_failed(uint8_t* file, uint32_t line)
  96. {
  97.   /* User can add his own implementation to report the file name and line number,
  98.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  99.   /* Infinite loop */
  100.   while (1)
  101.   {
  102.   }
  103. }
  104. #endif

  105. /**
  106.   * @}
  107.   */

  108. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
stn8.IWDG-独立看门狗.7z (5.88 MB, 下载次数: 4)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表