您的位置 首页 被动

msp430g2553与串口通讯的驱动程序

includeuarthincludemsp430g2553h>includetypedefhrece_datauart_buf;串口缓冲区voidinit_uart_buf(void){uar

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

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/beidong/255939.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部