⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart0.c

📁 msp430串口通讯程序
💻 C
字号:
#include "chip.h"
#include "string.h"
//unsigned char tx0flg,rx0flg;
unsigned char rxbuf0;

void InitUart0()
{
UCTL0=SWRST;          //Ready for configurating serial's parameters
UCTL0 |=CHAR;         //Data frame format(8-N-1) with listen 
UTCTL0=SSEL1+URXSE;   //Selection of SMCLK becomes baud ratio clock,edge of starting singal available
URCTL0=URXEIE;//+URXWIE;        //Wrong message interrupt of reception enable
UBR10=0x00;           //115200 @ 8Mhz extern OSC
UBR00=69;      
UMCTL0=0x55;          //Baud ratio adjusting
U0ME=URXE0+UTXE0;     //Functions of reception and transmitter enable
UCTL0 &=~SWRST;       //Functions of serial port enable
U0IE=URXIE0+UTXIE0;   //Enable interrupt of reception 
P3SEL |=0x30;         //P3.5(URXD0),P3.4(UTXD0)
P3DIR |=BIT4;         //Setting P3.4 ,p3.2, p3.1 and p3.0 direction to output
P3DIR &=~BIT5;        //Setting P3.5 input
//tx0flg=1;
//rx0flg=0;
}

#pragma vector=UART0TX_VECTOR
__interrupt void usart0_tx (void)
{
  __disable_interrupt();    //Entry routine of TX0 with clearing URXIFG0 automatically
 //tx0flg=1;  //Setting transmission successful flag
  __enable_interrupt();
}

#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
 // unsigned char len,i;
  //unsigned int j=0;
  /*if(!(IFG1 & URXIFG0))
  {
    __low_power_mode_off_on_exit(); //Activating DCO if it was disabled by low-power mode
    UTCTL0 &=~URXSE;     //Clear inner URXS flag without occuring URXIFGO
    UTCTL0 |=URXSE;      //Re-setting URXSE founction 
  } */ 
   //The following routine is a normal interrupt service  
 // else
  //{
    //rx0flg=1;            //Setting transmission successful flag;
    rxbuf0=RXBUF0;       //Reading data from RXBUF0 for clearing URXIFG0 automatically
  //} 
}

void Tx0Char(unsigned char c)
{
 while(!(UTCTL0 & TXEPT));
 //tx0flg=0;
 TXBUF0=c;
 while(!(IFG1 & UTXIFG0));
 //tx0flg=1;
}
 
void Tx0S(char *p)
{
 unsigned  i,j;
 i=strlen(p);
 if(i>=256) i=255;
 for(j=0;j<i;j++) 
 {
  if(*(p+j)=='\n')
  {
   Tx0Char(0x0d);
   Tx0Char(0x0a);
  }
  else    Tx0Char(*(p+j)); 
 }
}  

void Tx0HexC(unsigned char c)
{
 unsigned char temp;
 temp=(c & 0xf0) >>4;
 if(temp <10) Tx0Char(temp +0x30);
 else Tx0Char(temp +'A'-10);
 temp=c & 0x0f;
 if(temp <10) Tx0Char(temp +0x30);
 else Tx0Char(temp +'A'-10);
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -