/*****************************************************
程序功能:串口发送程序
1.发送字符
2.发送字符串
------------------------------------------------------
通信格式:N.8.1, 2400
------------------------------------------------------
******************************************************/
#include <msp430x14x.h>
typedef unsigned char uchar;
typedef unsigned int uint;
void Delays(void);
void Send_string(uchar *ptr);
void Send_char(uchar data);
/********************主函数**********************/
void main(void)
{
uchar *tishi = " MCU sends 0~127 to PC and the";
//用于数组
uchar value = 0xaa;
//用于字符
WDTCTL = WDTPW + WDTHOLD; // 关狗
P6DIR |= BIT2;P6OUT |= BIT2; // 关电平转换
P3SEL |= 0x30; // P3.4,5选择为UART收发端口
ME1 |= UTXE0 + URXE0; // 使能USART0收发
UCTL0 |= CHAR; // 8-bit character
UTCTL0 |= SSEL0; // UCLK = ACLK
UBR00 = 0x0D; // 32k/2400 - 13.65
UBR10 = 0x00; //
UMCTL0 = 0x6B; // Modulation
UCTL0 &= ~SWRST; // 初始化UART0状态机
IE1 |= URXIE0; // 使能接收中断
_EINT();
Send_string(tishi);
while(1)
{
Send_char(value);
//Send_char("");
Delays();
}
}
/*******************************************
函数名称:Send_string
功 能:向PC机发送字符串
参 数:无
返回值 :无
********************************************/
void Send_string(uchar *ptr)
{
while(*ptr != '\0')
{
while (!(IFG1 & UTXIFG0)); // TX缓存空闲?
TXBUF0 = *ptr++; // 发送数据
}
//while (!(IFG1 & UTXIFG0)); //换行
// TXBUF0 = '\n';
}
/*******************************************
函数名称:Delays
功 能:延时一会
参 数:无
返回值 :无
********************************************/
void Delays(void)
{
uchar i=20;
uint j;
while(i--)
{
j=2000;
while(j--);
}
}
/*******************************************
函数名称:Send_char
功 能:向PC机发送字符串
参 数:无
返回值 :无
********************************************/
void Send_char(uchar data)
{
while (!(IFG1 & UTXIFG0));
TXBUF0 = data;
}