您的位置 首页 国产IC

MSP430F149的串口RS232接口

1、概述具有同步串口模式(SPI),异步通信模式(UART)。作异步通信时,P34,P35,P36,P37第二功能分别是UTXD0,URXD0,UTXD1,

1、 概述

具有同步串口形式(SPI),异步通讯形式(UART)。

作异步通讯时,P3.4,P3.5,P3.6,P3.7第二功用分别是UTXD0, URXD0, UTXD1, UTXD2

2、 使用方法概述

2.1 程序架构

装备寄存器设置作业形式

{

设置IO口为第二功用作为串口收发引脚;

使能串口收发功用;

挑选每帧数据位为7或8;

挑选波特率发生器时钟源;

装备波特率(查表得出值再装备UxBR0, UxBR1,UxMCTL);

软件铲除串口复位位(SWRST);

若选用中止方法则使能承受、发送中止

}

编写承受/发送程序,可选用查询方法或中止方法。同51单片机不同的是,UTXIFG,URXIF在发送下一个数据和读取数据时被主动清零了,无需软件铲除。

2.2 细节描绘

装备波特率时用户手册上有速查表,如下

设置波特率时要挑选适宜的时钟源。关于较低的波特率(9600b/s及以下),可选ACLK,大于9600要选用SMCLK,由于串口波特率发生器分频系数要求大于3。UxBR0(低)UxBR1(高)值的计算式为:挑选的时钟源/波特率,再取整。为了准确,MSP430设置了小数分频功用,经过UxMCTL来完结。

3、相关寄存器

1.ME1, Module Enable Register 1

UTXE0 Bit 7 USART0 transmit enable. This bit enables the transmitter for USART0.

0 Module not enabled

1 Module enabled

URXE0 Bit 6 USART0 receive enable. This bit enables the receiver for USART0.

0 Module not enabled

1 Module enabled

2.UxCTL(UCTLx), USART Control Register

CHAR Bit 4 Character length. Selects 7-bit or 8-bit character length.

0 7-bit data

1 8-bit data

SWRST Bit 0 Software reset enable

0 Disabled. USART reset released for operation

1 Enabled. USART logic held in reset state

3.UxTCTL(UTCTLx), USART Transmit Control Register

SSELx Bits

5-4

Source select. These bits select the BRCLK source clock.

00 UCLKI

01 ACLK

10 SMCLK

11 SMCLK

4.UxBR0, USART Baud Rate Control Register 0,低8位

UxBR1, USART Baud Rate Control Register 1,高8位

5. UxMCTL, USART Modulation Control Register

UxMCTLx Bits

7−0

Modulation bits. These bits select the modulation for BRCLK.

6.IFG1, Interrupt Flag Register 1

UTXIFG0 Bit 7 USART0 transmit interrupt flag. UTXIFG0 is set when U0TXBUF is empty.

0 No interrupt pending

1 Interrupt pending

URXIFG0 Bit 6 USART0 receive interrupt flag. URXIFG0 is set when U0RXBUF has received

a complete character.

0 No interrupt pending

1 Interrupt pending

7.IE1, Interrupt Enable Register 1

UTXIE0 Bit 7 USART0 transmit interrupt enable. This bit enables the UTXIFG0 interrupt.

0 Interrupt not enabled

1 Interrupt enabled

URXIE0 Bit 6 USART0 receive interrupt enable. This bit enables the URXIFG0 interrupt.

0 Interrupt not enabled

1 Interrupt enabled

4、实例

4.1 装备为N.8.1,9600,查询方法收发数据

/*******************************************

函数称号: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

}

收数据

if(IFG1 & URXIFG0) Disp1Char(U0RXBUF); //假如收到字符

发数据

while (!(IFG1 & UTXIFG0)); TXBUF0 =Char;

4.2 装备接纳数据中止方法

P3SEL |= 0x30; // 挑选P3.4和P3.5做UART通讯端口

ME1 |= UTXE0 + URXE0; // 使能USART0的发送和承受

UCTL0 |= CHAR; // 挑选8位字符

UTCTL0 |= SSEL0; // UCLK = ACLK

UBR00 = 0x03; // 波特率9600

UBR10 = 0x00; //

UMCTL0 = 0x4A; // Modulation

UCTL0 &= ~SWRST; // 初始化UART状态机

IE1 |= URXIE0; // 使能USART0的接纳中止

_EINT();

中止服务函数

#pragma vector = UART0RX_VECTOR

__interrupt void UART0_RXISR(void)

{ }

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/bandaoti/ic/257282.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部