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
|