STM32窗口看门狗程序
软件例程:
//---------------------------wdg.c-----------------------
#include "wdg.h"
#include "led.h"
u8 wwdg_cnt=0x7f; //窗口看门狗计数器初值
void wwdg_init(u8 tr,u8 wr,u8 fprer)
{
RCC->APB1ENR|=1<<11; //使能WWDG时钟
wwdg_cnt=tr&wwdg_cnt; // 初始化计数器值
WWDG->CFR|=fprer<<7; //设置频率时基
WWDG->CFR|=1<<9; //使能串口看门狗中断
WWDG->CFR&=0xff80; //窗口值清零
WWDG->CFR|=wr; //设定窗口值
WWDG->CR|=(wwdg_cnt|1<<7); // 启动看门狗设置7位计数器初值
MY_NVIC_Init(2,3,WWDG_IRQChannel,2);
}
void wwdg_feed(u8 cnt)
{
WWDG->CR|=(cnt&0x7f); //喂狗值
}
void WWDG_IRQHandler(void)
{
u8 tr,wr;
wr=WWDG->CFR&0x7f;
tr=WWDG->CR&0x7f;
if(tr
WWDG->SR&=0x00; //清楚提前唤醒标志位
LED1=!LED1;
}
//------------------------wdg.h---------------------
///-----------------------主函数------------test.c---------------
//------------------------wdg.h---------------------
#ifndef _wdg_h
#define _wdg_h
#include "sys.h"
void wwdg_init(u8 tr,u8 wr,u8 fprer);
void wwdg_feed(u8 cnt);
#endif
//------------------led.c----------------------
#include "led.h"
void led_init(void)
{
RCC->APB2ENR|=1<<2;
RCC->APB2ENR|=1<<5;
GPIOA->CRH&=0xfffffff0;
GPIOA->CRH|=0x00000003;
GPIOD->CRL&=0xfffff0ff;
GPIOD->CRL|=0x00000300;
GPIOA->ODR|=1<<8;
GPIOD->ODR|=1<<2;
}
//---------------------led.h------------------
#ifndef _led_h
#define _led_h
#include "sys.h"
#define LED0 PAout(8)
#define LED1 PDout(2)
void led_init(void);
#endif
#include
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "wdg.h"
int main(void)
{
Stm32_Clock_Init(9);
delay_init(72);
uart_init(72,9600);
led_init();
wwdg_init(0x7f,0x5f,3);
LED0=0;
delay_ms(1800);
while(1)
{
LED0=1;
}
}
评论