[DemoCode下载] M051的ADC库函数操作方式

[复制链接]
3957|21
 楼主| wahahaheihei 发表于 2016-4-27 20:01 | 显示全部楼层 |阅读模式
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.0
  4. * $Revision: 7 $
  5. * $Date: 14/07/10 9:52a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 Series ADC Interface Controller Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M051Series.h"

  14. #define PLL_CLOCK       50000000


  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Define Function Prototypes                                                                              */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. void SYS_Init(void);
  19. void UART0_Init(void);
  20. void AdcBurstModeTest(void);


  21. /*---------------------------------------------------------------------------------------------------------*/
  22. /* Define global variables and constants                                                                   */
  23. /*---------------------------------------------------------------------------------------------------------*/
  24. #define BURST_COUNT 20
  25. volatile uint32_t g_u32AdcDataCount;
  26. uint16_t g_au16AdcData[BURST_COUNT];


  27. void SYS_Init(void)
  28. {
  29.     /*---------------------------------------------------------------------------------------------------------*/
  30.     /* Init System Clock                                                                                       */
  31.     /*---------------------------------------------------------------------------------------------------------*/

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

  34.     /* Waiting for Internal RC clock ready */
  35.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  36.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  37.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  38.     /* Enable external XTAL 12MHz clock */
  39.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  40.     /* Waiting for external XTAL clock ready */
  41.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  42.     /* Set core clock as PLL_CLOCK from PLL */
  43.     CLK_SetCoreClock(PLL_CLOCK);

  44.     /* Enable UART module clock */
  45.     CLK_EnableModuleClock(UART0_MODULE);

  46.     /* Enable ADC module clock */
  47.     CLK_EnableModuleClock(ADC_MODULE);

  48.     /* Select UART module clock source */
  49.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  50.     /* ADC clock source is 22.1184MHz, set divider to 7, ADC clock is 22.1184/7 MHz */
  51.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(7));

  52.     /*---------------------------------------------------------------------------------------------------------*/
  53.     /* Init I/O Multi-function                                                                                 */
  54.     /*---------------------------------------------------------------------------------------------------------*/

  55.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  56.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  57.     SYS->P3_MFP |= SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

  58.     /* Disable the P1.0 - P1.3 digital input path to avoid the leakage current */
  59.     GPIO_DISABLE_DIGITAL_PATH(P1, 0xF);

  60.     /* Configure the P1.0 - P1.3 ADC analog input pins */
  61.     SYS->P1_MFP &= ~(SYS_MFP_P10_Msk | SYS_MFP_P11_Msk | SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
  62.     SYS->P1_MFP |= SYS_MFP_P10_AIN0 | SYS_MFP_P11_AIN1 | SYS_MFP_P12_AIN2 | SYS_MFP_P13_AIN3 ;

  63. }

  64. /*---------------------------------------------------------------------------------------------------------*/
  65. /* Init UART                                                                                               */
  66. /*---------------------------------------------------------------------------------------------------------*/
  67. void UART0_Init()
  68. {
  69.     /* Reset IP */
  70.     SYS_ResetModule(UART0_RST);

  71.     /* Configure UART0 and set UART0 Baudrate */
  72.     UART_Open(UART0, 115200);
  73. }


  74. /*---------------------------------------------------------------------------------------------------------*/
  75. /* Function: ADC_GetConversionRate                                                                         */
  76. /*                                                                                                         */
  77. /* Parameters:                                                                                             */
  78. /*   None.                                                                                                 */
  79. /*                                                                                                         */
  80. /* Returns:                                                                                                */
  81. /*      Return the A/D conversion rate (sample/second)                                                     */
  82. /*                                                                                                         */
  83. /* Description:                                                                                            */
  84. /*   The conversion rate depends on the clock source of ADC clock.                                         */
  85. /*   It only needs 21 ADC clocks to complete an A/D conversion.                                            */
  86. /*---------------------------------------------------------------------------------------------------------*/
  87. static __INLINE uint32_t ADC_GetConversionRate()
  88. {
  89.     uint32_t u32AdcClkSrcSel;
  90.     uint32_t u32ClkTbl[4] = {__HXT, 0, 0, __HIRC};

  91.     /* Set the PLL clock frequency */
  92.     u32ClkTbl[1] = PllClock;
  93.     /* Set the system core clock frequency */
  94.     u32ClkTbl[2] = SystemCoreClock;
  95.     /* Get the clock source setting */
  96.     u32AdcClkSrcSel = ((CLK->CLKSEL1 & CLK_CLKSEL1_ADC_S_Msk) >> CLK_CLKSEL1_ADC_S_Pos);
  97.     /* Return the ADC conversion rate */
  98.     return ((u32ClkTbl[u32AdcClkSrcSel]) / (((CLK->CLKDIV & CLK_CLKDIV_ADC_N_Msk) >> CLK_CLKDIV_ADC_N_Pos) + 1) / 21);
  99. }

  100. /*---------------------------------------------------------------------------------------------------------*/
  101. /* Function: AdcBurstModeTest                                                                              */
  102. /*                                                                                                         */
  103. /* Parameters:                                                                                             */
  104. /*   None.                                                                                                 */
  105. /*                                                                                                         */
  106. /* Returns:                                                                                                */
  107. /*   None.                                                                                                 */
  108. /*                                                                                                         */
  109. /* Description:                                                                                            */
  110. /*   ADC burst mode test.                                                                                  */
  111. /*---------------------------------------------------------------------------------------------------------*/
  112. void AdcBurstModeTest()
  113. {
  114.     uint8_t  u8Option;
  115.     uint32_t u32Count;

  116.     printf("\n\nConversion rate: %d samples/second\n", ADC_GetConversionRate());
  117.     printf("\n");
  118.     printf("+----------------------------------------------------------------------+\n");
  119.     printf("|                      ADC burst mode sample code                      |\n");
  120.     printf("+----------------------------------------------------------------------+\n");
  121.     printf("\nIn this test, software will get %d conversion results from the specified channel.\n", BURST_COUNT);

  122.     while(1)
  123.     {
  124.         printf("\nSelect input mode:\n");
  125.         printf("  [1] Single end input (channel 2 only)\n");
  126.         printf("  [2] Differential input (channel pair 1 only)\n");
  127.         printf("  Other keys: exit burst mode test\n");
  128.         u8Option = getchar();
  129.         if(u8Option == '1')
  130.         {
  131.             printf("%d conversion results of channel 2:\n", BURST_COUNT);
  132.             /* Set the ADC operation mode as burst, input mode as single-end and enable the analog input channel 2 */
  133.             ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_BURST, 0x1 << 2);

  134.             /* Power on ADC module */
  135.             ADC_POWER_ON(ADC);

  136.             /* clear the A/D interrupt flag for safe */
  137.             ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  138.             /* Enable the ADC interrupt */
  139.             ADC_EnableInt(ADC, ADC_ADF_INT);
  140.             NVIC_EnableIRQ(ADC_IRQn);

  141.             /* Reset the ADC data counter and start A/D conversion */
  142.             g_u32AdcDataCount = 0;
  143.             ADC_START_CONV(ADC);

  144.             while(g_u32AdcDataCount < BURST_COUNT);

  145.             /* Stop A/D conversion */
  146.             ADC_STOP_CONV(ADC);
  147.             /* Disable the ADC interrupt */
  148.             ADC_DisableInt(ADC, ADC_ADF_INT);
  149.         }
  150.         else if(u8Option == '2')
  151.         {
  152.             printf("%d conversion results of differential input channel pair 1:\n", BURST_COUNT);
  153.             /* Set the ADC operation mode as burst, input mode as differential and
  154.                              enable analog input channel 2 for differential input channel pair 1*/
  155.             ADC_Open(ADC, ADC_ADCR_DIFFEN_DIFFERENTIAL, ADC_ADCR_ADMD_BURST, 1 << 2);

  156.             /* Power on ADC module */
  157.             ADC_POWER_ON(ADC);

  158.             /* clear the A/D interrupt flag for safe */
  159.             ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  160.             /* Enable the ADC interrupt */
  161.             ADC_EnableInt(ADC, ADC_ADF_INT);
  162.             NVIC_EnableIRQ(ADC_IRQn);

  163.             /* Reset the ADC data counter and start A/D conversion */
  164.             g_u32AdcDataCount = 0;
  165.             ADC_START_CONV(ADC);

  166.             while(g_u32AdcDataCount < BURST_COUNT);

  167.             /* Stop A/D conversion */
  168.             ADC_STOP_CONV(ADC);
  169.             /* Disable the ADC interrupt */
  170.             ADC_DisableInt(ADC, ADC_ADF_INT);
  171.         }
  172.         else
  173.             return ;

  174.         for(u32Count = 0; u32Count < BURST_COUNT; u32Count++)
  175.         {
  176.             printf("Conversion result: 0x%X\n", g_au16AdcData[u32Count]);
  177.         }

  178.         /* Change operation mode to clear burst mode FIFO buffers */
  179.         ADC_Open(ADC, ADC_ADCR_DIFFEN_DIFFERENTIAL, ADC_ADCR_ADMD_SINGLE, 2);

  180.     }
  181. }

  182. /*---------------------------------------------------------------------------------------------------------*/
  183. /* ADC interrupt handler                                                                                   */
  184. /*---------------------------------------------------------------------------------------------------------*/
  185. void ADC_IRQHandler(void)
  186. {
  187.     while(ADC_IS_DATA_VALID(ADC, 0)) /* Check the VALID bits */
  188.     {
  189.         if(g_u32AdcDataCount >= BURST_COUNT)
  190.             return;
  191.         /* In burst mode, the software always gets the conversion result of the specified channel from channel 0 */
  192.         g_au16AdcData[g_u32AdcDataCount++] = (uint16_t)ADC_GET_CONVERSION_DATA(ADC, 0);
  193.     }

  194.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* clear the A/D conversion flag */
  195. }

  196. /*---------------------------------------------------------------------------------------------------------*/
  197. /* MAIN function                                                                                           */
  198. /*---------------------------------------------------------------------------------------------------------*/

  199. main(void)
  200. {

  201.     /* Unlock protected registers */
  202.     SYS_UnlockReg();

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

  205.     /* Lock protected registers */
  206.     SYS_LockReg();

  207.     /* Init UART0 for printf */
  208.     UART0_Init();

  209.     /*---------------------------------------------------------------------------------------------------------*/
  210.     /* SAMPLE CODE                                                                                             */
  211.     /*---------------------------------------------------------------------------------------------------------*/

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

  213.     /* Burst Mode test */
  214.     AdcBurstModeTest();

  215.     /* Disable ADC module */
  216.     ADC_Close(ADC);

  217.     /* Disable ADC IP clock */
  218.     CLK_DisableModuleClock(ADC_MODULE);

  219.     /* Disable External Interrupt */
  220.     NVIC_DisableIRQ(ADC_IRQn);

  221.     printf("\nExit ADC sample code\n");

  222.     while(1);

  223. }


 楼主| wahahaheihei 发表于 2016-4-27 20:03 | 显示全部楼层
