Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130825
  • 博文数量: 64
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-26 18:44
文章分类

全部博文(64)

文章存档

2014年(64)

我的朋友

分类: C/C++

2014-04-26 19:06:28

原文地址:PWR待机模式例程描述文件 作者:

 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 PIN6LD1)被翻转,这是用来指示是否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-EVALSTM3210E-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.

注释:要在待机模式中测量功耗,你必须用一个电流表替代跳线JP9STM3210B-EVAL评估板中或者是替代跳线JP12STM3210E-EVAL

******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE******

-----------------------------------------以上是TI的例程------------------------------------------------------------

 STANDBY2.rar   

现在改为万利开发板STM3210B-LK1所用:

点击(此处)折叠或打开

  1. /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
  2. * File Name : main.c
  3. * Author : MCD Application Team
  4. * Version : V2.0.3
  5. * Date : 09/22/2008
  6. * Description : Main program body.
  7. ********************************************************************************
  8. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/
  15. /* Includes ------------------------------------------------------------------*/
  16. #include "stm32f10x_lib.h"
  17. #include "platform_config.h"
  18. /* Private typedef -----------------------------------------------------------*/
  19. /* Private define ------------------------------------------------------------*/
  20. /* Private macro -------------------------------------------------------------*/
  21. /* Private variables ---------------------------------------------------------*/
  22. ErrorStatus HSEStartUpStatus;
  23. /* Private function prototypes -----------------------------------------------*/
  24. void RCC_Configuration(void);
  25. void GPIO_Configuration(void);
  26. void EXTI_Configuration(void);
  27. void RTC_Configuration(void);
  28. void NVIC_Configuration(void);
  29. void SysTick_Configuration(void);
  30. /* Private functions ---------------------------------------------------------*/
  31. /*******************************************************************************
  32. * Function Name : main
  33. * Description : Main program.
  34. * Input : None
  35. * Output : None
  36. * Return : None
  37. *******************************************************************************/
  38. int main(void)
  39. {
  40. #ifdef DEBUG
  41. debug();
  42. #endif
  43. /* System Clocks Configuration */
  44. RCC_Configuration();
  45. /* GPIO configuration */
  46. GPIO_Configuration();
  47. /* Turn on led connected to GPIO_LED Pin6 */
  48. GPIO_SetBits(GPIO_LED, GPIO_Pin_7); //LED1亮
  49. /* Enable PWR and BKP clock */
  50. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  51. /* Enable WKUP pin */
  52. PWR_WakeUpPinCmd(ENABLE);//使能唤醒管脚功能 可以不用这句
  53. /* Allow access to BKP Domain */
  54. PWR_BackupAccessCmd(ENABLE); //使能RTC和后备寄存器访问  比用 后面的RTC闹钟设置得能用啊
  55. /* Configure RTC clock source and prescaler */
  56. RTC_Configuration();
  57. /* Configure EXTI Line to generate an interrupt on falling edge */
  58. EXTI_Configuration();
  59. /* NVIC configuration */
  60. NVIC_Configuration();
  61. /* Configure the SysTick to generate an interrupt each 250 ms */
  62. SysTick_Configuration();  //可以不用,方便观察而已
  63. while (1)
  64. {
  65. }
  66. }
  67. /*******************************************************************************
  68. * Function Name : RCC_Configuration
  69. * Description : Configures the different system clocks.
  70. * Input : None
  71. * Output : None
  72. * Return : None
  73. *******************************************************************************/
  74. void RCC_Configuration(void)
  75. {
  76. /* RCC system reset(for debug purpose) */
  77. RCC_DeInit();
  78. /* Enable HSE */
  79. RCC_HSEConfig(RCC_HSE_ON);
  80. /* Wait till HSE is ready */
  81. HSEStartUpStatus = RCC_WaitForHSEStartUp();
  82. if(HSEStartUpStatus == SUCCESS)
  83. {
  84. /* Enable Prefetch Buffer */
  85. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  86. /* Flash 2 wait state */
  87. FLASH_SetLatency(FLASH_Latency_2);
  88. /* HCLK = SYSCLK */
  89. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  90. /* PCLK2 = HCLK */
  91. RCC_PCLK2Config(RCC_HCLK_Div1);
  92. /* PCLK1 = HCLK/2 */
  93. RCC_PCLK1Config(RCC_HCLK_Div2);
  94. /* PLLCLK = 8MHz * 9 = 72 MHz */
  95. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  96. /* Enable PLL */
  97. RCC_PLLCmd(ENABLE);
  98. /* Wait till PLL is ready */
  99. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  100. {
  101. }
  102. /* Select PLL as system clock source */
  103. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  104. /* Wait till PLL is used as system clock source */
  105. while(RCC_GetSYSCLKSource() != 0x08)
  106. {
  107. }
  108. }
  109. }
  110. /*******************************************************************************
  111. * Function Name : GPIO_Configuration
  112. * Description : Configures the different GPIO ports.
  113. * Input : None
  114. * Output : None
  115. * Return : None
  116. *******************************************************************************/
  117. void GPIO_Configuration(void)
  118. {
  119. GPIO_InitTypeDef GPIO_InitStructure;
  120. /* Enable Key Button GPIO Port, GPIO_LED and AFIO clock */
  121. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_KEY_BUTTON | RCC_APB2Periph_GPIO_LED
  122. | RCC_APB2Periph_AFIO, ENABLE);
  123. /* Configure GPIO_LED Pin 6 and Pin 7 as Output push-pull */
  124. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7;
  125. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  126. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  127. GPIO_Init(GPIO_LED, &GPIO_InitStructure);
  128. /* Configure Key Button GPIO Pin as input floating */
  129. GPIO_InitStructure.GPIO_Pin = GPIO_PIN_KEY_BUTTON;
  130. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  131. GPIO_Init(GPIO_KEY_BUTTON, &GPIO_InitStructure);
  132. }
  133. /*******************************************************************************
  134. * Function Name : EXTI_Configuration
  135. * Description : Configures EXTI Line.
  136. * Input : None
  137. * Output : None
  138. * Return : None
  139. *******************************************************************************/
  140. void EXTI_Configuration(void)
  141. {
  142. EXTI_InitTypeDef EXTI_InitStructure;
  143. /* Connect Key Button EXTI Line to Key Button GPIO Pin */
  144. GPIO_EXTILineConfig(GPIO_PORT_SOURCE_KEY_BUTTON, GPIO_PIN_SOURCE_KEY_BUTTON);
  145. /* Configure Key Button EXTI Line to generate an interrupt on falling edge */
  146. EXTI_InitStructure.EXTI_Line = EXTI_LINE_KEY_BUTTON;
  147. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  148. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  149. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  150. EXTI_Init(&EXTI_InitStructure);
  151. }
  152. /*******************************************************************************
  153. * Function Name : RTC_Configuration
  154. * Description : Configures RTC clock source and prescaler.
  155. * Input : None
  156. * Output : None
  157. * Return : None
  158. *******************************************************************************/
  159. void RTC_Configuration(void)
  160. {
  161. /* Check if the StandBy flag is set */
  162. if(PWR_GetFlagStatus(PWR_FLAG_SB) != RESET) //如果系统进入待机模式
  163. {
  164. /* 系统从待机模式唤醒 */
  165. GPIO_SetBits(GPIO_LED, GPIO_Pin_6); //LED2亮
  166. PWR_ClearFlag(PWR_FLAG_SB); //清除待机位
  167. /* 等待最近一次对 RTC 寄存器的写操作完成 */
  168. RTC_WaitForSynchro();
  169. /* No need to configure the RTC as the RTC configuration(clock source, enable,
  170. prescaler,...) is kept after wake-up from STANDBY */
  171. }
  172. else
  173. {/* StandBy flag is not set */
  174. /* RTC clock source configuration ----------------------------------------*/
  175. /* Reset Backup Domain */
  176. BKP_DeInit();
  177. /* Enable LSE OSC */
  178. RCC_LSEConfig(RCC_LSE_ON);
  179. /* Wait till LSE is ready */
  180. while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  181. {
  182. }
  183. /* Select the RTC Clock Source */
  184. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  185. /* Enable the RTC Clock */
  186. RCC_RTCCLKCmd(ENABLE);
  187. /* RTC configuration -----------------------------------------------------*/
  188. /* Wait for RTC APB registers synchronisation */
  189. RTC_WaitForSynchro();
  190. /* Set the RTC time base to 1s */
  191. RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
  192. /* Wait until last write operation on RTC registers has finished */
  193. RTC_WaitForLastTask();
  194. }
  195. }
  196. /*******************************************************************************
  197. * Function Name : NVIC_Configuration
  198. * Description : Configures NVIC and Vector Table base location.
  199. * Input : None
  200. * Output : None
  201. * Return : None
  202. *******************************************************************************/
  203. void NVIC_Configuration(void)
  204. {
  205. NVIC_InitTypeDef NVIC_InitStructure;
  206. /* Set the Vector Table base location at 0x08000000 */
  207. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  208. NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQChannel;
  209. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  210. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  211. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  212. NVIC_Init(&NVIC_InitStructure);
  213. }
  214. /*******************************************************************************
  215. * Function Name : SysTick_Configuration
  216. * Description : Configures the SysTick to generate an interrupt each 250 ms.
  217. * Input : None
  218. * Output : None
  219. * Return : None
  220. *******************************************************************************/
  221. void SysTick_Configuration(void)
  222. {
  223. /* Select AHB clock(HCLK) divided by 8 as SysTick clock source */
  224. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //时钟除8
  225. /* Set SysTick Preemption Priority to 1 */
  226. NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 1, 0);//中断等级设置,一般设置的高一些会少受其他影响
  227. /* SysTick interrupt each 250 ms with HCLK equal to 9MHz */
  228. SysTick_SetReload(2250000); //计数周期长度
  229. /* Enable the SysTick Interrupt */
  230. SysTick_ITConfig(ENABLE); //打开中断
  231. /* Enable the SysTick Counter */
  232. SysTick_CounterCmd(SysTick_Counter_Enable); //启动计时器
  233. }
  234. #ifdef DEBUG
  235. /*******************************************************************************
  236. * Function Name : assert_failed
  237. * Description : Reports the name of the source file and the source line number
  238. * where the assert_param error has occurred.
  239. * Input : - file: pointer to the source file name
  240. * - line: assert_param error line source number
  241. * Output : None
  242. * Return : None
  243. *******************************************************************************/
  244. void assert_failed(u8* file, u32 line)
  245. {
  246. /* User can add his own implementation to report the file name and line number,
  247. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  248. /* Infinite loop */
  249. while (1)
  250. {
  251. }
  252. }
  253. #endif
  254. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

