新闻中心

EEPW首页>嵌入式系统>设计应用> stm32 独立看门狗[操作寄存器+库函数]

stm32 独立看门狗[操作寄存器+库函数]

作者: 时间:2016-11-30 来源:网络 收藏


库函数操作

main.c
view source
print?
001 #include "stm32f10x.h"
002 #include "stdio.h"
003
004 #define PRINTF_ON 1
005
006 void RCC_Configuration(void);
007 void GPIO_Configuration(void);
008 void NVIC_Configuration(void);
009 void USART_Configuration(void);
010 void IWDG_Configuration(void);
011 void EXTI_Configuration(void);
012
013 vu32 DelayTime;
014
015 int main(void)
016 {
017 RCC_Configuration();
018 GPIO_Configuration();
019 NVIC_Configuration();
020 USART_Configuration();
021 EXTI_Configuration();
022 IWDG_Configuration();
023
024 while(1){
025 if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET)
026 {
027 printf("The Stm32 has been reset by IWDG .");
028 RCC_ClearFlag();
029 }
030
031 //do sth. here
032 DelayTime = 100000;
033 while(--DelayTime);
034 // 延时17ms
035
036 IWDG_ReloadCounter(); //80ms不喂狗复位
037 GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)(1- GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_4)));
038 }
039 }
040
041 void EXTI_Configuration(void)
042 {
043 EXTI_InitTypeDef EXTI_InitStructure;
044
045 EXTI_InitStructure.EXTI_Line = EXTI_Line0;
046 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
047 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
048 EXTI_InitStructure.EXTI_LineCmd = ENABLE;
049 EXTI_Init(&EXTI_InitStructure);
050
051 GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
052
053 }
054
055 void GPIO_Configuration(void)
056 {
057 GPIO_InitTypeDef GPIO_InitStructure;
058 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
059 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
060 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
061 GPIO_Init(GPIOA , &GPIO_InitStructure);
062
063 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
064 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
065 GPIO_Init(GPIOA , &GPIO_InitStructure);
066
067
068 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
069 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
070 GPIO_Init(GPIOA , &GPIO_InitStructure);
071
072 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
073 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
074 GPIO_Init(GPIOA , &GPIO_InitStructure);
075
076 }
077
078 void IWDG_Configuration(void)
079 {
080 RCC_LSICmd(ENABLE); //打开LSI
081 while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY)==RESET);
082
083 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
084 IWDG_SetPrescaler(IWDG_Prescaler_32);
085 IWDG_SetReload(100); //80ms ,max 0xFFF 0~4095
086 IWDG_ReloadCounter();
087 IWDG_Enable();
088 }
089
090
091 void RCC_Configuration(void)
092 {
093 /* 定义枚举类型变量 HSEStartUpStatus */
094 ErrorStatus HSEStartUpStatus;
095
096 /* 复位系统时钟设置*/
097 RCC_DeInit();
098 /* 开启HSE*/
099 RCC_HSEConfig(RCC_HSE_ON);
100 /* 等待HSE起振并稳定*/
101 HSEStartUpStatus = RCC_WaitForHSEStartUp();
102 /* 判断HSE起是否振成功,是则进入if()内部 */
103 if(HSEStartUpStatus == SUCCESS)
104 {
105 /* 选择HCLK(AHB)时钟源为SYSCLK 1分频 */
106 RCC_HCLKConfig(RCC_SYSCLK_Div1);
107 /* 选择PCLK2时钟源为 HCLK(AHB) 1分频 */
108 RCC_PCLK2Config(RCC_HCLK_Div1);
109 /* 选择PCLK1时钟源为 HCLK(AHB) 2分频 */
110 RCC_PCLK1Config(RCC_HCLK_Div2);
111 /* 设置FLASH延时周期数为2 */
112 FLASH_SetLatency(FLASH_Latency_2);
113 /* 使能FLASH预取缓存 */
114 FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
115 /* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz * 9 = 72MHz */
116 RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
117 /* 使能PLL */
118 RCC_PLLCmd(ENABLE);
119 /* 等待PLL输出稳定 */
120 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
121 /* 选择SYSCLK时钟源为PLL */
122 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
123 /* 等待PLL成为SYSCLK时钟源 */
124 while(RCC_GetSYSCLKSource() != 0x08);
125 }
126 /* 打开APB2总线上的GPIOA时钟*/
127 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);
128
129 //RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP|RCC_APB1Periph_WWDG, ENABLE);
130
131 }
132
133
134 void USART_Configuration(void)
135 {
136 USART_InitTypeDef USART_InitStructure;
137 USART_ClockInitTypeDef USART_ClockInitStructure;
138
139 USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
140 USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
141 USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
142 USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
143 USART_ClockInit(USART1 , &USART_ClockInitStructure);
144
145 USART_InitStructure.USART_BaudRate = 9600;
146 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
147 USART_InitStructure.USART_StopBits = USART_StopBits_1;
148 USART_InitStructure.USART_Parity = USART_Parity_No;
149 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
150 USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
151 USART_Init(USART1,&USART_InitStructure);
152
153 USART_Cmd(USART1,ENABLE);
154 }
155
156
157 void NVIC_Configuration(void)
158 {
159 NVIC_InitTypeDef NVIC_InitStructure;
160
161
162 NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
163 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
164 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
165 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
166 NVIC_Init(&NVIC_InitStructure);
167
168
169 }
170
171
172 #if PRINTF_ON
173
174 int fputc(int ch,FILE *f)
175 {
176 USART_SendData(USART1,(u8) ch);
177 while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
178 return ch;
179 }
180
181 #endif

本文引用地址://m.amcfsurvey.com/article/201611/323737.htm

上一页 1 2 下一页

评论


技术专区

关闭