[DemoCode下载]

ADC不同时钟运行连续采样

[复制链接]
499|10
手机看帖
扫描二维码
随时随地手机跟帖
天灵灵地灵灵|  楼主 | 2020-7-4 22:57 | 显示全部楼层 |阅读模式
/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
* [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrate how to use HIRC as ADC clock source to achieve 1411 ksps ADC conversion rate.
*
* SPDX-License-Identifier: Apache-2.0
* [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2018 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "NuMicro.h"


/*---------------------------------------------------------------------------------------------------------*/
/* Define global variables and constants                                                                   */
/*---------------------------------------------------------------------------------------------------------*/
volatile uint32_t g_u32AdcIntFlag;


void SYS_Init(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Enable HIRC clock (Internal RC 48 MHz) */
    CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

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

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

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

    /* Switch UART0 clock source to HIRC */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

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

    /* ADC clock source is HCLK = PCLK1 = 48MHz, set divider to 2, ADC clock is 48/2 MHz */
    CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_PCLK1, CLK_CLKDIV0_ADC(2));

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
    SystemCoreClockUpdate();

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

    /* Set GPB multi-function pins for UART0 RXD and TXD */
    SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
    SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

    /* Set PB.0 ~ PB.3 to input mode */
    GPIO_SetMode(PB, BIT0|BIT1|BIT2|BIT3, GPIO_MODE_INPUT);

    /* Configure the GPB0 - GPB3 ADC analog input pins.  */
    SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB0MFP_Msk | SYS_GPB_MFPL_PB1MFP_Msk |
                       SYS_GPB_MFPL_PB2MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk);
    SYS->GPB_MFPL |= (SYS_GPB_MFPL_PB0MFP_ADC0_CH0 | SYS_GPB_MFPL_PB1MFP_ADC0_CH1 |
                      SYS_GPB_MFPL_PB2MFP_ADC0_CH2 | SYS_GPB_MFPL_PB3MFP_ADC0_CH3);

    /* Disable the GPB0 - GPB3 digital input path to avoid the leakage current. */
    GPIO_DISABLE_DIGITAL_PATH(PB, BIT0|BIT1|BIT2|BIT3);

    /* Lock protected registers */
    SYS_LockReg();
}

void ADC_FunctionTest()
{
    uint32_t u32ChannelCount;
    int32_t  i32ConversionData;

    printf("\n");
    printf("+----------------------------------------------------------------------+\n");
    printf("|           ADC 1411 ksps continuous scan mode sample code             |\n");
    printf("+----------------------------------------------------------------------+\n\n");

    printf("+----------------------------------------------------------------------+\n");
    printf("|   ADC clock source -> PCLK1  = 48 MHz                                |\n");
    printf("|   ADC clock divider          = 2                                     |\n");
    printf("|   ADC clock                  = 48 MHz / 2 = 24 MHz                   |\n");
    printf("|   ADC extended sampling time = 0                                     |\n");
    printf("|   ADC conversion time = 17 + ADC extended sampling time = 17         |\n");
    printf("|   ADC conversion rate = 24 MHz / 17 = 1411 ksps                      |\n");
    printf("+----------------------------------------------------------------------+\n");

    /* Enable ADC converter */
    ADC_POWER_ON(ADC);

    while(1)
    {
        printf(" Press any key to start the continuous scan mode test\n");
        getchar();
        /* Set the ADC operation mode as continuous scan, input mode as single-end and
             enable the analog input channel 0, 1, 2 and 3 */
        ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_CONTINUOUS, BIT0|BIT1|BIT2|BIT3);

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

        /* Enable the sample module interrupt */
        ADC_ENABLE_INT(ADC, ADC_ADF_INT);  // Enable sample module A/D interrupt.
        NVIC_EnableIRQ(ADC_IRQn);

        /* Reset the ADC interrupt indicator and trigger sample module 0 to start A/D conversion */
        g_u32AdcIntFlag = 0;
        ADC_START_CONV(ADC);

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

        /* Get the conversion result */
        for(u32ChannelCount = 0; u32ChannelCount < 4; u32ChannelCount++)
        {
            i32ConversionData = ADC_GET_CONVERSION_DATA(ADC, u32ChannelCount);
            printf("Conversion result of channel %d: 0x%X (%d)\n", u32ChannelCount, i32ConversionData, i32ConversionData);
        }

        printf("\n");

        /* Stop A/D conversion */
        ADC_STOP_CONV(ADC);

        /* Disable the sample module interrupt */
        ADC_DISABLE_INT(ADC, ADC_ADF_INT);
    }
}


