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

/*****************************************************
程序功能: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);

}


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



评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AllenSun-1990

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值