[技术问答] 中断问题

[复制链接]
1441|13
 楼主| dragonjun 发表于 2016-6-22 15:57 | 显示全部楼层 |阅读模式
ADC_IRQHandler(void)中断是怎么用的

TMR0_IRQHandler(void)定时器中断还好理解

ADC_IRQHandler(void)中断是怎么用的
UART0_IRQHandler(void)
EINT0_IRQHandler(void)
EINT1_IRQHandler(void)
都是中断,这样不是太乱吗?他们用起来产生中断条件是什么
09kk小熊 发表于 2016-6-22 16:25 | 显示全部楼层
看例程就好理解了
落叶行健ywm 发表于 2016-6-23 08:13 | 显示全部楼层
多看例子代码,大多数中断产生是硬件触发,这些函数就是触发后你的处理代码,要自己实现!
捉虫天师 发表于 2016-8-22 14:23 | 显示全部楼层
随便找个中断的例程,看看在什么地方设置启动中断,什么时候处理中断,怎么处理,怎么配置中断入口。
zhuotuzi 发表于 2016-8-22 19:56 来自手机 | 显示全部楼层
这个是中段处理函数,你可以写中断后要干什么
zhuomuniao110 发表于 2016-8-22 22:37 | 显示全部楼层
Handler主要用于异步消息的处理:当发出一个消息之后,首先进入一个消息队列,发送消息的函数即刻返回,而另外一个部分在消息队列中逐一将消息取出,然后对消息进行处理,也就是发送消息和接收消息不是同步的处理。 这种机制通常用来处理相对耗时比较长的操作。
mintspring 发表于 2016-8-23 08:48 | 显示全部楼层
大多数中断产生是硬件触发,这些函数就是触发后你的处理代码。就是你要在中断后干什么
数码小叶 发表于 2016-8-23 09:40 | 显示全部楼层
zhuomuniao110 发表于 2016-8-25 21:39 来自手机 | 显示全部楼层
对应的中断发生后就该执行这些对应的处理函数了
zhuotuzi 发表于 2016-8-25 22:24 | 显示全部楼层
void GPIOP0P1_IRQHandler(void)
{
    /* To check if P1.3 interrupt occurred */
    if(GPIO_GET_INT_FLAG(P1, BIT3))
    {
        GPIO_CLR_INT_FLAG(P1, BIT3);
        printf("P1.3 INT occurred.\n");
    }
    else
    {
        /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
        P0->ISRC = P0->ISRC;
        P1->ISRC = P1->ISRC;
        printf("Un-expected interrupts.\n");
    }
}

比如这个,GPIOP0和P1是共用一个中断入口,进来后就先检测是不是P1.3发生了中断,如果是,打印这个端口中断了,如果不是清理中断标志打印不是期望的中断,退出等待下次进来。

heisexingqisi 发表于 2016-8-25 22:25 | 显示全部楼层
楼上讲解的太好了,以前不懂怎么写个P0P1,原来是共用的中断入口。
734774645 发表于 2016-8-25 23:28 | 显示全部楼层
楼主随便找个中断的例程阅读两遍,看看什么时候干了什么。
dongnanxibei 发表于 2016-8-26 22:20 | 显示全部楼层

/*---------------------------------------------------------------------------------------------------------*/
/* ADC interrupt handler                                                                                   */
/*---------------------------------------------------------------------------------------------------------*/
void ADC_IRQHandler(void)
{
    if(ADC_GET_INT_FLAG(ADC, ADC_CMP0_INT) != 0)
    {
        g_u32AdcCmp0IntFlag = 1;
        ADC_CLR_INT_FLAG(ADC, ADC_CMP0_INT);     /* clear the A/D compare flag 0 */
    }

    if(ADC_GET_INT_FLAG(ADC, ADC_CMP1_INT) != 0)
    {
        g_u32AdcCmp1IntFlag = 1;
        ADC_CLR_INT_FLAG(ADC, ADC_CMP1_INT);     /* clear the A/D compare flag 1 */
    }
}

