• MSP430:串口输出


    初始化

     1 void Uart_Init(void)
     2 {
     3       BCSCTL1 = CALBC1_1MHZ;                    // Set DCO
     4       DCOCTL = CALDCO_1MHZ;
     5       P1SEL = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
     6       P1SEL2 = BIT1 + BIT2 ;                    // P1.1 = RXD, P1.2=TXD
     7       UCA0CTL1 |= UCSSEL_2;                     // SMCLK
     8       UCA0BR0 = 104;                            // 1MHz 9600
     9       UCA0BR1 = 0;                              // 1MHz 9600
    10       UCA0MCTL = UCBRS0;                        // Modulation UCBRSx = 1
    11       UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
    12       IE2 |= UCA0RXIE;                          // Enable USCI_A0 RX interrupt
    13 }
    Uart_Init
    1 //  Echo back RXed character, confirm TX buffer is ready first
    2 #pragma vector=USCIAB0RX_VECTOR
    3 __interrupt void USCI0RX_ISR(void)
    4 {
    5   while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
    6   UCA0TXBUF = UCA0RXBUF;                   // TX -> RXed character
    7 }
    接收数据
     1 //发送数据
     2 //发送字符
     3 void uart_send_ch(unsigned char ch)
     4 {
     5 
     6     while(!(IFG2& UCA0TXIFG)); //查询发送是否结束
     7     UCA0TXBUF = ch;
     8     IFG2&=~UCA0TXIFG; //清除发送一标志位
     9 }
    10 
    11 //发送字符串
    12 void uart_send_str(char *str)
    13 {
    14       for( ; *str ; )
    15       {
    16           uart_send_ch((unsigned char)*str);
    17           str++;
    18       }
    19 }
    发送

     在用uart_send_str()发送一个数组合紧接着发送回车会出现乱码,大约九个字符出现,在中间delay一下解决了

     char a[4];  

     uart_send_str(a);

     __delay_cycles(5);  

    uart_send_huiche();

    void uart_send_huiche(void)
    {
        uart_send_ch(0x0d);
        uart_send_ch(0x0a);
    }

  • 相关阅读:
    每天进步一小点
    C# 类
    XML JavaScript
    基础XML
    多态,重载,重写
    数据结构
    sql server规范
    .net core 使用TimeZoneInfo类的时间与时间戳转换
    git 重命名文件与文件夹
    IDEA spring boot 开启热加载
  • 原文地址:https://www.cnblogs.com/wwjdwy/p/3185684.html
Copyright © 2020-2023  润新知