请问MSP430的IO口设置为外部中断引脚时,必须用中断子程序实现,而用主程序中查询PxIFGx的方法处理外部中断事件是无法实现的吗,每次调试都不对,程序如下:
#include <msp430x24x.h>
char data;
#define LED_ON P2OUT &=~0x04;
#define LED_OFF P2OUT |= 0x04;
void delay(unsigned long int dl)
{
while(dl--);
}
void clock_init()
{
WDTCTL = WDTPW+WDTHOLD; // Stop WDT
//set CPU clock
if (CALBC1_16MHZ ==0xFF || CALDCO_16MHZ == 0xFF)
{
while(1);
}
BCSCTL1 = CALBC1_16MHZ; // Set DCO to 16MHz
DCOCTL = CALDCO_16MHZ;
//set UART clock
BCSCTL1 &= ~XT2OFF; // Activate XT2 high freq xtal
BCSCTL3 |= XT2S_2; // 16MHz crystal or resonator
BCSCTL2 |= SELS; // SMCLK = XT2 HF XTAL (safe)
}
void IO_init()
{
P2DIR |= 0x20; // Set P2.5(RESET) to output direction
P2DIR |= 0x40; // Set P2.6(START) to output direction
P3DIR |= 0x01; // Set P3.0(CS) to output direction
P2OUT &=~0x20;
delay(1000);
P2OUT |= 0x20; //RESET=1
P2OUT |= 0x40; //START=1
P3OUT &=~0x01; //CS=0
P2DIR |= 0x04; //Set P2.2 to output direction,this is LED
}
void UARTA1_init()
{
P3SEL |= 0x0C0; // Use P3.6/P3.7 for USCI_A1
UCA1CTL1 |= UCSSEL_2; // 01000001,ACLK,32.768k
UCA1BR0 = 139; // 16000000/115200=139;
UCA1BR1 = 0;
//UCA1CTL0=0x00; //00000000,8bits,no parity,1 stop bit,UART
UCA1CTL1 &= ~UCSWRST; // Resume operation
}
void UARTA1_send(unsigned char TxByte)
{
while (!(UC1IFG & UCA1TXIFG)); // USCI_A1 TX buffer ready?
UCA1TXBUF = TxByte; // TX and Rx character
}
void SPIB0_init()
{
P3SEL |= 0x0E; // P3.3,2 USCI_B0 option select
UCB0CTL0 |= UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI mstr, MSB 1st
UCB0CTL1 |= UCSSEL_2; // SMCLK
UCB0BR0 = 0x08;
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
void ADS1246_init()
{
delay(1000000);
UCB0TXBUF=0x43; //写SYS0寄存器
delay(1000000);
UCB0TXBUF=0x00; //待设置的寄存器数量为1
delay(1000000);
UCB0TXBUF=0x08; //内部增益为1,采样率为1k
delay(1000000);
}
void P27interrupt_init()
{
P2IE |= 0x80; // P2.7 interrupt enabled
P2IES |= 0x80; // P2.7 Hi/lo edge
P2IFG &= ~0x80; // P2.7 IFG cleared
__bis_SR_register(GIE); //interrupt enable
}
void main(void)
{
clock_init();
IO_init();
UARTA1_init();
SPIB0_init();
ADS1246_init();
P27interrupt_init();
while(1)
{
while(P2IFG&0x80)
{
UCB0TXBUF = 0xFF;
while (!(IFG2 & UCB0RXIFG)); // RXBUF ready?, data received??
data = UCB0RXBUF;
UARTA1_send(data); //MSB
UCB0TXBUF = 0xFF;
while (!(IFG2 & UCB0RXIFG)); // RXBUF ready?, data received??
data = UCB0RXBUF;
UARTA1_send(data); //Mid-Byte
UCB0TXBUF = 0xFF;
while (!(IFG2 & UCB0RXIFG)); // RXBUF ready?, data received??
data = UCB0RXBUF;
UARTA1_send(data); //LSB
P2IFG &= ~0x80; // P2.7 IFG cleared
}
}
} |