[DemoCode下载] N76E885 I/O 中断的使用

[复制链接]
2140|12
 楼主| 玛尼玛尼哄 发表于 2017-2-17 21:17 | 显示全部楼层 |阅读模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2015
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E885 I/O Interrupt demo code
  14. //***********************************************************************************************************

  15. #include <stdio.h>
  16. #include "N76E885.h"
  17. #include "Version.h"
  18. #include "Typedef.h"
  19. #include "Define.h"
  20. #include "SFR_Macro.h"
  21. #include "Common.h"
  22. #include "Delay.h"

  23. /*
  24. //-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
  25. //
  26. //<e0> System Clock Source Configuration
  27. // <o1> System Clock Source Selection
  28. //      <0=> 2~25MHz    XTAL
  29. //      <1=> 32.768KHz  XTAL
  30. //      <2=> 22.1184MHz Internal
  31. //      <3=> 10KHz      Internal
  32. //      <4=> OSC-In     External
  33. //</e>
  34. //
  35. //<e2> Clock Divider Configuration
  36. //     <o3.0..7>  System Clock Source Devider <1-255:1>
  37. //                     <i> Fsys = (System Clock Source) / (2 * Devider)
  38. //</e>
  39. //
  40. //<h> Port Interrupt Option
  41. // <o4.0..1> Port Pin Selection <0=> Port0 <1=> Port1  <2=> Port2  <3=> Port3
  42. //  <h> Edge/Level
  43. //      <o4.2> Channel 0,   trigger type selection <0=> Level <1=> Edge
  44. //      <o4.3> Channel 1,   trigger type selection <0=> Level <1=> Edge
  45. //      <o4.4> Channel 2,   trigger type selection <0=> Level <1=> Edge
  46. //      <o4.5> Channel 3,   trigger type selection <0=> Level <1=> Edge
  47. //      <o4.6> Channel 5,4, trigger type selection <0=> Level <1=> Edge
  48. //      <o4.7> Channel 7,6, trigger type selection <0=> Level <1=> Edge
  49. //  </h>
  50. //  <h>  Falling/Level
  51. //      <q5.0> Channel 0, Enabled level/falling edge detect.
  52. //      <q5.1> Channel 1, Enabled level/falling edge detect.
  53. //      <q5.2> Channel 2, Enabled level/falling edge detect.
  54. //      <q5.3> Channel 3, Enabled level/falling edge detect.
  55. //      <q5.4> Channel 4, Enabled level/falling edge detect.
  56. //      <q5.5> Channel 5, Enabled level/falling edge detect.
  57. //      <q5.6> Channel 6, Enabled level/falling edge detect.
  58. //      <q5.7> Channel 7, Enabled level/falling edge detect.
  59. //  </h>
  60. //  <h>  Rising/Level
  61. //      <q6.0> Channel 0, Enabled level/rising edge detect.
  62. //      <q6.1> Channel 1, Enabled level/rising edge detect.
  63. //      <q6.2> Channel 2, Enabled level/rising edge detect.
  64. //      <q6.3> Channel 3, Enabled level/rising edge detect.
  65. //      <q6.4> Channel 4, Enabled level/rising edge detect.
  66. //      <q6.5> Channel 5, Enabled level/rising edge detect.
  67. //      <q6.6> Channel 6, Enabled level/rising edge detect.
  68. //      <q6.7> Channel 7, Enabled level/rising edge detect.
  69. //  </h>
  70. //</h>
  71. //
  72. //-------- <<< end of configuration section >>> ------------------------------
  73. */

  74. #define SYS_CLK_EN      0
  75. #define SYS_SEL         2
  76. #define SYS_DIV_EN      0                   //0: Fsys=Fosc, 1: Fsys = Fosc/(2*CKDIV)
  77. #define SYS_DIV         1
  78. #define PICON_VAL       0xFC                //Pin Interrupt Control
  79. #define PINEN_VAL       0xFF                //Pin Interrupt Negative Polarity Enable.
  80. #define PIPEN_VAL       0xFF                //Pin Interrupt Positive Polarity Enable.
  81. bit BIT_TMP;
  82. /*----------------------------------------------------------------------------
  83.   Check the register settings
  84. *----------------------------------------------------------------------------*/
  85. #define CHECK_RANGE(val, min, max)          ((val < min) || (val > max))
  86. #define CHECK_RSVD(val, mask)               (val & mask)

  87. #if (PICON_VAL&0x03) == 0x01  //Port1, only P1[2:0] available
  88.     #if CHECK_RSVD(PINEN_VAL, ~0x07)
  89.         #error "Port1[2:0] Interrup Pin Select Error"
  90.     #elif CHECK_RSVD(PIPEN_VAL, ~0x07)
  91.         #error "Port1[2:0] Interrup Pin Select Error"
  92.     #endif
  93. #endif

  94. #if (PICON_VAL&0x03) == 0x02  //Port2, only P2[6:0] available
  95.     #if CHECK_RSVD(PINEN_VAL, ~0x7F)
  96.         #error "Port2[6:0] Interrup Pin Select Error"
  97.     #elif CHECK_RSVD(PIPEN_VAL, ~0x7F)
  98.         #error "Port2[6:0] Interrup Pin Select Error"
  99.     #endif
  100. #endif
  101. /******************************************************************************
  102. * FUNCTION_PURPOSE: I/O Pin interrupt Service Routine
  103. ******************************************************************************/
  104. void PinInterrupt_ISR (void) interrupt 7
  105. {
  106.     PIF = 0x00;                             //clear interrupt flag
  107.    
  108.     #if (PICON_VAL&0x03) == 0x00            //Port0 are interrupt pin
  109.         P30 = ~P30;
  110.     #endif
  111.    
  112.     #if (PICON_VAL&0x03) == 0x01            //Port1 are interrupt pin
  113.         P30 = ~P30;
  114.     #endif
  115.    
  116.     #if (PICON_VAL&0x03) == 0x02            //Port2 are interrupt pin
  117.         P30 = ~P30;
  118.     #endif
  119.    
  120.     #if (PICON_VAL&0x03) == 0x03            //Port3 are interrupt pin
  121.         P00 = ~P00;
  122.     #endif
  123. }
  124. /******************************************************************************
  125. The main C function.  Program execution starts
  126. here after stack initialization.
  127. ******************************************************************************/
  128. void main (void)
  129. {
  130.     /* Note
  131.        MCU power on system clock is HIRC (22.1184MHz), so Fsys = 22.1184MHz
  132.     */
  133.    
  134.     Set_All_GPIO_Quasi_Mode();
  135.     InitialUART0_Timer1_Type1(9600);             /* 9600 Baud Rate*/

  136.     Show_FW_Version_Number_To_PC();

  137.     printf ("\n*===================================================================");
  138.     printf ("\n*  Name: N76E885 Pins Interrupt Demo Code.");
  139.     printf ("\n*===================================================================\n");
  140.         
  141.     /* Change system closk source */
  142.     #if SYS_CLK_EN == 1
  143.         #if   SYS_SEL == 0
  144.             System_Clock_Select(E_HXTEN);   //Fosc = 2~25MHz XTAL
  145.         #elif SYS_SEL == 1
  146.             System_Clock_Select(E_LXTEN);   //Fosc = 32.768KHz XTAL
  147.         #elif SYS_SEL == 2
  148.             System_Clock_Select(E_HIRCEN);  //Fosc = 22.1184MHz Internal RC
  149.         #elif SYS_SEL == 3
  150.             System_Clock_Select(E_LIRCEN);  //Fosc = 10KHz Internal RC
  151.         #elif SYS_SEL == 4
  152.             System_Clock_Select(E_OSCEN);   //Fosc = OSC-In External OSC
  153.         #endif
  154.     #endif
  155.    
  156.     #if SYS_DIV_EN == 1
  157.         CKDIV = SYS_DIV;                        //Fsys = Fosc / (2* CLKDIV) = Fcpu
  158.     #endif
  159.    
  160.     P0 = 0xFF;
  161.     P1 = 0x07;
  162.     P2 = 0x7F;
  163.     P3 = 0xFF;
  164.    
  165.     PICON = PICON_VAL;
  166.     PINEN = PINEN_VAL;
  167.     PIPEN = PIPEN_VAL;
  168.    
  169.     set_EPI;
  170.     EA = 1;
  171.    
  172.     while(1)
  173.     {
  174.         set_PD;                                 //Enter Power Down
  175.     }
  176. }



