我正在尝试为 msp430g2452 mcu 编写一些代码。它应该循环使用中断和 switch 语句来打开或关闭 LED 的几种模式。我才刚刚开始学习编码、c 和 python :) 所以我不确定出了什么问题。
据我所知,一切正常,除了我没有捕获 p1.3 上的中断,p1.3 是我的启动板仿真/开发板上的一个按钮。如果我使用 code composer studio 5 进行调试并暂停仿真,然后更改 P1IFG.3 的位并恢复仿真,正确的事件链就会发生。所以在我看来,问题在于捕捉那个开关边缘。
如果有人对这里发生的事情有任何想法,我将非常感激。 有兴趣的人可以使用mcu 数据表。我用来遵循的代码:
#include <msp430g2452.h>
#define button BIT3 // set button to p1.3
#define grnLED BIT6 // set green led to p1.6
#define redLED BIT0 // set red led to p1.0
volatile unsigned int stateID = 0; // declare and clear stateID
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= (grnLED + redLED); // set bit0 and bit6 to output, led's
P1OUT &= ~(grnLED + redLED); // both led's off
P1IE |= button; // enable interrupts on button
P1REN |= button; // enable pull-up resistor on button
P1IFG &= ~button; // flag for button off
_EINT(); // enable interrupts
// loop forever
while (1){
switch(stateID){ //switch based on state of stateID
case 1:
{
P1OUT = grnLED; //turn green led on
break;
}
case 2:
{
P1OUT = redLED; //turn red led on
break;
}
case 3:
{
P1OUT = (grnLED + redLED); // turn both led's on
break;
}
case 4:
{
stateID = 0; // reset stateID
break;
}
default:
{
P1OUT &= ~(grnLED + redLED); // turn both led's off again
break;
}
}
}
}
// interrupt handler
#pragma vector=PORT1_VECTOR
__interrupt void Port_1 (void){
_DINT(); // disable interrupts to avoid nesting
stateID=(stateID+1); // increment stateID
P1IFG &= ~button; // clear interrupt flag on button
_EINT(); // re-enable interrupts
}
这是固定代码
#include <msp430g2553.h>
/* written by: rob j loranger
* date: april 2013
*
* this program should emulate a typical headlamp operation,
* from a power off state one button press should turn on the lamp in mode one.
* each successive press should cycle through modes until it returns to an off state
*
* main.c
*/
#define button BIT3 // set button to p1.3
#define grnLED BIT6 // set green led to p1.6
#define redLED BIT0 // set red led to p1.0
volatile unsigned int stateID = 0; // declare and clear stateID
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= (grnLED + redLED); // set bit0 and bit6 to output, led's
P1OUT &= ~(grnLED + redLED); // both led's off
P1OUT |= button; // set button to output
/* i set button to an output and i set the edge high
* for the interrupt as well.
*/
P1IE |= button; // enable interrupts on button
P1IES |= button; // set edge high
P1REN |= button; // enable pull-up resistor on button
P1IFG &= ~button; // flag for button off
_EINT(); // enable interrupts
/* i then made some changes to my loop to prevent the output
* state on button from being changed
*/
// loop forever
while (1){
switch(stateID){ //switch based on state of stateID
case 1:
{
P1OUT |= grnLED; //turn green led on
break;
}
case 2:
{
P1OUT &= ~grnLED; // turn green led off
P1OUT |= redLED; //turn red led on
break;
}
case 3:
{
P1OUT |= grnLED; // turn green led on, both are on now
break;
}
case 4:
{
stateID = 0; // reset stateID
break;
}
default:
{
P1OUT &= ~(grnLED + redLED); // turn both led's off again
break;
}
}
}
}
// interrupt handler
#pragma vector=PORT1_VECTOR
__interrupt void Port_1 (void){
_DINT(); // disable interrupts to avoid nesting
stateID=(stateID+1); // increment stateID
P1IFG &= ~button; // clear interrupt flag on button
_EINT(); // re-enable interrupts
}