记录一下,做了那么久,MSP430,CSS版本串口发送和接收

#include <msp430.h> 
#include "FG12864A.h"
#include "UCS.h"
volatile uint8_t rx_complete = 0;

unsigned char rx_buff[100];
unsigned char tx_buff[]={"nihao"};
int main(void) {
    void send_buf(unsigned char *ptr);


    P4SEL = BIT4+BIT5;
    WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer

            UCA1CTL1 |= UCSWRST;                      // **Put state machine in reset**
             UCA1CTL1 |= UCSSEL_1;                     // CLK = ACLK
             UCA1BR0 = 0x03;                           // 32kHz/9600=3.41 (see User's Guide)
             UCA1BR1 = 0x00;                           //9600
             UCA1MCTL = UCBRS_3+UCBRF_0;               // Modulation UCBRSx=3, UCBRFx=0
             UCA1CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
             UCA1IE |= UCRXIE;
            P1DIR |=BIT0;
            P4DIR |= BIT7;
            send_buf(tx_buff);


            __bis_SR_register(LPM3_bits + GIE);

       }
       #pragma vector = USCI_A1_VECTOR
       __interrupt void USCI_A1_ISR(void)
       {    void send_buf(unsigned char *ptr);

           char ch;
           int i=0;

         if(rx_complete==0)
           {while((ch=UCA1RXBUF)!='\r')
           {
               rx_buff[i++]=ch;
               while(!(UCA1IFG & UCRXIFG));
           }
         rx_buff[i]='\0';
         rx_complete=1;
           }
         else
             { send_buf(rx_buff);
               rx_complete=0;

                                    }
         
       }


void send_buf(unsigned char *ptr)
{
while( *ptr !='\0')
{
    while(!(UCA1IFG & UCTXIFG));
    UCA1TXBUF = *ptr;
    ptr++;
    delay_ms(10);
}
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,下面是一个MSP430F5529的串口通信示例程序,其中包含了接收发送功能: ```c #include <msp430.h> void UART_init() { P3SEL |= BIT3 + BIT4; // 将P3.3和P3.4设置为UART模式 UCA0CTL1 |= UCSWRST; // 复位UART模块 UCA0CTL1 |= UCSSEL_2; // 选择SMCLK作为时钟源 UCA0BR0 = 6; // 设置波特率为9600 UCA0BR1 = 0; UCA0MCTL |= UCBRS_0 + UCBRF_13 + UCOS16; UCA0CTL1 &= ~UCSWRST; // 解除UART模块复位 UCA0IE |= UCRXIE; // 打开接收中断 } void UART_sendChar(char c) { while (!(UCA0IFG & UCTXIFG)); // 等待发送缓冲区为空 UCA0TXBUF = c; // 发送字符 } char UART_receiveChar() { while (!(UCA0IFG & UCRXIFG)); // 等待接收缓冲区有数据 return UCA0RXBUF; // 返回接收到的字符 } void main(void) { WDTCTL = WDTPW | WDTHOLD; // 停止看门狗定时器 UART_init(); // 初始化UART模块 __enable_interrupt(); // 全局中断使能 while (1) { // 接收发送数据 char receivedChar = UART_receiveChar(); UART_sendChar(receivedChar); } } #pragma vector=USCI_A0_VECTOR __interrupt void USCI_A0_ISR(void) { switch (__even_in_range(UCA0IV, 4)) { case 0: break; // Vector 0 - 无中断 case 2: // Vector 2 - 接收中断 // 处理接收到的数据 break; case 4: break; // Vector 4 - 发送中断 default: break; } } ``` 这个示例程序在MSP430F5529上初始化了UART模块,并设置波特率为9600。在`main`函数中,通过调用`UART_receiveChar`函数来接收数据,并使用`UART_sendChar`函数发送数据。你可以在`USCI_A0_ISR`函数中添加对接收数据的处理逻辑。 请注意,此示例程序仅供参考,你可能需要根据你的具体要求进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值