新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > stm32 低功耗設(shè)計(jì)[操作寄存器+庫(kù)函數(shù)]

stm32 低功耗設(shè)計(jì)[操作寄存器+庫(kù)函數(shù)]

作者: 時(shí)間:2016-11-25 來源:網(wǎng)絡(luò) 收藏
庫(kù)函數(shù)操作
main.c
001#include "stm32f10x.h"
002#include "stdio.h"
003
004#define PRINTF_ON 1
005
006voidRCC_Configuration(void);
007voidGPIO_Configuration(void);
008voidNVIC_Configuration(void);
009voidEXTI_Configuration(void);
010voidIWDG_Configuration(void);
011
012#define PWR_MODE_Sleep 0 //開啟睡眠模式
013
014#define PWR_MODE_DeepSleep 1 //開啟停機(jī)模式
015
016#define PWR_MODE_STANDBY 0 //開啟待機(jī)模式
017
018
019vu32 DelayTime = 10000000;
020
021intmain(void)
022{
023RCC_Configuration();
024GPIO_Configuration();
025NVIC_Configuration();
026EXTI_Configuration();
027
028SysTick_Config(10000000);
029
030while(--DelayTime);
031
032#if PWR_MODE_Sleep //睡眠模式
033
034PWR_EnterSTOPMode(PWR_Regulator_ON,PWR_STOPEntry_WFI);//喚醒后時(shí)鐘變?yōu)閮?nèi)置8MHz
035
036#elif PWR_MODE_DeepSleep //停機(jī)模式
037
038PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI);//喚醒后時(shí)鐘變?yōu)閮?nèi)置8MHz
039
040#elif PWR_MODE_STANDBY //待機(jī)模式
041
042IWDG_Configuration();//設(shè)置為2s內(nèi)不喂狗復(fù)位,使用獨(dú)立看門狗喚醒
043
044PWR_EnterSTANDBYMode();//喚醒后會(huì)初始化程序
045
046#endif
047
048while(1);
049
050}
051
052voidIWDG_Configuration(void)
053{
054RCC_LSICmd(ENABLE);//打開LSI
055while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY)==RESET);
056
057IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
058IWDG_SetPrescaler(IWDG_Prescaler_32);
059IWDG_SetReload(2000);//max 0xFFF 0~4095
060IWDG_ReloadCounter();
061IWDG_Enable();
062}
063
064voidGPIO_Configuration(void)
065{
066GPIO_InitTypeDef GPIO_InitStructure;
067GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
068GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_7;
069GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
070GPIO_Init(GPIOA , &GPIO_InitStructure);
071
072GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
073GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
074GPIO_Init(GPIOA , &GPIO_InitStructure);
075
076GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
077GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
078GPIO_Init(GPIOA , &GPIO_InitStructure);
079
080GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
081GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
082GPIO_Init(GPIOA , &GPIO_InitStructure);
083
084GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
085}
086
087
088voidRCC_Configuration(void)
089{
090/* 定義枚舉類型變量 HSEStartUpStatus */
091ErrorStatus HSEStartUpStatus;
092
093/* 復(fù)位系統(tǒng)時(shí)鐘設(shè)置*/
094RCC_DeInit();
095/* 開啟HSE*/
096RCC_HSEConfig(RCC_HSE_ON);
097/* 等待HSE起振并穩(wěn)定*/
098HSEStartUpStatus = RCC_WaitForHSEStartUp();
099/* 判斷HSE起是否振成功,是則進(jìn)入if()內(nèi)部 */
100if(HSEStartUpStatus == SUCCESS)
101{
102/* 選擇HCLK(AHB)時(shí)鐘源為SYSCLK 1分頻 */
103RCC_HCLKConfig(RCC_SYSCLK_Div1);
104/* 選擇PCLK2時(shí)鐘源為 HCLK(AHB) 1分頻 */
105RCC_PCLK2Config(RCC_HCLK_Div1);
106/* 選擇PCLK1時(shí)鐘源為 HCLK(AHB) 2分頻 */
107RCC_PCLK1Config(RCC_HCLK_Div2);
108/* 設(shè)置FLASH延時(shí)周期數(shù)為2 */
109FLASH_SetLatency(FLASH_Latency_2);
110/* 使能FLASH預(yù)取緩存 */
111FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
112/* 選擇鎖相環(huán)(PLL)時(shí)鐘源為HSE 1分頻,倍頻數(shù)為9,則PLL輸出頻率為 8MHz * 9 = 72MHz */
113RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
114/* 使能PLL */
115RCC_PLLCmd(ENABLE);
116/* 等待PLL輸出穩(wěn)定 */
117while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
118/* 選擇SYSCLK時(shí)鐘源為PLL */
119RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
120/* 等待PLL成為SYSCLK時(shí)鐘源 */
121while(RCC_GetSYSCLKSource() != 0x08);
122}
123/* 打開APB2總線上的GPIOA時(shí)鐘*/
124RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);
125
126//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
127
128RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
129//RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP|RCC_APB1Periph_WWDG, ENABLE);
130
131}
132
133
134voidUSART_Configuration(void)
135{
136USART_InitTypeDef USART_InitStructure;
137USART_ClockInitTypeDef USART_ClockInitStructure;
138
139USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
140USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
141USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
142USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
143USART_ClockInit(USART1 , &USART_ClockInitStructure);
144
145USART_InitStructure.USART_BaudRate = 9600;
146USART_InitStructure.USART_WordLength = USART_WordLength_8b;
147USART_InitStructure.USART_StopBits = USART_StopBits_1;
148USART_InitStructure.USART_Parity = USART_Parity_No;
149USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
150USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
151USART_Init(USART1,&USART_InitStructure);
152
153USART_Cmd(USART1,ENABLE);
154}
155
156voidEXTI_Configuration(void)
157{
158EXTI_InitTypeDef EXTI_InitStructure;
159
160EXTI_InitStructure.EXTI_Line = EXTI_Line0;
161EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
162EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
163EXTI_InitStructure.EXTI_LineCmd = ENABLE;
164
165EXTI_Init(&EXTI_InitStructure);
166
167}
168
169voidNVIC_Configuration(void)
170{
171NVIC_InitTypeDef NVIC_InitStructure;
172
173NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
174
175NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
176NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
177NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
178NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
179NVIC_Init(&NVIC_InitStructure);
180}
181
182#if PRINTF_ON
183
184intfputc(intch,FILE*f)
185{
186USART_SendData(USART1,(u8) ch);
187while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
188returnch;
189}
190
191#endif
stm32f10x_it.c
view source
print?
01#include "stm32f10x_it.h"
02
03#include "stdio.h"
04
05
06voidEXTI0_IRQHandler(void)
07{
08GPIO_WriteBit(GPIOA,GPIO_Pin_7,Bit_SET);
09
10//EXTI_ClearFlag(EXTI_Line0); //清除此中斷標(biāo)志位,系統(tǒng)由于喚醒將直接復(fù)位
11
12}
13
14voidSysTick_Handler(void)
15{
16GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_4)));
17}


上一頁(yè) 1 2 下一頁(yè)

評(píng)論


技術(shù)專區(qū)

關(guān)閉