新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > msp430g2553與串口通信的驅(qū)動(dòng)程序

msp430g2553與串口通信的驅(qū)動(dòng)程序

作者: 時(shí)間:2016-11-28 來(lái)源:網(wǎng)絡(luò) 收藏
#include "uart.h"

#include <msp430g2553.h>
#include "typedef.h"

本文引用地址:http://m.butianyuan.cn/article/201611/322700.htm

rece_data uart_buf; //串口緩沖區(qū)

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

//獲取串口數(shù)據(jù)
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;
}

//保存串口數(shù)據(jù)
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();
}

//發(fā)送數(shù)據(jù)
//發(fā)送字符
void uart_send_ch(u8 ch)
{

while(!(IFG2& UCA0TXIFG)); //查詢(xún)發(fā)送是否結(jié)束
UCA0TXBUF = ch;
IFG2&=~UCA0TXIFG; //清除發(fā)送一標(biāo)志位
}

//發(fā)送字符串
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);//保存數(shù)據(jù)
}



評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