void ADC_IRQHandler(void)
{
    g_u32AdcIntFlag = 1;
    ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* Clear the A/D interrupt flag */
}

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

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

int32_t main(void)
{
    /* Init System, IP clock and multi-function I/O. */
    SYS_Init();

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

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

    /* ADC function test */
    ADC_FunctionTest();

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

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

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

    while(1);
}


使用特权

评论回复
天灵灵地灵灵|  楼主 | 2020-7-4 22:57 | 显示全部楼层
/**************************************************************************//**
* @file     main.c
* @version  V3.00
* @brief    Demonstrate how to use PLL as ADC clock source to achieve 2 Msps ADC conversion rate.
*
* SPDX-License-Identifier: Apache-2.0
* @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "NuMicro.h"

#define PLL_CLOCK   68000000
#define PDMA_CH     1

/*---------------------------------------------------------------------------------------------------------*/
/* Define global variables and constants                                                                   */
/*---------------------------------------------------------------------------------------------------------*/
typedef struct dma_desc_t
{
    uint32_t ctl;
    uint32_t src;
    uint32_t dest;
    uint32_t offset;
} DMA_DESC_T;
DMA_DESC_T DMA_DESC[2];

uint32_t g_u32DMAConfig = 0;


void SYS_Init(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Enable PLL module clock from HIRC */
    CLK_EnablePLL(CLK_PLLCTL_PLLSRC_HIRC_DIV4, PLL_CLOCK);

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

    /* Switch UART0 clock source to HIRC */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

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

    /* ADC clock source is 68 MHz from PLL, set divider to 2, ADC clock is 68/2 MHz */
    CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_PLL, CLK_CLKDIV0_ADC(2));

    /* Enable PDMA clock source */
    CLK_EnableModuleClock(PDMA_MODULE);

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate Pll Clock, SystemCoreClock and CycylesPerUs automatically. */
    SystemCoreClockUpdate();

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

    /* Set GPB multi-function pins for UART0 RXD and TXD */
    SYS->GPB_MFPH = ((SYS->GPB_MFPH & (~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)))
                     | (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD));

    /* Configure the GPB2 - GPB3 ADC analog input pins.  */
    GPIO_SetMode(PB, BIT2, GPIO_MODE_INPUT);
    GPIO_SetMode(PB, BIT3, GPIO_MODE_INPUT);
    SYS->GPB_MFPL = ((SYS->GPB_MFPL & (~(SYS_GPB_MFPL_PB2MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk)))
                     | (SYS_GPB_MFPL_PB2MFP_ADC0_CH2 | SYS_GPB_MFPL_PB3MFP_ADC0_CH3));

    /* Disable the GPB2 digital input path to avoid the leakage current. */
    GPIO_DISABLE_DIGITAL_PATH(PB, BIT2|BIT3);

    /* Set PA.0 ~ PA11 to GPIO output mode */
    GPIO_SetMode(PA, 0x0FFF, GPIO_MODE_OUTPUT);
    SYS->GPA_MFPL = 0;
    SYS->GPA_MFPH = SYS->GPA_MFPH & (~0xFFFF);

    /* Lock protected registers */
    SYS_LockReg();
}


