闲来无事,拿起STM32 的板子,实验一下看门狗程序, 结果发现 IWDG 和 WWDG 似乎均不起作用: 把程序贴上:
IWDG:
/* Private functions ---------------------------------------------------------*/ /******************************************************************************* * Function Name : main * Description : Main program. * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif
/* System Clocks Configuration ---------------------------------------------*/ RCC_Configuration(); /* GPIO configuration ------------------------------------------------------*/ GPIO_Configuration();
/* Check if the system has resumed from IWDG reset -------------------------*/ if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET) { /* IWDGRST flag set */ /* Turn on led connected to PC.06 */ GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
/* Clear reset flags */ RCC_ClearFlag(); } else { /* IWDGRST flag is not set */ /* Turn off led connected to PC.06 */ GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_RESET); }
/* Configure EXTI Line9 to generate an interrupt on falling edge -----------*/ EXTI_Configuration();
/* NVIC configuration ------------------------------------------------------*/ NVIC_Configuration();
/* Configure SysTick to generate an interrupt each 250ms -------------------*/ SysTick_Configuration();
/* IWDG timeout equal to 350ms (the timeout may varies due to LSI frequency dispersion) -------------------------------------------------------------*/ /* Enable write access to IWDG_PR and IWDG_RLR registers */ IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
/* IWDG counter clock: 32KHz(LSI) / 32 = 1KHz */ IWDG_SetPrescaler(IWDG_Prescaler_32);
/* Set counter reload value to 349 */ IWDG_SetReload(349);
/* Reload IWDG counter */ IWDG_ReloadCounter();
/* Enable IWDG (the LSI oscillator will be enabled by hardware) */ IWDG_Enable();
while(1) { } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit();
/* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS) { /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK */ RCC_PCLK1Config(RCC_HCLK_Div1);
/* Flash 0 wait state */ FLASH_SetLatency(FLASH_Latency_0); /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Select HSE as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
/* Wait till HSE is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x04) { } } /* Enable GPIOC, GPIOB and AFIO clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, 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;
/* Configure PC.06 and PC.07 as Output push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure PB9 as input floating (EXTI Line9) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); }
WWDG:
/* Private functions ---------------------------------------------------------*/ /******************************************************************************* * Function Name : main * Description : Main program. * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif
/* System Clocks Configuration ---------------------------------------------*/ RCC_Configuration(); /* GPIO configuration ------------------------------------------------------*/ GPIO_Configuration();
/* Check if the system has resumed from WWDG reset -------------------------*/ if(RCC_GetFlagStatus(RCC_FLAG_WWDGRST) != RESET) { /* WWDGRST flag set */ /* Turn on led connected to PC.06 */ GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
/* Clear reset flags */ RCC_ClearFlag(); } else { /* WWDGRST flag is not set */ /* Turn off led connected to PC.06 */ GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET); } /* WWDG configuration --------------------------------------------------------*/ /* Enable WWDG clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
/* WWDG clock counter = (PCLK1/4096)/8 = 244 Hz (~4 ms) */ WWDG_SetPrescaler(WWDG_Prescaler_8);
/* Set Window value to 0x41 */ WWDG_SetWindowValue(0x41);
/* Enable WWDG and set counter value to 0x7F, WWDG timeout = ~4 ms * 64 = 262 ms */ WWDG_Enable(0x7F);
/* Clear EWI flag */ WWDG_ClearFlag();
/* Enable EW interrupt */ WWDG_EnableIT();
while (1) { } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit();
/* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS) { /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK */ RCC_PCLK1Config(RCC_HCLK_Div1);
/* Flash 0 wait state */ FLASH_SetLatency(FLASH_Latency_0); /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Select HSE as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
/* Wait till HSE is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x04) { } } /* Enable GPIOC, GPIOB and AFIO clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
}
请大家看看。 |