看手册上说有两种情况会复位:一、计数器小于0x40;二、在窗口外设置计数器。
现在我用想看看在窗口内进行喂狗的效果。所以用定时器中断在窗口内进行喂狗,程序如下:
#include "stm32f10x.h"
#define LED_OFF GPIO_SetBits(GPIOF, GPIO_Pin_7);
#define LED_ON GPIO_ResetBits(GPIOF, GPIO_Pin_7);
void GPIO_Configuration(void);
void RCC_Configuration(void);
void NVIC_Configuration(void);
void WWDG_Configuration(void);
void Timer_Configuration(void);
void Delay(u32);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
WWDG_Configuration();
NVIC_Configuration();
Timer_Configuration();
LED_ON; //开启LED灯,提示复位
Delay(100);
LED_OFF; //关闭LED灯,提示复位结束
TIM_Cmd(TIM1, ENABLE);
while(1)
{
}
}
void WWDG_IRQHandler(void) //窗口看门狗中断服务程序,设置计数器的值
{
WWDG_ClearFlag();
WWDG_SetCounter(0x43);
}
void TIM1_UP_IRQHandler(void) //定时器中断服务程序,设置窗口看门狗计数器的值
{
WWDG_SetCounter(0x43);
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* Enable Prefetch Buffer 设置预取缓冲器可以提高CPU的执行速度*/
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
//也可以用:while (!(RCC_WaitForHSEStartUp()== SUCCESS));
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08);
/* Enable GPIOB clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOF|RCC_APB2Periph_TIM1 , ENABLE);// 使能APB2外设GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //选择PX.10/11
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //模式为推挽输出
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
//GPIO的配置设置,包括管脚、模式和频率
void WWDG_Configuration(void) //窗口看门狗初始化程序
{
WWDG_SetPrescaler(WWDG_Prescaler_8);
WWDG_SetWindowValue(0x70);
WWDG_Enable(0x43);
WWDG_ClearFlag();
WWDG_EnableIT();
}
void NVIC_Configuration(void) //定时器和窗口看门狗中断的中断优先级
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannel=TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_Init(&NVIC_InitStructure);
}
void Delay(u32 dly)
{ u16 i;
for(; dly>0; dly--)
for(i=0; i<600; i++);
}
void Timer_Configuration(void) //定时器初始化程序,设置为很短的时间进入中断,然后进行喂狗
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0; // No prescaling ,可以改变编码器计数对于脉冲输入个数的倍数。
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
//Reset counter
TIM_Cmd(TIM1, DISABLE);
}
程序运行结果是:LED灯始终在亮,应该是一直在复位。设置的计数器的值都是在窗口内,为什么还会复位呢? |