单片机
返回首页

msp430g2553与串口通信的驱动程序

2015-08-25 来源:eefocus

#include 'uart.h'

#include
#include 'typedef.h'

rece_data uart_buf; //串口缓冲区

void init_uart_buf(void)
{
    uart_buf.head = 0;
    uart_buf.tail = uart_buf.head;
}

//获取串口数据
u8 get_uart_data(u8* data)
{
    if(uart_buf.tail == uart_buf.head)
    {
        return 0;
    }
    *data = uart_buf.buf[uart_buf.head];
    uart_buf.head = (uart_buf.head + 1) % BUF_SIZE;
    return 1;
}

//保存串口数据
void save_uart_data(u8 data)
{
    uart_buf.buf[uart_buf.tail] = data;
    uart_buf.tail = (uart_buf.tail + 1) % BUF_SIZE;
}

//串口初始化
void uart_Init(void)
{
    WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
    BCSCTL1 = CALBC1_1MHZ;                    // Set DCO
    DCOCTL = CALDCO_1MHZ;                                                                                                                                                                                                            
    P1SEL = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
    P1SEL2 = BIT1 + BIT2 ;                     // P1.1 = RXD, P1.2=TXD
   
    UCA0CTL1 |= UCSSEL_2;                     // SMCLK
    UCA0BR0 = 104;                            // 1MHz 9600
    UCA0BR1 = 0;                              // 1MHz 9600
    UCA0MCTL = UCBRS0;                        // Modulation UCBRSx = 1
    UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
    IE2 |= UCA0RXIE;                          // Enable USCI_A0 RX interrupt
    _EINT();                                   //Enable interrupt
    init_uart_buf(); 
}

//发送数据
//发送字符
void uart_send_ch(u8 ch)
{
  
    while(!(IFG2& UCA0TXIFG)); //查询发送是否结束
    UCA0TXBUF = ch;
    IFG2&=~UCA0TXIFG; //清除发送一标志位
}

//发送字符串
void uart_send_str(char *str)
{
      for( ; *str ; )
      {
          uart_send_ch((u8)*str);
          str++;
      }
}

//  Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
    u8 data;
    while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
 //   UCA0TXBUF = UCA0RXBUF;                    // TX -> RXed character
    data = UCA0RXBUF;
    save_uart_data(data);                     //保存数据
}

进入单片机查看更多内容>>
相关视频
  • RISC-V嵌入式系统开发

  • SOC系统级芯片设计实验

  • 云龙51单片机实训视频教程(王云,字幕版)

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

精选电路图
  • 基于IC555的可变PWM振荡器电路

  • 优化电路板布局的简单方法

  • 如何使用LED驱动器LM3915制作振动计

  • 分享一个电网倾角计电路

  • 电谐波图形均衡器示意图

  • 一种构建12V和230V双直流电源的简单方法

    相关电子头条文章