脉冲模式,下的ADC应该是可以捕捉哪些变化很快的脉冲信号的。
 楼主| wahahaheihei 发表于 2016-4-27 20:06 | 显示全部楼层
那么谁能详细说一下这个Burst模式有什么特点吗
 楼主| wahahaheihei 发表于 2016-4-27 20:08 | 显示全部楼层
  1. ADC_ContinuousScanMode,连续扫描模式,这种模式其实就是不听的查询,又成查询模式。


 楼主| wahahaheihei 发表于 2016-4-27 20:10 | 显示全部楼层
ADC_PwmTrigger
PWM 触发模式
/****************************************************************************
* @file    main.c
* @version  V3.0
* $Revision: 4 $
* $Date: 14/01/28 11:44a $
* @brief    M051 Series ADC Interface Controller Driver Sample Code
*
* @note
* Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "M051Series.h"

#define PLL_CLOCK       50000000


/*---------------------------------------------------------------------------------------------------------*/
/* Define Function Prototypes                                                                              */
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Init(void);
void UART0_Init(void);
void ADC_PWMTrigTest_SingleOpMode(void);


void SYS_Init(void)
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/

    /* Enable Internal RC 22.1184MHz clock */
    CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

    /* Waiting for Internal RC clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

    /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
    CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

    /* Enable external XTAL 12MHz clock */
    CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

    /* Waiting for external XTAL clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

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

    /* Enable UART module clock */
    CLK_EnableModuleClock(UART0_MODULE);

    /* Enable ADC module clock */
    CLK_EnableModuleClock(ADC_MODULE);

    /* Enable PWM01 module clock */
    CLK_EnableModuleClock(PWM01_MODULE);

    /* Select UART module clock source */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

    /* Select PWM01 module clock source */
    CLK_SetModuleClock(PWM01_MODULE, CLK_CLKSEL1_PWM01_S_HXT, 0);

    /* ADC clock source is 22.1184MHz, set divider to 7, ADC clock is 22.1184/7 MHz */
    CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(7));

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/

    /* Set P3 multi-function pins for UART0 RXD and TXD */
    SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
    SYS->P3_MFP |= SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

    /* Disable the P1.0 - P1.3 digital input path to avoid the leakage current */
    GPIO_DISABLE_DIGITAL_PATH(P1, 0xF);

    /* Configure the P1.0 - P1.3 ADC analog input pins */
    SYS->P1_MFP &= ~(SYS_MFP_P10_Msk | SYS_MFP_P11_Msk | SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
    SYS->P1_MFP |= SYS_MFP_P10_AIN0 | SYS_MFP_P11_AIN1 | SYS_MFP_P12_AIN2 | SYS_MFP_P13_AIN3 ;

    /* Configure the P2.0 as PWM0 output pin */
    SYS->P2_MFP &= ~(SYS_MFP_P20_Msk);
    SYS->P2_MFP |= SYS_MFP_P20_PWM0;

}

