[MCU] MSP430F149单片机实现uart数据接收中断

[复制链接]
 楼主| 八层楼 发表于 2020-8-5 17:19 | 显示全部楼层 |阅读模式
/*****************************************************
程序功能:MCU不停向PC机发送数据,在屏幕上显示0~127对应
          的ASCII字符
------------------------------------------------------
通信格式:N.8.1, 9600
------------------------------------------------------
测试说明:打开串口调试精灵,正确设置通信格式,观察屏幕
******************************************************/


#include  <msp430x14x.h>


typedef unsigned char uchar;
typedef unsigned int  uint;


extern void Delays(void);
extern uchar GetChar(void);
extern void PutChar(uchar c);
extern void PutString(uchar *ptr);
extern void InitUART(void);


static uchar pstr = 'A';


/********************主函数**********************/
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;
    //uchar c;
    int i = 10;
    int j = 100;
   
    WDTCTL = WDTPW + WDTHOLD;                 // 关狗
   
    InitUART();
    _EINT();                                  //打开全局中断
   
    while(i--)
    {   
        while (!(IFG1 & UTXIFG0));
        TXBUF0 = value++;
        value &= 0x7f;                        // 保证value的数值小于128
        while (!(IFG1 & UTXIFG0));
        TXBUF0 = '\n';
        Delays();
    }
   
    PutString(tishi);


    while(j--)
    {
        PutChar(pstr);   
        //if(IFG1 & URXIFG0)      //如果收到字符
          //c = RXBUF0;
        //PutChar(c);        
        Delays();   
    }

    while(1)
    {
        Delays();            
    }
}


#include  <msp430x14x.h>


typedef unsigned char uchar;
typedef unsigned int  uint;


void Delays(void);
uchar GetChar(void);
void PutChar(uchar c);
void PutString(uchar *ptr);
void InitUART(void);




/*******************************************
函数名称:GetChar
功    能:向开发板发送字符
参    数:无
返回值  :char
********************************************/
uchar GetChar(void)
{
      uchar c;
      
      while(URXIFG0 == 1);
      c = RXBUF0;
      
      return c;                       //将收到的字符发送出去
}


/*******************************************
函数名称:PutChar
功    能:向开发板发送字符
参    数:uchar
返回值  :无
********************************************/
void PutChar(uchar c)
{
      while (!(IFG1 & UTXIFG0));
      TXBUF0 = c;              //将收到的字符发送出去
}


/*******************************************
函数名称: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--);
    }
}
/*******************************************
函数名称:InitUART
功    能:初始化UART端口
参    数:无
返回值  :无
********************************************/
void InitUART(void)
{
    P3SEL |= 0x30;                            // P3.4,5 = USART0 TXD/RXD
    ME1 |= URXE0 + UTXE0;                             // Enable USART0 T/RXD
    UCTL0 |= CHAR;                            // 8-bit character
    UTCTL0 |= SSEL0;                          // UCLK = ACLK
    UBR00 = 0x03;                             // 32k/9600 - 3.41
    UBR10 = 0x00;                             //
    UMCTL0 = 0x4A;                            // Modulation
    UCTL0 &= ~SWRST;                          // Initialize USART state machine
    IE1 |= URXIE0;                            // 使能USART0的接收中断
}


#pragma vector=UART0RX_VECTOR
__interrupt void UART0_RXISR(void)
{
    uchar *Pstring = " Receive Data :";
    pstr = RXBUF0;
    PutString(Pstring);
    PutChar(pstr);

}



 楼主| 八层楼 发表于 2020-8-5 17:20 | 显示全部楼层

在串口调试助手显示如下:


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
coshi 发表于 2020-9-2 17:22 | 显示全部楼层
非常感谢楼主分享
aoyi 发表于 2020-9-2 17:23 | 显示全部楼层
基本上不用看门狗
drer 发表于 2020-9-2 17:23 | 显示全部楼层
非常不错的代码
gwsan 发表于 2020-9-2 17:23 | 显示全部楼层
可以通用吗  这个系列的
kxsi 发表于 2020-9-2 17:24 | 显示全部楼层
帮楼主顶一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

113

主题

4338

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部

113

主题

4338

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部