[DemoCode下载] M451的ADC用定时器触发

[复制链接]
1021|9
 楼主| huahuagg 发表于 2018-11-18 19:42 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 5 $
  5. * $Date: 15/09/02 10:04a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to trigger ADC by timer.
  7. * @note
  8. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include "stdio.h"
  12. #include "M451Series.h"

  13. #define PLLCTL_SETTING      CLK_PLLCTL_72MHz_HXT
  14. #define PLL_CLOCK           72000000

  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Define global variables and constants                                                                   */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. volatile uint32_t g_u32AdcIntFlag, g_u32COVNUMFlag = 0;

  19. /*---------------------------------------------------------------------------------------------------------*/
  20. /* Define functions prototype                                                                              */
  21. /*---------------------------------------------------------------------------------------------------------*/
  22. int32_t main(void);
  23. void EADC_FunctionTest(void);


  24. void SYS_Init(void)
  25. {

  26.     /*---------------------------------------------------------------------------------------------------------*/
  27.     /* Init System Clock                                                                                       */
  28.     /*---------------------------------------------------------------------------------------------------------*/

  29.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  30.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

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

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

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

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

  39.     /* Wait for HXT clock ready */
  40.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_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.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  46.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  47.     /* Enable EADC module clock */
  48.     CLK_EnableModuleClock(EADC_MODULE);

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

  51.     /* Enable Timer 0 module clock */
  52.     CLK_EnableModuleClock(TMR0_MODULE);

  53.     /* Select timer 0 module clock source as HXT */
  54.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);

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

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

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

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

  68. }

  69. void UART0_Init()
  70. {
  71.     /*---------------------------------------------------------------------------------------------------------*/
  72.     /* Init UART                                                                                               */
  73.     /*---------------------------------------------------------------------------------------------------------*/
  74.     /* Reset UART module */
  75.     SYS_ResetModule(UART0_RST);

  76.     /* Configure UART0 and set UART0 baud rate */
  77.     UART_Open(UART0, 115200);
  78. }

  79. void TIMER0_Init()
  80. {
  81.     /*---------------------------------------------------------------------------------------------------------*/
  82.     /* Init TIMER0                                                                                             */
  83.     /*---------------------------------------------------------------------------------------------------------*/

  84.     /* Set timer0 periodic time-out period is 3us if timer clock is 12 MHz */
  85.     TIMER_SET_CMP_VALUE(TIMER0, 36);//TIMER0->CMP = 36;

  86.     /* Start timer counter in periodic mode and enable timer interrupt trigger EADC */
  87.     TIMER0->CTL = TIMER_PERIODIC_MODE | TIMER_CTL_TRGEADC_Msk;

  88. }

  89. /*---------------------------------------------------------------------------------------------------------*/
  90. /* EADC function test                                                                                      */
  91. /*---------------------------------------------------------------------------------------------------------*/
  92. void EADC_FunctionTest()
  93. {
  94.     uint8_t  u8Option;
  95.     int32_t  i32ConversionData[6] = {0};

  96.     printf("\n");
  97.     printf("+----------------------------------------------------------------------+\n");
  98.     printf("|                      Timer trigger mode test                         |\n");
  99.     printf("+----------------------------------------------------------------------+\n");

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

  101.     while(1)
  102.     {
  103.         printf("Select input mode:\n");
  104.         printf("  [1] Single end input (channel 2 only)\n");
  105.         printf("  [2] Differential input (channel pair 1 only)\n");
  106.         printf("  Other keys: exit single mode test\n");
  107.         u8Option = getchar();
  108.         if(u8Option == '1')
  109.         {
  110.             /* Set the ADC internal sampling time, input mode as single-end and enable the A/D converter */
  111.             EADC_Open(EADC, EADC_CTL_DIFFEN_SINGLE_END);
  112.             EADC_SetInternalSampleTime(EADC, 6);

  113.             /* Configure the sample module 0 for analog input channel 2 and enable Timer0 trigger source */
  114.             EADC_ConfigSampleModule(EADC, 0, EADC_TIMER0_TRIGGER, 2);

  115.             /* Clear the A/D ADINT0 interrupt flag for safe */
  116.             EADC_CLR_INT_FLAG(EADC, 0x1);

  117.             /* Enable the sample module 0 interrupt.  */
  118.             EADC_ENABLE_INT(EADC, 0x1);//Enable sample module A/D ADINT0 interrupt.
  119.             EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 0, 0x1);//Enable sample module 0 interrupt.
  120.             NVIC_EnableIRQ(ADC00_IRQn);

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

  122.             /* Reset the ADC indicator and enable Timer0 counter */
  123.             g_u32AdcIntFlag = 0;
  124.             g_u32COVNUMFlag = 0;
  125.             TIMER_Start(TIMER0);

  126.             while(1)
  127.             {
  128.                 /* Wait ADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */
  129.                 while(g_u32AdcIntFlag == 0);

  130.                 /* Reset the EADC interrupt indicator */
  131.                 g_u32AdcIntFlag = 0;

  132.                 /* Get the conversion result of the sample module 0 */
  133.                 i32ConversionData[g_u32COVNUMFlag - 1] = EADC_GET_CONV_DATA(EADC, 0);

  134.                 if(g_u32COVNUMFlag > 6)
  135.                     break;
  136.             }

  137.             /* Disable Timer0 counter */
  138.             TIMER_Stop(TIMER0);

  139.             /* Disable the ADINT0 interrupt */
  140.             EADC_DISABLE_INT(EADC, 0x1);

  141.             for(g_u32COVNUMFlag = 0; (g_u32COVNUMFlag) < 6; g_u32COVNUMFlag++)
  142.                 printf("                                0x%X (%d)\n", i32ConversionData[g_u32COVNUMFlag], i32ConversionData[g_u32COVNUMFlag]);

  143.         }
  144.         else if(u8Option == '2')
  145.         {
  146.             /* Set the ADC internal sampling time, input mode as differential and enable the A/D converter */
  147.             EADC_Open(EADC, EADC_CTL_DIFFEN_DIFFERENTIAL);
  148.             EADC_SetInternalSampleTime(EADC, 6);

  149.             /* Configure the sample module 0 for analog input channel 2 and enable Timer0 trigger source */
  150.             EADC_ConfigSampleModule(EADC, 0, EADC_TIMER0_TRIGGER, 2);

  151.             /* Clear the A/D ADINT0 interrupt flag for safe */
  152.             EADC_CLR_INT_FLAG(EADC, 0x1);

  153.             /* Enable the sample module 0 interrupt.  */
  154.             EADC_ENABLE_INT(EADC, 0x1);//Enable sample module A/D ADINT0 interrupt.
  155.             EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 0, 0x1);//Enable sample module 0 interrupt.
  156.             NVIC_EnableIRQ(ADC00_IRQn);

  157.             printf("Conversion result of channel pair 1:\n");

  158.             /* Reset the EADC indicator and enable Timer0 counter */
  159.             g_u32AdcIntFlag = 0;
  160.             g_u32COVNUMFlag = 0;
  161.             TIMER_Start(TIMER0);

  162.             while(1)
  163.             {
  164.                 /* Wait ADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */
  165.                 while(g_u32AdcIntFlag == 0);

  166.                 /* Reset the ADC interrupt indicator */
  167.                 g_u32AdcIntFlag = 0;

  168.                 /* Get the conversion result of the sample module 0 */
  169.                 i32ConversionData[g_u32COVNUMFlag - 1] = EADC_GET_CONV_DATA(EADC, 0);

  170.                 if(g_u32COVNUMFlag > 6)
  171.                     break;
  172.             }

  173.             /* Disable Timer0 counter */
  174.             TIMER_Stop(TIMER0);

  175.             /* Disable the ADINT0 interrupt */
  176.             EADC_DISABLE_INT(EADC, 0x1);

  177.             for(g_u32COVNUMFlag = 0; (g_u32COVNUMFlag) < 6; g_u32COVNUMFlag++)
  178.                 printf("                                0x%X (%d)\n", i32ConversionData[g_u32COVNUMFlag], i32ConversionData[g_u32COVNUMFlag]);

  179.         }
  180.         else
  181.             return ;

  182.     }
  183. }



  184. /*---------------------------------------------------------------------------------------------------------*/
  185. /* EADC interrupt handler                                                                                  */
  186. /*---------------------------------------------------------------------------------------------------------*/
  187. void ADC00_IRQHandler(void)
  188. {
  189.     g_u32AdcIntFlag = 1;
  190.     g_u32COVNUMFlag++;
  191.     EADC_CLR_INT_FLAG(EADC, 0x1);/* Clear the A/D ADINT0 interrupt flag */
  192. }

  193. /*---------------------------------------------------------------------------------------------------------*/
  194. /*  Main Function                                                                                          */
  195. /*---------------------------------------------------------------------------------------------------------*/
  196. int32_t main(void)
  197. {

  198.     /* Unlock protected registers */
  199.     SYS_UnlockReg();

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

  202.     /* Lock protected registers */
  203.     SYS_LockReg();

  204.     /* Init UART0 for printf */
  205.     UART0_Init();

  206.     /* Init TIMER0 for EADC */
  207.     TIMER0_Init();

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

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

  212.     /* EADC function test */
  213.     EADC_FunctionTest();

  214.     /* Reset Timer0 module */
  215.     SYS_ResetModule(TMR0_RST);

  216.     /* Reset EADC module */
  217.     SYS_ResetModule(EADC_RST);

  218.     /* Disable Timer0 IP clock */
  219.     CLK_DisableModuleClock(TMR0_MODULE);

  220.     /* Disable EADC IP clock */
  221.     CLK_DisableModuleClock(EADC_MODULE);

  222.     /* Disable External Interrupt */
  223.     NVIC_DisableIRQ(ADC00_IRQn);

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

  225.     while(1);

  226. }



 楼主| huahuagg 发表于 2018-11-18 20:28 | 显示全部楼层
这跟ST的标准库有一拼
21mengnan 发表于 2018-11-18 21:59 | 显示全部楼层
看不太懂啊
643757107 发表于 2018-11-18 23:08 | 显示全部楼层
M0用的很不错, 要学M4了。
643757107 发表于 2018-11-18 23:08 | 显示全部楼层
M0用的很不错, 要学M4了。
dongnanxibei 发表于 2018-11-19 22:38 | 显示全部楼层
这个关系还挺复杂
幸福小强 发表于 2018-11-20 21:09 | 显示全部楼层
这是一种触发模式。
幸福小强 发表于 2018-11-20 23:18 | 显示全部楼层
确实例子有点小难懂,主要好多宏不知道全称是什么
天灵灵地灵灵 发表于 2018-11-20 23:29 | 显示全部楼层
看懂最后怎么退出了吗,先重置模块,再关闭
734774645 发表于 2018-11-24 00:45 | 显示全部楼层
新唐的设计考虑很全的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1430

帖子

2

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