/*---------------------------------------------------------------------------------------------------------*/
/* Init UART                                                                                               */
/*---------------------------------------------------------------------------------------------------------*/
void UART0_Init()
{
    /* Reset IP */
    SYS_ResetModule(UART0_RST);

    /* Configure UART0 and set UART0 Baudrate */
    UART_Open(UART0, 115200);
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: ADC_PWMTrigTest_SingleOpMode                                                                  */
/*                                                                                                         */
/* Parameters:                                                                                             */
/*   None.                                                                                                 */
/*                                                                                                         */
/* Returns:                                                                                                */
/*   None.                                                                                                 */
/*                                                                                                         */
/* Description:                                                                                            */
/*   ADC hardware trigger test.                                                                            */
/*---------------------------------------------------------------------------------------------------------*/
void ADC_PWMTrigTest_SingleOpMode()
{
    printf("\n<<< PWM trigger test (Single mode) >>>\n");

    /* Set the ADC operation mode as single, input mode as single-end and enable the analog input channel 2 */
    ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE, 0x1 << 2);

    /* Power on ADC module */
    ADC_POWER_ON(ADC);

    /* Configure the hardware trigger condition and enable hardware trigger; PWM trigger delay: (4*10) system clock cycles*/
    ADC_EnableHWTrigger(ADC, ADC_ADCR_TRGS_PWM, 10);

    /* clear the A/D interrupt flag for safe */
    ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

    /* Edge-aligned type; one-shot mode. */
    PWM_SET_ALIGNED_TYPE(PWMA, 0x1, PWM_EDGE_ALIGNED);
    /* Clock prescaler */
    PWM_SET_PRESCALER(PWMA, 0, 2);
    /* Timer 0 divisor=1 */
    PWM_SET_DIVIDER(PWMA, 0, PWM_CLK_DIV_1);
    /* PWM counter value */ /* PWM frequency = PWM clock source/(clock prescaler setting + 1)/divisor/(CNR+1) */
    PWM_SET_CNR(PWMA, 0, 5);
    /* PWM compare value */
    PWM_SET_CMR(PWMA, 0, 1);
    /* Enable PWM0 to trigger ADC */
    PWM_EnableADCTrigger(PWMA, 0, PWM_PERIOD_TRIGGER_ADC);
    /* PWM0 pin output enabled. PWM frequency 1MHz, duty 30%. */
    PWM_EnableOutput(PWMA, 0x1);
    /* Start PWM module */
    PWM_Start(PWMA, 0x1);

    /* Wait conversion done */
    while(!ADC_GET_INT_FLAG(ADC, ADC_ADF_INT));

    /* Clear the ADC interrupt flag */
    ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

    printf("Channel 2: 0x%X\n", ADC_GET_CONVERSION_DATA(ADC, 2));

    /* Disable ADC */
    ADC_POWER_DOWN(ADC);
    /* Stop PWM generation */
    PWM_ForceStop(PWMA, 0x1);

}





