想請問一下,我在網路上看到這段example code , 大家都說可以執行,且可以做UART收發的動作,但是我的不管怎樣就是沒反應~
代碼如下:
- #include "msp430g2553.h"
-
- #define TXLED BIT0
- #define RXLED BIT6
- #define TXD BIT2
- #define RXD BIT1
-
- const char string[] = { "Hello World
" };
- unsigned int i; //Counter
-
- int main(void)
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
- DCOCTL = 0; // Select lowest DCOx and MODx settings
- BCSCTL1 = CALBC1_1MHZ; // Set DCO
- DCOCTL = CALDCO_1MHZ;
- P2DIR |= 0xFF; // All P2.x outputs
- P2OUT &= 0x00; // All P2.x reset
- P1SEL |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD
- P1SEL2 |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD
- P1DIR |= RXLED + TXLED;
- P1OUT &= 0x00;
- UCA0CTL1 |= UCSSEL_2; // SMCLK
- UCA0BR0 = 0x08; // 1MHz 115200
- UCA0BR1 = 0x00; // 1MHz 115200
- UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5
- UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
- UC0IE |= UCA0RXIE; // Enable USCI_A0 RX interrupt
- __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ int until Byte RXed
- while (1)
- { }
- }
-
- #pragma vector=USCIAB0TX_VECTOR
- __interrupt void USCI0TX_ISR(void)
- {
- P1OUT |= TXLED;
- UCA0TXBUF = string[i++]; // TX next character
- if (i == sizeof string - 1) // TX over?
- UC0IE &= ~UCA0TXIE; // Disable USCI_A0 TX interrupt
- P1OUT &= ~TXLED; }
-
- #pragma vector=USCIAB0RX_VECTOR
- __interrupt void USCI0RX_ISR(void)
- {
- P1OUT |= RXLED;
- if (UCA0RXBUF == 'a') // 'a' received?
- {
- i = 0;
- UC0IE |= UCA0TXIE; // Enable USCI_A0 TX interrupt
- UCA0TXBUF = string[i++];
- }
- P1OUT &= ~RXLED;
- }
复制代码原文網址在此
http://www.embeddedrelated.com/showarticle/420.php
感謝各位幫忙
此帖出自
小平头技术问答
可以用仿真器仿真啊,遇到问题的最好解决方法就是仿真。
#include "msp430g2553.h"
#define TXLED BIT0
#define RXLED BIT6
#define TXD BIT2
#define RXD BIT1
const char string[] = { "Hello World " };
unsigned int i; //Counter
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P2DIR |= 0xFF; // All P2.x outputs
P2OUT &= 0x00; // All P2.x reset
P1SEL |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD
P1SEL2 |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD
P1DIR |= RXLED + TXLED;
P1OUT &= 0x00;
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 0x08; // 1MHz 115200
UCA0BR1 = 0x00; // 1MHz 115200
UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UC0IE |= UCA0RXIE; // Enable USCI_A0 RX interrupt
UCA0TXBUF ='a';
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ int until Byte RXed
while (1)
{ }
}
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void)
{
P1OUT |= TXLED;
UCA0TXBUF = string[i++]; // TX next character
if (i == sizeof string - 1) // TX over?
UC0IE &= ~UCA0TXIE; // Disable USCI_A0 TX interrupt
__delay_cycles (100000);
P1OUT &= ~TXLED; }
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
P1OUT |= RXLED;
if (UCA0RXBUF == 'a') // 'a' received?
{
i = 0;
UC0IE |= UCA0TXIE; // Enable USCI_A0 TX interrupt
UCA0TXBUF = string[i++];
}
__delay_cycles (100000);
P1OUT &= ~RXLED;
}
1.在进入低功耗之前手动发送了‘a'
即 UCA0TXBUF ='a';
2.3.熄灭LED之前加入了延时
__delay_cycles (100000);
一周热门 更多>