[DemoCode下载] ACMP的一般应用方法

[复制链接]
1644|10
 楼主| zhuomuniao110 发表于 2019-6-30 23:32 | 显示全部楼层 |阅读模式
  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. ***/




wahahaheihei 发表于 2019-6-30 23:37 | 显示全部楼层
多谢分享。
 楼主| zhuomuniao110 发表于 2019-7-22 13:35 | 显示全部楼层
比较器在判断上通常采用中断。
xuanhuanzi 发表于 2019-7-23 11:38 | 显示全部楼层
中断比较好。
xinxianshi 发表于 2019-7-23 21:17 | 显示全部楼层
这个响应速度快吗
xinxianshi 发表于 2019-7-23 21:17 | 显示全部楼层
如果有波动会不会来回的跳。
星星点灯69 发表于 2019-7-24 09:22 | 显示全部楼层
楼主这是新唐M051官方BSP的例程,如果信号频率比较高的话,中断里的printf会出问题
小灵通2018 发表于 2019-7-29 10:50 | 显示全部楼层
多谢分享,我试试这个怎么玩。
小明的同学 发表于 2019-7-31 00:13 | 显示全部楼层
中断里不能搞太多的。
monitoring 发表于 2019-7-31 15:12 | 显示全部楼层
多谢分享!最好能有个简单的说明
zhuotuzi 发表于 2019-7-31 15:45 | 显示全部楼层
这个要多练习了,多谢提供参考。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

233

主题

3529

帖子

11

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