[技术问答] M451系列,ADC如何设置软件手动触发多通道连续转换?

[复制链接]
250|7
| 2019-1-4 14:26 | 显示全部楼层
手动?那你设置个手动检测程序触发就行了。
| 2019-1-7 14:22 | 显示全部楼层
  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: 6 $
  5. * $Date: 15/09/02 10:04a $
  6. * @brief
  7. *           Monitor the conversion result of channel 2 by the digital compare function.
  8. *
  9. * @note
  10. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  11. *
  12. ******************************************************************************/
  13. #include "stdio.h"
  14. #include "M451Series.h"

  15. #define PLLCTL_SETTING      CLK_PLLCTL_72MHz_HXT
  16. #define PLL_CLOCK           72000000

  17. /*---------------------------------------------------------------------------------------------------------*/
  18. /* Define global variables and constants                                                                   */
  19. /*---------------------------------------------------------------------------------------------------------*/
  20. volatile uint32_t g_u32AdcCmp0IntFlag;
  21. volatile uint32_t g_u32AdcCmp1IntFlag;

  22. /*---------------------------------------------------------------------------------------------------------*/
  23. /* Define functions prototype                                                                              */
  24. /*---------------------------------------------------------------------------------------------------------*/
  25. int32_t main(void);
  26. void EADC_FunctionTest(void);


  27. void SYS_Init(void)
  28. {

  29.     /*---------------------------------------------------------------------------------------------------------*/
  30.     /* Init System Clock                                                                                       */
  31.     /*---------------------------------------------------------------------------------------------------------*/

  32.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  33.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  34.     /* Wait for HIRC clock ready */
  35.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

  38.     /* Set PLL to Power-down mode and PLLSTB bit in CLK_STATUS register will be cleared by hardware.*/
  39.     CLK_DisablePLL();

  40.     /* Enable HXT clock (external XTAL 12MHz) */
  41.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  42.     /* Wait for HXT clock ready */
  43.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  44.     /* Set core clock as PLL_CLOCK from PLL */
  45.     CLK_SetCoreClock(PLL_CLOCK);

  46.     /* Enable UART module clock */
  47.     CLK_EnableModuleClock(UART0_MODULE);

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

  50.     /* Enable EADC module clock */
  51.     CLK_EnableModuleClock(EADC_MODULE);

  52.     /* EADC clock source is 72MHz, set divider to 8, ADC clock is 72/8 MHz */
  53.     CLK_SetModuleClock(EADC_MODULE, 0, CLK_CLKDIV0_EADC(8));

  54.     /*---------------------------------------------------------------------------------------------------------*/
  55.     /* Init I/O Multi-function                                                                                 */
  56.     /*---------------------------------------------------------------------------------------------------------*/

  57.     /* Set PD multi-function pins for UART0 RXD and TXD */
  58.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  59.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  60.     /* Configure the GPB0 - GPB3 ADC analog input pins.  */
  61.     SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB0MFP_Msk | SYS_GPB_MFPL_PB1MFP_Msk |
  62.                        SYS_GPB_MFPL_PB2MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk);
  63.     SYS->GPB_MFPL |= (SYS_GPB_MFPL_PB0MFP_EADC_CH0 | SYS_GPB_MFPL_PB1MFP_EADC_CH1 |
  64.                       SYS_GPB_MFPL_PB2MFP_EADC_CH2 | SYS_GPB_MFPL_PB3MFP_EADC_CH3);

  65.     /* Disable the GPB0 - GPB3 digital input path to avoid the leakage current. */
  66.     GPIO_DISABLE_DIGITAL_PATH(PB, 0xF);

  67. }

  68. void UART0_Init()
  69. {
  70.     /*---------------------------------------------------------------------------------------------------------*/
  71.     /* Init UART                                                                                               */
  72.     /*---------------------------------------------------------------------------------------------------------*/
  73.     /* Reset UART IP */
  74.     SYS->IPRST1 |=  SYS_IPRST1_UART0RST_Msk;
  75.     SYS->IPRST1 &= ~SYS_IPRST1_UART0RST_Msk;

  76.     /* Configure UART0 and set UART0 baud rate */
  77.     UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 115200);
  78.     UART0->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
  79. }

  80. /*---------------------------------------------------------------------------------------------------------*/
  81. /* EADC function test                                                                                       */
  82. /*---------------------------------------------------------------------------------------------------------*/
  83. void EADC_FunctionTest()
  84. {
  85.     printf("\n");
  86.     printf("+----------------------------------------------------------------------+\n");
  87.     printf("|           EADC compare function (result monitor) sample code         |\n");
  88.     printf("+----------------------------------------------------------------------+\n");
  89.     printf("\nIn this test, software will compare the conversion result of channel 2.\n");

  90.     /* Set the ADC internal sampling time, input mode as single-end and enable the A/D converter */
  91.     EADC_Open(EADC, EADC_CTL_DIFFEN_SINGLE_END);
  92.     EADC_SetInternalSampleTime(EADC, 6);

  93.     /* Configure the sample module 0 for analog input channel 2 and ADINT0 trigger source */
  94.     EADC_ConfigSampleModule(EADC, 0, EADC_ADINT0_TRIGGER, 2);

  95.     /* Enable EADC comparator 0. Compare condition: conversion result < 0x800; match Count=5 */
  96.     printf("   Set the compare condition of comparator 0: channel 2 is less than 0x800; match count is 5.\n");
  97.     EADC_ENABLE_CMP0(EADC, 0, EADC_CMP_CMPCOND_LESS_THAN, 0x800, 0x5);

  98.     /* Enable EADC comparator 1. Compare condition: conversion result >= 0x800; match Count=5 */
  99.     printf("   Set the compare condition of comparator 1 : channel 2 is greater than or equal to 0x800; match count is 5.\n");
  100.     EADC_ENABLE_CMP1(EADC, 0, EADC_CMP_CMPCOND_GREATER_OR_EQUAL, 0x800, 0x5);

  101.     /* Enable sample module 0 interrupt */
  102.     EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 0, 0x1);

  103.     /* Clear the A/D ADINT3 interrupt flag for safe */
  104.     EADC_CLR_INT_FLAG(EADC, (0x1 << 3));
  105.     /* Enable ADINT3 interrupt */
  106.     EADC_ENABLE_INT(EADC, (0x1 << 3));
  107.     NVIC_EnableIRQ(ADC03_IRQn);

  108.     /* Clear the EADC comparator 0 interrupt flag for safe */
  109.     EADC_CLR_INT_FLAG(EADC, (0x1 << 4));
  110.     /* Enable ADC comparator 0 interrupt */
  111.     EADC_ENABLE_CMP_INT(EADC, 0);

  112.     /* Clear the EADC comparator 1 interrupt flag for safe */
  113.     EADC_CLR_INT_FLAG(EADC, (0x1 << 5));
  114.     /* Enable ADC comparator 1 interrupt */
  115.     EADC_ENABLE_CMP_INT(EADC, 1);

  116.     /* Reset the EADC interrupt indicator and trigger sample module 0 to start A/D conversion */
  117.     g_u32AdcCmp0IntFlag = 0;
  118.     g_u32AdcCmp1IntFlag = 0;
  119.     EADC_START_CONV(EADC, 0x1);

  120.     /* Wait EADC compare interrupt */
  121.     while((g_u32AdcCmp0IntFlag == 0) && (g_u32AdcCmp1IntFlag == 0));

  122.     /* Disable the sample module 0 interrupt */
  123.     EADC_DISABLE_SAMPLE_MODULE_INT(EADC, 0, 0x1);

  124.     /* Disable ADC comparator interrupt */
  125.     EADC_DISABLE_CMP_INT(EADC, 0);
  126.     EADC_DISABLE_CMP_INT(EADC, 1);
  127.     /* Disable compare function */
  128.     EADC_DISABLE_CMP0(EADC);
  129.     EADC_DISABLE_CMP1(EADC);

  130.     if(g_u32AdcCmp0IntFlag == 1)
  131.     {
  132.         printf("Comparator 0 interrupt occurs.\nThe conversion result of channel 2 is less than 0x800\n");
  133.     }
  134.     else
  135.     {
  136.         printf("Comparator 1 interrupt occurs.\nThe conversion result of channel 2 is greater than or equal to 0x800\n");
  137.     }

  138. }

  139. /*---------------------------------------------------------------------------------------------------------*/
  140. /* EADC interrupt handler                                                                                  */
  141. /*---------------------------------------------------------------------------------------------------------*/
  142. void ADC03_IRQHandler(void)
  143. {
  144.     if(EADC_GET_INT_FLAG(EADC, (0x1 << 4)))
  145.     {
  146.         g_u32AdcCmp0IntFlag = 1;
  147.         EADC_CLR_INT_FLAG(EADC, (0x1 << 4));/* Clear the A/D compare flag 0 */
  148.     }

  149.     if(EADC_GET_INT_FLAG(EADC, (0x1 << 5)))
  150.     {
  151.         g_u32AdcCmp1IntFlag = 1;
  152.         EADC_CLR_INT_FLAG(EADC, (0x1 << 5));/* Clear the A/D compare flag 1 */
  153.     }
  154. }

  155. /*---------------------------------------------------------------------------------------------------------*/
  156. /*  Main Function                                                                                          */
  157. /*---------------------------------------------------------------------------------------------------------*/
  158. int32_t main(void)
  159. {

  160.     /* Unlock protected registers */
  161.     SYS_UnlockReg();

  162.     /* Init System, IP clock and multi-function I/O */
  163.     SYS_Init();

  164.     /* Lock protected registers */
  165.     SYS_LockReg();

  166.     /* Init UART0 for printf */
  167.     UART0_Init();

  168.     /*---------------------------------------------------------------------------------------------------------*/
  169.     /* SAMPLE CODE                                                                                             */
  170.     /*---------------------------------------------------------------------------------------------------------*/

  171.     printf("\nSystem clock rate: %d Hz", SystemCoreClock);

  172.     /* EADC function test */
  173.     EADC_FunctionTest();

  174.     /* Reset EADC module */
  175.     SYS_ResetModule(EADC_RST);

  176.     /* Disable EADC IP clock */
  177.     CLK_DisableModuleClock(EADC_MODULE);

  178.     /* Disable External Interrupt */
  179.     NVIC_DisableIRQ(ADC00_IRQn);

  180.     printf("Exit EADC sample code\n");

  181.     while(1);

  182. }

复制代码
| 2019-1-7 14:22 | 显示全部楼层
参考上面的例子,BSP里面的标准外设例子。
| 2019-1-7 15:36 | 显示全部楼层
用IO的中断,或者扫描IO的按键动作触发转换指令
    EADC_START_CONV(EADC, 0x1);
| 2019-1-7 21:47 | 显示全部楼层
跟你手动触发的方式有关。你只需要在手动触发的函数里,触发执行ADC启动转换就行了。
| 2019-1-7 22:09 | 显示全部楼层
楼主先研究好,ADC转换,按键程序,就明白了。
 楼主 | 2019-1-12 10:09 | 显示全部楼层
哪里看出来是多通道转换呢?如何启动呢?启动后第一个转换的是哪一个通道呢?

新唐资料太少了。
扫描二维码,随时随地手机跟帖
您需要登录后才可以回帖 登录 | 注册

本版积分规则

我要发帖 投诉建议 创建版块 申请版主

快速回复

您需要登录后才可以回帖
登录 | 注册
高级模式

论坛热帖

快速回复 返回顶部 返回列表