void PDMA_Init()
{
    /* Configure PDMA to Scatter Gather mode with ping-pong buffer */
    /* to move ADC conversion data to GPIO output without PDMA interrupt. */

    /* Open Channel 1 */
    PDMA_Open(PDMA, 1 << PDMA_CH);

    /* Enable Scatter Gather mode, assign the first scatter-gather descriptor table is table 1,
       and set transfer mode as ADC_RX to GPIO */
    PDMA_SetTransferMode(PDMA, PDMA_CH, PDMA_ADC_RX, TRUE, (uint32_t)&DMA_DESC[0]);

    /* Scatter-Gather descriptor table configuration */
    g_u32DMAConfig = \
                     (1 << PDMA_DSCT_CTL_TXCNT_Pos) |   /* Transfer count is 2 */
                     PDMA_WIDTH_16 |    /* Transfer width is 16 bits */
                     PDMA_SAR_FIX |     /* Source increment size is fixed (no increment) */
                     PDMA_DAR_FIX |     /* Destination increment size is fixed (no increment) */
                     PDMA_REQ_SINGLE |  /* Transfer type is single transfer type */
                     PDMA_BURST_1 |     /* Burst size is 128. No effect in single transfer type */
                     PDMA_OP_SCATTER;   /* Operation mode is scatter-gather mode */

    DMA_DESC[0].ctl = g_u32DMAConfig;
    /* Configure source address */
    DMA_DESC[0].src = (uint32_t)&ADC->ADPDMA;   /* Ping-Pong buffer 1 */
    /* Configure destination address */
    DMA_DESC[0].dest = (uint32_t)&PA->DOUT;
    /* Configure next descriptor table address */
    DMA_DESC[0].offset = (uint32_t)&DMA_DESC[1] - (PDMA->SCATBA);   /* next operation table is table 2 */

    DMA_DESC[1].ctl = g_u32DMAConfig;
    /* Configure source address */
    DMA_DESC[1].src = (uint32_t)&ADC->ADPDMA;   /* Ping-Pong buffer 2 */
    /* Configure destination address */
    DMA_DESC[1].dest = (uint32_t)&PA->DOUT;
    /* Configure next descriptor table address */
    DMA_DESC[1].offset = (uint32_t)&DMA_DESC[0] - (PDMA->SCATBA);   /* next operation table is table 1 */

    /* Don't enable any interrupt to make ADC SPS can up to 2MHz */
    // PDMA_EnableInt(PDMA, PDMA_CH, PDMA_INT_TRANS_DONE);
    // NVIC_EnableIRQ(PDMA_IRQn);
}


void ADC_FunctionTest()
{
    printf("\n");
    printf("+----------------------------------------------------------------------+\n");
    printf("|       Demonstrate how to perform the ADC in 2 Msps continuous mode.  |\n");
    printf("|       ADC clock = PLL/2 = 68/2 MHz = 34 MHz                          |\n");
    printf("|       ADC conversion rate = 34 MHz / 17 = 2 Msps                     |\n");
    printf("+----------------------------------------------------------------------+\n");
    printf("   ADC conversion data will be moved to GPIO pins PA11 ~ PA0 by PDMA.\n");
    printf("   Please connect PB2 to 0V and PB3 to 3.3V\n");
    printf("   and monitor PA11 (MSB of 12-bit ADC conversion data) on scope.\n");
    printf("   The real ADC SPS shoule be (PA11 frequency * 2).\n");

    /* Enable ADC converter */
    ADC_POWER_ON(ADC);

    /* Set input mode as single-end, continuous mode, and select channel 2 and 3 */
    ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_CONTINUOUS, BIT2|BIT3);

    /* ADC enable PDMA transfer */
    ADC_ENABLE_PDMA(ADC);

    /* Start ADC conversion */
    ADC_START_CONV(ADC);

    /* Don't interrupt ADC or PDMA in order to make ADC SPS can up to 2MHz */

    printf("press any key to stop ADC conversion ...\n");
    getchar();
    ADC_STOP_CONV(ADC);
}


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

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


int32_t main(void)
{
    /* Init System, IP clock and multi-function I/O. */
    SYS_Init();

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

    /* Init PDMA for ADC */
    PDMA_Init();

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

    /* ADC function test */
    ADC_FunctionTest();

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

    /* Disable PDMA clock source */
    CLK_DisableModuleClock(PDMA_MODULE);

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

    while(1);
}

使用特权

评论回复
jiekou001| | 2020-7-5 22:53 | 显示全部楼层
ADC是可以设置时钟。

使用特权

评论回复
huangcunxiake| | 2020-7-6 23:02 | 显示全部楼层
这个时钟设置的意义是什么,提高连续采样频率?

使用特权

评论回复
zhuomuniao110| | 2020-7-7 23:09 | 显示全部楼层
非常强大 的选项。

使用特权

评论回复
aoyi| | 2020-8-3 17:39 | 显示全部楼层
非常感谢楼主分享

使用特权

评论回复
drer| | 2020-8-3 17:39 | 显示全部楼层
楼主的资料好多啊

使用特权

评论回复
gwsan| | 2020-8-3 17:40 | 显示全部楼层
非常不错的资料

使用特权

评论回复
kxsi| | 2020-8-3 17:40 | 显示全部楼层
很好 学习一下

使用特权

评论回复
nawu| | 2020-8-3 17:40 | 显示全部楼层
必须支持楼主啊

使用特权

评论回复
yiy| | 2020-8-3 20:31 | 显示全部楼层
可以分开采用不同时钟吗

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

3272

帖子

13

粉丝