/*****************************************************
程序功能:MCU不停向PC机发送数据,在屏幕上显示0~127对应
的ASCII字符
------------------------------------------------------
通信格式:N.8.1, 9600
------------------------------------------------------
测试说明:打开串口调试助手,正确设置通信格式,观察屏幕
******************************************************/
#include <msp430x14x.h>
typedef unsigned char uchar;
typedef unsigned int uint;
void Delays(void);
void PutString(uchar *ptr);
///////////////////////////////////////////
void initsys(void)
{
BCSCTL1 &= ~XT2OFF;
uchar i;
do
{
IFG1 &= ~OFIFG;
for(i=0xee;i>0;i--)
{
;
}
}
while((IFG1&OFIFG)!=0);
BCSCTL2 |= SELM_2 + SELS;
}
void set()
{
U0CTL |= SWRST; //开启设置
U0TCTL = SSEL1; //选择 SMCLK=6M 波特率=9600
/* 6M / 9600 = 625 625 = 0x0271 0.000 * 8= 0*/
/* 32768/9600=3.4133 0.4133*8=4*/
U0BR0 = 0x41;
U0BR1 = 0x03;
U0MCTL =0x00; //00000000
U0CTL |= CHAR; //长度8位
U0CTL &= ~SWRST;
}
/********************主函数**********************/
void main(void)
{
uchar *tishi = " MCU sends 0~127 to PC and the\
\n screen will display their corresponding\
\n ASCII code as follows:";
uchar value = 0;
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
initsys();
P3DIR |= 0x10;
P4DIR |= 0xff;
P4OUT = 0x00;
P3SEL |= 0x30;
set();
ME1 |= UTXE0 + URXE0;
IE1 |= URXIE0;
_EINT();
////////////////////////////////////////////////
PutString(tishi);
while(1)
{
while (!(IFG1 & UTXIFG0));
TXBUF0 = value++;
value &= 0x7f; // 保证value的数值小于128
while (!(IFG1 & UTXIFG0));
TXBUF0 = '\n';
Delays();
Delays();
Delays();
Delays();
Delays();
Delays();
Delays();
}
}
/*******************************************
函数名称:PutSting
功 能:向PC机发送字符串
参 数:无
返回值 :无
********************************************/
void PutString(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--);
}
}