首页 > 解决方案 > MSP430:尝试使用按钮和 LED 闪烁来学习中断

问题描述

我是第一次学习 MSP430,并试图自学中断。
我正在尝试遵循这些示例1 2 3 4。我正在使用 MSP430FR6989 评估板并在 Code Composer Studio 中编写代码。

当我按下 P1.1 按钮(即,使用中断)时,我试图让板上的 REDLED 切换。我可以使用单独的代码使 LED 闪烁,所以我知道电路板可以工作。这是我试图开始工作的代码。

#include <msp430.h>
#include "driverlib.h"
int main(void)  //Main program

{
   WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

   P1DIR |= BIT0; // Set P1.0 to output and P1.3 to input direction
   P1OUT &= ~BIT0; // set P1.0 to Off
   P1IE |= BIT3; // P1.3 interrupt enabled
   P1IFG &= ~BIT3; // P1.3 interrupt flag cleared

   __bis_SR_register(GIE); // Enable all interrupts



   while(1) //Loop forever, we'll do our job in the interrupt routine...
   {}
}
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
    P1OUT ^= BIT0;  // Toggle P1.0
    P1IFG &= ~BIT3; // P1.3 interrupt flag cleared
}

当我按下按钮时,LED 不亮,我不知道为什么。
我会很感激任何帮助!

根据用户@CL 的要求显示工作的 LED 闪烁程序

#include <msp430.h>
#include "driverlib.h"

int main(void)
{

    WDTCTL = WDTPW + WDTHOLD; // Disables the watchdog
    PM5CTL0 &= ~LOCKLPM5;     // allows output pins to be set... turning off pullups

    P1DIR = BIT0; // Make a pin an output... RED LED
    long x = 0; // Will be used to slow down blinking

    while(1) // Continuously repeat everything below
    {
     for(x=0 ; x < 30000 ; x=x+1); // Count from 0 to 30,000 for a delay
     P1OUT = BIT0; // Turn red LED light on
     for(x=0 ; x < 30000 ; x=x+1); // Count from 0 to 30,000 for a delay
     P1OUT ^= BIT0; // Turn off the red LED light
    }
}

标签: interruptmsp430

解决方案


在 MSP430FR6989 LaunchPad 上,P1.3 未连接到按钮。请改用 P1.1。

该按钮需要一个上拉电阻,因此您必须在 P1REN 和 P1OUT 中对其进行配置。

在 P1IES 中为中断配置信号沿可能是个好主意。

您必须清除 LOCKLPM5 才能激活端口设置。

所有这些都可以在msp430fr69xx_p1_03.c 示例程序中看到。


推荐阅读