[牛人杂谈] M051的模拟比较器 (ACMP)

[复制链接]
 楼主| zhuotuzi 发表于 2016-7-6 22:45 | 显示全部楼层 |阅读模式
模拟比较器 (ACMP)

NuMicro M05xxBN/DN/DE最多有4个比较器,可以在不同的配置下使用。当正端输入大于负端输入时,比较器输出逻辑”1”,否则输出”0”。 当比较器输出值改变,每个比较器可以配置发生中断。
特性
        模拟输入电压范围: 0~AVDD
        支持迟滞功能
        每个模拟比较器负端可以选择输入内部参考电压
        4/2个比较器共享2/1个中断向量


为何这里还多达呢,因为还要看具体型号。

  
  
M05xxBN
M05xxDN/DE
ACMP数量
2
4
ACMP 输出反转功能
-





 楼主| zhuotuzi 发表于 2016-7-7 09:47 | 显示全部楼层
先看看普通用法。
  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: 7 $
  5. * $Date: 15/05/22 2:56p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrate how ACMP works with internal band-gap voltage.
  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. int32_t main(void)
  16. {
  17.     /* Unlock protected registers */
  18.     SYS_UnlockReg();
  19.     /* Init System, IP clock and multi-function I/O. */
  20.     SYS_Init();
  21.     /* Lock protected registers */
  22.     SYS_LockReg();

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

  25.     printf("\n\n");
  26.     printf("+---------------------------------------+\n");
  27.     printf("|         M051 ACMP Sample Code         |\n");
  28.     printf("+---------------------------------------+\n");

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

  32.     /* Configure ACMP0. Enable ACMP0 and select internal reference voltage as negative input. */
  33.     ACMP_Open(ACMP01, 0, ACMP_CR_VNEG_BANDGAP, ACMP_CR_HYSTERESIS_DISABLE);
  34.     /* Enable ACMP0 interrupt function */
  35.     ACMP_ENABLE_INT(ACMP01, 0);

  36.     /* Enable ACMP01 interrupt */
  37.     NVIC_EnableIRQ(ACMP01_IRQn);

  38.     while(1);

  39. }

  40. void ACMP01_IRQHandler(void)
  41. {
  42.     static uint32_t u32Cnt = 0;

  43.     /* Clear ACMP 0 interrupt flag */
  44.     ACMP_CLR_INT_FLAG(ACMP01, 0);
  45.     /* Check Comparator 0 Output Status */
  46.     if(ACMP_GET_OUTPUT(ACMP01, 0))
  47.         printf("ACMP0_P voltage > Band-gap voltage (%d)\n", u32Cnt);
  48.     else
  49.         printf("ACMP0_P voltage <= Band-gap voltage (%d)\n", u32Cnt);

  50.     u32Cnt++;
  51. }


  52. void SYS_Init(void)
  53. {
  54.     /*---------------------------------------------------------------------------------------------------------*/
  55.     /* Init System Clock                                                                                       */
  56.     /*---------------------------------------------------------------------------------------------------------*/

  57.     /* Enable external 12MHz XTAL */
  58.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  59.     /* Waiting for clock ready */
  60.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

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

  63.     /* Enable UART peripheral clock */
  64.     CLK_EnableModuleClock(UART0_MODULE);
  65.     /* Enable ACMP01 peripheral clock */
  66.     CLK_EnableModuleClock(ACMP01_MODULE);

  67.     /* Update System Core Clock */
  68.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
  69.     SystemCoreClockUpdate();


  70.     /*---------------------------------------------------------------------------------------------------------*/
  71.     /* Init I/O Multi-function                                                                                 */
  72.     /*---------------------------------------------------------------------------------------------------------*/
  73.     /* Set P1.5 multi-function pin for ACMP0 positive input pin */
  74.     SYS->P1_MFP = SYS_MFP_P15_ACMP0_P;

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

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

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



 楼主| zhuotuzi 发表于 2016-7-7 09:49 | 显示全部楼层
  1. /******************************************************************************
  2. * @file     main.c
  3. * @version  V3.00
  4. * $Revision: 7 $
  5. * $Date: 15/05/22 2:56p $
  6. * @brief    Demonstrate how ACMP works with internal band-gap voltage.
  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. int32_t main(void)
  16. {
  17.     /* Unlock protected registers */
  18.     SYS_UnlockReg();
  19.     /* Init System, IP clock and multi-function I/O. */
  20.     SYS_Init();
  21.     /* Lock protected registers */
  22.     SYS_LockReg();

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

  25.     printf("\n\n");
  26.     printf("+---------------------------------------+\n");
  27.     printf("|         M051 ACMP Sample Code         |\n");
  28.     printf("+---------------------------------------+\n");

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

  32.     /* Configure ACMP0. Enable ACMP0 and select internal reference voltage as negative input. */
  33.     ACMP_Open(ACMP01, 0, ACMP_CR_VNEG_BANDGAP, ACMP_CR_HYSTERESIS_DISABLE);
  34.     /* Enable ACMP0 interrupt function */
  35.     ACMP_ENABLE_INT(ACMP01, 0);

  36.     /* Enable ACMP01 interrupt */
  37.     NVIC_EnableIRQ(ACMP01_IRQn);

  38.     while(1);

  39. }

  40. void ACMP01_IRQHandler(void)
  41. {
  42.     static uint32_t u32Cnt = 0;

  43.     /* Clear ACMP 0 interrupt flag */
  44.     ACMP_CLR_INT_FLAG(ACMP01, 0);
  45.     /* Check Comparator 0 Output Status */
  46.     if(ACMP_GET_OUTPUT(ACMP01, 0))
  47.         printf("ACMP0_P voltage > Band-gap voltage (%d)\n", u32Cnt);
  48.     else
  49.         printf("ACMP0_P voltage <= Band-gap voltage (%d)\n", u32Cnt);

  50.     u32Cnt++;
  51. }


  52. void SYS_Init(void)
  53. {
  54.     /*---------------------------------------------------------------------------------------------------------*/
  55.     /* Init System Clock                                                                                       */
  56.     /*---------------------------------------------------------------------------------------------------------*/

  57.     /* Enable external 12MHz XTAL */
  58.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  59.     /* Waiting for clock ready */
  60.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

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

  63.     /* Enable UART peripheral clock */
  64.     CLK_EnableModuleClock(UART0_MODULE);
  65.     /* Enable ACMP01 peripheral clock */
  66.     CLK_EnableModuleClock(ACMP01_MODULE);

  67.     /* Update System Core Clock */
  68.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
  69.     SystemCoreClockUpdate();


  70.     /*---------------------------------------------------------------------------------------------------------*/
  71.     /* Init I/O Multi-function                                                                                 */
  72.     /*---------------------------------------------------------------------------------------------------------*/
  73.     /* Set P1.5 multi-function pin for ACMP0 positive input pin */
  74.     SYS->P1_MFP = SYS_MFP_P15_ACMP0_P;

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

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

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



 楼主| zhuotuzi 发表于 2016-7-7 09:51 | 显示全部楼层
This sample code demonstrates ACMP0 function. Using ACMP0_P (P1.5) as ACMP0positive input and using internal band-gap voltage as the negative input
The compare result reflects on ACMP0_O (P3.6)


    /* Configure ACMP0. Enable ACMP0 and select internal reference voltage as negative input. */
    ACMP_Open(ACMP01, 0, ACMP_CR_VNEG_BANDGAP, ACMP_CR_HYSTERESIS_DISABLE);
    /* Enable ACMP0 interrupt function */
    ACMP_ENABLE_INT(ACMP01, 0);

    /* Enable ACMP01 interrupt */
    NVIC_EnableIRQ(ACMP01_IRQn);


我们看到这个是会产生中断的。
中断处理函数为
void ACMP01_IRQHandler(void)
{
    static uint32_t u32Cnt = 0;

    /* Clear ACMP 0 interrupt flag */
    ACMP_CLR_INT_FLAG(ACMP01, 0);
    /* Check Comparator 0 Output Status */
    if(ACMP_GET_OUTPUT(ACMP01, 0))
        printf("ACMP0_P voltage > Band-gap voltage (%d)\n", u32Cnt);
    else
        printf("ACMP0_P voltage <= Band-gap voltage (%d)\n", u32Cnt);

    u32Cnt++;
}


 楼主| zhuotuzi 发表于 2016-7-7 09:59 | 显示全部楼层
另外一个功能就是唤醒断电模式的系统了。就好比关机或者休眠后的唤醒。
  1. /******************************************************************************
  2. * @file     main.c
  3. * @version  V3.00
  4. * $Revision: 2 $
  5. * $Date: 15/05/22 2:58p $
  6. * @brief    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. ***/



 楼主| zhuotuzi 发表于 2016-7-7 10:01 | 显示全部楼层
/* Configure ACMP0. Enable ACMP0 and select internal reference voltage as negative input. */
    ACMP_Open(ACMP01, 0, ACMP_CR_VNEG_BANDGAP, ACMP_CR_HYSTERESIS_DISABLE);
    __NOP();
    for(u32DelayCount = 0; u32DelayCount < 100; u32DelayCount++); /* For ACMP setup time */
    __NOP();
    /* Clear ACMP 0 interrupt flag */
    ACMP_CLR_INT_FLAG(ACMP01, 0);

    /* Enable ACMP0 interrupt function */
    ACMP_ENABLE_INT(ACMP01, 0);

    /* Enable ACMP01 interrupt */
    NVIC_EnableIRQ(ACMP01_IRQn);

    /* To program PWRCTL register, it needs to disable register protection first. */
    SYS_UnlockReg();
    PowerDownFunction();
    printf("Wake up by ACMP0!\n");
——————————————————————————
我们从这看,系统使用了断电函数。
其他地方是一样的,下面是中断处理的不同,这里有关闭中断,清除中断标志,这是因为这个就是一次实验。模拟系统通过这种方式开机。
void ACMP01_IRQHandler(void)
{
    printf("\nACMP0 interrupt!\n");
    /* Disable interrupt */
    ACMP_DISABLE_INT(ACMP01, 0);
    /* Clear ACMP 0 interrupt flag */
    ACMP_CLR_INT_FLAG(ACMP01, 0);
}


 楼主| zhuotuzi 发表于 2016-7-7 10:06 | 显示全部楼层
当然系统初始化的时候是要配置功能的。

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

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

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

 楼主| zhuotuzi 发表于 2016-7-7 10:08 | 显示全部楼层
配置时候可参看这个图。
1111.png 2.png
 楼主| zhuotuzi 发表于 2016-7-7 10:11 | 显示全部楼层
断电的方式是
  1. void PowerDownFunction(void)
  2. {
  3.     printf("\nSystem enter power-down mode ... ");

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

  6.     /* Deep sleep mode is selected */
  7.     SCB->SCR = SCB_SCR_SLEEPDEEP_Msk;

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

  11.     __WFI();
  12. }


Micachl 发表于 2016-7-9 20:59 | 显示全部楼层
这个单片机有没有类似模拟开关的功能?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

214

主题

3368

帖子

7

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

214

主题

3368

帖子

7

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