最近一直被这个问题困扰,串口发送不了数据,拿到示波器上看,串口输出是正弦波,希望大家帮忙看看是程序有问题吗?
- #include <msp430f149.h>
- unsigned char shuju;
- #define DGUS_HEAD 0x5AA5
- int LocalColorNum = 0;
- void TS_SendChar(unsigned Chr)
- {
- shuju = Chr;
- _BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/interrupt
- }
- void SendByte(int cont)
- {
- //WaitForTxBufEmpty
- TS_SendChar((unsigned char)cont);
- }
- void SendWord(int cont)
- {
- SendByte(cont >> 8);
- SendByte(cont & 0x00FF);
- }
- void EndSend(void)
- {
- ;
- }
- void main(void)
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
- P3SEL = 0x30; // P3.4,5 = USART1 TXD/RXD
- U0CTL |= SWRST;
- ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD
- U0CTL |= CHAR;
- U0TCTL |= SSEL1; //ACLK
- U0BR1 = 0x03; // 32k/9600 - 3.41
- U0BR0 = 0x00; //
- U0MCTL=0x4A;
- U0CTL &= ~SWRST; // Initialize USART state machine
- IE1 |= UTXIE0; // Enable USART0 RX interrupt
- _BIS_SR( LPM3_bits +GIE); // Enter LPM3 w/ interrupt
- while(1)
- {
- SendWord(DGUS_HEAD);
- SendWord(0x00FF);
- SendWord(0x0582);
- SendWord(LocalColorNum);
- EndSend();
- }
- }
- #pragma vector=USART0TX_VECTOR
- __interrupt void usart0_tx (void)
- {
- // while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
- U0TXBUF = shuju;
- IFG1 &= ~UTXIFG0; // Clear inital flag on POR
- }
复制代码
- 只向电脑发送字符串
- #include "msp430x14x.h"
- #define uchar unsigned char
- uchar DATA[]="hello world my name!";
- void main()
- {
- // Stop watchdog timer to prevent time out reset
- uchar i,j;
- WDTCTL = WDTPW + WDTHOLD;
- //使用外部高频晶体振荡器
- BCSCTL1&=~XT2OFF;
- do{
- IFG1&=~OFIFG;
- for(j=0XFF;j>0;j--);
- }while((IFG1&OFIFG));
- BCSCTL2|=SELM_2;
- P3DIR|=BIT4+BIT7;
- P3SEL|=BIT4+BIT7;
-
- ME1 |= UTXE0 + URXE0; // 使能USART0收发
- UCTL0 |= CHAR; // 8-bit 数据,一位停止位
- UTCTL0 |= SSEL0; // 选择时钟,UCLK = ACLK,32768
- UBR00 = 0x03; // 32k/9600
- UBR10 = 0x00; //
- UMCTL0 = 0x4a; // Modulation
- UCTL0 &= ~SWRST; // 初始化UART0状态机,一般要设置好串口之后才复位
- for(i=0;i<=20;i++)
- {
- TXBUF0=DATA[i];
- while((UTCTL0&0X01)==0);//等待数据发送完毕
- }
- while(1);
- }
复制代码一周热门 更多>