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


評論