stm32_看门狗实验-WWDG

/*********************************************************************************************************
*
* File : main.c
* Hardware Environment: STM32F107VC
* Build Environment : RealView MDK-ARM Version: 4.74
* Version : V3.5.0
* By : 海鸥
* 设计原理:设置外部中断服务优先级高于看门狗早起唤醒中断,按下按键后,程序进入while
* 死循环,喂狗操作被打断
* 现象:未按下按钮(按钮与P13连接)前,串口输出 :The STM32 has not been reset by WWDG before
* The Windows Watch Dog has been flash
* 按下按钮后,串口输出:The STM32 has been reset by WWDG
*********************************************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include
#include

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
#define USART1_TX GPIO_Pin_6
#define USART1_RX GPIO_Pin_7
#define USART2_TX GPIO_Pin_5
#define USART2_RX GPIO_Pin_6
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void NVIC_Configuration(void);
void Delay (uint32_t nCount);
void EXTI_Configuration(void);
void WWDG_Configuration(void);
void Putstring(uint8_t *string);

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

/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
EXTI_Configuration();
NVIC_Configuration();


if(RCC_GetFlagStatus(RCC_FLAG_WWDGRST)!=RESET)
{
Putstring(" The STM32 has been reset by WWDG \r\n");
RCC_ClearFlag();

}
else
{
WWDG_Configuration();
Putstring(" The STM32 has not been reset by WWDG before \r\n");

}
while(1);


}
void Putstring(uint8_t *string)
{
while(*string!='\0')
{
USART_SendData(USART2,*string);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET);
string++;
}

}
void RCC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG,ENABLE); //开启APB1总线上的窗口看门狗时钟
RCC_APB2PeriphClockCmd( RCC_APB

2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //开启USART2的总线时钟



}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configure GPIO Pin
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

//USART2 GPIO配置
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); //打开USART2功能复用IO
GPIO_InitStructure.GPIO_Pin = USART2_TX ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = USART2_RX ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);





}
void USART_Configuration(void)
{
//USART2配置
USART_InitTypeDef USART_InitStructure;

USART_https://www.360docs.net/doc/a111407930.html,ART_BaudRate = 115200;
USART_https://www.360docs.net/doc/a111407930.html,ART_WordLength = USART_WordLength_8b;
USART_https://www.360docs.net/doc/a111407930.html,ART_StopBits = USART_StopBits_1;
USART_https://www.360docs.net/doc/a111407930.html,ART_Parity = USART_Parity_No;
USART_https://www.360docs.net/doc/a111407930.html,ART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_https://www.360docs.net/doc/a111407930.html,ART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART2,&USART_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_Cmd(USART2,ENABLE);

/* //USART1配置
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_https://www.360docs.net/doc/a111407930.html,ART_BaudRate = 115200;
USART_https://www.360docs.net/doc/a111407930.html,ART_WordLength = USART_WordLength_8b;
USART_https://www.360docs.net/doc/a111407930.html,ART_StopBits = USART_StopBits_1;
USART_https://www.360docs.net/doc/a111407930.html,ART_Parity = USART_Parity_No;
USART_https://www.360docs.net/doc/a111407930.html,ART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_https://www.360docs.net/doc/a111407930.html,ART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
*/

}


void NVIC_Configuration(void)
{
/* USART2 中断配置 */
NVIC_InitTypeDef NVIC_InitStructure; //定义数据结构体

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);//将中断矢量放到Flash的0地址

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置优先级配置的模式,详情请阅读原材料中的文章

//使能串口中断,并设置优先级
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); //将结构体丢到配置函数,即写入到对应寄存器中

//使能外部中断,0级先占优先级,0级次站优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; // EXTI15_10_IRQChannel
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

//使能窗口看门狗中断,1级先占优先级
NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_ClearITPendingBit(EXTI_Line13);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource13);
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void WWDG_Configuration(void)
{
WWDG_SetPrescaler(WWDG_Prescaler_8);
WWDG_Enable(0x7F);
WWDG_ClearFlag();
WWDG_EnableIT();

}
/*******************************************************************************
* Function Name : Delay
* Description : Delay Time
* Input : - nCount: Delay Time
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Delay (uint32_t nCount)
{
for(; nCount != 0; nCount--);
}


#ifdef USE_FULL_ASSERT

/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* Infinite loop */
while (1)
{
}
}
#endif

/*************************************** END OF FILE *************************************/

/* stm32f10x_it.c 文件中添加如下程序 */

void EXTI15_10_IRQHandler(void)
{
while(1);
}
void WWDG_IRQHandler(void)
{
WWDG_SetCounter(0x7F);
WWDG_ClearFlag();
Putstring(" The Windows Watch Dog has been flash \r\n");

}

相关文档
最新文档