STANDBY.rar /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name : readme.txt
* Author : MCD Application Team
* Version : V2.0.3
* Date : 09/22/2008
* Description : Description of the PWR STANDBY Example.
*******************************************************************************
Example description 例程描述
===================
This example shows how to enters the system to STANDBY mode and wake-up from this
mode using: external RESET, RTC Alarm or WKUP pin.
In the associated software, the system clock is set to 72 MHz, an EXTI line is configured to generate an interrupt on falling edge and the SysTick is programmed
to generate an interrupt each 250 ms. In the SysTick interrupt handler, a led
connected to GPIO_LED Pin6(LD1) is toggled, this is used to indicate whether the MCU is
in STANDBY or RUN mode.
这个例子显示如何从正在使用的模式(外部复位,RTC报警或WKUP引脚)进入系统待机模式和唤醒模式。
在相关的软件配置中,系统时钟被设置为72MHZ,外部中断线配置为产生一个下降沿中断,SysTick滴答时钟编程用于每250毫秒产生一个中断。在SysTick中断处理程序中,一个LED连接到GPIO_LED PIN6(LD1)被翻转,这是用来指示是否MCU在待机或运行模式。
When a falling edge is detected on the EXTI line an interrupt is generated. In the
EXTI handler routine the RTC is configured to generate an Alarm event in 3 second
then the system enters STANDBY mode causing the LD1 to stop toggling.
A rising edge on WKUP pin or an external RESET will wake-up the system from
STANDBY. If within 3 second neither rising edge on WKUP pin nor external RESET
are generated, the RTC Alarm will wake-up the system.
After wake-up from STANDBY mode, program execution restarts in the same way as after
a RESET, the LD1 restarts toggling, GPIO_LED Pin6 is set to high level and the RTC
configuration (clock source, enable, prescaler,...) is kept. As result there is no
need to configure the RTC.
当在外部中断线上检测到一个下降沿将发生中断。在中断处理例程中这RTC被配置用于在3秒内产生一个报警事件,然后系统进入待机模式引发LD1灯停止翻转。当在WKUP引脚或者是外部复位上出现上升沿将把系统从待机模式唤醒,如果3秒内在WKUP引脚或者是外部复位上没上升沿,这RTC时钟将唤醒系统。
在从待机模式唤醒系统后,用同样的方式让执行程序重新启动作为一个复位,这LD1重新翻转,GPIO_LED Pin6被设置为高电平,这RTC配置(时钟源,使能,预分频,..)被保存,因而也没需要配置RTC。
Two leds connected to GPIO_LED Pin6(LD1) and Pin7(LD2) pins are used to monitor
the system state as following:
- LD1 toggling: system in RUN mode LD1一闪一亮表示系统处在运行模式
- LD1 off / LD2 off: system in STANDBY mode LD1灭和LD2灭在待机模式
- LD2 on: system resumed from STANDBY mode LD2亮表示从待机模式唤醒
Directory contents 目录内容
==================
platform_config.h Evaluation board specific configuration file 评估板特定的配置文件
stm32f10x_conf.h Library Configuration file 库配置文件
stm32f10x_it.c Interrupt handlers 中断处理程序
stm32f10x_it.h Header for stm32f10x_it.c 中断处理程序头文件
main.c Main program 主程序
main.h Header for main.c 主程序头文件
Hardware environment 硬件环境
====================
This example runs on STMicroelectronics STM3210B-EVAL and STM3210E-EVAL evaluation boards and can be easily tailored to any other hardware.
To select the STMicroelectronics evaluation board used to run the example, uncomment
the corresponding line in platform_config.h file.
这个例程可以运行在意法半导体STM3210B-EVAL和STM3210E-EVAL评估板上,并可以很容易地移植到任何其他硬件。
如果要选择意法半导体相应的评估板用于运行例子,则记得要取消platform_config.h文件中相应的行。
+ STM3210B-EVAL
- Use LD1 and LD2 leds connected respectively to PC.06 and PC.07 pins
- Use the Key push-button connected to pin PB.09 (EXTI Line9).
- Use the Wakep push-button connected to WKUP(PA.00) pin
+ STM3210E-EVAL(略)
- Use LD1 and LD2 leds connected respectively to PF.06 and PF.07 pins
- Use the Key push-button connected to pin PG.08 (EXTI Line8).
- Use the Wakep push-button connected to WKUP(PA.00) pin
Note: the jumper JP4 must be not fit to be able to use the Wakeup push-button
How to use it 如何使用例程
=============
In order to make the program work, you must do the following :为了使该方案的工作,你必须做到以下几点:
- Create a project and setup all your toolchain's start-up files:创建一个项目,并设置您的使用工具的启动文件
- Compile the directory content files and required Library files:编译的目录内容的文件和所需的库文件
+ stm32f10x_nvic.c
+ stm32f10x_rcc.c
+ stm32f10x_lib.c
+ stm32f10x_gpio.c
+ stm32f10x_rtc.c
+ stm32f10x_pwr.c
+ stm32f10x_bkp.c
+ stm32f10x_exti.c
+ stm32f10x_flash.c
+ stm32f10x_systick.c
- Link all compiled files and load your image into target memory :链接所有编译的文件,并加载你的映像文件到目标存储器中
- Run the example in standalone mode (without debugger connection):运行示例在独立模式(没有调试器连接)
NOTE: For power consumption measurement in STANDBY mode, you have to replace
jumper JP9 in the STM3210B-EVAL board or JP12 in the STM3210E-EVAL board
by an amperemeter.
注释:要在待机模式中测量功耗,你必须用一个电流表替代跳线JP9在STM3210B-EVAL评估板中或者是替代跳线JP12在STM3210E-EVAL中
******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE******
-----------------------------------------以上是TI的例程------------------------------------------------------------
STANDBY2.rar
现在改为万利开发板STM3210B-LK1所用:
- /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
- * File Name : main.c
- * Author : MCD Application Team
- * Version : V2.0.3
- * Date : 09/22/2008
- * Description : Main program body.
- ********************************************************************************
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
- * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
- * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
- * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
- * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *******************************************************************************/
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x_lib.h"
- #include "platform_config.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- ErrorStatus HSEStartUpStatus;
- /* Private function prototypes -----------------------------------------------*/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void EXTI_Configuration(void);
- void RTC_Configuration(void);
- void NVIC_Configuration(void);
- void SysTick_Configuration(void);
- /* Private functions ---------------------------------------------------------*/
- /*******************************************************************************
- * Function Name : main
- * Description : Main program.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- int main(void)
- {
- #ifdef DEBUG
- debug();
- #endif
-
- /* System Clocks Configuration */
- RCC_Configuration();
- /* GPIO configuration */
- GPIO_Configuration();
- /* Turn on led connected to GPIO_LED Pin6 */
- GPIO_SetBits(GPIO_LED, GPIO_Pin_7); //LED1亮
- /* Enable PWR and BKP clock */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
- /* Enable WKUP pin */
- PWR_WakeUpPinCmd(ENABLE);//使能唤醒管脚功能 可以不用这句
- /* Allow access to BKP Domain */
- PWR_BackupAccessCmd(ENABLE); //使能RTC和后备寄存器访问 比用 后面的RTC闹钟设置得能用啊
- /* Configure RTC clock source and prescaler */
- RTC_Configuration();
- /* Configure EXTI Line to generate an interrupt on falling edge */
- EXTI_Configuration();
- /* NVIC configuration */
- NVIC_Configuration();
- /* Configure the SysTick to generate an interrupt each 250 ms */
- SysTick_Configuration(); //可以不用,方便观察而已
-
- while (1)
- {
- }
- }
- /*******************************************************************************
- * Function Name : RCC_Configuration
- * Description : Configures the different system clocks.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void RCC_Configuration(void)
- {
- /* RCC system reset(for debug purpose) */
- RCC_DeInit();
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
-
- /* HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- /* PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* Enable PLL */
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- }
- /*******************************************************************************
- * Function Name : GPIO_Configuration
- * Description : Configures the different GPIO ports.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable Key Button GPIO Port, GPIO_LED and AFIO clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_KEY_BUTTON | RCC_APB2Periph_GPIO_LED
- | RCC_APB2Periph_AFIO, ENABLE);
- /* Configure GPIO_LED Pin 6 and Pin 7 as Output push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIO_LED, &GPIO_InitStructure);
- /* Configure Key Button GPIO Pin as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_PIN_KEY_BUTTON;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIO_KEY_BUTTON, &GPIO_InitStructure);
- }
- /*******************************************************************************
- * Function Name : EXTI_Configuration
- * Description : Configures EXTI Line.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void EXTI_Configuration(void)
- {
- EXTI_InitTypeDef EXTI_InitStructure;
- /* Connect Key Button EXTI Line to Key Button GPIO Pin */
- GPIO_EXTILineConfig(GPIO_PORT_SOURCE_KEY_BUTTON, GPIO_PIN_SOURCE_KEY_BUTTON);
- /* Configure Key Button EXTI Line to generate an interrupt on falling edge */
- EXTI_InitStructure.EXTI_Line = EXTI_LINE_KEY_BUTTON;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- }
- /*******************************************************************************
- * Function Name : RTC_Configuration
- * Description : Configures RTC clock source and prescaler.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void RTC_Configuration(void)
- {
- /* Check if the StandBy flag is set */
- if(PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) //如果系统进入待机模式
- {
- /* 系统从待机模式唤醒 */
-
- GPIO_SetBits(GPIO_LED, GPIO_Pin_6); //LED2亮
-
- PWR_ClearFlag(PWR_FLAG_SB); //清除待机位
- /* 等待最近一次对 RTC 寄存器的写操作完成 */
- RTC_WaitForSynchro();
- /* No need to configure the RTC as the RTC configuration(clock source, enable,
- prescaler,...) is kept after wake-up from STANDBY */
- }
- else
- {/* StandBy flag is not set */
- /* RTC clock source configuration ----------------------------------------*/
- /* Reset Backup Domain */
- BKP_DeInit();
-
- /* Enable LSE OSC */
- RCC_LSEConfig(RCC_LSE_ON);
- /* Wait till LSE is ready */
- while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
- {
- }
- /* Select the RTC Clock Source */
- RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
- /* Enable the RTC Clock */
- RCC_RTCCLKCmd(ENABLE);
- /* RTC configuration -----------------------------------------------------*/
- /* Wait for RTC APB registers synchronisation */
- RTC_WaitForSynchro();
- /* Set the RTC time base to 1s */
- RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
- /* Wait until last write operation on RTC registers has finished */
- RTC_WaitForLastTask();
- }
- }
- /*******************************************************************************
- * Function Name : NVIC_Configuration
- * Description : Configures NVIC and Vector Table base location.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Set the Vector Table base location at 0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- /*******************************************************************************
- * Function Name : SysTick_Configuration
- * Description : Configures the SysTick to generate an interrupt each 250 ms.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SysTick_Configuration(void)
- {
- /* Select AHB clock(HCLK) divided by 8 as SysTick clock source */
- SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //时钟除8
- /* Set SysTick Preemption Priority to 1 */
- NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 1, 0);//中断等级设置,一般设置的高一些会少受其他影响
-
- /* SysTick interrupt each 250 ms with HCLK equal to 9MHz */
- SysTick_SetReload(2250000); //计数周期长度
- /* Enable the SysTick Interrupt */
- SysTick_ITConfig(ENABLE); //打开中断
- /* Enable the SysTick Counter */
- SysTick_CounterCmd(SysTick_Counter_Enable); //启动计时器
- }
- #ifdef DEBUG
- /*******************************************************************************
- * Function Name : assert_failed
- * Description : Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * Input : - file: pointer to the source file name
- * - line: assert_param error line source number
- * Output : None
- * Return : None
- *******************************************************************************/
- void assert_failed(u8* file, u32 line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
中断处理程序:
- void EXTI3_IRQHandler(void)
- {
- if(EXTI_GetITStatus(EXTI_LINE_KEY_BUTTON) != RESET)
- {
- /* Clear the Key Button EXTI line pending bit */
- EXTI_ClearITPendingBit(EXTI_LINE_KEY_BUTTON);
- /* Turn on led connected to GPIO_LED1 pin7 */
- GPIO_SetBits(GPIO_LED, GPIO_Pin_7);
- /* Wait till RTC Second event occurs不要忘记这里,会出现退出异常 */
- RTC_ClearFlag(RTC_FLAG_SEC);
- while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET);
- /* Set the RTC Alarm after 3s这个必须要,RTC闹钟可以在3秒后唤醒待机模式的 */
- RTC_SetAlarm(RTC_GetCounter()+ 3);
- /* Wait until last write operation on RTC registers has finished */
- RTC_WaitForLastTask();
- /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */
- PWR_EnterSTANDBYMode();
- }
- }
- void SysTickHandler(void)
- {
- /* Toggle Led connected to GPIO_LED Pin6 */
- GPIO_WriteBit(GPIO_LED, GPIO_Pin_7, (BitAction)(1-GPIO_ReadOutputDataBit(GPIO_LED, GPIO_Pin_7)));
- }