/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function                                                                                           */
/*---------------------------------------------------------------------------------------------------------*/
main(void)
{

    /* Unlock protected registers */
    SYS_UnlockReg();

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

    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART0 for printf */
    UART0_Init();

    /*---------------------------------------------------------------------------------------------------------*/
    /* SAMPLE CODE                                                                                             */
    /*---------------------------------------------------------------------------------------------------------*/

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

    /* ADC hardware trigger test */
    ADC_PWMTrigTest_SingleOpMode();

    /* Disable ADC module */
    ADC_Close(ADC);

    /* Disable ADC IP clock */
    CLK_DisableModuleClock(ADC_MODULE);

    /* Disable External Interrupt */
    NVIC_DisableIRQ(ADC_IRQn);

    printf("\nExit ADC sample code\n");

    while(1);

}


 楼主| wahahaheihei 发表于 2016-4-27 20:12 | 显示全部楼层
这个PWM触发模式也是很难理解的,有高手懂吗
 楼主| wahahaheihei 发表于 2016-4-27 20:13 | 显示全部楼层
ResultMonitor 模式,这个到底是咋回事,结果监视器??
  1. /****************************************************************************
  2. * @file     main.c
  3. * @version  V3.0
  4. * $Revision: 4 $
  5. * $Date: 14/01/28 11:44a $
  6. * @brief    M051 Series ADC Interface Controller Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M051Series.h"

  14. #define PLL_CLOCK       50000000



  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Define Function Prototypes                                                                              */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. void SYS_Init(void);
  19. void UART0_Init(void);
  20. void AdcResultMonitorTest(void);


  21. /*---------------------------------------------------------------------------------------------------------*/
  22. /* Define global variables and constants                                                                   */
  23. /*---------------------------------------------------------------------------------------------------------*/
  24. volatile uint32_t g_u32AdcCmp0IntFlag;
  25. volatile uint32_t g_u32AdcCmp1IntFlag;


  26. void SYS_Init(void)
  27. {
  28.     /*---------------------------------------------------------------------------------------------------------*/
  29.     /* Init System Clock                                                                                       */
  30.     /*---------------------------------------------------------------------------------------------------------*/

  31.     /* Enable Internal RC 22.1184MHz clock */
  32.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  33.     /* Waiting for Internal RC clock ready */
  34.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  35.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  36.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  37.     /* Enable external XTAL 12MHz clock */
  38.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  39.     /* Waiting for external XTAL clock ready */
  40.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  41.     /* Set core clock as PLL_CLOCK from PLL */
  42.     CLK_SetCoreClock(PLL_CLOCK);

  43.     /* Enable UART module clock */
  44.     CLK_EnableModuleClock(UART0_MODULE);

  45.     /* Enable ADC module clock */
  46.     CLK_EnableModuleClock(ADC_MODULE);

  47.     /* Select UART module clock source */
  48.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  49.     /* ADC clock source is 22.1184MHz, set divider to 7, ADC clock is 22.1184/7 MHz */
  50.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(7));

  51.     /*---------------------------------------------------------------------------------------------------------*/
  52.     /* Init I/O Multi-function                                                                                 */
  53.     /*---------------------------------------------------------------------------------------------------------*/

  54.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  55.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  56.     SYS->P3_MFP |= SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

  57.     /* Disable the P1.0 - P1.3 digital input path to avoid the leakage current */
  58.     GPIO_DISABLE_DIGITAL_PATH(P1, 0xF);

  59.     /* Configure the P1.0 - P1.3 ADC analog input pins */
  60.     SYS->P1_MFP &= ~(SYS_MFP_P10_Msk | SYS_MFP_P11_Msk | SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
  61.     SYS->P1_MFP |= SYS_MFP_P10_AIN0 | SYS_MFP_P11_AIN1 | SYS_MFP_P12_AIN2 | SYS_MFP_P13_AIN3 ;

  62. }

  63. /*---------------------------------------------------------------------------------------------------------*/
  64. /* Init UART                                                                                               */
  65. /*---------------------------------------------------------------------------------------------------------*/
  66. void UART0_Init()
  67. {
  68.     /* Reset IP */
  69.     SYS_ResetModule(UART0_RST);

  70.     /* Configure UART0 and set UART0 Baudrate */
  71.     UART_Open(UART0, 115200);
  72. }

  73. /*---------------------------------------------------------------------------------------------------------*/
  74. /* Function: AdcResultMonitorTest                                                                          */
  75. /*                                                                                                         */
  76. /* Parameters:                                                                                             */
  77. /*   None.                                                                                                 */
  78. /*                                                                                                         */
  79. /* Returns:                                                                                                */
  80. /*   None.                                                                                                 */
  81. /*                                                                                                         */
  82. /* Description:                                                                                            */
  83. /*   ADC result monitor function test.                                                                     */
  84. /*---------------------------------------------------------------------------------------------------------*/
  85. void AdcResultMonitorTest()
  86. {
  87.     printf("\n");
  88.     printf("+----------------------------------------------------------------------+\n");
  89.     printf("|           ADC compare function (result monitor) sample code          |\n");
  90.     printf("+----------------------------------------------------------------------+\n");
  91.     printf("\nIn this test, software will compare the conversion result of channel 2.\n");

  92.     /* Set the ADC operation mode as continuous scan, input mode as single-end and enable the analog input channel 2 */
  93.     ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_CONTINUOUS, 0x1 << 2);

  94.     /* Power on ADC module */
  95.     ADC_POWER_ON(ADC);

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

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

  102.     /* clear the ADC comparator 0 interrupt flag for safe */
  103.     ADC_CLR_INT_FLAG(ADC, ADC_CMP0_INT);
  104.     /* enable ADC comparator 0 interrupt */
  105.     ADC_EnableInt(ADC, ADC_CMP0_INT);

  106.     /* clear the ADC comparator 1 interrupt flag for safe */
  107.     ADC_CLR_INT_FLAG(ADC, ADC_CMP1_INT);
  108.     /* enable ADC comparator 1 interrupt */
  109.     ADC_EnableInt(ADC, ADC_CMP1_INT);

  110.     NVIC_EnableIRQ(ADC_IRQn);

  111.     g_u32AdcCmp0IntFlag = 0;
  112.     g_u32AdcCmp1IntFlag = 0;

  113.     /* Clear the ADC interrupt flag */
  114.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  115.     /* start A/D conversion */
  116.     ADC_START_CONV(ADC);

  117.     /* Wait ADC compare interrupt */
  118.     while((g_u32AdcCmp0IntFlag == 0) && (g_u32AdcCmp1IntFlag == 0));

  119.     /* Stop A/D conversion */
  120.     ADC_STOP_CONV(ADC);
  121.     /* Disable ADC comparator interrupt */
  122.     ADC_DisableInt(ADC, ADC_CMP0_INT);
  123.     ADC_DisableInt(ADC, ADC_CMP1_INT);
  124.     /* Disable compare function */
  125.     ADC_DISABLE_CMP0(ADC);
  126.     ADC_DISABLE_CMP1(ADC);

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


  136. /*---------------------------------------------------------------------------------------------------------*/
  137. /* ADC interrupt handler                                                                                   */
  138. /*---------------------------------------------------------------------------------------------------------*/
  139. void ADC_IRQHandler(void)
  140. {
  141.     if(ADC_GET_INT_FLAG(ADC, ADC_CMP0_INT) != 0)
  142.     {
  143.         g_u32AdcCmp0IntFlag = 1;
  144.         ADC_CLR_INT_FLAG(ADC, ADC_CMP0_INT);     /* clear the A/D compare flag 0 */
  145.     }

  146.     if(ADC_GET_INT_FLAG(ADC, ADC_CMP1_INT) != 0)
  147.     {
  148.         g_u32AdcCmp1IntFlag = 1;
  149.         ADC_CLR_INT_FLAG(ADC, ADC_CMP1_INT);     /* clear the A/D compare flag 1 */
  150.     }
  151. }
  152. /*---------------------------------------------------------------------------------------------------------*/
  153. /* MAIN function                                                                                           */
  154. /*---------------------------------------------------------------------------------------------------------*/

  155. main(void)
  156. {

  157.     /* Unlock protected registers */
  158.     SYS_UnlockReg();

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

  161.     /* Lock protected registers */
  162.     SYS_LockReg();

  163.     /* Init UART0 for printf */
  164.     UART0_Init();

  165.     /*---------------------------------------------------------------------------------------------------------*/
  166.     /* SAMPLE CODE                                                                                             */
  167.     /*---------------------------------------------------------------------------------------------------------*/

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

  169.     /* Result monitor test */
  170.     AdcResultMonitorTest();

  171.     /* Disable ADC module */
  172.     ADC_Close(ADC);

  173.     /* Disable ADC IP clock */
  174.     CLK_DisableModuleClock(ADC_MODULE);

  175.     /* Disable External Interrupt */
  176.     NVIC_DisableIRQ(ADC_IRQn);

  177.     printf("\nExit ADC sample code\n");

  178.     while(1);

  179. }


 楼主| wahahaheihei 发表于 2016-4-27 20:15 | 显示全部楼层
