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

[复制链接]
1555|8
 楼主| orangebanana 发表于 2016-1-3 21:09 | 显示全部楼层 |阅读模式
  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. }  
 楼主| orangebanana 发表于 2016-1-4 20:14 | 显示全部楼层
如何保证外部中断是稳定工作的呢?
mintspring 发表于 2016-1-5 21:30 | 显示全部楼层
进入中断后第一件事就是关闭总中断,这样保证不会被打断,然后执行程序内容,最后退出前清理中断标志。
598330983 发表于 2016-1-5 22:26 | 显示全部楼层
这种中断进入后,需要清除中断标志,和关闭总中断吗?看楼主没有写啊。
dongnanxibei 发表于 2016-1-5 22:49 | 显示全部楼层
给你个完整版本的。
  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: 2 $
  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       Port0/Port1 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 Port0/Port1 default IRQ, declared in startup_M051Series.s.
  22. */
  23. void GPIOP0P1_IRQHandler(void)
  24. {
  25.     /* To check if P1.3 interrupt occurred */
  26.     if(GPIO_GET_INT_FLAG(P1, BIT3))
  27.     {
  28.         GPIO_CLR_INT_FLAG(P1, BIT3);
  29.         printf("P1.3 INT occurred.\n");
  30.     }
  31.     else
  32.     {
  33.         /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
  34.         P0->ISRC = P0->ISRC;
  35.         P1->ISRC = P1->ISRC;
  36.         printf("Un-expected interrupts.\n");
  37.     }
  38. }

  39. /**
  40. * @brief       Port2/Port3/Port4 IRQ
  41. *
  42. * @param       None
  43. *
  44. * @return      None
  45. *
  46. * @details     The Port2/Port3/Port4 default IRQ, declared in startup_M051Series.s.
  47. */
  48. void GPIOP2P3P4_IRQHandler(void)
  49. {
  50.     /* To check if P4.5 interrupt occurred */
  51.     if(GPIO_GET_INT_FLAG(P4, BIT5))
  52.     {
  53.         GPIO_CLR_INT_FLAG(P4, BIT5);
  54.         printf("P4.5 INT occurred.\n");
  55.     }
  56.     else
  57.     {
  58.         /* Un-expected interrupt. Just clear all PORT2, PORT3 and PORT4 interrupts */
  59.         P2->ISRC = P2->ISRC;
  60.         P3->ISRC = P3->ISRC;
  61.         P4->ISRC = P4->ISRC;
  62.         printf("Un-expected interrupts.\n");
  63.     }
  64. }

  65. void SYS_Init(void)
  66. {
  67.     /*---------------------------------------------------------------------------------------------------------*/
  68.     /* Init System Clock                                                                                       */
  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Enable Internal RC 22.1184MHz clock */
  71.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  72.     /* Waiting for Internal RC clock ready */
  73.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  76.     /* Enable external XTAL 12MHz clock */
  77.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  78.     /* Waiting for external XTAL clock ready */
  79.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  80.     /* Set core clock as PLL_CLOCK from PLL */
  81.     CLK_SetCoreClock(PLL_CLOCK);

  82.     /* Enable UART module clock */
  83.     CLK_EnableModuleClock(UART0_MODULE);

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

  86.     /*---------------------------------------------------------------------------------------------------------*/
  87.     /* Init I/O Multi-function                                                                                 */
  88.     /*---------------------------------------------------------------------------------------------------------*/

  89.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  90.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  91.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);

  92. }

  93. void UART0_Init(void)
  94. {
  95.     /*---------------------------------------------------------------------------------------------------------*/
  96.     /* Init UART                                                                                               */
  97.     /*---------------------------------------------------------------------------------------------------------*/
  98.     /* Reset UART */
  99.     SYS_ResetModule(UART0_RST);

  100.     /* Configure UART0 and set UART0 Baudrate */
  101.     UART_Open(UART0, 115200);
  102. }

  103. /*---------------------------------------------------------------------------------------------------------*/
  104. /* MAIN function                                                                                           */
  105. /*---------------------------------------------------------------------------------------------------------*/
  106. int main(void)
  107. {
  108.     /* Unlock protected registers */
  109.     SYS_UnlockReg();

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

  112.     /* Lock protected registers */
  113.     SYS_LockReg();

  114.     /* Init UART0 for printf */
  115.     UART0_Init();

  116.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  117.     printf("+------------------------------------------------+\n");
  118.     printf("|    GPIO P1.3 and P4.5 Interrupt Sample Code    |\n");
  119.     printf("+------------------------------------------------+\n\n");

  120.     /*-----------------------------------------------------------------------------------------------------*/
  121.     /* GPIO Interrupt Function Test                                                                        */
  122.     /*-----------------------------------------------------------------------------------------------------*/
  123.     printf("P1.3 and P4.5 are used to test interrupt ......\n");

  124.     /* Configure P1.3 as Input mode and enable interrupt by rising edge trigger */
  125.     GPIO_SetMode(P1, BIT3, GPIO_PMD_INPUT);
  126.     GPIO_EnableInt(P1, 3, GPIO_INT_RISING);
  127.     NVIC_EnableIRQ(GPIO_P0P1_IRQn);

  128.     /*  Configure P4.5 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  129.     GPIO_SetMode(P4, BIT5, GPIO_PMD_QUASI);
  130.     GPIO_EnableInt(P4, 5, GPIO_INT_FALLING);
  131.     NVIC_EnableIRQ(GPIO_P2P3P4_IRQn);

  132.     /* Waiting for interrupts */
  133.     while(1);
  134. }

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


dongnanxibei 发表于 2016-1-5 22:49 | 显示全部楼层
所以可以参考一下BSP软件开发包,里面有丰富的例程和库函数。
mintspring 发表于 2016-1-26 12:45 | 显示全部楼层
可以设计可靠的外部电路来实现。比如简单的滤波电路。
yiyigirl2014 发表于 2016-1-31 14:11 | 显示全部楼层
mintspring 发表于 2016-1-26 12:45
可以设计可靠的外部电路来实现。比如简单的滤波电路。

能详细讲解一下吗
mintspring 发表于 2016-2-11 19:56 | 显示全部楼层
采用模拟电路实现滤波,利用单片机进行调节控制参数。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

18

主题

113

帖子

3

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