我利用官方给的程序进行串口通信,想在串口助手上输出一段字符串,可是串口助手没有反应,麻烦大家帮忙看看,谢谢啦。
这是代码:
#include <msp430.h>
#include <math.h>
unsigned char buffer[]={"I'm MSP430!\n"};
void delay(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<100;j++);
}
void main(void)
{
unsigned int i;
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
// Setup P2.4 UCA0RXD, P2.5 UCA0TXD
P2SEL |= BIT4 | BIT5; // Set P2.4, P2.5 to non-IO
P2DIR |= BIT4 | BIT5; // Enable UCA0RXD, UCA0TXD
//Setup LFXT1
UCSCTL6 &= ~(XT1OFF); // XT1 On
UCSCTL6 |= XCAP_3; // Internal load cap
// Loop until XT1 fault flag is cleared
do
{
UCSCTL7 &= ~(XT2OFFG | XT1LFOFFG | DCOFFG);
SFRIFG1 &= ~OFIFG; // Clear fault flags
} while (SFRIFG1 & OFIFG); // Test oscillator fault flag
// Setup eUSCI_A0
UCA0CTLW0 |= UCSWRST; // **Put state machine in reset**
UCA0CTLW0 |= UCSSEL_1; // CLK = ACLK
UCA0BRW_L = 0x03; // 32kHz/9600=3.41 (see User's Guide)
UCA0BRW_H = 0x00;
UCA0MCTLW = 0x5300; // Modulation UCBRSx=0x53, UCBRFx=0
UCA0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCTXIE; // Enable USCI_A0 RX interrupt
while(1)
{
while (!(UCA0IFG & UCTXIFG)) ; // USCI_A0 TX buffer ready?
for(i=0;i<sizeof(buffer);i++)
{
UCA0TXBUF=buffer[i];
delay(20);
}
}
}
HG:
什么开发板?哪个程序?
shari zhang:
回复 HG:
你好,我是买来芯片自己做的一块板子,参考程序:
#include <msp430.h>
void main(void){ WDTCTL = WDTPW | WDTHOLD; // Stop WDT
// Setup P1.2 UCA0RXD, P1.3 UCA0TXD P1SEL |= BIT2 | BIT3; // Set P1.2, P1.3 to non-IO P1DIR |= BIT2 | BIT3; // Enable UCA0RXD, UCA0TXD
// Setup LFXT1 UCSCTL6 &= ~(XT1OFF); // XT1 On UCSCTL6 |= XCAP_3; // Internal load cap // Loop until XT1 fault flag is cleared do { UCSCTL7 &= ~(XT2OFFG | XT1LFOFFG | DCOFFG); // Clear XT2,XT1,DCO fault flags SFRIFG1 &= ~OFIFG; // Clear fault flags } while (SFRIFG1 & OFIFG); // Test oscillator fault flag
// Setup eUSCI_A0 UCA0CTLW0 |= UCSWRST; // **Put state machine in reset** UCA0CTLW0 |= UCSSEL_1; // CLK = ACLK UCA0BRW_L = 0x03; // 32kHz/9600=3.41 (see User's Guide) UCA0BRW_H = 0x00; // UCA0MCTLW = 0x5300; // Modulation UCBRSx=0x53, UCBRFx=0 UCA0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine** UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM3_bits | GIE); // Enter LPM3, interrupts enabled __no_operation(); // For debugger}
// USCI_A0 interrupt service routine#pragma vector=USCI_A0_VECTOR__interrupt void USCI_A0_ISR(void){ switch (__even_in_range(UCA0IV, 4)) { case USCI_NONE: break; // No interrupt case USCI_UART_UCRXIFG: // RXIFG while (!(UCA0IFG & UCTXIFG)) ; // USCI_A0 TX buffer ready? UCA0TXBUF = UCA0RXBUF; // TX -> RXed character break; case USCI_UART_UCTXIFG: break; // TXIFG case USCI_UART_UCSTTIFG: break; // TTIFG case USCI_UART_UCTXCPTIFG: break; // TXCPTIFG default: break; }}
shari zhang:
回复 HG:
您好,我是自己买来芯片做的板子,参考程序:
#include <msp430.h>
void main(void){ WDTCTL = WDTPW | WDTHOLD; // Stop WDT
// Setup P1.2 UCA0RXD, P1.3 UCA0TXD P1SEL |= BIT2 | BIT3; // Set P1.2, P1.3 to non-IO P1DIR |= BIT2 | BIT3; // Enable UCA0RXD, UCA0TXD
// Setup LFXT1 UCSCTL6 &= ~(XT1OFF); // XT1 On UCSCTL6 |= XCAP_3; // Internal load cap // Loop until XT1 fault flag is cleared do { UCSCTL7 &= ~(XT2OFFG | XT1LFOFFG | DCOFFG); // Clear XT2,XT1,DCO fault flags SFRIFG1 &= ~OFIFG; } while (SFRIFG1 & OFIFG);
// Setup eUSCI_A0 UCA0CTLW0 |= UCSWRST; UCA0CTLW0 |= UCSSEL_1; // CLK = ACLK UCA0BRW_L = 0x03; // 32kHz/9600=3.41 (see User's Guide) UCA0BRW_H = 0x00; // UCA0MCTLW = 0x5300; // Modulation UCBRSx=0x53, UCBRFx=0 UCA0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine** UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM3_bits | GIE); // Enter LPM3, interrupts enabled __no_operation(); // For debugger}
// USCI_A0 interrupt service routine#pragma vector=USCI_A0_VECTOR__interrupt void USCI_A0_ISR(void){ switch (__even_in_range(UCA0IV, 4)) { case USCI_NONE: break; // No interrupt case USCI_UART_UCRXIFG: // RXIFG while (!(UCA0IFG & UCTXIFG)) ; // USCI_A0 TX buffer ready? UCA0TXBUF = UCA0RXBUF; // TX -> RXed character break; case USCI_UART_UCTXIFG: break; // TXIFG case USCI_UART_UCSTTIFG: break; // TTIFG case USCI_UART_UCTXCPTIFG: break; // TXCPTIFG default: break; }}
HG:
回复 shari zhang:
这个代码是echo的,是你用调试助手发,单片机收到了,再把收到的发出去在调试中断显示出来
while (!(UCA0IFG & UCTXIFG)) ; // USCI_A0 TX buffer ready?UCA0TXBUF = UCA0RXBUF; // TX -> RXed characterbreak;
你看看是不是流程错了。
如果操作对的话,尝试在
case USCI_UART_UCTXIFG: break; // TXIFG
UCA0TXBUF = 0x55
如果还不对,检查你的时钟,管脚配置对不对,是在不行换个调试助手试试
shari zhang:
回复 HG:
谢谢您的耐心解答,我还是太业余了,先从基础看起吧。
灰小子:
回复 shari zhang:
如果是自己做的板子,建议先把RX和TX短接,用单片机发送一个字符看能不能收到。如果能正确收到,说明板子在硬件上没有问题。
shari zhang:
回复 灰小子:
您好,我将RX和TX短接后,向板子发送字符再返回时正常的,但和其他设备串口通信时返回值就会乱码,请问这是什么原因?谢谢了~
rookiecalf:
回复 shari zhang:
shari zhang
您好,我将RX和TX短接后,向板子���送字符再返回时正常的,但和其他设备串口通信时返回值就会乱码,请问这是什么原因?谢谢了~
shari zhang:
回复 rookiecalf:
您好,串口两边配置的串口通信格式具体指的是什么?波特率吗?有可能是板子上的XT1晶振有问题吗?