04. 程序示例
wwdg.h
- #ifndef __WWDG_H__
- #define __WWDG_H__
- #include "sys.h"
- //初始化函数
- void WWDG_Init(u8 tr,u8 wr,u32 fprer);
- //中断处理函数
- void WWDG_IRQHandler(void);
- #endif /*__WWDG_H__*/
复制代码
wwdg.c
- #include "wwdg.h"
- #include "led.h"
- //保存WWDG计数器的设置值,默认为最大.
- u8 WWDG_CNT=0X7F;
- //初始化函数
- //tr计数器的值
- //wr窗口值
- //fprer 预分频值
- void WWDG_Init(u8 tr,u8 wr,u32 fprer)
- {
- NVIC_InitTypeDef NVIC_InitStruct;
- //使能WWDG时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
- //设置预分频
- WWDG_SetPrescaler(fprer);
- //设置窗口值
- WWDG_SetWindowValue(wr & WWDG_CNT);
- //使能看门狗
- WWDG_Enable(WWDG_CNT);
- //设置中断
- NVIC_InitStruct.NVIC_IRQChannel = WWDG_IRQn;
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
- NVIC_Init(&NVIC_InitStruct);
- //清除提前唤醒中断标志位
- WWDG_ClearFlag();
- //开启提前唤醒中断
- WWDG_EnableIT();
- }
- //中断处理函数
- void WWDG_IRQHandler(void)
- {
- //清除提前唤醒中断标志位
- WWDG_ClearFlag();
- WWDG_SetCounter(WWDG_CNT);
- LED2 = !LED2;
- }
复制代码
|