dongnanxibei 发表于 2016-8-26 22:21 | 显示全部楼层
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V2.0
  4. * $Revision: 2 $
  5. * $Date: 14/12/25 10:24a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Monitor the conversion result of channel 2 by the digital compare function.
  7. * @note
  8. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M0518.h"


  13. #define PLL_CLOCK       50000000



  14. /*---------------------------------------------------------------------------------------------------------*/
  15. /* Define Function Prototypes                                                                              */
  16. /*---------------------------------------------------------------------------------------------------------*/
  17. void SYS_Init(void);
  18. void UART0_Init(void);
  19. void AdcResultMonitorTest(void);


  20. /*---------------------------------------------------------------------------------------------------------*/
  21. /* Define global variables and constants                                                                   */
  22. /*---------------------------------------------------------------------------------------------------------*/
  23. volatile uint32_t g_u32AdcCmp0IntFlag;
  24. volatile uint32_t g_u32AdcCmp1IntFlag;


  25. void SYS_Init(void)
  26. {
  27.     /*---------------------------------------------------------------------------------------------------------*/
  28.     /* Init System Clock                                                                                       */
  29.     /*---------------------------------------------------------------------------------------------------------*/

  30.     /* Enable Internal RC 22.1184MHz clock */
  31.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  32.     /* Waiting for Internal RC clock ready */
  33.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  34.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  35.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  36.     /* Enable external XTAL 12MHz clock */
  37.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  38.     /* Waiting for external XTAL clock ready */
  39.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  40.     /* Set core clock as PLL_CLOCK from PLL */
  41.     CLK_SetCoreClock(PLL_CLOCK);

  42.     /* Enable UART module clock */
  43.     CLK_EnableModuleClock(UART0_MODULE);

  44.     /* Enable ADC module clock */
  45.     CLK_EnableModuleClock(ADC_MODULE);

  46.     /* Select UART module clock source */
  47.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));

  48.     /* ADC clock source is 22.1184MHz, set divider to 7, ADC clock is 22.1184/7 MHz */
  49.     CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(7));

  50.     /*---------------------------------------------------------------------------------------------------------*/
  51.     /* Init I/O Multi-function                                                                                 */
  52.     /*---------------------------------------------------------------------------------------------------------*/

  53.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  54.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  55.     SYS->GPB_MFP |= SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

  56.     /* Disable the GPA0 - GPA3 digital input path to avoid the leakage current. */
  57.     GPIO_DISABLE_DIGITAL_PATH(PA, 0xF);

  58.     /* Configure the GPA0 - GPA3 ADC analog input pins */
  59.     SYS->GPA_MFP &= ~(SYS_GPA_MFP_PA0_Msk | SYS_GPA_MFP_PA1_Msk | SYS_GPA_MFP_PA2_Msk | SYS_GPA_MFP_PA3_Msk) ;
  60.     SYS->GPA_MFP |= SYS_GPA_MFP_PA0_ADC0 | SYS_GPA_MFP_PA1_ADC1 | SYS_GPA_MFP_PA2_ADC2 | SYS_GPA_MFP_PA3_ADC3 ;

  61. }

  62. /*---------------------------------------------------------------------------------------------------------*/
  63. /* Init UART                                                                                               */
  64. /*---------------------------------------------------------------------------------------------------------*/
  65. void UART0_Init()
  66. {
  67.     /* Reset IP */
  68.     SYS_ResetModule(UART0_RST);

  69.     /* Configure UART0 and set UART0 Baudrate */
  70.     UART_Open(UART0, 115200);
  71. }

  72. /*---------------------------------------------------------------------------------------------------------*/
  73. /* Function: AdcResultMonitorTest                                                                          */
  74. /*                                                                                                         */
  75. /* Parameters:                                                                                             */
  76. /*   None.                                                                                                 */
  77. /*                                                                                                         */
  78. /* Returns:                                                                                                */
  79. /*   None.                                                                                                 */
  80. /*                                                                                                         */
  81. /* Description:                                                                                            */
  82. /*   ADC result monitor function test.                                                                     */
  83. /*---------------------------------------------------------------------------------------------------------*/
  84. void AdcResultMonitorTest()
  85. {
  86.     printf("\n");
  87.     printf("+----------------------------------------------------------------------+\n");
  88.     printf("|           ADC compare function (result monitor) sample code          |\n");
  89.     printf("+----------------------------------------------------------------------+\n");
  90.     printf("\nIn this test, software will compare the conversion result of channel 2.\n");

  91.     /* Set the ADC operation mode as continuous scan, input mode as single-end and enable the analog input channel 2 */
  92.     ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_CONTINUOUS, 0x1 << 2);

  93.     /* Power on ADC module */
  94.     ADC_POWER_ON(ADC);

  95.     /* Enable ADC comparator 0. Compare condition: conversion result < 0x800; match Count=5. */
  96.     printf("   Set the compare condition of comparator 0: channel 2 is less than 0x800; match count is 5.\n");
  97.     ADC_ENABLE_CMP0(ADC, 2, ADC_ADCMPR_CMPCOND_LESS_THAN, 0x800, 5);

  98.     /* Enable ADC comparator 1. Compare condition: conversion result >= 0x800; match Count=5. */
  99.     printf("   Set the compare condition of comparator 1: channel 2 is greater than or equal to 0x800; match count is 5.\n");
  100.     ADC_ENABLE_CMP1(ADC, 2, ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL, 0x800, 5);

  101.     /* Clear the ADC comparator 0 interrupt flag for safe */
  102.     ADC_CLR_INT_FLAG(ADC, ADC_CMP0_INT);
  103.     /* Enable ADC comparator 0 interrupt */
  104.     ADC_EnableInt(ADC, ADC_CMP0_INT);

  105.     /* Clear the ADC comparator 1 interrupt flag for safe */
  106.     ADC_CLR_INT_FLAG(ADC, ADC_CMP1_INT);
  107.     /* Enable ADC comparator 1 interrupt */
  108.     ADC_EnableInt(ADC, ADC_CMP1_INT);

  109.     NVIC_EnableIRQ(ADC_IRQn);

  110.     g_u32AdcCmp0IntFlag = 0;
  111.     g_u32AdcCmp1IntFlag = 0;

  112.     /* Clear the ADC interrupt flag */
  113.     ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);

  114.     /* Start A/D conversion */
  115.     ADC_START_CONV(ADC);

  116.     /* Wait ADC compare interrupt */
  117.     while((g_u32AdcCmp0IntFlag == 0) && (g_u32AdcCmp1IntFlag == 0));

  118.     /* Stop A/D conversion */
  119.     ADC_STOP_CONV(ADC);
  120.     /* Disable ADC comparator interrupt */
  121.     ADC_DisableInt(ADC, ADC_CMP0_INT);
  122.     ADC_DisableInt(ADC, ADC_CMP1_INT);
  123.     /* Disable compare function */
  124.     ADC_DISABLE_CMP0(ADC);
  125.     ADC_DISABLE_CMP1(ADC);

  126.     if(g_u32AdcCmp0IntFlag == 1)
  127.     {
  128.         printf("Comparator 0 interrupt occurs.\nThe conversion result of channel 2 is less than 0x800\n");
  129.     }
  130.     else
  131.     {
  132.         printf("Comparator 1 interrupt occurs.\nThe conversion result of channel 2 is greater than or equal to 0x800\n");
  133.     }
  134. }


  135. /*---------------------------------------------------------------------------------------------------------*/
  136. /* ADC interrupt handler                                                                                   */
  137. /*---------------------------------------------------------------------------------------------------------*/
  138. void ADC_IRQHandler(void)
  139. {
  140.     if(ADC_GET_INT_FLAG(ADC, ADC_CMP0_INT) != 0)
  141.     {
  142.         g_u32AdcCmp0IntFlag = 1;
  143.         ADC_CLR_INT_FLAG(ADC, ADC_CMP0_INT);     /* clear the A/D compare flag 0 */
  144.     }

  145.     if(ADC_GET_INT_FLAG(ADC, ADC_CMP1_INT) != 0)
  146.     {
  147.         g_u32AdcCmp1IntFlag = 1;
  148.         ADC_CLR_INT_FLAG(ADC, ADC_CMP1_INT);     /* clear the A/D compare flag 1 */
  149.     }
  150. }
  151. /*---------------------------------------------------------------------------------------------------------*/
  152. /* MAIN function                                                                                           */
  153. /*---------------------------------------------------------------------------------------------------------*/

  154. int main(void)
  155. {

  156.     /* Unlock protected registers */
  157.     SYS_UnlockReg();

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

  160.     /* Lock protected registers */
  161.     SYS_LockReg();

  162.     /* Init UART0 for printf */
  163.     UART0_Init();

  164.     /*---------------------------------------------------------------------------------------------------------*/
  165.     /* SAMPLE CODE                                                                                             */
  166.     /*---------------------------------------------------------------------------------------------------------*/

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

  168.     /* Result monitor test */
  169.     AdcResultMonitorTest();

  170.     /* Disable ADC module */
  171.     ADC_Close(ADC);

  172.     /* Disable ADC IP clock */
  173.     CLK_DisableModuleClock(ADC_MODULE);

  174.     /* Disable External Interrupt */
  175.     NVIC_DisableIRQ(ADC_IRQn);

  176.     printf("\nExit ADC sample code\n");

  177.     while(1);

  178. }


您需要登录后才可以回帖 登录 | 注册

本版积分规则

20

主题

26

帖子

0

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