打印
[DemoCode下载]

ADC的高速连续扫描模式

[复制链接]
155|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
玛尼玛尼哄|  楼主 | 2024-2-28 20:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
AD, ADC, dc
/**************************************************************************//**
* [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) 2020 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);
}


使用特权

评论回复
沙发
玛尼玛尼哄|  楼主 | 2024-2-28 20:52 | 只看该作者
这段代码是一个用于演示如何使用高精度内部振荡器(HIRC)作为ADC(模数转换器)时钟源以实现1411千样本每秒(ksps)ADC转换速率的示例代码。以下是代码的主要功能和流程:

初始化函数 SYS_Init():

解锁受保护的寄存器以允许对其进行写入操作。
启用HIRC(内部RC 48 MHz)作为系统时钟,并等待其稳定。
配置系统时钟和模块时钟,如UART0和ADC模块。
配置GPIO以及ADC的模拟输入引脚,并禁用数字输入路径以避免泄漏电流。
锁定受保护的寄存器以防止进一步更改。
ADC功能测试函数 ADC_FunctionTest():

输出ADC的配置信息,包括时钟源、时钟分频、扩展采样时间、转换时间和转换速率。
在循环中,等待用户按下任意键来启动连续扫描模式测试。
配置ADC的操作模式、输入模式和要启用的模拟输入通道。
清除ADC中断标志并启用ADC中断。
启动ADC转换并等待ADC中断完成。
获取每个通道的转换结果并打印输出。
停止ADC转换并禁用ADC中断。
ADC中断处理函数 ADC_IRQHandler():

在ADC中断中设置标志以指示转换完成,并清除ADC中断标志。
初始化UART0函数 UART0_Init():

重置UART0模块。
配置UART0并设置波特率为115200。
主函数 main():

调用SYS_Init()和UART0_Init()来初始化系统和UART0模块。
输出系统时钟频率。
调用ADC_FunctionTest()来执行ADC功能测试。
禁用ADC模块的时钟和ADC中断。
循环等待。

使用特权

评论回复
板凳
玛尼玛尼哄|  楼主 | 2024-2-28 20:52 | 只看该作者
这段代码的主要目的是演示如何配置并使用NUC029系列微控制器的ADC模块以实现高速ADC转换。通过使用HIRC作为ADC时钟源,可以实现较高的ADC转换速率。

使用特权

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

本版积分规则

158

主题

3008

帖子

2

粉丝