STM32独立看门狗与窗口看门狗

香水城:如何使用STM32的窗口看门狗


菜农:看门狗专栏






/*注:stm32f10x_stdperiph_lib_v3.0.0  编译器:MDK3.24A*/
/* Includes -----------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_wwdg.h"


/* Private function prototypes ----------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void SysTick_Configuration(void);
void IWDG_Configuration(void);
void WWDG_Configuration(void);


void Delay(__IO uint32_t nCount);
void Test(void);






/* Private functions --------------------------------------*/


/**===============================
  * @brief  Main program.
  * @param  None
  * @retval : None
  =================================*/
int main(void)
{
 RCC_Configuration();
 GPIO_Configuration();
 Test();
 NVIC_Configuration(); 
 SysTick_Configuration(); /* Generate an interrupt each 100ms */ 
 IWDG_Configuration();
 WWDG_Configuration();



 while (1) {
  ;
 }
}


/**---------------------------------------------------------
  * @brief  [测试]若由IWDG或WWDG引起复位,则Pin4闪一下(注:首次Pin4闪由上电引起,不是由看门狗复位引起);若不断复位,则Pin4不断闪烁。注: 可通过更改程序中的相关值而引起看门狗复位。
  * @param  None
  * @retval : None
  ------------------------------------------------------*/
void Test(void)
{
 GPIO_SetBits(GPIOC, GPIO_Pin_4);
 Delay(0xFFFFF);
 GPIO_ResetBits(GPIOC, GPIO_Pin_4);  
}


/**---------------------------------------------------------
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval : None
  ------------------------------------------------------*/

void RCC_Configuration(void)
{
 SystemInit();
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); 
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);  
}
/**------------------------------------------------
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval : None
  ---------------------------------------------------*/
void GPIO_Configuration(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;
 
 /* Configure GPIOC as Output push-pull */
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_7;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOC, &GPIO_InitStructure);
}
 /**----------------------------------------------------------------
  * @brief  Configures NVIC and Vector Table base location.
  * @param  None
  * @retval : None
  -------------------------------------------------------------------*/
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_Init(&NVIC_InitStructure);
 
 /* Set SysTick interrupt vector Preemption Priority to 1 */
 NVIC_SetPriority(SysTick_IRQn, 0x08);
}
/**--------------------------------------------------------------------
  * @brief  Configures SysTick to generate an interrupt.
  * @param  None
  * @retval : None
  ----------------------------------------------------------------------*/
void SysTick_Configuration(void)
{
 /* SysTick interrupt each 100ms */
 if (SysTick_Config(SystemFrequency / 10)){ 
  /* Capture error */ 
  while (1);
 }
}
/**-----------------------------------------------------------------
  * @brief  Inserts a delay time.
  * @param nCount: specifies the delay time length.
  * @retval : None
  ------------------------------------------------------------------*/

void Delay(__IO uint32_t nCount)
{
 for(; nCount != 0; nCount--);
}


/**-------------------------------
  * @brief  Configures IWDG
  * @param  None
  * @retval : None
  ------------------------------*/
void IWDG_Configuration(void)
{
 /* IWDG timeout equal to 200 ms (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: 40KHz(LSI) / 16 = ? KHz */
 IWDG_SetPrescaler(IWDG_Prescaler_16);
          
 /* Set counter reload value: (500)x16/40K=200msec */
 IWDG_SetReload(500);
                    
 /* Reload IWDG counter */
 IWDG_ReloadCounter();
   
 /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
 IWDG_Enable();  
}


/**-------------------------------
  * @brief  Configures WWDG
  * @param  None
  * @retval : None
  ------------------------------*/

void WWDG_Configuration(void)
{
 /* WWDG clock counter = (PCLK1/4096)/8 = 244 Hz (~4 ms)  */
 WWDG_SetPrescaler(WWDG_Prescaler_8);
 
 /* Set Window value to 65 */
 WWDG_SetWindowValue(65);
 
 /* Enable WWDG and set counter value to 127, WWDG timeout = ~4 ms * 64 = 262 ms */
 WWDG_Enable(127);   
 
 /* Clear EWI flag */
 WWDG_ClearFlag();
 
 /* Enable EW interrupt */
 WWDG_EnableIT();  
}





/* Includes -----------------------------------------*/
#include "stm32f10x_it.h"
#include "stm32f10x_wwdg.h"



/**--------------------------------------------------------
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval : None
  ------------------------------------------------------*/

void SysTick_Handler(void)
{
 /* Reload IWDG counter */
 IWDG_ReloadCounter();
 
 /* Toggle GPIO_LED pin 7 */
    GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7)));
}


/**----------------------------------------------------------
  * @brief  This function handles WWDG interrupt request.
  * @param  None
  * @retval : None
  -------------------------------------------------------------*/

void WWDG_IRQHandler(void)
{
  /* Update WWDG counter */
  WWDG_SetCounter(0x7F);


  /* Clear EWI flag */
  WWDG_ClearFlag();


  /* Toggle GPIO_Led pin 6 */
  GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6)));
}




你可能感兴趣的:(STM32独立看门狗与窗口看门狗)