打印
[DemoCode下载]

N76E885 I/O 中断的使用

[复制链接]
1788|12
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
玛尼玛尼哄|  楼主 | 2017-2-17 21:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

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

//***********************************************************************************************************
//  File Function: N76E885 I/O Interrupt demo code
//***********************************************************************************************************

#include <stdio.h>
#include "N76E885.h"
#include "Version.h"
#include "Typedef.h"
#include "Define.h"
#include "SFR_Macro.h"
#include "Common.h"
#include "Delay.h"

/*
//-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
//
//<e0> System Clock Source Configuration
// <o1> System Clock Source Selection
//      <0=> 2~25MHz    XTAL
//      <1=> 32.768KHz  XTAL
//      <2=> 22.1184MHz Internal
//      <3=> 10KHz      Internal
//      <4=> OSC-In     External
//</e>
//
//<e2> Clock Divider Configuration
//     <o3.0..7>  System Clock Source Devider <1-255:1>
//                     <i> Fsys = (System Clock Source) / (2 * Devider)
//</e>
//
//<h> Port Interrupt Option
// <o4.0..1> Port Pin Selection <0=> Port0 <1=> Port1  <2=> Port2  <3=> Port3
//  <h> Edge/Level
//      <o4.2> Channel 0,   trigger type selection <0=> Level <1=> Edge
//      <o4.3> Channel 1,   trigger type selection <0=> Level <1=> Edge
//      <o4.4> Channel 2,   trigger type selection <0=> Level <1=> Edge
//      <o4.5> Channel 3,   trigger type selection <0=> Level <1=> Edge
//      <o4.6> Channel 5,4, trigger type selection <0=> Level <1=> Edge
//      <o4.7> Channel 7,6, trigger type selection <0=> Level <1=> Edge
//  </h>
//  <h>  Falling/Level
//      <q5.0> Channel 0, Enabled level/falling edge detect.
//      <q5.1> Channel 1, Enabled level/falling edge detect.
//      <q5.2> Channel 2, Enabled level/falling edge detect.
//      <q5.3> Channel 3, Enabled level/falling edge detect.
//      <q5.4> Channel 4, Enabled level/falling edge detect.
//      <q5.5> Channel 5, Enabled level/falling edge detect.
//      <q5.6> Channel 6, Enabled level/falling edge detect.
//      <q5.7> Channel 7, Enabled level/falling edge detect.
//  </h>
//  <h>  Rising/Level
//      <q6.0> Channel 0, Enabled level/rising edge detect.
//      <q6.1> Channel 1, Enabled level/rising edge detect.
//      <q6.2> Channel 2, Enabled level/rising edge detect.
//      <q6.3> Channel 3, Enabled level/rising edge detect.
//      <q6.4> Channel 4, Enabled level/rising edge detect.
//      <q6.5> Channel 5, Enabled level/rising edge detect.
//      <q6.6> Channel 6, Enabled level/rising edge detect.
//      <q6.7> Channel 7, Enabled level/rising edge detect.
//  </h>
//</h>
//
//-------- <<< end of configuration section >>> ------------------------------
*/

#define SYS_CLK_EN      0
#define SYS_SEL         2
#define SYS_DIV_EN      0                   //0: Fsys=Fosc, 1: Fsys = Fosc/(2*CKDIV)
#define SYS_DIV         1
#define PICON_VAL       0xFC                //Pin Interrupt Control
#define PINEN_VAL       0xFF                //Pin Interrupt Negative Polarity Enable.
#define PIPEN_VAL       0xFF                //Pin Interrupt Positive Polarity Enable.
bit BIT_TMP;
/*----------------------------------------------------------------------------
  Check the register settings
*----------------------------------------------------------------------------*/
#define CHECK_RANGE(val, min, max)          ((val < min) || (val > max))
#define CHECK_RSVD(val, mask)               (val & mask)

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

#if (PICON_VAL&0x03) == 0x02  //Port2, only P2[6:0] available
    #if CHECK_RSVD(PINEN_VAL, ~0x7F)
        #error "Port2[6:0] Interrup Pin Select Error"
    #elif CHECK_RSVD(PIPEN_VAL, ~0x7F)
        #error "Port2[6:0] Interrup Pin Select Error"
    #endif
#endif
/******************************************************************************
* FUNCTION_PURPOSE: I/O Pin interrupt Service Routine
******************************************************************************/
void 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
}
/******************************************************************************
The main C function.  Program execution starts
here after stack initialization.
******************************************************************************/
void main (void)
{
    /* Note
       MCU power on system clock is HIRC (22.1184MHz), so Fsys = 22.1184MHz
    */
   
    Set_All_GPIO_Quasi_Mode();
    InitialUART0_Timer1_Type1(9600);             /* 9600 Baud Rate*/

    Show_FW_Version_Number_To_PC();

    printf ("\n*===================================================================");
    printf ("\n*  Name: N76E885 Pins Interrupt Demo Code.");
    printf ("\n*===================================================================\n");
        
    /* Change system closk source */
    #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
        #endif
    #endif
   
    #if SYS_DIV_EN == 1
        CKDIV = SYS_DIV;                        //Fsys = Fosc / (2* CLKDIV) = Fcpu
    #endif
   
    P0 = 0xFF;
    P1 = 0x07;
    P2 = 0x7F;
    P3 = 0xFF;
   
    PICON = PICON_VAL;
    PINEN = PINEN_VAL;
    PIPEN = PIPEN_VAL;
   
    set_EPI;
    EA = 1;
   
    while(1)
    {
        set_PD;                                 //Enter Power Down
    }
}



沙发
天灵灵地灵灵| | 2017-2-17 22:02 | 只看该作者
这种方式操作的,也就是直接操作寄存器。

使用特权

评论回复
板凳
598330983| | 2017-2-19 19:27 | 只看该作者
由于IO口都是双向模式的,很容易操作,不用考虑什么方向寄存器。模式也比较简单。

使用特权

评论回复
地板
heisexingqisi| | 2017-2-19 23:15 | 只看该作者
这个例程主要高速我们,如果在代码阶段直接用预处理指令,那么很有用

使用特权

评论回复
5
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
通过这些选择了不同的时钟源。

使用特权

评论回复
6
yiyigirl2014| | 2017-2-20 00:09 | 只看该作者
寄存器标志位的值也可以读出来,不仅仅可以触发中断。

使用特权

评论回复
7
yiyigirl2014| | 2017-2-20 00:10 | 只看该作者
新唐为了方便大家开发,提前做了不少的头文件,里面有很多函数的封装。

使用特权

评论回复
8
天灵灵地灵灵| | 2017-2-20 00:19 | 只看该作者
51一般只有两个管脚可以中断,新唐改进了不少,基本上都可以中断

使用特权

评论回复
9
zhuomuniao110| | 2017-2-21 19:26 | 只看该作者
通过中断唤醒掉电模式。

使用特权

评论回复
10
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
}
直接全部清,然后再判断。

使用特权

评论回复
11
wahahaheihei| | 2017-2-21 20:15 | 只看该作者
51单片机上,都是通过寄存器直接操作的比较多。也是为了效率,毕竟存储空间有限。

使用特权

评论回复
12
天灵灵地灵灵| | 2017-2-21 21:43 | 只看该作者
因为外设少,所以用到的寄存器也不多,因此用寄存器操作可以满足内存小的不足。

使用特权

评论回复
13
玛尼玛尼哄|  楼主 | 2017-2-21 21:51 | 只看该作者
必须亲力亲为的去验证才好理解。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

170

主题

3047

帖子

2

粉丝