[DemoCode下载] M451多通道连续中断触发

[复制链接]
923|8
 楼主| wanduzi 发表于 2019-1-19 22:38 | 显示全部楼层 |阅读模式
  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: 6 $
  5. * $Date: 15/09/02 10:04a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Use ADINT interrupt to do the ADC continuous scan conversion.
  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.     /*---------------------------------------------------------------------------------------------------------*/
  52.     /* Init I/O Multi-function                                                                                 */
  53.     /*---------------------------------------------------------------------------------------------------------*/

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

  57.     /* Configure the GPB0 - GPB3 ADC analog input pins.  */
  58.     SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB0MFP_Msk | SYS_GPB_MFPL_PB1MFP_Msk |
  59.                        SYS_GPB_MFPL_PB2MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk);
  60.     SYS->GPB_MFPL |= (SYS_GPB_MFPL_PB0MFP_EADC_CH0 | SYS_GPB_MFPL_PB1MFP_EADC_CH1 |
  61.                       SYS_GPB_MFPL_PB2MFP_EADC_CH2 | SYS_GPB_MFPL_PB3MFP_EADC_CH3);

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

  64. }

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

  72.     /* Configure UART0 and set UART0 baud rate */
  73.     UART_Open(UART0, 115200);
  74. }

  75. /*---------------------------------------------------------------------------------------------------------*/
  76. /* EADC function test                                                                                       */
  77. /*---------------------------------------------------------------------------------------------------------*/
  78. void EADC_FunctionTest()
  79. {
  80.     uint8_t  u8Option, u32SAMPLECount = 0;
  81.     int32_t  i32ConversionData[8] = {0};

  82.     printf("\n");
  83.     printf("+----------------------------------------------------------------------+\n");
  84.     printf("|                      ADINT trigger mode test                         |\n");
  85.     printf("+----------------------------------------------------------------------+\n");

  86.     printf("\nIn this test, software will get 2 cycles of conversion result from the specified channels.\n");

  87.     while(1)
  88.     {
  89.         printf("\n\nSelect input mode:\n");
  90.         printf("  [1] Single end input (channel 0, 1, 2 and 3)\n");
  91.         printf("  [2] Differential input (input channel pair 0 and 1)\n");
  92.         printf("  Other keys: exit continuous scan mode test\n");
  93.         u8Option = getchar();
  94.         if(u8Option == '1')
  95.         {
  96.             /* Set the ADC internal sampling time, input mode as single-end and enable the A/D converter */
  97.             EADC_Open(EADC, EADC_CTL_DIFFEN_SINGLE_END);
  98.             EADC_SetInternalSampleTime(EADC, 6);

  99.             /* Configure the sample 4 module for analog input channel 0 and enable ADINT0 trigger source */
  100.             EADC_ConfigSampleModule(EADC, 4, EADC_ADINT0_TRIGGER, 0);
  101.             /* Configure the sample 5 module for analog input channel 1 and enable ADINT0 trigger source */
  102.             EADC_ConfigSampleModule(EADC, 5, EADC_ADINT0_TRIGGER, 1);
  103.             /* Configure the sample 6 module for analog input channel 2 and enable ADINT0 trigger source */
  104.             EADC_ConfigSampleModule(EADC, 6, EADC_ADINT0_TRIGGER, 2);
  105.             /* Configure the sample 7 module for analog input channel 3 and enable ADINT0 trigger source */
  106.             EADC_ConfigSampleModule(EADC, 7, EADC_ADINT0_TRIGGER, 3);

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

  109.             /* Enable the sample module 7 interrupt */
  110.             EADC_ENABLE_INT(EADC, 0x1);//Enable sample module  A/D ADINT0 interrupt.
  111.             EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 0, (0x1 << 7));//Enable sample module 7 interrupt.
  112.             NVIC_EnableIRQ(ADC00_IRQn);

  113.             /* Reset the ADC indicator and trigger sample module 7 to start A/D conversion */
  114.             g_u32AdcIntFlag = 0;
  115.             g_u32COVNUMFlag = 0;
  116.             EADC_START_CONV(EADC, (0x1 << 7));

  117.             /* Wait EADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */
  118.             while(g_u32AdcIntFlag == 0);
  119.             /* Reset the EADC interrupt indicator */
  120.             g_u32AdcIntFlag = 0;

  121.             /* Wait EADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */
  122.             while(g_u32AdcIntFlag == 0);
  123.             /* Reset the EADC interrupt indicator */
  124.             g_u32AdcIntFlag = 0;

  125.             /* Disable the sample module 7 interrupt */
  126.             EADC_DISABLE_SAMPLE_MODULE_INT(EADC, 0, (0x1 << 7));

  127.             /* Get the conversion result of the sample module */
  128.             for(u32SAMPLECount = 0; u32SAMPLECount < 4; u32SAMPLECount++)
  129.                 i32ConversionData[u32SAMPLECount] = EADC_GET_CONV_DATA(EADC, (u32SAMPLECount + 4));

  130.             /* Wait conversion done */
  131.             while(EADC_GET_DATA_VALID_FLAG(EADC, 0xF0) != 0xF0);

  132.             /* Get the conversion result of the sample module */
  133.             for(u32SAMPLECount = 4; u32SAMPLECount < 8; u32SAMPLECount++)
  134.                 i32ConversionData[u32SAMPLECount] = EADC_GET_CONV_DATA(EADC, u32SAMPLECount);

  135.             for(g_u32COVNUMFlag = 0; (g_u32COVNUMFlag) < 8; g_u32COVNUMFlag++)
  136.                 printf("Conversion result of channel %d: 0x%X (%d)\n", (g_u32COVNUMFlag % 4), i32ConversionData[g_u32COVNUMFlag], i32ConversionData[g_u32COVNUMFlag]);

  137.         }
  138.         else if(u8Option == '2')
  139.         {
  140.             /* Set the ADC internal sampling time, input mode as differential and enable the A/D converter */
  141.             EADC_Open(EADC, EADC_CTL_DIFFEN_DIFFERENTIAL);
  142.             EADC_SetInternalSampleTime(EADC, 6);

  143.             /* Configure the sample module 4 for analog input channel 0 and enable ADINT0 trigger source */
  144.             EADC_ConfigSampleModule(EADC, 4, EADC_ADINT0_TRIGGER, 0);
  145.             /* Configure the sample module 5 for analog input channel 1 and enable ADINT0 trigger source */
  146.             EADC_ConfigSampleModule(EADC, 5, EADC_ADINT0_TRIGGER, 1);
  147.             /* Configure the sample module 6 for analog input channel 2 and enable ADINT0 trigger source */
  148.             EADC_ConfigSampleModule(EADC, 6, EADC_ADINT0_TRIGGER, 2);
  149.             /* Configure the sample module 7 for analog input channel 3 and enable ADINT0 trigger source */
  150.             EADC_ConfigSampleModule(EADC, 7, EADC_ADINT0_TRIGGER, 3);

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

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

  157.             /* Reset the ADC indicator and trigger sample module 7 to start A/D conversion */
  158.             g_u32AdcIntFlag = 0;
  159.             g_u32COVNUMFlag = 0;
  160.             EADC_START_CONV(EADC, (0x1 << 7));

  161.             /* Wait EADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */
  162.             while(g_u32AdcIntFlag == 0);
  163.             /* Reset the EADC interrupt indicator */
  164.             g_u32AdcIntFlag = 0;

  165.             /* Wait EADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */
  166.             while(g_u32AdcIntFlag == 0);
  167.             /* Reset the EADC interrupt indicator */
  168.             g_u32AdcIntFlag = 0;

  169.             /* Disable the sample module 7 interrupt */
  170.             EADC_DISABLE_SAMPLE_MODULE_INT(EADC, 0, (0x1 << 7));

  171.             /* Get the conversion result of the sample module */
  172.             for(u32SAMPLECount = 0; u32SAMPLECount < 4; u32SAMPLECount++)
  173.                 i32ConversionData[u32SAMPLECount] = EADC_GET_CONV_DATA(EADC, (u32SAMPLECount + 4));

  174.             /* Wait conversion done */
  175.             while(EADC_GET_DATA_VALID_FLAG(EADC, 0xF0) != 0xF0);

  176.             /* Get the conversion result of the sample module */
  177.             for(u32SAMPLECount = 4; u32SAMPLECount < 8; u32SAMPLECount++)
  178.                 i32ConversionData[u32SAMPLECount] = EADC_GET_CONV_DATA(EADC, u32SAMPLECount);

  179.             for(g_u32COVNUMFlag = 0; (g_u32COVNUMFlag) < 8; g_u32COVNUMFlag++)
  180.                 printf("Conversion result of channel %d: 0x%X (%d)\n", (g_u32COVNUMFlag % 4), i32ConversionData[g_u32COVNUMFlag], i32ConversionData[g_u32COVNUMFlag]);

  181.         }
  182.         else
  183.             return ;

  184.     }
  185. }



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

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

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

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

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

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

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

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

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

  213.     /* Reset EADC module */
  214.     SYS_ResetModule(EADC_RST);

  215.     /* Disable EADC IP clock */
  216.     CLK_DisableModuleClock(EADC_MODULE);

  217.     /* Disable External Interrupt */
  218.     NVIC_DisableIRQ(ADC00_IRQn);

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

  220.     while(1);

  221. }



 楼主| wanduzi 发表于 2019-1-19 23:07 | 显示全部楼层
