[CC2530 Introduction Course-03] CC2530 Interrupt System and External Interrupt Application

[CC2530 Introduction Tutorial-06] CC2530's ADC working principle and application

[CC2530 Introduction Tutorial-05] The principle and application of the serial interface of CC2530

[CC2530 Introduction Course-04] The principle and application of CC2530 timer/counter

[CC2530 Introduction Course-03] CC2530 Interrupt System and External Interrupt Application

[CC2530 Introduction Tutorial-02] CC2530's general I/O port input and output control

[CC2530 Getting Started Tutorial-01] CC2530 Microcontroller Development Getting Started Basics

 


1. Basic concepts related to interrupts

       There are two main ways of interaction between the  kernel and peripherals : polling and interruption . The polling method seems fair, but the actual work efficiency is very low, and it cannot respond to emergencies in time; the interrupt system makes the kernel have the ability to respond to emergencies.

        During the execution of the current program of the CPU, due to some urgent situation in the system, the CPU suspends the program being executed, and then executes another special program to deal with the emergent affairs. After the processing is over, the CPU automatically returns to the original Continue execution in the suspended program. This kind of program is interrupted during execution due to external reasons, which is called interruption .

        Two important concepts:

        <1>  Interrupt service function : the corresponding processing program executed after the kernel responds to the interrupt.

        <2>  Interrupt vector : the entry address of the interrupt service routine. Each interrupt source corresponds to a fixed entry address. When the kernel responds to an interrupt request, it will suspend the current program execution, and then jump to the entry address to execute the code.

Second, the interrupt system of CC2530

        CC2530 has 18 interrupt sources , and each interrupt source is controlled by its own series of special function registers . Can be programmed to set the relevant special function registers, set the priority of 18 interrupt sources and enable interrupt application response. The commonly used interrupt sources are as follows:

Three, CC2530 interrupt processing function writing method

        The interrupt service function is different from the general custom function and has a specific writing format :

<1> Before each interrupt service function, an initial sentence must be added :

        #pragma vector  = < interrupt vector >

        <Interrupt vector> indicates that the interrupt service function to be written next serves that interrupt source. There are two ways to write this statement:

        #pragma vector = 0x7B     or     #pragma vector = P1INT_VECTOR

        The former is the entry address of the interrupt vector, and the latter is the macro definition in the header file "ioCC2530.h" .

<2> The  _ _interrupt keyword indicates that the function is an interrupt service function, <function name> can be customized, and the function body can not have parameters or return values .

Fourth, the external interrupt of CC2530

        Each pin in the P0, P1, and P2 ports of CC2530 has an external interrupt input function. To make some pins have an external interrupt function, you need to properly set the IENx register, PxIEN register and PICTL register. In addition to each interrupt source having its own interrupt enable switch, the interrupt system also has a master switch, which can be turned on with " EA = 1 ;".

        Ports P0, P1, and P2 use P0IF, P1IF, and P2IF as interrupt flag bits, respectively. When an external interrupt occurs on a pin on any port group, the interrupt flag of the corresponding port group will be automatically set. Note that the external interrupt flag must be manually cleared in the interrupt service function, otherwise the CPU will repeatedly enter the interrupt. The port status flag registers P0IFG , P1IGF, and P2IFG correspond to the interrupt trigger status of each pin in the three ports respectively. When an external interrupt trigger occurs on a pin, the corresponding flag bit will be automatically set. This flag also needs to be cleared manually.

Five, training case: external interrupt input to control LED lights

[1] Design the external interrupt initialization function Init_INTP()

        The external interrupt initialization function is mainly to complete the configuration of special function registers related to interrupts:

        <1> Configure the IENx register to enable the interrupt function of the port group.

        <2> Configure the PxIEN register to enable specific external interrupt pins.

        <3> Configure the PICTL register and set the interrupt trigger mode.

[2] Design the external interrupt service function Int1_Sevice()

        When writing interrupt service functions, the writing format must be correct, and the interrupt vector must not be mistaken. Pay special attention: Clear the port group and pin flags in the function, otherwise the CPU will repeatedly enter the interrupt. You must first clear the pin flag PxIFG, and then clear the port group flag PxIF.

[3] The source code and comments of the training project

#include "ioCC2530.h"

#define  LED6   P1_4
#define  LED3   P1_0
#define  LED4   P1_1
/*===================延时函数=========================*/
void Delay(unsigned int t)
{
  while(t--);
}
/*==================端口初始化函数=====================*/
void Init_Port()
{
  //将P1_0、P1_1和P1_4设置为通用I/O端口功能
  P1SEL &= ~0x13;
  //将P1_0、P1_1和P1_4的端口传输方式设置为输出
  P1DIR |= 0x13;
  LED6 = 0;
  LED3 = 0;
  LED4 = 0;
}
/*==================跑马灯子函数=====================*/
void LED_Running()
{
  LED3 = 1;
  Delay(50000);
  LED4 = 1;
  Delay(50000);
  LED3 = 0;
  Delay(50000);
  LED4 = 0;
  Delay(50000);
}
/*===============外部中断初始化函数==================*/
void Init_INTP()
{
  IEN2 |= 0x10;         //端口1中断使能
  P1IEN |= 0x04;        //端口P1_2外部中断使能
  PICTL |= 0x02;        //端口P1_0到P1_3下降沿触发
  EA = 1;               //使能总中断
}
/*================外部中断1服务函数====================*/
#pragma vector = P1INT_VECTOR     //外部中断1的向量入口
__interrupt void Int1_Sevice()
{
  LED6 = ~LED6;
/*先清除引脚标志位,再清除端口标志位,否则会不断进入中断*/
  P1IFG &= ~ 0x04;        //软件清除P1_2引脚的标志位
  P1IF = 0;               //软件清除P1端口组的标志位
}
/*====================主函数==========================*/
void main()
{
  Init_Port();          //初始化通用I/O端口
  Init_INTP();          //初始化外部中断
  while(1)
  {
    LED_Running();     //跑马灯
  }
}

 

Guess you like

Origin blog.csdn.net/weixin_44643510/article/details/114319628