[DemoCode下载] M051可以通过ACMP从掉电模式唤醒

[复制链接]
633|8
 楼主| jiekou001 发表于 2019-9-29 23:50 | 显示全部楼层 |阅读模式
ACM, TE, TI, ck
  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: 15/05/22 2:58p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to wake up MCU from Power-down mode by ACMP wake-up function.
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"

  13. /* Function prototype declaration */
  14. void SYS_Init(void);
  15. void PowerDownFunction(void);
  16. int IsDebugFifoEmpty(void);

  17. int32_t main(void)
  18. {
  19.     uint32_t u32DelayCount;

  20.     /* Unlock protected registers */
  21.     SYS_UnlockReg();
  22.     /* Init System, IP clock and multi-function I/O. */
  23.     SYS_Init();
  24.     /* Lock protected registers */
  25.     SYS_LockReg();

  26.     /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
  27.     UART_Open(UART0, 115200);

  28.     printf("\n\n");
  29.     printf("+---------------------------------------+\n");
  30.     printf("|         M051 ACMP Sample Code         |\n");
  31.     printf("+---------------------------------------+\n");

  32.     printf("\nThis sample code demonstrates ACMP0 wake-up function. Using ACMP0_P (P1.5) as ACMP0\n");
  33.     printf("positive input and using internal band-gap voltage as the negative input.\n");
  34.     printf("The compare result reflects on ACMP0_O (P3.6).\n");

  35.     printf("When the voltage of the positive input is greater than the voltage of the negative input,\n");
  36.     printf("the analog comparator outputs logical one; otherwise, it outputs logical zero.\n");
  37.     printf("This chip will be waked up from power down mode when detecting a transition of analog comparator's output.\n");
  38.     printf("Press any key to enter power down mode ...");
  39.     getchar();
  40.     printf("\n");

  41.     /* Configure ACMP0. Enable ACMP0 and select internal reference voltage as negative input. */
  42.     ACMP_Open(ACMP01, 0, ACMP_CR_VNEG_BANDGAP, ACMP_CR_HYSTERESIS_DISABLE);
  43.     __NOP();
  44.     for(u32DelayCount = 0; u32DelayCount < 100; u32DelayCount++); /* For ACMP setup time */
  45.     __NOP();
  46.     /* Clear ACMP 0 interrupt flag */
  47.     ACMP_CLR_INT_FLAG(ACMP01, 0);
  48.    
  49.     /* Enable ACMP0 interrupt function */
  50.     ACMP_ENABLE_INT(ACMP01, 0);

  51.     /* Enable ACMP01 interrupt */
  52.     NVIC_EnableIRQ(ACMP01_IRQn);

  53.     /* To program PWRCTL register, it needs to disable register protection first. */
  54.     SYS_UnlockReg();
  55.     PowerDownFunction();
  56.     printf("Wake up by ACMP0!\n");
  57.     while(1);

  58. }

  59. void ACMP01_IRQHandler(void)
  60. {
  61.     printf("\nACMP0 interrupt!\n");
  62.     /* Disable interrupt */
  63.     ACMP_DISABLE_INT(ACMP01, 0);
  64.     /* Clear ACMP 0 interrupt flag */
  65.     ACMP_CLR_INT_FLAG(ACMP01, 0);
  66. }


  67. void SYS_Init(void)
  68. {
  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Init System Clock                                                                                       */
  71.     /*---------------------------------------------------------------------------------------------------------*/

  72.     /* Enable external 12MHz XTAL */
  73.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  74.     /* Waiting for clock ready */
  75.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  76.     /* Select HXT as the clock source of UART */
  77.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  78.     /* Enable UART peripheral clock */
  79.     CLK_EnableModuleClock(UART0_MODULE);
  80.     /* Enable ACMP01 peripheral clock */
  81.     CLK_EnableModuleClock(ACMP01_MODULE);

  82.     /* Update System Core Clock */
  83.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
  84.     SystemCoreClockUpdate();


  85.     /*---------------------------------------------------------------------------------------------------------*/
  86.     /* Init I/O Multi-function                                                                                 */
  87.     /*---------------------------------------------------------------------------------------------------------*/
  88.     /* Set P1.5 multi-function pin for ACMP0 positive input pin */
  89.     SYS->P1_MFP = SYS_MFP_P15_ACMP0_P;

  90.     /* Disable digital input path of analog pin ACMP0_P to prevent leakage */
  91.     GPIO_DISABLE_DIGITAL_PATH(P1, BIT5);

  92.     /* Set P3 multi-function pins for UART0 RXD, TXD and ACMP0 output */
  93.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0 | SYS_MFP_P36_ACMP0_O;
  94. }

  95. void PowerDownFunction(void)
  96. {
  97.     printf("\nSystem enter power-down mode ... ");

  98.     /* To check if all the debug messages are finished */
  99.     while(IsDebugFifoEmpty() == 0);

  100.     /* Deep sleep mode is selected */
  101.     SCB->SCR = SCB_SCR_SLEEPDEEP_Msk;

  102.     /* To program PWRCTL register, it needs to disable register protection first. */
  103.     CLK->PWRCON &= ~(CLK_PWRCON_PD_WAIT_CPU_Msk | CLK_PWRCON_PWR_DOWN_EN_Msk);
  104.     CLK->PWRCON |= (CLK_PWRCON_PD_WAIT_CPU_Msk | CLK_PWRCON_PWR_DOWN_EN_Msk | CLK_PWRCON_PD_WU_INT_EN_Msk);

  105.     __WFI();
  106. }

  107. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/




yiy 发表于 2019-11-9 17:20 | 显示全部楼层
唤醒好像从任何中断源都行。
yiy 发表于 2019-11-9 17:20 | 显示全部楼层
唤醒系统,操作完再休眠
zhuotuzi 发表于 2019-11-12 13:05 | 显示全部楼层
貌似只要发生中断就行了。
yiyigirl2014 发表于 2019-11-15 00:03 | 显示全部楼层
   NVIC_EnableIRQ(ACMP01_IRQn);
yiyigirl2014 发表于 2019-11-15 00:03 | 显示全部楼层
必须要有这个才行吧
susceptibility 发表于 2019-11-17 18:09 | 显示全部楼层
中断都可以唤醒的吧
xixi2017 发表于 2019-11-17 23:50 | 显示全部楼层
    __WFI();
这个什么操作。
zhuotuzi 发表于 2019-11-18 23:56 | 显示全部楼层
可选的方式挺多
您需要登录后才可以回帖 登录 | 注册

本版积分规则

147

主题

1539

帖子

2

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