SingleCycleScan
字面上是单周期扫描,如何理解呢?是单通道周期扫描吗?
  1. /****************************************************************************
  2. * @file    main.c
  3. * @version  V3.0
  4. * $Revision: 4 $
  5. * $Date: 14/01/28 11:44a $
  6. * @brief    M051 Series ADC Interface Controller Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M051Series.h"

  14. #define PLL_CLOCK       50000000


  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Define Function Prototypes                                                                              */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. void SYS_Init(void);
  19. void UART0_Init(void);
  20. void AdcSingleCycleScanModeTest(void);


  21. void SYS_Init(void)
  22. {
  23.     /*---------------------------------------------------------------------------------------------------------*/
  24.     /* Init System Clock                                                                                       */
  25.     /*---------------------------------------------------------------------------------------------------------*/

  26.     /* Enable Internal RC 22.1184MHz clock */
  27.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  28.     /* Waiting for Internal RC clock ready */
  29.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  30.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  31.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  32.     /* Enable external XTAL 12MHz clock */
  33.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  34.     /* Waiting for external XTAL clock ready */
  35.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  36.     /* Set core clock as PLL_CLOCK from PLL */
  37.     CLK_SetCoreClock(PLL_CLOCK);

  38.     /* Enable UART module clock */
  39.     CLK_EnableModuleClock(UART0_MODULE);

  40.     /* Enable ADC module clock */
  41.     CLK_EnableModuleClock(ADC_MODULE);

  42.     /* Select UART module clock source */
  43.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  44.     /* ADC clock source is 22.1184MHz, set divider to 7, ADC clock is 22.1184/7 MHz */
  45.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(7));

  46.     /*---------------------------------------------------------------------------------------------------------*/
  47.     /* Init I/O Multi-function                                                                                 */
  48.     /*---------------------------------------------------------------------------------------------------------*/

  49.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  50.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  51.     SYS->P3_MFP |= SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

  52.     /* Disable the P1.0 - P1.3 digital input path to avoid the leakage current */
  53.     GPIO_DISABLE_DIGITAL_PATH(P1, 0xF);

  54.     /* Configure the P1.0 - P1.3 ADC analog input pins */
  55.     SYS->P1_MFP &= ~(SYS_MFP_P10_Msk | SYS_MFP_P11_Msk | SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
  56.     SYS->P1_MFP |= SYS_MFP_P10_AIN0 | SYS_MFP_P11_AIN1 | SYS_MFP_P12_AIN2 | SYS_MFP_P13_AIN3 ;

  57. }

  58. /*---------------------------------------------------------------------------------------------------------*/
  59. /* Init UART                                                                                               */
  60. /*---------------------------------------------------------------------------------------------------------*/
  61. void UART0_Init()
  62. {
  63.     /* Reset IP */
  64.     SYS_ResetModule(UART0_RST);

  65.     /* Configure UART0 and set UART0 Baudrate */
  66.     UART_Open(UART0, 115200);
  67. }

  68. /*---------------------------------------------------------------------------------------------------------*/
  69. /* Function: AdcSingleCycleScanModeTest                                                                    */
  70. /*                                                                                                         */
  71. /* Parameters:                                                                                             */
  72. /*   None.                                                                                                 */
  73. /*                                                                                                         */
  74. /* Returns:                                                                                                */
  75. /*   None.                                                                                                 */
  76. /*                                                                                                         */
  77. /* Description:                                                                                            */
  78. /*   ADC single cycle scan mode test.                                                                      */
  79. /*---------------------------------------------------------------------------------------------------------*/
  80. void AdcSingleCycleScanModeTest()
  81. {
  82.     uint8_t  u8Option;
  83.     uint32_t u32ChannelCount;
  84.     int32_t  i32ConversionData;

  85.     printf("\n");
  86.     printf("+----------------------------------------------------------------------+\n");
  87.     printf("|                 ADC single cycle scan mode sample code               |\n");
  88.     printf("+----------------------------------------------------------------------+\n");

  89.     while(1)
  90.     {
  91.         printf("\n\nSelect input mode:\n");
  92.         printf("  [1] Single end input (channel 0, 1, 2 and 3)\n");
  93.         printf("  [2] Differential input (input channel pair 0 and 1)\n");
  94.         printf("  Other keys: exit single cycle scan mode test\n");
  95.         u8Option = getchar();
  96.         if(u8Option == '1')
  97.         {

  98.             /* Set the ADC operation mode as single-cycle, input mode as single-end and
  99.                  enable the analog input channel 0, 1, 2 and 3 */
  100.             ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE_CYCLE, 0xF);

  101.             /* Power on ADC module */
  102.             ADC_POWER_ON(ADC);

  103.             /* clear the A/D interrupt flag for safe */
  104.             ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  105.             /* start A/D conversion */
  106.             ADC_START_CONV(ADC);

  107.             /* Wait conversion done */
  108.             while(!ADC_GET_INT_FLAG(ADC, ADC_ADF_INT));

  109.             for(u32ChannelCount = 0; u32ChannelCount < 4; u32ChannelCount++)
  110.             {
  111.                 i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, u32ChannelCount);
  112.                 printf("Conversion result of channel %d: 0x%X (%d)\n", u32ChannelCount, i32ConversionData, i32ConversionData);
  113.             }
  114.         }
  115.         else if(u8Option == '2')
  116.         {

  117.             /* Set the ADC operation mode as single-cycle, input mode as differential and
  118.                enable analog input channel 0 and 2 */
  119.             ADC_Open(ADC, ADC_ADCR_DIFFEN_DIFFERENTIAL, ADC_ADCR_ADMD_SINGLE_CYCLE, 0x5);

  120.             /* Power on ADC module */
  121.             ADC_POWER_ON(ADC);

  122.             /* clear the A/D interrupt flag for safe */
  123.             ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  124.             /* start A/D conversion */
  125.             ADC_START_CONV(ADC);

  126.             /* Wait conversion done */
  127.             while(!ADC_GET_INT_FLAG(ADC, ADC_ADF_INT));

  128.             for(u32ChannelCount = 0; u32ChannelCount < 2; u32ChannelCount++)
  129.             {
  130.                 i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, u32ChannelCount * 2);
  131.                 printf("Conversion result of differential input pair %d: 0x%X (%d)\n", u32ChannelCount, i32ConversionData, i32ConversionData);
  132.             }
  133.         }
  134.         else
  135.             return ;

  136.     }
  137. }

  138. /*---------------------------------------------------------------------------------------------------------*/
  139. /* MAIN function                                                                                           */
  140. /*---------------------------------------------------------------------------------------------------------*/

  141. main(void)
  142. {

  143.     /* Unlock protected registers */
  144.     SYS_UnlockReg();

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

  147.     /* Lock protected registers */
  148.     SYS_LockReg();

  149.     /* Init UART0 for printf */
  150.     UART0_Init();

  151.     /*---------------------------------------------------------------------------------------------------------*/
  152.     /* SAMPLE CODE                                                                                             */
  153.     /*---------------------------------------------------------------------------------------------------------*/

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

  155.     /* Single cycle scan mode test */
  156.     AdcSingleCycleScanModeTest();

  157.     /* Disable ADC module */
  158.     ADC_Close(ADC);

  159.     /* Disable ADC IP clock */
  160.     CLK_DisableModuleClock(ADC_MODULE);

  161.     /* Disable External Interrupt */
  162.     NVIC_DisableIRQ(ADC_IRQn);

  163.     printf("\nExit ADC sample code\n");

  164.     while(1);

  165. }


 楼主| wahahaheihei 发表于 2016-4-27 20:23 | 显示全部楼层

