微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > msp430g2553与串口通信的驱动程序

msp430g2553与串口通信的驱动程序

时间:11-28 来源:互联网 点击:
#include "uart.h"

#include msp430g2553.h>
#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);//保存数据
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top