访问电脑版页面

导航:老古开发网手机版MSP430单片机的串口

msp430f449单片机与RS232接口程序

导读:This procedure for the MCU and RS232 interface msp430f449 procedures, the use of C language.#include "msp430x44x.h" void init_uart(void); void uart_start(void); void uart_send(void); void uart_r
关键字:
msp430f449,单片机,rs232接口,

This procedure for the MCU and RS232 interface msp430f449 procedures, the use of C language.

#include "msp430x44x.h"

void init_uart(void);
void uart_start(void);
void uart_send(void);
void uart_receive(void);

unsigned char RXDATA_BUF[10],TXDATA_BUF[10]={0xbd,0x77,0x3d,0xc5,0xc6,0xf5,0x68,0x0d,0x6b,0xff};

unsigned int RX_count=0,TX_count=0,TXBUF_num=10,RXBUT_num=10,FLAG;

void main(void)
{ init_uart();
uart_start();
while(1)
{_BIS_SR(CPUOFF);
_NOP();
}
}

--------------------------------------------------------
void init_uart(void) //初始化UART
{ unsigned int i;
WDTCTL=WDTPW+WDTHOLD;
FLL_CTL1|=XT2OFF+SELM_XT2+SELS; //选择MCLK时钟源
do //等待时钟稳定
{ IFG1&=~OFIFG;
for(i=0;i<0xff;i++);
}while((FLL_CTL0&XT2OF)==XT2OF);
UCTL0|=SWRST; //控制寄存器位
UCTL0|=CHAR;
UTCTL0=SSEL0+SSEL1; //选择发送时钟源复位
UBR00=0XA0; //波特率9600
UBR10=0X01;
UMCTL0=0X5E;
UCTL0&=~SWRST; //选择发送时钟源置位
ME1|=URXE0+UTXE0; //使能TXD RXD
IE1|=URXIE0;
IFG1=0X00;
P2SEL|=0X30; //模块选用
P2DIR|=0X10;
for(i=0;i<10;i++) {RXDATA_BUF=0;}//清接收缓存
_EINT();
}

---------------------------------------------------------
void uart_start(void) //启动发送
{ IE1|=UTXIE0;
while((UTCTL0&BIT0)!=BIT0);
TXBUF0=TXDATA_BUF[0];
TX_count=1;
}

----------------------------------------------------------
void uart_receive(void) //接收
{ RXDATA_BUF[RX_count]=RXBUF0;
RX_count+=1;
if(RX_count>=RXBUT_num) {RX_count=0;}
}
-----------------------------------------------------------
void uart_send(void) //发送
{ TXBUF_num-=1;
if(TXBUF_num>0)
{ TXBUF0=TXDATA_BUF[TX_count];
TX_count+=1;

}
else
{ FLAG+=1;
if(FLAG>10) {IE1&=~UTXIE0;}
else { TXBUF_num=10;
TX_count=0;
uart_start();
}

}
}
-------------------------------------------------------------
interrupt [UART0TX_VECTOR] void uart_tx(void)//发送中断响应
{
uart_send();
}

--------------------------------------------------------------
interrupt [UART0RX_VECTOR] void uart_rx(void)//接收中断响应
{
uart_receive();
}

来源:互联网   作者:karen  2018/12/13 11:20:02
栏目: [ MSP430单片机的串口]

相关阅读

MSP430串口应用需要注意的问题

MSP430寄存器中文注释----串口寄存器

MSP430单片机串口应用程序更新的方法解析

MSP430单片机实现模拟串口通信的设计

msp430f449单片机与RS232接口程序

详细解读:MSP430G2553单片机串口通信

基于MSP430对UART的控制方案

基于VC6.0的多台MSP430单片机和PC机串口通讯实现方案