ADC_SingleMode 单通道模式,也应该是单次模式。就是一下而已。
  1. /****************************************************************************
  2. * @file     main.c
  3. * @version  V3.0
  4. * $Revision: 4 $
  5. * $Date: 14/01/28 11:44a $
  6. * @brief    M051 Series ADC Interface Controller Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M051Series.h"

  14. #define PLL_CLOCK       50000000



  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Define Function Prototypes                                                                              */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. void SYS_Init(void);
  19. void UART0_Init(void);
  20. void AdcSingleModeTest(void);


  21. /*---------------------------------------------------------------------------------------------------------*/
  22. /* Define global variables and constants                                                                   */
  23. /*---------------------------------------------------------------------------------------------------------*/
  24. volatile uint32_t g_u32AdcIntFlag;


  25. void SYS_Init(void)
  26. {
  27.     /*---------------------------------------------------------------------------------------------------------*/
  28.     /* Init System Clock                                                                                       */
  29.     /*---------------------------------------------------------------------------------------------------------*/

  30.     /* Enable Internal RC 22.1184MHz clock */
  31.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  32.     /* Waiting for Internal RC clock ready */
  33.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  34.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  35.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  36.     /* Enable external XTAL 12MHz clock */
  37.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  38.     /* Waiting for external XTAL clock ready */
  39.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  40.     /* Set core clock as PLL_CLOCK from PLL */
  41.     CLK_SetCoreClock(PLL_CLOCK);

  42.     /* Enable UART module clock */
  43.     CLK_EnableModuleClock(UART0_MODULE);

  44.     /* Enable ADC module clock */
  45.     CLK_EnableModuleClock(ADC_MODULE);

  46.     /* Select UART module clock source */
  47.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));

  48.     /* ADC clock source is 22.1184MHz, set divider to 7, ADC clock is 22.1184/7 MHz */
  49.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(7));

  50.     /*---------------------------------------------------------------------------------------------------------*/
  51.     /* Init I/O Multi-function                                                                                 */
  52.     /*---------------------------------------------------------------------------------------------------------*/

  53.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  54.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  55.     SYS->P3_MFP |= SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

  56.     /* Disable the P1.0 - P1.3 digital input path to avoid the leakage current */
  57.     GPIO_DISABLE_DIGITAL_PATH(P1, 0xF);

  58.     /* Configure the P1.0 - P1.3 ADC analog input pins */
  59.     SYS->P1_MFP &= ~(SYS_MFP_P10_Msk | SYS_MFP_P11_Msk | SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
  60.     SYS->P1_MFP |= SYS_MFP_P10_AIN0 | SYS_MFP_P11_AIN1 | SYS_MFP_P12_AIN2 | SYS_MFP_P13_AIN3 ;

  61. }

  62. /*---------------------------------------------------------------------------------------------------------*/
  63. /* Init UART                                                                                               */
  64. /*---------------------------------------------------------------------------------------------------------*/
  65. void UART0_Init()
  66. {
  67.     /* Reset IP */
  68.     SYS_ResetModule(UART0_RST);

  69.     /* Configure UART0 and set UART0 Baudrate */
  70.     UART_Open(UART0, 115200);
  71. }

  72. /*---------------------------------------------------------------------------------------------------------*/
  73. /* Function: AdcSingleModeTest                                                                             */
  74. /*                                                                                                         */
  75. /* Parameters:                                                                                             */
  76. /*   None.                                                                                                 */
  77. /*                                                                                                         */
  78. /* Returns:                                                                                                */
  79. /*   None.                                                                                                 */
  80. /*                                                                                                         */
  81. /* Description:                                                                                            */
  82. /*   ADC single mode test.                                                                                 */
  83. /*---------------------------------------------------------------------------------------------------------*/
  84. void AdcSingleModeTest()
  85. {
  86.     uint8_t  u8Option;
  87.     int32_t  i32ConversionData;

  88.     printf("\n");
  89.     printf("+----------------------------------------------------------------------+\n");
  90.     printf("|                      ADC single mode sample code                     |\n");
  91.     printf("+----------------------------------------------------------------------+\n");

  92.     while(1)
  93.     {
  94.         printf("Select input mode:\n");
  95.         printf("  [1] Single end input (channel 2 only)\n");
  96.         printf("  [2] Differential input (channel pair 1 only)\n");
  97.         printf("  Other keys: exit single mode test\n");
  98.         u8Option = getchar();
  99.         if(u8Option == '1')
  100.         {

  101.             /* Set the ADC operation mode as single, input mode as single-end and enable the analog input channel 2 */
  102.             ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE, 0x1 << 2);

  103.             /* Power on ADC module */
  104.             ADC_POWER_ON(ADC);

  105.             /* clear the A/D interrupt flag for safe */
  106.             ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  107.             /* Enable the ADC interrupt */
  108.             ADC_EnableInt(ADC, ADC_ADF_INT);
  109.             NVIC_EnableIRQ(ADC_IRQn);

  110.             /* Reset the ADC interrupt indicator and Start A/D conversion */
  111.             g_u32AdcIntFlag = 0;
  112.             ADC_START_CONV(ADC);

  113.             /* Wait ADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function)*/
  114.             while(g_u32AdcIntFlag == 0);

  115.             /* Disable the ADC interrupt */
  116.             ADC_DisableInt(ADC, ADC_ADF_INT);

  117.             /* Get the conversion result of the ADC channel 2 */
  118.             i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, 2);
  119.             printf("Conversion result of channel 2: 0x%X (%d)\n\n", i32ConversionData, i32ConversionData);
  120.         }
  121.         else if(u8Option == '2')
  122.         {

  123.             /* Set the ADC operation mode as single, input mode as differential and
  124.                enable analog input channel 2 for differential input channel pair 1*/
  125.             ADC_Open(ADC, ADC_ADCR_DIFFEN_DIFFERENTIAL, ADC_ADCR_ADMD_SINGLE, 0x1 << 2);

  126.             /* Power on ADC module */
  127.             ADC_POWER_ON(ADC);

  128.             /* clear the A/D interrupt flag for safe */
  129.             ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  130.             /* Enable the ADC interrupt */
  131.             ADC_EnableInt(ADC, ADC_ADF_INT);
  132.             NVIC_EnableIRQ(ADC_IRQn);

  133.             /* Reset the ADC interrupt indicator and Start A/D conversion */
  134.             g_u32AdcIntFlag = 0;
  135.             ADC_START_CONV(ADC);

  136.             /* Wait ADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function)*/
  137.             while(g_u32AdcIntFlag == 0);

  138.             /* Disable the ADC interrupt */
  139.             ADC_DisableInt(ADC, ADC_ADF_INT);

  140.             /* Get the conversion result of the specified ADC channel */
  141.             i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, 2);
  142.             printf("Conversion result of channel pair 1: 0x%X (%d)\n\n", i32ConversionData, i32ConversionData);
  143.         }
  144.         else
  145.             return ;

  146.     }
  147. }



  148. /*---------------------------------------------------------------------------------------------------------*/
  149. /* ADC interrupt handler                                                                                   */
  150. /*---------------------------------------------------------------------------------------------------------*/
  151. void ADC_IRQHandler(void)
  152. {
  153.     g_u32AdcIntFlag = 1;
  154.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* clear the A/D conversion flag */
  155. }

  156. /*---------------------------------------------------------------------------------------------------------*/
  157. /* MAIN function                                                                                           */
  158. /*---------------------------------------------------------------------------------------------------------*/

  159. main(void)
  160. {

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

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

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

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

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

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

  173.     /* Single Mode test */
  174.     AdcSingleModeTest();

  175.     /* Disable ADC module */
  176.     ADC_Close(ADC);

  177.     /* Disable ADC IP clock */
  178.     CLK_DisableModuleClock(ADC_MODULE);

  179.     /* Disable External Interrupt */
  180.     NVIC_DisableIRQ(ADC_IRQn);

  181.     printf("\nExit ADC sample code\n");

  182.     while(1);

  183. }


 楼主| wahahaheihei 发表于 2016-4-27 20:50 | 显示全部楼层
