用MSP430F149單片機發送數據到PC程序
- /*****************************************************
- 程序功能:MCU不停向PC機發送數據,在屏幕上顯示0~127對應
- 的ASCII字符
- ------------------------------------------------------
- 通信格式:N.8.1, 2400
- ------------------------------------------------------
- 測試說明:打開串口調試助手,正確設置通信格式,觀察屏幕
- ******************************************************/
- #include <msp430x14x.h>
- #define uchar unsigned char
- #define uint unsigned int
- void Delays(void);
- void PutString(uchar *ptr);
- /********************主函數**********************/
- 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; // 關狗
- 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();
- PutString(tishi);
- while(1)
- {
- while (!(IFG1 & UTXIFG0));
- TXBUF0 = value++;
- value &= 0x7f; // 保證value的數值小于128
- while (!(IFG1 & UTXIFG0));
- TXBUF0 = '\n';
- 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--);
- }
- }
復制代碼
|