该例子分2个部分测试:0,1,2,3一共4个通道,作为单输入时候的中断触发方式。
另外一个就是采用通道0和1作为一个一对的输入方式。
 楼主| wanduzi 发表于 2019-1-19 23:09 | 显示全部楼层
603055c433da538281.png
这里在等待中断函数里置位。
 楼主| wanduzi 发表于 2019-1-19 23:11 | 显示全部楼层
注意到这里重复搞了两次。莫非是程序员不小心复制粘贴了一回?值得思考。
那么重复一次是否有效呢?
不过可以看出这里重复一次,肯定是把第一次的数据给丢了。
 楼主| wanduzi 发表于 2019-1-19 23:12 | 显示全部楼层
  1. void ADC00_IRQHandler(void)
  2. {
  3.     g_u32AdcIntFlag = 1;
  4.     EADC_CLR_INT_FLAG(EADC, 0x1);      /* Clear the A/D ADINT0 interrupt flag */
  5. }

ADC0的中断里,仅仅做了基本的操作。这里是我们学习的地方。不要搞太多。
 楼主| wanduzi 发表于 2019-1-19 23:18 | 显示全部楼层
  1.             for(u32SAMPLECount = 0; u32SAMPLECount < 4; u32SAMPLECount++)
  2.                 i32ConversionData[u32SAMPLECount] = EADC_GET_CONV_DATA(EADC, (u32SAMPLECount + 4));

  3.             /* Wait conversion done */
  4.             while(EADC_GET_DATA_VALID_FLAG(EADC, 0xF0) != 0xF0);

  5.             /* Get the conversion result of the sample module */
  6.             for(u32SAMPLECount = 4; u32SAMPLECount < 8; u32SAMPLECount++)
  7.                 i32ConversionData[u32SAMPLECount] = EADC_GET_CONV_DATA(EADC, u32SAMPLECount);

这里的操作看着是重复的,实际上获取了两次数据。
存在了同一个数组里。
 楼主| wanduzi 发表于 2019-1-19 23:19 | 显示全部楼层
            for(g_u32COVNUMFlag = 0; (g_u32COVNUMFlag) < 8; g_u32COVNUMFlag++)
                printf("Conversion result of channel %d: 0x%X (%d)\n", (g_u32COVNUMFlag % 4), i32ConversionData[g_u32COVNUMFlag], i32ConversionData[g_u32COVNUMFlag]);

这个打印输出也是将两次的内容输出出来,但是作为统一的标号。
作者的深意在哪里?
 楼主| wanduzi 发表于 2019-1-19 23:21 | 显示全部楼层
只有你自己跑一跑,改一改,才好更好的理解。
dongnanxibei 发表于 2019-1-21 23:50 | 显示全部楼层
451的看着就比051的复杂多了,功能更强,更丰富了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

156

主题

1882

帖子

3

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