void UART0_Init(void)
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init UART                                                                                               */
    /*---------------------------------------------------------------------------------------------------------*/
    /* Reset UART */
    SYS_ResetModule(UART0_RST);

    /* Configure UART0 and set UART0 Baudrate */
    UART_Open(UART0, 115200);
}
这个就是好多例程里用到的串口打印的初始化函数,我们看,用的是串口0.
系统重置模块串口0,然后就是配置波特率打开端口,多么的简单啊,这就是新唐库函数的魅力。

Roderman_z 发表于 2016-4-27 20:55 | 显示全部楼层
如何设置adc的采样率呢,是需要和定时器搭配使用吗
 楼主| wahahaheihei 发表于 2016-4-27 21:01 | 显示全部楼层
Roderman_z 发表于 2016-4-27 20:55
如何设置adc的采样率呢,是需要和定时器搭配使用吗

采样率就要靠定时器了,可以根据需要做个定时中断,在中断里完成一次采样。
yiyigirl2014 发表于 2016-4-28 11:00 | 显示全部楼层
  • /*---------------------------------------------------------------------------------------------------------*/
  •     /* Init I/O Multi-function                                                                                 */
  •     /*---------------------------------------------------------------------------------------------------------*/
  •     /* Set P3 multi-function pins for UART0 RXD and TXD */
  •     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  •     SYS->P3_MFP |= SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
  •     /* Disable the P1.0 - P1.3 digital input path to avoid the leakage current */
  •     GPIO_DISABLE_DIGITAL_PATH(P1, 0xF);
  •     /* Configure the P1.0 - P1.3 ADC analog input pins */
  •     SYS->P1_MFP &= ~(SYS_MFP_P10_Msk | SYS_MFP_P11_Msk | SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
  •     SYS->P1_MFP |= SYS_MFP_P10_AIN0 | SYS_MFP_P11_AIN1 | SYS_MFP_P12_AIN2 | SYS_MFP_P13_AIN3 ;
  • }