天灵灵地灵灵 发表于 2017-2-17 22:02 | 显示全部楼层
这种方式操作的,也就是直接操作寄存器。
598330983 发表于 2017-2-19 19:27 | 显示全部楼层
由于IO口都是双向模式的,很容易操作,不用考虑什么方向寄存器。模式也比较简单。
heisexingqisi 发表于 2017-2-19 23:15 | 显示全部楼层
这个例程主要高速我们,如果在代码阶段直接用预处理指令,那么很有用
gejigeji521 发表于 2017-2-20 00:04 | 显示全部楼层
   #if SYS_CLK_EN == 1
        #if   SYS_SEL == 0
            System_Clock_Select(E_HXTEN);   //Fosc = 2~25MHz XTAL
        #elif SYS_SEL == 1
            System_Clock_Select(E_LXTEN);   //Fosc = 32.768KHz XTAL
        #elif SYS_SEL == 2
            System_Clock_Select(E_HIRCEN);  //Fosc = 22.1184MHz Internal RC
        #elif SYS_SEL == 3
            System_Clock_Select(E_LIRCEN);  //Fosc = 10KHz Internal RC
        #elif SYS_SEL == 4
            System_Clock_Select(E_OSCEN);   //Fosc = OSC-In External OSC
通过这些选择了不同的时钟源。
yiyigirl2014 发表于 2017-2-20 00:09 | 显示全部楼层
寄存器标志位的值也可以读出来,不仅仅可以触发中断。
yiyigirl2014 发表于 2017-2-20 00:10 | 显示全部楼层
新唐为了方便大家开发,提前做了不少的头文件,里面有很多函数的封装。
天灵灵地灵灵 发表于 2017-2-20 00:19 | 显示全部楼层
51一般只有两个管脚可以中断,新唐改进了不少,基本上都可以中断
zhuomuniao110 发表于 2017-2-21 19:26 | 显示全部楼层
通过中断唤醒掉电模式。
huangcunxiake 发表于 2017-2-21 20:01 | 显示全部楼层
oid PinInterrupt_ISR (void) interrupt 7
{
    PIF = 0x00;                             //clear interrupt flag
   
    #if (PICON_VAL&0x03) == 0x00            //Port0 are interrupt pin
        P30 = ~P30;
    #endif
   
    #if (PICON_VAL&0x03) == 0x01            //Port1 are interrupt pin
        P30 = ~P30;
    #endif
   
    #if (PICON_VAL&0x03) == 0x02            //Port2 are interrupt pin
        P30 = ~P30;
    #endif
   
    #if (PICON_VAL&0x03) == 0x03            //Port3 are interrupt pin
        P00 = ~P00;
    #endif
}
直接全部清,然后再判断。
wahahaheihei 发表于 2017-2-21 20:15 | 显示全部楼层
51单片机上,都是通过寄存器直接操作的比较多。也是为了效率,毕竟存储空间有限。
天灵灵地灵灵 发表于 2017-2-21 21:43 | 显示全部楼层
因为外设少,所以用到的寄存器也不多,因此用寄存器操作可以满足内存小的不足。
 楼主| 玛尼玛尼哄 发表于 2017-2-21 21:51 | 显示全部楼层
必须亲力亲为的去验证才好理解。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

196

主题

3261

帖子

2

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