zoukankan      html  css  js  c++  java
  • MSP430F149的串口RS232接口

    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)

    {  }

  • 相关阅读:
    java cp命令
    Ubuntu相关IP配置(转)
    (转)Linux操作系统下VMware的多网卡桥接转换
    Linux问题FAQ1
    hadoop运行常见问题FAQ
    hadoop运行故障问题解决1——datanode节点启动后自动关闭
    Java程序设计9——泛型
    一道灵活的css笔试题
    inherit与auto
    再谈visibility:hidden和display:none
  • 原文地址:https://www.cnblogs.com/yuesheng/p/2101916.html
Copyright © 2011-2022 走看看