C 如何使用中断服务例程检测此MSP430代码上的按钮事件?

C 如何使用中断服务例程检测此MSP430代码上的按钮事件?,c,microcontroller,msp430,isr,code-composer,C,Microcontroller,Msp430,Isr,Code Composer,下面是MSP430G2微控制器的代码。到目前为止,我很难将中断服务例程添加到我的代码中。此代码是一个两位计数器,使用启动板上的S2按钮向上计数,然后使用连接到插脚1.7和2.4的外部连接开关向下计数,这可以在代码中看到。如果有人能帮助解释如何将ISR添加到此代码中,我将非常感谢您的帮助 #include <msp430.h> #define LEDS (BIT6 + BIT0) // Global variables to hold current state and # of

下面是MSP430G2微控制器的代码。到目前为止,我很难将中断服务例程添加到我的代码中。此代码是一个两位计数器,使用启动板上的S2按钮向上计数,然后使用连接到插脚1.7和2.4的外部连接开关向下计数,这可以在代码中看到。如果有人能帮助解释如何将ISR添加到此代码中,我将非常感谢您的帮助

#include <msp430.h>

#define LEDS (BIT6 + BIT0)

// Global variables to hold current state and # of pushes
char pushes;


// Initialization function
void init(void)
{
    WDTCTL = WDTPW + WDTHOLD;                         // Stop watchdog timer
    P1DIR |= LEDS;                                   // Set P1.0, P1.6 to output direction
    P1DIR &= ~ BIT3;                                // Turn P1.3 into input
    P1REN |= BIT3;                                 // Enable internal resistor
    P1OUT |= BIT3;                                // Enable as pullup resistor
    P1DIR |= BIT7;                               // Set BIT 7 to output
    P1OUT |= BIT7;                              // Set BIT 7 as VCC



    P2DIR &= ~ (BIT4);                       // Makes this an input
    P2REN |= (BIT4);                        // Turns on internal resistor
    P2OUT &= ~(BIT4);                      // Make internal resistor pulldown

    pushes = 0;                          // Initialize global variable
    P1OUT &= ~(LEDS);                   // Clear output LEDs
}

// Read input function
char readInput(void)
{
    char local = 0;

    if (!(BIT3 & P1IN))                        // Check for button push
    {
        local = 1;
        _delay_cycles(10000);                 // Wait for bouncing to end
    }
    if ((P2IN & BIT4))                       // Check for button push
    {
        local = 2;
        _delay_cycles(10000);              // Wait for bouncing to end
    }

    return local;
}

// Count up function
void countUp(void)
{
    pushes += 1;                          // increment pushes variable
    if (pushes & BIT0){
    P1OUT |= BIT6;
    }
    else{
        P1OUT &= ~BIT6;
        }
    if (pushes & BIT1){
        P1OUT |= BIT0;
        }
    else{
        P1OUT &= ~BIT0;
    }

}

// Count down function
void countDown(void)
{
    pushes -= 1;                          // decrement pushes variable
    if (pushes & BIT0){
        P1OUT |= BIT6;
        }
        else{
            P1OUT &= ~BIT6;
            }
        if (pushes & BIT1){
            P1OUT |= BIT0;
            }
        else{
            P1OUT &= ~BIT0;
        }
}


// Main function
int main(void)
{
  char enable = 0;                        // Holds status of the S2 button
  char previousState = 0;                 // Holds previous state of S2 button
  init();                                 // One time initialization function

  while (1)
  {

      enable = readInput();               // Check the buttons

      if(enable && !previousState){
                                          // If button is pressed, allow counter to increment/decrement once
         if(readInput()==1){
             countUp();

       }

         if(readInput()==2){
             countDown();

             }
        }
      previousState = enable;


  }
}
#包括
#定义LED(位6+位0)
//保存当前状态和#的全局变量
炭推;
//初始化函数
void init(void)
{
WDTCTL=WDTPW+WDTHOLD;//停止看门狗定时器
P1DIR |=LED;//将P1.0、P1.6设置为输出方向
P1DIR&=~BIT3;//将P1.3转换为输入
P1REN |=比特3;//启用内部电阻器
P1OUT |=位3;//启用为上拉电阻器
P1DIR |=位7;//将位7设置为输出
P1OUT |=位7;//将位7设置为VCC
P2DIR&=~(BIT4);//将其作为输入
P2REN |=(位4);//打开内部电阻器
P2OUT&=~(BIT4);//使内部电阻下拉
push=0;//初始化全局变量
P1OUT&=~(LED);//清除输出LED
}
//读取输入函数
字符读取输入(无效)
{
char local=0;
如果(!(位3和P1IN))//检查按钮是否按下
{
局部=1;
_延迟周期(10000);//等待反弹结束
}
if((P2IN&BIT4))//检查按钮是否按下
{
局部=2;
_延迟周期(10000);//等待反弹结束
}
返回本地;
}
//计数函数
作废倒计时(作废)
{
pushs+=1;//增量pushs变量
if(推送和位0){
P1OUT |=位6;
}
否则{
P1OUT&=~位6;
}
if(推送和位1){
P1OUT |=位0;
}
否则{
P1OUT&=~BIT0;
}
}
//倒计时功能
作废倒计时(作废)
{
pushs-=1;//减量pushs变量
if(推送和位0){
P1OUT |=位6;
}
否则{
P1OUT&=~位6;
}
if(推送和位1){
P1OUT |=位0;
}
否则{
P1OUT&=~BIT0;
}
}
//主要功能
内部主(空)
{
char enable=0;//保存S2按钮的状态
char previousState=0;//保存S2按钮的先前状态
init();//一次性初始化函数
而(1)
{
enable=readInput();//检查按钮
如果(启用&&!先前状态){
//如果按下按钮,允许计数器增加/减少一次
如果(readInput()==1){
倒计时();
}
如果(readInput()==2){
倒计时();
}
}
前一状态=启用;
}
}

要为特定端口和pin添加中断,您需要做几件事。以P1.3为例;P2.4的中断可以以类似的方式添加

  • 设置微控制器的“中断启用”位:

    P1IE |=位3;//P1.3中断启用

    并配置中断是发生在上升沿还是下降沿:

    P1IES |=位3;//P1.3下降沿中断

  • 定义端口的中断服务例程(ISR)。看到您使用了CCS,我相信
    #pragma vector=PORT1_vector
    是一个不错的选择

  • 清除ISR中的中断标志,以避免重复触发。另一个警告是,每个端口只能有一个ISR,并且并非所有端口都支持中断。如果需要处理多个管脚上的中断,则需要通过查看中断标志状态在处理程序中对其进行解复用

  • 代码:

  • 确保状态寄存器的全局中断启用(
    GIE
    )位已设置。如果有疑问,请将其设置为,例如:

    \uuuu bis\usr\u寄存器(GIE)


  • 你没学过什么是中断,它是如何工作的吗?你不能在许多使用ISR的示例程序上对其进行建模吗?你被困在哪一步?
    #pragma vector=PORT1_VECTOR
    __interrupt void Port_1(void)
    {
      if (P1IFG & BIT3) { // P1.3 interrupt?
    
        // TODO: add the application logic here
    
        P1IFG &= ~BIT3;   // clear the P1.3 interrupt flag
    }