stm32 獨(dú)立看門狗[操作寄存器+庫函數(shù)]
庫函數(shù)操作
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不喂狗復(fù)位
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 /* 復(fù)位系統(tǒng)時鐘設(shè)置*/
097 RCC_DeInit();
098 /* 開啟HSE*/
099 RCC_HSEConfig(RCC_HSE_ON);
100 /* 等待HSE起振并穩(wěn)定*/
101 HSEStartUpStatus = RCC_WaitForHSEStartUp();
102 /* 判斷HSE起是否振成功,是則進(jìn)入if()內(nèi)部 */
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 /* 設(shè)置FLASH延時周期數(shù)為2 */
112 FLASH_SetLatency(FLASH_Latency_2);
113 /* 使能FLASH預(yù)取緩存 */
114 FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
115 /* 選擇鎖相環(huán)(PLL)時鐘源為HSE 1分頻,倍頻數(shù)為9,則PLL輸出頻率為 8MHz * 9 = 72MHz */
116 RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
117 /* 使能PLL */
118 RCC_PLLCmd(ENABLE);
119 /* 等待PLL輸出穩(wěn)定 */
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本文引用地址:http://m.butianyuan.cn/article/201611/323737.htm
評論