[DemoCode下载] PWM触发ADC采样,通过PDMA传输采样结果

[复制链接]
983|7
 楼主| gejigeji521 发表于 2023-10-30 09:25 | 显示全部楼层 |阅读模式
  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. * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrate how to trigger ADC by PWM and transfer conversion data by PDMA.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2020 Nuvoton Technology Corp. All rights reserved.
  8. ******************************************************************************/
  9. #include <stdio.h>
  10. #include "NuMicro.h"


  11. /*---------------------------------------------------------------------------------------------------------*/
  12. /* Define global variables and constants                                                                   */
  13. /*---------------------------------------------------------------------------------------------------------*/
  14. volatile uint32_t g_u32AdcIntFlag, g_u32COVNUMFlag = 0;
  15. volatile uint32_t g_u32IsTestOver = 0;
  16. int16_t  g_i32ConversionData[6] = {0};
  17. uint32_t g_u32SampleModuleNum = 0;


  18. void SYS_Init(void)
  19. {
  20.     /* Unlock protected registers */
  21.     SYS_UnlockReg();

  22.     /* Enable HIRC */
  23.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  24.     /* Waiting for HIRC clock ready */
  25.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  26.     /* Switch HCLK clock source to HIRC */
  27.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  28.     /* Set both PCLK0 and PCLK1 as HCLK/2 */
  29.     CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2);

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

  32.     /* Enable UART peripheral clock */
  33.     CLK_EnableModuleClock(UART0_MODULE);

  34.     /* Enable PWM0 module clock */
  35.     CLK_EnableModuleClock(PWM0_MODULE);

  36.     /* Enable ADC module clock */
  37.     CLK_EnableModuleClock(ADC_MODULE);

  38.     /* ADC clock source is PCLK1, set divider to 1 */
  39.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL2_ADCSEL_PCLK1, CLK_CLKDIV0_ADC(1));

  40.     /* Enable PDMA clock source */
  41.     CLK_EnableModuleClock(PDMA_MODULE);

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

  45.     /*----------------------------------------------------------------------*/
  46.     /* Init I/O Multi-function                                              */
  47.     /*----------------------------------------------------------------------*/
  48.     /* Set PB multi-function pins for UART0 RXD=PB.6 and TXD=PB.4 */
  49.     SYS->GPB_MFP1 = (SYS->GPB_MFP1 & ~(SYS_GPB_MFP1_PB4MFP_Msk | SYS_GPB_MFP1_PB6MFP_Msk)) |
  50.                     (SYS_GPB_MFP1_PB4MFP_UART0_TXD | SYS_GPB_MFP1_PB6MFP_UART0_RXD);

  51.     /* Set PA.2, PC.0 to input mode */
  52.     GPIO_SetMode(PA, BIT2, GPIO_MODE_INPUT);
  53.     GPIO_SetMode(PC, BIT0, GPIO_MODE_INPUT);

  54.     /* Configure the PA.2, PC.0 ADC analog input pins.  */
  55.     SYS->GPA_MFP0 = (SYS->GPA_MFP0 & ~(SYS_GPA_MFP0_PA2MFP_Msk)) |
  56.                     (SYS_GPA_MFP0_PA2MFP_ADC0_CH2);
  57.     SYS->GPC_MFP0 = (SYS->GPC_MFP0 & ~(SYS_GPC_MFP0_PC0MFP_Msk)) |
  58.                     (SYS_GPC_MFP0_PC0MFP_ADC0_CH3);

  59.     /* Disable the PA.2, PC.0 digital input path to avoid the leakage current. */
  60.     GPIO_DISABLE_DIGITAL_PATH(PA, BIT2);
  61.     GPIO_DISABLE_DIGITAL_PATH(PC, BIT0);

  62.     /* Lock protected registers */
  63.     SYS_LockReg();
  64. }

  65. void PWM0_Init()
  66. {
  67.     /* Set PWM0 timer clock prescaler */
  68.     PWM_SET_PRESCALER(PWM0, 0, 0);

  69.     /* Set up counter type */
  70.     PWM0->CTL1 &= ~PWM_CTL1_CNTTYPE0_Msk;

  71.     /* Set PWM0 timer duty */
  72.     PWM_SET_CMR(PWM0, 0, 108);

  73.     /* Set PWM0 timer period */
  74.     PWM_SET_CNR(PWM0, 0, 216);

  75.     /* PWM period point trigger ADC enable */
  76.     PWM_EnableADCTrigger(PWM0, 0, PWM_TRIGGER_ADC_EVEN_PERIOD_POINT);

  77.     /* Set output level at zero, compare up, period(center) and compare down of specified channel */
  78.     PWM_SET_OUTPUT_LEVEL(PWM0, BIT0, PWM_OUTPUT_HIGH, PWM_OUTPUT_LOW, PWM_OUTPUT_NOTHING, PWM_OUTPUT_NOTHING);

  79.     /* Enable output of PWM0 channel 0 */
  80.     PWM_EnableOutput(PWM0, BIT0);
  81. }

  82. void PDMA_Init()
  83. {
  84.     /* Configure PDMA peripheral mode form ADC to memory */
  85.     /* Open Channel 1 */
  86.     PDMA_Open(PDMA, BIT1);

  87.     /* transfer width is half word(16 bit) and transfer count is 6 */
  88.     PDMA_SetTransferCnt(PDMA, 1, PDMA_WIDTH_16, 6);

  89.     /* Set source address as ADC PDMA Current Transfer Data register (no increment) and destination address as g_i32ConversionData array (increment) */
  90.     PDMA_SetTransferAddr(PDMA, 1, (uint32_t)&ADC->ADPDMA, PDMA_SAR_FIX, (uint32_t)g_i32ConversionData, PDMA_DAR_INC);

  91.     /* Select PDMA request source as ADC RX */
  92.     PDMA_SetTransferMode(PDMA, 1, PDMA_ADC_RX, FALSE, 0);

  93.     /* Set PDMA as single request type for ADC */
  94.     PDMA_SetBurstType(PDMA, 1, PDMA_REQ_SINGLE, PDMA_BURST_4);

  95.     PDMA_EnableInt(PDMA, 1, PDMA_INT_TRANS_DONE);
  96.     NVIC_EnableIRQ(PDMA_IRQn);
  97. }

  98. void ReloadPDMA()
  99. {
  100.     /* transfer width is half word(16 bit) and transfer count is 6 */
  101.     PDMA_SetTransferCnt(PDMA, 1, PDMA_WIDTH_16, 6);

  102.     /* Select PDMA request source as ADC RX */
  103.     PDMA_SetTransferMode(PDMA, 1, PDMA_ADC_RX, FALSE, 0);
  104. }

  105. void ADC_FunctionTest()
  106. {
  107.     uint8_t  u8Option;

  108.     printf("\n");
  109.     printf("+----------------------------------------------------------------------+\n");
  110.     printf("|   PWM trigger mode and transfer ADC conversion data by PDMA test     |\n");
  111.     printf("+----------------------------------------------------------------------+\n");

  112.     printf("\nIn this test, software will get 6 conversion result from the specified channel.\n");

  113.     /* Enable ADC converter */
  114.     ADC_POWER_ON(ADC);

  115.     while(1)
  116.     {
  117.         /* reload PDMA configuration for next transmission */
  118.         ReloadPDMA();

  119.         printf("Select input mode:\n");
  120.         printf("  [1] Single end input (channel 2 only)\n");
  121.         printf("  [2] Differential input (channel pair 1 only (channel 2 and 3))\n");
  122.         printf("  Other keys: exit single mode test\n");
  123.         u8Option = getchar();
  124.         if(u8Option == '1')
  125.         {
  126.             /* Set input mode as single-end, Single mode, and select channel 2 */
  127.             ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE, BIT2);

  128.             /* Configure the sample module and enable PWM0 trigger source */
  129.             ADC_EnableHWTrigger(ADC, ADC_ADCR_TRGS_PWM, 0);

  130.             /* ADC enable PDMA transfer */
  131.             ADC_ENABLE_PDMA(ADC);

  132.             printf("Conversion result of channel 2:\n");

  133.             /* Enable PWM0 channel 0 counter */
  134.             PWM_Start(PWM0, PWM_CH_0_MASK);

  135.             while(1)
  136.             {
  137.                 /* Wait PDMA interrupt (g_u32IsTestOver will be set at IRQ_Handler function) */
  138.                 while(g_u32IsTestOver == 0);
  139.                 break;
  140.             }
  141.             g_u32IsTestOver = 0;

  142.             /* Disable PWM0 channel 0 counter */
  143.             PWM_ForceStop(PWM0, BIT0);  /* PWM0 counter stop running. */

  144.             for(g_u32COVNUMFlag = 0; (g_u32COVNUMFlag) < 6; g_u32COVNUMFlag++)
  145.                 printf("                                0x%X (%d)\n", g_i32ConversionData[g_u32COVNUMFlag], g_i32ConversionData[g_u32COVNUMFlag]);
  146.         }
  147.         else if(u8Option == '2')
  148.         {
  149.             /* Set input mode as differential, Single mode, and select channel 2 */
  150.             ADC_Open(ADC, ADC_ADCR_DIFFEN_DIFFERENTIAL, ADC_ADCR_ADMD_SINGLE, BIT2);

  151.             /* Configure the sample module and enable PWM0 trigger source */
  152.             ADC_EnableHWTrigger(ADC, ADC_ADCR_TRGS_PWM, 0);

  153.             /* ADC enable PDMA transfer */
  154.             ADC_ENABLE_PDMA(ADC);

  155.             printf("Conversion result of channel 2:\n");

  156.             /* Enable PWM0 channel 0 counter */
  157.             PWM_Start(PWM0, PWM_CH_0_MASK);

  158.             while(1)
  159.             {
  160.                 /* Wait PDMA interrupt (g_u32IsTestOver will be set at IRQ_Handler function) */
  161.                 while(g_u32IsTestOver == 0);
  162.                 break;
  163.             }
  164.             g_u32IsTestOver = 0;

  165.             /* Disable EPWM0 channel 0 counter */
  166.             PWM_ForceStop(PWM0, BIT0);  /* PWM0 counter stop running. */

  167.             for(g_u32COVNUMFlag = 0; (g_u32COVNUMFlag) < 6; g_u32COVNUMFlag++)
  168.                 printf("                                0x%X (%d)\n", g_i32ConversionData[g_u32COVNUMFlag], g_i32ConversionData[g_u32COVNUMFlag]);
  169.         }
  170.         else
  171.             return;
  172.     }
  173. }

  174. void PDMA_IRQHandler(void)
  175. {
  176.     uint32_t status = PDMA_GET_INT_STATUS(PDMA);

  177.     if(status & PDMA_INTSTS_ABTIF_Msk)    /* abort */
  178.     {
  179.         if(PDMA_GET_ABORT_STS(PDMA) & PDMA_ABTSTS_ABTIF1_Msk)
  180.             g_u32IsTestOver = 2;
  181.         PDMA_CLR_ABORT_FLAG(PDMA, PDMA_ABTSTS_ABTIF1_Msk);
  182.     }
  183.     else if(status & PDMA_INTSTS_TDIF_Msk)      /* done */
  184.     {
  185.         if(PDMA_GET_TD_STS(PDMA) & PDMA_TDSTS_TDIF1_Msk)
  186.             g_u32IsTestOver = 1;
  187.         PDMA_CLR_TD_FLAG(PDMA, PDMA_TDSTS_TDIF1_Msk);
  188.     }
  189.     else
  190.         printf("unknown PDMA interrupt !!\n");
  191. }

  192. /*----------------------------------------------------------------------*/
  193. /* Init UART0                                                           */
  194. /*----------------------------------------------------------------------*/
  195. void UART0_Init(void)
  196. {
  197.     /* Reset UART0 */
  198.     SYS_ResetModule(UART0_RST);

  199.     /* Configure UART0 and set UART0 baud rate */
  200.     UART_Open(UART0, 115200);
  201. }

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

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

  208.     /* Init PWM for ADC */
  209.     PWM0_Init();

  210.     /* Init PDMA for ADC */
  211.     PDMA_Init();

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

  213.     /* ADC function test */
  214.     ADC_FunctionTest();

  215.     /* Disable ADC IP clock */
  216.     CLK_DisableModuleClock(ADC_MODULE);

  217.     /* Disable PWM0 IP clock */
  218.     CLK_DisableModuleClock(PWM0_MODULE);

  219.     /* Disable PDMA clock source */
  220.     CLK_DisableModuleClock(PDMA_MODULE);

  221.     /* Disable PDMA Interrupt */
  222.     NVIC_DisableIRQ(PDMA_IRQn);

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

  224.     while(1);
  225. }


tpgf 发表于 2024-1-5 12:08 | 显示全部楼层
通过pwm的哪个参数来触发adc的采集呢
aoyi 发表于 2024-1-5 13:10 | 显示全部楼层
可以通过不同的pwm触发不同采样频率的adc吗
tfqi 发表于 2024-1-5 21:29 | 显示全部楼层
可以使用一路pwm同时触发多路adc吗
gwsan 发表于 2024-1-5 22:00 | 显示全部楼层
这个是以哪个功能作为主要功能呢
zljiu 发表于 2024-1-5 22:31 | 显示全部楼层
pwm波形是自身产生的波形吗
nawu 发表于 2024-1-5 23:12 | 显示全部楼层
这两个的节奏如何能配合好呢
AloneKaven 发表于 2024-1-10 09:00 来自手机 | 显示全部楼层
应该可以用不同的pwm触发
您需要登录后才可以回帖 登录 | 注册

本版积分规则

198

主题

2479

帖子

8

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