STM32窗口看門狗程序
軟件例程:
//---------------------------wdg.c-----------------------
#include "wdg.h"
#include "led.h"
u8 wwdg_cnt=0x7f; //窗口看門狗計(jì)數(shù)器初值
void wwdg_init(u8 tr,u8 wr,u8 fprer)
{
RCC->APB1ENR|=1<<11; //使能WWDG時(shí)鐘
wwdg_cnt=tr&wwdg_cnt; // 初始化計(jì)數(shù)器值
WWDG->CFR|=fprer<<7; //設(shè)置頻率時(shí)基
WWDG->CFR|=1<<9; //使能串口看門狗中斷
WWDG->CFR&=0xff80; //窗口值清零
WWDG->CFR|=wr; //設(shè)定窗口值
WWDG->CR|=(wwdg_cnt|1<<7); // 啟動(dòng)看門狗設(shè)置7位計(jì)數(shù)器初值
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; //清楚提前喚醒標(biāo)志位
LED1=!LED1;
}
//------------------------wdg.h---------------------
///-----------------------主函數(shù)------------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;
}
}
評(píng)論