找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3719|回复: 0
收起左侧

MSP430F149发送数据到PC

[复制链接]
ID:119055 发表于 2016-5-7 14:47 | 显示全部楼层 |阅读模式
用MSP430F149单片机发送数据到PC程序

  1. /*****************************************************
  2. 程序功能:MCU不停向PC机发送数据,在屏幕上显示0~127对应
  3.           的ASCII字符
  4. ------------------------------------------------------
  5. 通信格式:N.8.1, 2400
  6. ------------------------------------------------------
  7. 测试说明:打开串口调试助手,正确设置通信格式,观察屏幕
  8. ******************************************************/

  9. #include  <msp430x14x.h>
  10. #define uchar unsigned char
  11. #define uint  unsigned int
  12. void Delays(void);
  13. void PutString(uchar *ptr);

  14. /********************主函数**********************/
  15. void main(void)
  16. {
  17.     uchar *tishi = " MCU sends 0~127 to PC and the\
  18.                     \n screen will display their corresponding\
  19.                     \n ASCII code as follows:";
  20.     uchar value = 0;
  21.   
  22.     WDTCTL = WDTPW + WDTHOLD;                 // 关狗
  23.     P3SEL |= 0x30;                            // P3.4,5选择为UART收发端口
  24.     ME1 |= UTXE0 + URXE0;                     // 使能USART0收发
  25.     UCTL0 |= CHAR;                            // 8-bit character
  26.     UTCTL0 |= SSEL0;                          // UCLK = ACLK
  27.     UBR00 = 0x0D;                             // 32k/2400 - 13.65
  28.     UBR10 = 0x00;                             //
  29.     UMCTL0 = 0x6B;                            // Modulation
  30.     UCTL0 &= ~SWRST;                          // 初始化UART0状态机
  31.     IE1 |= URXIE0;                            // 使能接收中断
  32.     _EINT();
  33.     PutString(tishi);
  34.     while(1)
  35.     {     
  36.         while (!(IFG1 & UTXIFG0));
  37.         TXBUF0 = value++;
  38.         value &= 0x7f;                        // 保证value的数值小于128
  39.         while (!(IFG1 & UTXIFG0));
  40.         TXBUF0 = '\n';
  41.         Delays();
  42.     }
  43. }
  44. /*******************************************
  45. 函数名称:PutSting
  46. 功    能:向PC机发送字符串
  47. 参    数:无
  48. 返回值  :无
  49. ********************************************/
  50. void PutString(uchar *ptr)
  51. {
  52.       while(*ptr != '\0')
  53.       {
  54.             while (!(IFG1 & UTXIFG0));                // TX缓存空闲?
  55.             TXBUF0 = *ptr++;                       // 发送数据
  56.       }
  57.       while (!(IFG1 & UTXIFG0));
  58.       TXBUF0 = '\n';
  59. }
  60. /*******************************************
  61. 函数名称:Delays
  62. 功    能:延时一会
  63. 参    数:无
  64. 返回值  :无
  65. ********************************************/
  66. void Delays(void)
  67. {
  68.     uchar i=20;
  69.     uint j;

  70.     while(i--)
  71.     {
  72.             j=2000;
  73.             while(j--);
  74.     }
  75. }
复制代码



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表