中断处理程序:

点击(此处)折叠或打开

  1. void EXTI3_IRQHandler(void)
  2. {
  3. if(EXTI_GetITStatus(EXTI_LINE_KEY_BUTTON) != RESET)
  4. {
  5. /* Clear the Key Button EXTI line pending bit */
  6. EXTI_ClearITPendingBit(EXTI_LINE_KEY_BUTTON);
  7. /* Turn on led connected to GPIO_LED1 pin7 */
  8. GPIO_SetBits(GPIO_LED, GPIO_Pin_7);
  9. /* Wait till RTC Second event occurs不要忘记这里,会出现退出异常 */
  10. RTC_ClearFlag(RTC_FLAG_SEC);
  11. while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET);
  12. /* Set the RTC Alarm after 3s这个必须要,RTC闹钟可以在3秒后唤醒待机模式的 */
  13. RTC_SetAlarm(RTC_GetCounter()+ 3);
  14. /* Wait until last write operation on RTC registers has finished */
  15. RTC_WaitForLastTask();
  16. /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */
  17. PWR_EnterSTANDBYMode();
  18. }
  19. }

点击(此处)折叠或打开

  1. void SysTickHandler(void)
  2. {
  3. /* Toggle Led connected to GPIO_LED Pin6 */
  4. GPIO_WriteBit(GPIO_LED, GPIO_Pin_7, (BitAction)(1-GPIO_ReadOutputDataBit(GPIO_LED, GPIO_Pin_7)));
  5. }

阅读(1056) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~