情景如下:led一闪一灭,当我们按下某个按键时,他就保持亮,不按的话就会闪烁,实现一直按一直亮,不按则闪烁。
窗口看门狗实验一般步骤:
①:使能看门狗时钟:
RCC_APB1PeriphClockCmd();
②:设置分频系数
WWDG_SetPrescaler();
③:设置上窗口值
WWDG_SetWindowValue();
④:开启提前唤醒中断并分组(可选)
WWDG_EnableIT();
NVIC_Init();
⑤:使能看门狗
WWDG_Enable();
⑥:喂狗
WWDG_Enable();
⑦:编写中断服务函数
WWDG_IRQHandler();
我们还是先写.h文件
#ifndef __WDG_H
#define __WDG_H
#include "sys.h"
void IWDG_Init(u8 prer,u16 rlr);
void IWDG_Feed(void);
void WWDG_Init(u8 tr,u8 wr,u32 fprer);
void WWDG_Set_Counter(u8 cnt);
void WWDG_NVIC_Init(void);
#endif
然后.c文件:
#include "wdg.h"
#include "led.h"
void IWDG_Init(u8 prer,u16 rlr)
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_SetPrescaler(prer);
IWDG_SetReload(rlr);
IWDG_ReloadCounter();
IWDG_Enable();
}
void IWDG_Feed(void)
{
IWDG_ReloadCounter();
}
void WWDG_Init(u8 tr,u8 wr,u32 fprer)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
WWDG_CNT=tr&WWDG_CNT;
WWDG_SetPrescaler(fprer);
WWDG_SetWindowValue(wr);
WWDG_Enable(WWDG_CNT);
WWDG_ClearFlag();
WWDG_NVIC_Init();
WWDG_EnableIT();
}
void WWDG_Set_Counter(u8 cnt)
{
WWDG_Enable(cnt);
}
void WWDG_NVIC_Init()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void WWDG_IRQHandler(void)
{
WWDG_SetCounter(WWDG_CNT);
WWDG_ClearFlag();
LED1=!LED1;
}
最后是主函数:
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "wdg.h"
int main(void)
{
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
KEY_Init();
LED0=0;
delay_ms(300);
WWDG_Init(0X7F,0X5F,WWDG_Prescaler_8);
while(1)
{
LED0=1;
}
}
如果大家想通过这个类似的功能实现蜂鸣器或者其他功能的
改一下主函数的器材控制的哪些代码即可。