14、看门狗
一、独立看门狗
1、独立看门狗框图
2、键值寄存器IWDG_KR (只写寄存器,读出值为0x0000)
(1).写入0x5555表示允许访问IWDG_PR和IWDG_RLR寄存器。
(2).设定预分频和计数器初值。
(3).软件必须以一定的间隔写入0xAAAA,否则,当计数器为0时, 看门狗会产生复位。
(4).写入0xCCCC,启动看门狗工作.
3、例程代码:
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:main.c 4 * 文件功能:主函数文件 5 * 6 ***************************************************************************************************************************************/ 7 8 #include "pbdata.h"//调用自定义公共函数库 9 10 11 /********************************************************************************* 12 * 13 * 初始化操作 14 * 15 *********************************************************************************/ 16 void RCC_Configuration(void);//系统时钟初始化函数声明 17 void GPIO_Configuration(void);//GPIO初始化函数声明 18 void NVIC_Configuration(void);//中断优先级配置函数声明 19 void USART_Configuration(void);//串口配置函数声明 20 void IWDG_Configuration(void);//独立看门狗配置 21 22 23 24 25 /******************************************************************************** 26 * 27 * 函数名称:main(void) 28 * 函数功能:主函数 29 * 函数说明:不能用void定义主函数 30 * 31 ********************************************************************************/ 32 int main(void)//void不能void定义主函数 33 { 34 35 RCC_Configuration(); //系统时钟初始化 36 37 GPIO_Configuration();//端口初始化 38 39 USART_Configuration();//串口配置 40 41 NVIC_Configuration();//中断优先级配置 42 43 printf("liubo\n");//显示 44 45 while(1) 46 { 47 IWDG_ReloadCounter();//喂狗 48 printf("feed watch_dog\n");//显示 49 delay_ms(500);//每隔500ms喂一次 50 51 } 52 53 } 54 55 56 57 58 /******************************************************************************** 59 * 60 * 函数名称:RCC_Configuration(void) 61 * 函数功能:系统时钟高初始化函数 62 * 63 ********************************************************************************/ 64 void RCC_Configuration(void)//系统时钟高初始化函数 65 { 66 67 SystemInit();//系统初始化 68 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//串口对应GPIO时钟使能 69 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口时钟使能 70 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//引脚复用 71 72 } 73 74 75 76 /******************************************************************************* 77 * 78 * 函数名称:GPIO_Configuration(void) 79 * 函数功能:GPIO初始化函数 80 * 81 ********************************************************************************/ 82 83 void GPIO_Configuration(void)//GPIO初始化函数 84 { 85 86 87 /*串口引脚配置*/ 88 GPIO_InitTypeDef GPIO_InitStructure;//定义一个GPIO设置的结构体变量 89 90 /*输出引脚配置*/ 91 /*结构体变量赋值*/ 92 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;////引脚配置TX 93 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率 94 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//发送要配置成复用推挽输出 95 /*对应的GPIO初始化*/ 96 GPIO_Init(GPIOA,&GPIO_InitStructure); 97 98 99 /*输入引脚配置*/ 100 /*结构体变量赋值*/ 101 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;////引脚配置RX 102 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//接收引脚配置成浮空输入 103 /*对应的GPIO初始化*/ 104 GPIO_Init(GPIOA,&GPIO_InitStructure); 105 106 107 } 108 109 110 /**************************************************************************** 111 * 112 * 函数名称:NVIC_Configuration(void) 113 * 函数功能:配置中断优先级 114 * 115 ****************************************************************************/ 116 117 void NVIC_Configuration(void) 118 { 119 NVIC_InitTypeDef NVIC_InitStructure; //定义一个优先级配置结构体变量 120 121 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//分组 122 123 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 124 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级 125 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级 126 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能 127 128 NVIC_Init(&NVIC_InitStructure);//初始化 129 } 130 131 132 /********************************************************************************* 133 * 134 * 函数名称:USART_Configuration(void) 135 * 函数功能:串口配置函数 136 * 137 *********************************************************************************/ 138 void USART_Configuration(void) 139 { 140 /*定义串口配置结构体变量*/ 141 USART_InitTypeDef USART_InitStructure;//定义一个串口配置结构体变量 142 143 144 /*结构体变量赋值*/ 145 USART_InitStructure.USART_BaudRate = 9600;//波特率9600 146 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//位宽,8位 147 USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位1 148 USART_InitStructure.USART_Parity = USART_Parity_No;//不奇偶校验 149 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//流控禁止 150 USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送使能 151 152 153 /*发送串口配置初始化*/ 154 USART_Init(USART1, &USART_InitStructure); 155 156 157 /*打开串口接收中断*/ 158 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//当接收到数据时,会产生中断 159 160 161 /*打开串口*/ 162 USART_Cmd(USART1,ENABLE);//串口使能,打开 163 164 /*清空中断标志位*/ 165 USART_ClearFlag(USART1,USART_FLAG_TC); 166 } 167 168 169 170 /********************************************************************************* 171 * 172 * 函数名称:IWDG_Configuration(void) 173 * 函数功能:独立看门狗配置 174 * 175 *********************************************************************************/ 176 void IWDG_Configuration(void) 177 { 178 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//使能寄存器写 179 180 IWDG_SetPrescaler(IWDG_Prescaler_64);//设置预分频:40k/64=0.625k,一个周期1.6ms 181 182 IWDG_SetReload(800);//设置初值,800*1.6ms=1.28s,1.28s内喂一次,就不会复位 183 184 IWDG_ReloadCounter();//喂狗 185 186 IWDG_Enable();//使能看门狗 187 188 } 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
1 /**************************************************************************************************************** 2 * 3 * 文件名称:pbdata.c 4 * 文件功能:自定义函数或者全局变量的初始化 5 * 6 ****************************************************************************************************************/ 7 8 /*头文件声明*/ 9 #include "pbdata.h" 10 11 12 13 14 /******************************************************************************************** 15 * 16 * 自定义全局变量 17 * 18 ********************************************************************************************/ 19 u8 dt=0; 20 21 22 23 24 25 /****************************************************************************************** 26 * 27 * 自定义函数 28 * 29 ******************************************************************************************/ 30 31 32 33 /************************************************** 34 * 35 * 函数名称:delay_us(u32 nus) 36 * 函数功能:微秒延时函数 37 * 输入参数:输入值为延时us 38 * 39 ***************************************************/ 40 void delay_us(u32 nus) 41 { 42 u32 temp; 43 SysTick->LOAD = 9*nus;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9次 44 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 45 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 46 47 do 48 { 49 temp=SysTick->CTRL;//标志位,等到一直减到0 50 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 51 52 SysTick->CTRL=0x00; //关闭计数器 53 SysTick->VAL =0X00; //清空计数器 54 } 55 56 57 58 59 60 61 62 /*************************************************** 63 * 64 * 函数名称:delay_ms(u16 nms) 65 * 函数功能:毫秒级延时 66 * 输入参数:输入值位延时ms 67 * 68 ****************************************************/ 69 void delay_ms(u16 nms) 70 { 71 u32 temp; 72 SysTick->LOAD = 9000*nms;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9000次 73 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 74 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 75 76 do 77 { 78 temp=SysTick->CTRL;//标志位,等到一直减到0 79 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 80 81 SysTick->CTRL=0x00; //关闭计数器 82 SysTick->VAL =0X00; //清空计数器 83 } 84 85 86 87 /**************************************************** 88 * 89 * 重定义printf函数部分 90 * 91 ****************************************************/ 92 int fputc(int ch,FILE *F) 93 { 94 95 USART_SendData(USART1,(u8)ch); 96 97 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位 98 99 return ch; 100 }
1 /*pbdata.h*/ 2 /*************************************************************************************************** 3 * 4 * 文件名称:pbdata.h 5 * 文件功能:自定义的函数和全局变量的声明头文件 6 * 7 ***************************************************************************************************/ 8 9 #ifndef _pbdata_H 10 #define _pbdata_H 11 12 13 14 15 16 /******************************************************************** 17 * 18 * 调用的头文件放在这里 19 * 20 ********************************************************************/ 21 #include "stm32f10x.h" 22 23 24 #include "stm32f10x_rcc.h" 25 #include "stm32f10x_gpio.h" 26 #include "misc.h" 27 #include "stm32f10x_adc.h" 28 #include "stm32f10x_bkp.h" 29 #include "stm32f10x_can.h" 30 #include "stm32f10x_cec.h" 31 #include "stm32f10x_dac.h" 32 #include "stm32f10x_dbgmcu.h" 33 #include "stm32f10x_dma.h" 34 #include "stm32f10x_exti.h" 35 #include "stm32f10x_flash.h" 36 #include "stm32f10x_fsmc.h" 37 #include "stm32f10x_i2c.h" 38 #include "stm32f10x_iwdg.h" 39 #include "stm32f10x_pwr.h" 40 #include "stm32f10x_rtc.h" 41 #include "stm32f10x_sdio.h" 42 #include "stm32f10x_spi.h" 43 #include "stm32f10x_tim.h" 44 #include "stm32f10x_usart.h" 45 #include "stm32f10x_wwdg.h" 46 47 48 #include "stdio.h" 49 50 51 /******************************************************************** 52 * 53 * 自定义全局变量声明 54 * 55 ********************************************************************/ 56 extern u8 dt; 57 58 59 60 61 62 /******************************************************************** 63 * 64 * 自定义全函数声明 65 * 66 ********************************************************************/ 67 void delay(u32 nCount); 68 void delay_us(u32 nus); 69 void delay_ms(u16 nms); 70 int fputc(int ch,FILE *F); 71 72 73 74 #endif
4、工程下载:
http://download.csdn.net/detail/a1181803348/8765603
二、窗口看门狗
1、WWDG简介
窗口看门狗通常被用来监测,由外部干扰或不可预见的逻辑条件造成的应用程序背离正常的运行序列而产生的软件故障。除非递减计数器的值在T6位变成0前被刷新,看门狗电路在达到预置的时间周期时,会产生一个MCU复位。在递减计数器达到窗口寄存器数值之前,如果7位的递减计数器数值(在控制寄存器中)被刷新, 那么也将产生一个MCU复位。这表明递减计数器需要在一个有限的时间窗口中被刷新。
2、窗口看门狗工作原理
上窗口时间:T_min = 4096 * (2^WDGTB)*(T - W + 1)/36 (us)
下窗口时间: T_max = 4096 * (2^WDGTB)*(T - 0x40 + 1)/36 (us)
3、控制寄存器
4、配置寄存器
5、状态寄存器
6、例程代码:
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:main.c 4 * 文件功能:主函数文件 5 * 6 ***************************************************************************************************************************************/ 7 8 #include "pbdata.h"//调用自定义公共函数库 9 10 11 /********************************************************************************* 12 * 13 * 初始化操作 14 * 15 *********************************************************************************/ 16 void RCC_Configuration(void);//系统时钟初始化函数声明 17 void GPIO_Configuration(void);//GPIO初始化函数声明 18 void NVIC_Configuration(void);//中断优先级配置函数声明 19 void USART_Configuration(void);//串口配置函数声明 20 void WWDG_Configuration(void);//窗口看门狗配置函数 21 22 23 24 /******************************************************************************** 25 * 26 * 函数名称:main(void) 27 * 函数功能:主函数 28 * 函数说明:不能用void定义主函数 29 * 30 ********************************************************************************/ 31 int main(void)//void不能void定义主函数 32 { 33 34 RCC_Configuration(); //系统时钟初始化 35 36 GPIO_Configuration();//端口初始化 37 38 USART_Configuration();//串口配置 39 40 NVIC_Configuration();//中断优先级配置 41 42 WWDG_Configuration();//窗口看门狗配置 43 44 printf("hello,liubo\n"); 45 46 47 while(1); 48 } 49 50 51 52 53 /******************************************************************************** 54 * 55 * 函数名称:RCC_Configuration(void) 56 * 函数功能:系统时钟高初始化函数 57 * 58 ********************************************************************************/ 59 void RCC_Configuration(void)//系统时钟高初始化函数 60 { 61 62 SystemInit();//系统初始化 63 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//串口对应GPIO时钟使能 64 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口时钟使能 65 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//引脚复用 66 67 RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG,ENABLE);//打开窗口看门狗时钟 68 69 } 70 71 72 73 /******************************************************************************* 74 * 75 * 函数名称:GPIO_Configuration(void) 76 * 函数功能:GPIO初始化函数 77 * 78 ********************************************************************************/ 79 80 void GPIO_Configuration(void)//GPIO初始化函数 81 { 82 83 84 /*串口引脚配置*/ 85 GPIO_InitTypeDef GPIO_InitStructure;//定义一个GPIO设置的结构体变量 86 87 /*输出引脚配置*/ 88 /*结构体变量赋值*/ 89 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;////引脚配置TX 90 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率 91 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//发送要配置成复用推挽输出 92 /*对应的GPIO初始化*/ 93 GPIO_Init(GPIOA,&GPIO_InitStructure); 94 95 96 /*输入引脚配置*/ 97 /*结构体变量赋值*/ 98 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;////引脚配置RX 99 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//接收引脚配置成浮空输入 100 /*对应的GPIO初始化*/ 101 GPIO_Init(GPIOA,&GPIO_InitStructure); 102 103 104 } 105 106 107 /**************************************************************************** 108 * 109 * 函数名称:NVIC_Configuration(void) 110 * 函数功能:配置中断优先级 111 * 112 ****************************************************************************/ 113 114 void NVIC_Configuration(void) 115 { 116 NVIC_InitTypeDef NVIC_InitStructure; //定义一个优先级配置结构体变量 117 118 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//分组 119 120 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 121 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级 122 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级 123 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能 124 125 NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQn; 126 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级 127 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应优先级,最高 128 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能 129 130 NVIC_Init(&NVIC_InitStructure);//初始化 131 } 132 133 134 /********************************************************************************* 135 * 136 * 函数名称:USART_Configuration(void) 137 * 函数功能:串口配置函数 138 * 139 *********************************************************************************/ 140 void USART_Configuration(void) 141 { 142 /*定义串口配置结构体变量*/ 143 USART_InitTypeDef USART_InitStructure;//定义一个串口配置结构体变量 144 145 146 /*结构体变量赋值*/ 147 USART_InitStructure.USART_BaudRate = 9600;//波特率9600 148 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//位宽,8位 149 USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位1 150 USART_InitStructure.USART_Parity = USART_Parity_No;//不奇偶校验 151 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//流控禁止 152 USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送使能 153 154 155 /*发送串口配置初始化*/ 156 USART_Init(USART1, &USART_InitStructure); 157 158 159 /*打开串口接收中断*/ 160 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//当接收到数据时,会产生中断 161 162 163 /*打开串口*/ 164 USART_Cmd(USART1,ENABLE);//串口使能,打开 165 166 /*清空中断标志位*/ 167 USART_ClearFlag(USART1,USART_FLAG_TC); 168 } 169 170 171 172 173 174 /********************************************************************************* 175 * 176 * 函数名称:WWDG_Configuration(void) 177 * 函数功能:窗口看门狗配置函数 178 * 179 *********************************************************************************/ 180 181 void WWDG_Configuration(void) 182 { 183 WWDG_SetPrescaler(WWDG_Prescaler_8);//设置WWDG预分频数值 184 185 WWDG_SetWindowValue(0x5f);//窗口上边界数值 186 187 WWDG_Enable(0x7f);//使能窗口看门狗 188 189 WWDG_ClearFlag();//清除提前唤醒中断标志 190 191 WWDG_EnableIT();//开启窗口看门狗中断 192 193 } 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
1 /** 2 ****************************************************************************** 3 * @file GPIO/IOToggle/stm32f10x_it.c 4 * @author MCD Application Team 5 * @version V3.0.0 6 * @date 04/06/2009 7 * @brief Main Interrupt Service Routines. 8 * This file provides template for all exceptions handler and 9 * peripherals interrupt service routine. 10 ****************************************************************************** 11 * @copy 12 * 13 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 14 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 15 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 16 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 17 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 18 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 19 * 20 * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2> 21 */ 22 23 /* Includes ------------------------------------------------------------------*/ 24 #include "stm32f10x_it.h" 25 #include "stm32f10x_exti.h" 26 #include "stm32f10x_rcc.h" 27 #include "stm32f10x_tim.h" 28 #include "misc.h" 29 #include "pbdata.h" 30 31 void NMI_Handler(void) 32 { 33 } 34 35 36 37 /** 38 * @brief This function handles Hard Fault exception. 39 * @param None 40 * @retval : None 41 */ 42 void HardFault_Handler(void) 43 { 44 /* Go to infinite loop when Hard Fault exception occurs */ 45 while (1) 46 { 47 } 48 } 49 50 /** 51 * @brief This function handles Memory Manage exception. 52 * @param None 53 * @retval : None 54 */ 55 void MemManage_Handler(void) 56 { 57 /* Go to infinite loop when Memory Manage exception occurs */ 58 while (1) 59 { 60 } 61 } 62 63 /** 64 * @brief This function handles Bus Fault exception. 65 * @param None 66 * @retval : None 67 */ 68 void BusFault_Handler(void) 69 { 70 /* Go to infinite loop when Bus Fault exception occurs */ 71 while (1) 72 { 73 } 74 } 75 76 /** 77 * @brief This function handles Usage Fault exception. 78 * @param None 79 * @retval : None 80 */ 81 void UsageFault_Handler(void) 82 { 83 /* Go to infinite loop when Usage Fault exception occurs */ 84 while (1) 85 { 86 } 87 } 88 89 /** 90 * @brief This function handles SVCall exception. 91 * @param None 92 * @retval : None 93 */ 94 void SVC_Handler(void) 95 { 96 } 97 98 /** 99 * @brief This function handles Debug Monitor exception. 100 * @param None 101 * @retval : None 102 */ 103 void DebugMon_Handler(void) 104 { 105 } 106 107 /** 108 * @brief This function handles PendSV_Handler exception. 109 * @param None 110 * @retval : None 111 */ 112 void PendSV_Handler(void) 113 { 114 } 115 116 /** 117 * @brief This function handles SysTick Handler. 118 * @param None 119 * @retval : None 120 */ 121 void SysTick_Handler(void) 122 { 123 } 124 125 126 127 /**************************************************************************** 128 * 129 * 函数名称:void TIM3_IRQHandler(void) 130 * 外部中断处理函数 131 * 132 ****************************************************************************/ 133 134 135 void TIM3_IRQHandler(void) 136 { 137 TIM_ClearITPendingBit(TIM3,TIM_IT_Update); 138 139 if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET) 140 { 141 if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_5)==Bit_RESET) 142 { 143 //LED 熄灭 144 GPIO_SetBits(GPIOB,GPIO_Pin_5); 145 } 146 else 147 { 148 //LED 发光 149 GPIO_ResetBits(GPIOB,GPIO_Pin_5); 150 } 151 } 152 153 } 154 155 156 157 158 /**************************************************************************** 159 * 160 * 函数名称:void EXTI9_5_IRQHandler(void) 161 * 外部中断处理函数 162 * 163 ****************************************************************************/ 164 void EXTI9_5_IRQHandler(void) 165 { 166 if(EXTI_GetITStatus(EXTI_Line5)==SET) //判断是否产生了中断 167 { 168 EXTI_ClearITPendingBit(EXTI_Line5);//中断清除 169 170 delay_ms(10);//延时消抖 171 172 if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET) //再次判断,消抖 173 { 174 if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_5)==Bit_RESET)//如果LED是亮 175 { 176 GPIO_SetBits(GPIOB,GPIO_Pin_5); //则熄灭 177 } 178 else 179 { 180 GPIO_ResetBits(GPIOB,GPIO_Pin_5);//如果是熄灭的,就亮 181 } 182 } 183 } 184 } 185 186 187 188 189 190 191 192 193 194 /**************************************************************************** 195 * Ļ Ԇúvoid EXTI1_IRQHandler(void) 196 * ٦ ŜúEXTI2אԦmԌѲ 197 * ɫࠚӎ˽úϞ 198 * Զࠚӎ˽úϞ 199 * ˵ ķú 200 * ַԃרúϞ 201 ****************************************************************************/ 202 void EXTI1_IRQHandler(void) 203 { 204 205 } 206 207 /**************************************************************************** 208 * Ļ Ԇúvoid EXTI2_IRQHandler(void) 209 * ٦ ŜúEXTI2אԦmԌѲ 210 * ɫࠚӎ˽úϞ 211 * Զࠚӎ˽úϞ 212 * ˵ ķú 213 * ַԃרúϞ 214 ****************************************************************************/ 215 void EXTI2_IRQHandler(void) 216 { 217 218 } 219 220 /**************************************************************************** 221 * 222 * 函数名称:void EXTI3_IRQHandler(void) 223 * 函数功能:外部中断XTI3中断处理函数 224 * 225 ****************************************************************************/ 226 void EXTI3_IRQHandler(void) 227 { 228 229 230 } 231 232 233 /**************************************************************************** 234 * 235 * 函数名称:USART1_IRQHandler(void) 236 * 函数功能:串口中断1处理函数 237 * 238 ****************************************************************************/ 239 void USART1_IRQHandler(void) 240 { 241 242 if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//判断是否发生了串口中断 243 { 244 245 USART_SendData(USART1,USART_ReceiveData(USART1));//从串口中读取数据,然后发送出去 246 247 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位 248 249 } 250 } 251 252 /**************************************************************************** 253 * 254 * 函数名称:WWDG_IRQHandler(void) 255 * 函数功能:窗口看门狗中断处理函数 256 * 257 ****************************************************************************/ 258 void WWDG_IRQHandler(void) 259 { 260 261 WWDG_SetCounter(0x7f);//喂狗 262 263 WWDG_ClearFlag();//清空中断标志 264 265 printf("feed_win_watchdog\n");//打印出来 266 267 }
1 /*pbdata.h*/ 2 /*************************************************************************************************** 3 * 4 * 文件名称:pbdata.h 5 * 文件功能:自定义的函数和全局变量的声明头文件 6 * 7 ***************************************************************************************************/ 8 9 #ifndef _pbdata_H 10 #define _pbdata_H 11 12 13 14 15 16 /******************************************************************** 17 * 18 * 调用的头文件放在这里 19 * 20 ********************************************************************/ 21 #include "stm32f10x.h" 22 23 24 #include "stm32f10x_rcc.h" 25 #include "stm32f10x_gpio.h" 26 #include "misc.h" 27 #include "stm32f10x_adc.h" 28 #include "stm32f10x_bkp.h" 29 #include "stm32f10x_can.h" 30 #include "stm32f10x_cec.h" 31 #include "stm32f10x_dac.h" 32 #include "stm32f10x_dbgmcu.h" 33 #include "stm32f10x_dma.h" 34 #include "stm32f10x_exti.h" 35 #include "stm32f10x_flash.h" 36 #include "stm32f10x_fsmc.h" 37 #include "stm32f10x_i2c.h" 38 #include "stm32f10x_iwdg.h" 39 #include "stm32f10x_pwr.h" 40 #include "stm32f10x_rtc.h" 41 #include "stm32f10x_sdio.h" 42 #include "stm32f10x_spi.h" 43 #include "stm32f10x_tim.h" 44 #include "stm32f10x_usart.h" 45 #include "stm32f10x_wwdg.h" 46 47 48 #include "stdio.h" 49 50 51 52 53 54 /******************************************************************** 55 * 56 * 自定义全局变量声明 57 * 58 ********************************************************************/ 59 extern u8 dt; 60 61 62 63 64 65 /******************************************************************** 66 * 67 * 自定义全函数声明 68 * 69 ********************************************************************/ 70 void delay(u32 nCount); 71 void delay_us(u32 nus); 72 void delay_ms(u16 nms); 73 int fputc(int ch,FILE *F); 74 75 76 77 #endif
1 /**************************************************************************************************************** 2 * 3 * 文件名称:pbdata.c 4 * 文件功能:自定义函数或者全局变量的初始化 5 * 6 ****************************************************************************************************************/ 7 8 /*头文件声明*/ 9 #include "pbdata.h" 10 11 12 13 14 /******************************************************************************************** 15 * 16 * 自定义全局变量 17 * 18 ********************************************************************************************/ 19 u8 dt=0; 20 21 22 23 24 25 /****************************************************************************************** 26 * 27 * 自定义函数 28 * 29 ******************************************************************************************/ 30 31 32 33 /************************************************** 34 * 35 * 函数名称:delay_us(u32 nus) 36 * 函数功能:微秒延时函数 37 * 输入参数:输入值为延时us 38 * 39 ***************************************************/ 40 void delay_us(u32 nus) 41 { 42 u32 temp; 43 SysTick->LOAD = 9*nus;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9次 44 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 45 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 46 47 do 48 { 49 temp=SysTick->CTRL;//标志位,等到一直减到0 50 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 51 52 SysTick->CTRL=0x00; //关闭计数器 53 SysTick->VAL =0X00; //清空计数器 54 } 55 56 57 58 59 60 61 62 /*************************************************** 63 * 64 * 函数名称:delay_ms(u16 nms) 65 * 函数功能:毫秒级延时 66 * 输入参数:输入值位延时ms 67 * 68 ****************************************************/ 69 void delay_ms(u16 nms) 70 { 71 u32 temp; 72 SysTick->LOAD = 9000*nms;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9000次 73 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 74 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 75 76 do 77 { 78 temp=SysTick->CTRL;//标志位,等到一直减到0 79 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 80 81 SysTick->CTRL=0x00; //关闭计数器 82 SysTick->VAL =0X00; //清空计数器 83 } 84 85 86 87 /**************************************************** 88 * 89 * 重定义printf函数部分 90 * 91 ****************************************************/ 92 int fputc(int ch,FILE *F) 93 { 94 95 USART_SendData(USART1,(u8)ch); 96 97 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位 98 99 return ch; 100 }
7、工程下载:
http://download.csdn.net/detail/a1181803348/8766317