[牛人杂谈] 掉电模式的应用

[复制链接]
1400|9
 楼主| xuanhuanzi 发表于 2022-1-21 13:58 | 显示全部楼层 |阅读模式
TE, ck, UART, IO, se, gp
单片机低功耗的关键就是休眠,休眠的最高境界就是掉电模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 2 $
  5. * $Date: 16/10/25 4:29p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to wake up system from Power-down mode by GPIO interrupt.
  7. * @note
  8. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC029xGE.h"


  12. #define PLLCTL_SETTING  CLK_PLLCTL_72MHz_HXT
  13. #define PLL_CLOCK       72000000


  14. /*---------------------------------------------------------------------------------------------------------*/
  15. /*  Function for System Entry to Power Down Mode                                                           */
  16. /*---------------------------------------------------------------------------------------------------------*/
  17. void PowerDownFunction(void)
  18. {
  19.     /* Check if all the debug messages are finished */
  20.     UART_WAIT_TX_EMPTY(UART0);

  21.     /* Enter to Power-down mode */
  22.     CLK_PowerDown();
  23. }

  24. /**
  25. * @brief       PortA/PortB IRQ
  26. *
  27. * @param       None
  28. *
  29. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  30. *
  31. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The PortA/PortB default IRQ, declared in startup_NUC029xGE.s.
  32. */
  33. void GPAB_IRQHandler(void)
  34. {
  35.     /* To check if PB.3 interrupt occurred */
  36.     if(GPIO_GET_INT_FLAG(PB, BIT3))
  37.     {
  38.         GPIO_CLR_INT_FLAG(PB, BIT3);
  39.         printf("PB.3 INT occurred.\n");
  40.     }
  41.     else
  42.     {
  43.         /* Un-expected interrupt. Just clear all PB interrupts */
  44.         PB->INTSRC = PB->INTSRC;
  45.         printf("Un-expected interrupts.\n");
  46.     }
  47. }

  48. void SYS_Init(void)
  49. {

  50.     /*---------------------------------------------------------------------------------------------------------*/
  51.     /* Init System Clock                                                                                       */
  52.     /*---------------------------------------------------------------------------------------------------------*/

  53.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  54.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  55.     /* Wait for HIRC clock ready */
  56.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  57.     /* Select HCLK clock source as HIRC and HCLK source divider as 1 */
  58.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  59.     /* Enable HXT clock (external XTAL 12MHz) */
  60.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  61.     /* Wait for HXT clock ready */
  62.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  63.     /* Set core clock as PLL_CLOCK from PLL */
  64.     CLK_SetCoreClock(PLL_CLOCK);

  65.     /* Enable UART module clock */
  66.     CLK_EnableModuleClock(UART0_MODULE);

  67.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  68.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Init I/O Multi-function                                                                                 */
  71.     /*---------------------------------------------------------------------------------------------------------*/

  72.     /* Set multi-function pins for UART0 RXD and TXD */
  73.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
  74.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);

  75. }

  76. void UART0_Init()
  77. {
  78.     /*---------------------------------------------------------------------------------------------------------*/
  79.     /* Init UART                                                                                               */
  80.     /*---------------------------------------------------------------------------------------------------------*/
  81.     /* Reset UART0 */
  82.     SYS_ResetModule(UART0_RST);

  83.     /* Configure UART0 and set UART0 baud rate */
  84.     UART_Open(UART0, 115200);
  85. }

  86. /*---------------------------------------------------------------------------------------------------------*/
  87. /* MAIN function                                                                                           */
  88. /*---------------------------------------------------------------------------------------------------------*/
  89. int main(void)
  90. {
  91.     /* Unlock protected registers */
  92.     SYS_UnlockReg();

  93.     /* Init System, peripheral clock and multi-function I/O */
  94.     SYS_Init();

  95.     /* Lock protected registers */
  96.     SYS_LockReg();

  97.     /* Init UART0 for printf */
  98.     UART0_Init();

  99.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  100.     printf("+-------------------------------------------------------+\n");
  101.     printf("|    GPIO Power-Down and Wake-up by PB.3 Sample Code    |\n");
  102.     printf("+-------------------------------------------------------+\n\n");

  103.     /* Configure PB.3 as Input mode and enable interrupt by rising edge trigger */
  104.     GPIO_SetMode(PB, BIT3, GPIO_MODE_INPUT);
  105.     GPIO_EnableInt(PB, 3, GPIO_INT_RISING);
  106.     NVIC_EnableIRQ(GPAB_IRQn);

  107.     /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 clocks of LIRC clock */
  108.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBCTL_DBCLKSRC_LIRC, GPIO_DBCTL_DBCLKSEL_1024);
  109.     GPIO_ENABLE_DEBOUNCE(PB, BIT3);

  110.     /* Unlock protected registers before entering Power-down mode */
  111.     SYS_UnlockReg();

  112.     /* Waiting for PB.3 rising-edge interrupt event */
  113.     printf("Enter to Power-Down ......\n");

  114.     /* Enter to Power-down mode */
  115.     PowerDownFunction();

  116.     printf("System waken-up done.\n\n");

  117.     while(1);

  118. }

  119. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/


 楼主| xuanhuanzi 发表于 2022-1-21 13:58 | 显示全部楼层
  1. CLK_PowerDown();

掉电模式的关键函数BSP已经提供了,我们看看到底怎么实现的
  1. /**
  2.   * @brief      Enter to Power-down mode
  3.   * @param      None
  4.   * @return     None
  5.   * @details    This function is used to let system enter to Power-down mode. \n
  6.   *             The register write-protection function should be disabled before using this function.
  7.   */
  8. void CLK_PowerDown(void)
  9. {
  10.     /* Set the processor uses deep sleep as its low power mode */
  11.     SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;

  12.     /* Set system Power-down enabled*/
  13.     CLK->PWRCTL |= CLK_PWRCTL_PDEN_Msk;

  14.     /* Chip enter Power-down mode after CPU run WFI instruction */
  15.     __WFI();
  16. }
 楼主| xuanhuanzi 发表于 2022-1-21 14:04 | 显示全部楼层
看着还挺难懂,如果不看手册。
WFI      Wait For Interrupt
sparrow054 发表于 2022-1-21 17:27 | 显示全部楼层
确实比较难懂,一堆代码啊
asmine 发表于 2022-1-26 13:49 | 显示全部楼层
掉电和休眠是一会事么?有什么区别
cr315 发表于 2022-1-26 14:44 | 显示全部楼层
所以掉电是休眠的一种
redone 发表于 2022-2-9 15:45 | 显示全部楼层
有点复杂
sparrow054 发表于 2022-2-14 14:22 | 显示全部楼层
我一般需要有人讲解下,尤其是长代码
twjiang 发表于 2022-2-14 14:25 | 显示全部楼层
掉电会触发“低压” or "什么“中断,得有个后备电源(电容,UPS)吧,然后进入”休眠“?
twjiang 发表于 2022-2-14 14:29 | 显示全部楼层
实现原理是这样的?”掉电“会产生”电压抖动“,这一事件可以由”GPIO防抖功能 de-bounce“捕捉到,然后接下来就可以干自己想做的事情了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

2331

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部