新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > MSP430F149的串口RS232接口

        MSP430F149的串口RS232接口

        作者: 時間:2016-11-27 來源:網絡 收藏
        1、 概述

        具有同步串口模式(SPI),異步通信模式(UART)。

        本文引用地址:http://www.104case.com/article/201611/322346.htm

        作異步通信時,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)

        { }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 怀仁县| 全南县| 五大连池市| 锡林浩特市| 县级市| 黄陵县| 博罗县| 阜南县| 安图县| 广东省| 奈曼旗| 泌阳县| 沧源| 闽清县| 九龙坡区| 新昌县| 浦北县| 财经| 新河县| 阜南县| 洪雅县| 任丘市| 桃园县| 景谷| 沧源| 来宾市| 洪雅县| 仁寿县| 日照市| 唐河县| 大余县| 滁州市| 房产| 凯里市| 威信县| 阜宁县| 扶风县| 辽阳县| 平远县| 肥乡县| 汝阳县|