新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > msp430g2553與串口通信的驅(qū)動程序

        msp430g2553與串口通信的驅(qū)動程序

        作者: 時間:2016-11-28 來源:網(wǎng)絡(luò) 收藏
        #include "uart.h"

        #include <msp430g2553.h>
        #include "typedef.h"

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

        rece_data uart_buf; //串口緩沖區(qū)

        void init_uart_buf(void)
        {
        uart_buf.head = 0;
        uart_buf.tail = uart_buf.head;
        }

        //獲取串口數(shù)據(jù)
        u8 get_uart_data(u8* data)
        {
        if(uart_buf.tail == uart_buf.head)
        {
        return 0;
        }
        *data = uart_buf.buf[uart_buf.head];
        uart_buf.head = (uart_buf.head + 1) % BUF_SIZE;
        return 1;
        }

        //保存串口數(shù)據(jù)
        void save_uart_data(u8 data)
        {
        uart_buf.buf[uart_buf.tail] = data;
        uart_buf.tail = (uart_buf.tail + 1) % BUF_SIZE;
        }

        //串口初始化
        void uart_Init(void)
        {
        WDTCTL = WDTPW + WDTHOLD;// Stop WDT
        BCSCTL1 = CALBC1_1MHZ;// Set DCO
        DCOCTL = CALDCO_1MHZ;
        P1SEL = BIT1 + BIT2 ;// P1.1 = RXD, P1.2=TXD
        P1SEL2 = BIT1 + BIT2 ;// P1.1 = RXD, P1.2=TXD

        UCA0CTL1 |= UCSSEL_2;// SMCLK
        UCA0BR0 = 104;// 1MHz 9600
        UCA0BR1 = 0;// 1MHz 9600
        UCA0MCTL = UCBRS0;// Modulation UCBRSx = 1
        UCA0CTL1 &= ~UCSWRST;// **Initialize USCI state machine**
        IE2 |= UCA0RXIE;// Enable USCI_A0 RX interrupt
        _EINT();//Enable interrupt
        init_uart_buf();
        }

        //發(fā)送數(shù)據(jù)
        //發(fā)送字符
        void uart_send_ch(u8 ch)
        {

        while(!(IFG2& UCA0TXIFG)); //查詢發(fā)送是否結(jié)束
        UCA0TXBUF = ch;
        IFG2&=~UCA0TXIFG; //清除發(fā)送一標志位
        }

        //發(fā)送字符串
        void uart_send_str(char *str)
        {
        for( ; *str ; )
        {
        uart_send_ch((u8)*str);
        str++;
        }
        }

        //Echo back RXed character, confirm TX buffer is ready first
        #pragma vector=USCIAB0RX_VECTOR
        __interrupt void USCI0RX_ISR(void)
        {
        u8 data;
        while (!(IFG2&UCA0TXIFG));// USCI_A0 TX buffer ready?
        //UCA0TXBUF = UCA0RXBUF;// TX -> RXed character
        data = UCA0RXBUF;
        save_uart_data(data);//保存數(shù)據(jù)
        }



        評論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 遂平县| 平武县| 塔城市| 鹤壁市| 嘉善县| 札达县| 会理县| 黑龙江省| 灯塔市| 鹤壁市| 台南县| 呼图壁县| 青浦区| 灵丘县| 拜城县| 鸡东县| 长岭县| 分宜县| 即墨市| 井冈山市| 葫芦岛市| 正镶白旗| 台南县| 冕宁县| 昭平县| 郴州市| 霍林郭勒市| 峡江县| 定边县| 咸宁市| 竹北市| 贵州省| 长白| 蒙山县| 石景山区| 平阴县| 周至县| 宁河县| 高阳县| 湘西| 内丘县|