[DemoCode下载] arm Cortex-M0新唐m052外部中断

[复制链接]
1072|5
 楼主| Micachl 发表于 2016-7-9 20:56 | 显示全部楼层 |阅读模式
  1. #include "register.h"  

  2. int main(void)  
  3. {  
  4.     rREGWRPROT = 0x59;  //解锁  
  5.     rREGWRPROT = 0x16;  
  6.     rREGWRPROT = 0x88;   
  7.     rPWRCON |= (0x1<<0); //使能晶振  
  8.     while( (rCLKSTATUS & (0x1<<0)) == 0); //等待时钟稳定  

  9.     rP1_PMD = 0x5555;  
  10.     rP1_DOUT = 0xff;  
  11.     rP3_IMD &= ~(0x1<<3); //下降沿触发中断     
  12.     rDBNCECON = (0x1<<5) | (0xf); //采样周期为128*256个时钟  
  13.     rP3_DBEN |= (0x1<<3);//防反弹使能  
  14.     rP3_IEN |= (0x1<<3);  //使能下降沿中断  
  15.     rNVIC_ISER |= (0x1<<3);   

  16.     rREGWRPROT = 0x0;       //上锁  

  17.     while(1)  
  18.     {     

  19.     }     
  20. }  

  21. void EINT1_IRQHandler(void)  
  22. {  
  23.         rP1_DOUT ^= 0xff;   
  24.         rP3_ISRC = rP3_ISRC;         
  25. }  


wahahaheihei 发表于 2016-7-10 00:08 | 显示全部楼层
给你一个基于库函数的
  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: 3 $
  5. * $Date: 14/01/28 11:44a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 Series GPIO Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"


  13. #define PLL_CLOCK           50000000


  14. /**
  15. * @brief       External INT0 IRQ
  16. *
  17. * @param       None
  18. *
  19. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  20. *
  21. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The External INT0(P3.2) default IRQ, declared in startup_M051Series.s.
  22. */
  23. void EINT0_IRQHandler(void)
  24. {
  25.     /* For P3.2, clear the INT flag */
  26.     GPIO_CLR_INT_FLAG(P3, BIT2);

  27.     printf("P3.2 EINT0 occurred.\n");
  28. }

  29. /**
  30. * @brief       External INT1 IRQ
  31. *
  32. * @param       None
  33. *
  34. * @return      None
  35. *
  36. * @details     The External INT1(P3.3) default IRQ, declared in startup_M051Series.s.
  37. */
  38. void EINT1_IRQHandler(void)
  39. {
  40.     /* For P3.3, clear the INT flag */
  41.     GPIO_CLR_INT_FLAG(P3, BIT3);

  42.     printf("P3.3 EINT1 occurred.\n");
  43. }

  44. void SYS_Init(void)
  45. {
  46.     /*---------------------------------------------------------------------------------------------------------*/
  47.     /* Init System Clock                                                                                       */
  48.     /*---------------------------------------------------------------------------------------------------------*/
  49.     /* Enable Internal RC 22.1184MHz clock */
  50.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  51.     /* Waiting for Internal RC clock ready */
  52.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  55.     /* Enable external XTAL 12MHz clock */
  56.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  57.     /* Waiting for external XTAL clock ready */
  58.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  59.     /* Set core clock as PLL_CLOCK from PLL */
  60.     CLK_SetCoreClock(PLL_CLOCK);

  61.     /* Enable UART module clock */
  62.     CLK_EnableModuleClock(UART0_MODULE);

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

  65.     /*---------------------------------------------------------------------------------------------------------*/
  66.     /* Init I/O Multi-function                                                                                 */
  67.     /*---------------------------------------------------------------------------------------------------------*/

  68.     /* Set P3 multi-function pins for UART0 RXD, TXD, EINT0 and EINT1 */
  69.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk | SYS_MFP_P32_Msk | SYS_MFP_P33_Msk);
  70.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0 | SYS_MFP_P32_INT0 | SYS_MFP_P33_INT1);

  71. }

  72. void UART0_Init(void)
  73. {
  74.     /*---------------------------------------------------------------------------------------------------------*/
  75.     /* Init UART                                                                                               */
  76.     /*---------------------------------------------------------------------------------------------------------*/
  77.     /* Reset UART */
  78.     SYS_ResetModule(UART0_RST);

  79.     /* Configure UART0 and set UART0 Baudrate */
  80.     UART_Open(UART0, 115200);
  81. }

  82. /*---------------------------------------------------------------------------------------------------------*/
  83. /* MAIN function                                                                                           */
  84. /*---------------------------------------------------------------------------------------------------------*/
  85. int main(void)
  86. {
  87.     /* Unlock protected registers */
  88.     SYS_UnlockReg();

  89.     /* Init System, peripheral clock and multi-function I/O */
  90.     SYS_Init();

  91.     /* Lock protected registers */
  92.     SYS_LockReg();

  93.     /* Init UART0 for printf */
  94.     UART0_Init();

  95.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  96.     printf("+------------------------------------------------------------+\n");
  97.     printf("|    GPIO EINT0/EINT1 Interrupt and De-bounce Sample Code    |\n");
  98.     printf("+------------------------------------------------------------+\n\n");

  99.     /*-----------------------------------------------------------------------------------------------------*/
  100.     /* GPIO External Interrupt Function Test                                                               */
  101.     /*-----------------------------------------------------------------------------------------------------*/
  102.     printf("EINT0(P3.2) and EINT1(P3.3) are used to test interrupt \n");

  103.     /* Configure P3.2 as EINT0 pin and enable interrupt by falling edge trigger */
  104.     GPIO_SetMode(P3, BIT2, GPIO_PMD_INPUT);
  105.     GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
  106.     NVIC_EnableIRQ(EINT0_IRQn);

  107.     /* Configure P3.3 as EINT1 pin and enable interrupt by rising and falling edge trigger */
  108.     GPIO_SetMode(P3, BIT3, GPIO_PMD_INPUT);
  109.     GPIO_EnableEINT1(P3, 3, GPIO_INT_BOTH_EDGE);
  110.     NVIC_EnableIRQ(EINT1_IRQn);

  111.     /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 * 10 KHz clock */
  112.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_LIRC, GPIO_DBCLKSEL_1024);
  113.     GPIO_ENABLE_DEBOUNCE(P3, BIT2 | BIT3);

  114.     /* Waiting for interrupts */
  115.     while(1);
  116. }

  117. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


稳稳の幸福 发表于 2016-7-10 21:40 | 显示全部楼层
不懂这个ARM怎么中断都要弄个NVIC的函数。
ccw1986 发表于 2016-7-11 12:53 | 显示全部楼层
外部中断需要关中断和开中断,如果在ucos ii中用的话需要的更多
天灵灵地灵灵 发表于 2016-7-12 15:57 | 显示全部楼层
使用寄存器操作的效率高,看着简单,但是不好理解,如果没有注释,而库函数的,易于理解,但是看着冗长。
zhuomuniao110 发表于 2016-7-12 16:26 | 显示全部楼层
GPIO_SetMode(P3, BIT2, GPIO_PMD_INPUT);

    GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);

    NVIC_EnableIRQ(EINT0_IRQn);
最后一条是有什么讲究?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

43

主题

300

帖子

1

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