//通行格式:N.8.1,2400
#include<msp430g2553.h>
typedef unsigned char uchar;
typedef unsigned int uint;
void Delays(void);
void PutString(uchar *ptr);
void initUART(void);
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;
WDTCTL = WDTPW + WDTHOLD;
initUART();
_EINT();
PutString(tishi);
while(1) //MCU向PC发信息
{
while(!(IFG2&UCA0TXIFG)); //TX 缓存空闲?
UCA0TXBUF = value; // 发送数据
value&=0x7f; //使value的值不要超过128
IFG2&=~UCA0TXIFG; //标志寄存器清零
while(!(IFG2&UCA0TXIFG));
UCA0TXBUF ='\n'; //换行
Delays();
}
}
void initUART(void) //各种初始化
{
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ; //时钟的设置 (可以不用设置,要设置不能设置的太大)
P1SEL |=0x06; // P1.1,2选择为UART收发端口
P1SEL2 |=0x06; //将P1.1 P1.2设为第二功能
UCA0BR0 = 0x6D; //波特率9600
UCA0BR1 = 0x00;
UCA0MCTL =0; // 选择模式
UCA0CTL1|=UCSSEL_2+UCSWRST; //SMCLK时钟
IE2|= UCA0RXIE+UCA0TXIE; // 使能接收中断 在这里可以不用
UCA0CTL1&= ~UCSWRST; // 初始化
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void get()
{
uchar value;
value=UCA0RXBUF;
IFG2&=~UCA0RXIFG;
}
void PutString(uchar *ptr)
{
while(*ptr!='\0')
{
while(!(IFG2&UCA0TXIFG)); //TX 缓存空闲?
UCA0TXBUF = *ptr++; // 发送数据
IFG2&=~UCA0TXIFG; //标志寄存器清零
}
while(!(IFG2&UCA0TXIFG));
UCA0TXBUF = '\n';
}
void Delays(void) //延时函数
{
uint i;
for(i=60000;i>0;i--);
}
|