其实好多都在这个系统初始化函数里了,比如这个IO多功能选择的函数,就是配置哪个端口作为串口的。通过映射方式映射到P3
还有关闭哪个数字输入漏电流是什么意思。
最后配置1.0到1.3的模拟输入管脚怎么理解。

wawa2016 发表于 2016-4-28 14:59 | 显示全部楼层
好多。。。
heisexingqisi 发表于 2016-4-28 22:40 | 显示全部楼层
/* Disable the P1.0 - P1.3 digital input path to avoid the leakage current */
    GPIO_DISABLE_DIGITAL_PATH(P1, 0xF);
这个是关闭输入旁路漏电流吧。不知道没有这句话会怎么样
zhuomuniao110 发表于 2016-4-28 23:40 | 显示全部楼层
使用库函数的操作现在是主流,特别那么多高级的芯片,外设特别多。
heisexingqisi 发表于 2016-4-29 17:08 | 显示全部楼层
官方应该出来,弄一本PDF的中文版教程,这样大家才好学。英语差的实在没法弄了。
gejigeji521 发表于 2016-4-29 22:10 | 显示全部楼层
  /* Set P3 multi-function pins for UART0 RXD and TXD */
    SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
    SYS->P3_MFP |= SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
这里配置有库函数的方式没,看不懂这寄存器意思。
天灵灵地灵灵 发表于 2016-4-30 12:33 | 显示全部楼层
   /* Waiting for Internal RC clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
每次时钟配置后都是要等待稳定的,实际上这个时间很短吧,不等待是不是会有问题出现?
稳稳の幸福 发表于 2016-4-30 13:54 | 显示全部楼层
/* Set core clock as PLL_CLOCK from PLL */设置内核时钟为PLL
    CLK_SetCoreClock(PLL_CLOCK);

    /* Enable UART module clock */ 使能串口时钟模块
    CLK_EnableModuleClock(UART0_MODULE);

    /* Enable ADC module clock */ 使能模数转换模块时钟
    CLK_EnableModuleClock(ADC_MODULE);

    /* Select UART module clock source */ 选择串口模块的时钟源
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

    /* ADC clock source is 22.1184MHz, set divider to 7, ADC clock is 22.1184/7 MHz */
    CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(7));
您需要登录后才可以回帖 登录 | 注册

本版积分规则

232

主题

3223

帖子

12

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