標(biāo)題:
MSP430F149發(fā)送數(shù)據(jù)到PC
[打印本頁(yè)]
作者:
北京北2016
時(shí)間:
2016-5-7 14:47
標(biāo)題:
MSP430F149發(fā)送數(shù)據(jù)到PC
用MSP430F149單片機(jī)發(fā)送數(shù)據(jù)到PC程序
/*****************************************************
程序功能:MCU不停向PC機(jī)發(fā)送數(shù)據(jù),在屏幕上顯示0~127對(duì)應(yīng)
的ASCII字符
------------------------------------------------------
通信格式:N.8.1, 2400
------------------------------------------------------
測(cè)試說明:打開串口調(diào)試助手,正確設(shè)置通信格式,觀察屏幕
******************************************************/
#include <msp430x14x.h>
#define uchar unsigned char
#define uint unsigned int
void Delays(void);
void PutString(uchar *ptr);
/********************主函數(shù)**********************/
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; // 關(guān)狗
P3SEL |= 0x30; // P3.4,5選擇為UART收發(fā)端口
ME1 |= UTXE0 + URXE0; // 使能USART0收發(fā)
UCTL0 |= CHAR; // 8-bit character
UTCTL0 |= SSEL0; // UCLK = ACLK
UBR00 = 0x0D; // 32k/2400 - 13.65
UBR10 = 0x00; //
UMCTL0 = 0x6B; // Modulation
UCTL0 &= ~SWRST; // 初始化UART0狀態(tài)機(jī)
IE1 |= URXIE0; // 使能接收中斷
_EINT();
PutString(tishi);
while(1)
{
while (!(IFG1 & UTXIFG0));
TXBUF0 = value++;
value &= 0x7f; // 保證value的數(shù)值小于128
while (!(IFG1 & UTXIFG0));
TXBUF0 = '\n';
Delays();
}
}
/*******************************************
函數(shù)名稱:PutSting
功 能:向PC機(jī)發(fā)送字符串
參 數(shù):無
返回值 :無
********************************************/
void PutString(uchar *ptr)
{
while(*ptr != '\0')
{
while (!(IFG1 & UTXIFG0)); // TX緩存空閑?
TXBUF0 = *ptr++; // 發(fā)送數(shù)據(jù)
}
while (!(IFG1 & UTXIFG0));
TXBUF0 = '\n';
}
/*******************************************
函數(shù)名稱:Delays
功 能:延時(shí)一會(huì)
參 數(shù):無
返回值 :無
********************************************/
void Delays(void)
{
uchar i=20;
uint j;
while(i--)
{
j=2000;
while(j--);
}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1