新聞中心

        EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于51單片機(jī)的串口檢測(cè)程序

        基于51單片機(jī)的串口檢測(cè)程序

        作者: 時(shí)間:2012-07-07 來(lái)源:網(wǎng)絡(luò) 收藏

        #i nclude reg51_STC.H
        #define uchar unsigned char
        #define uint unsigned int

        //--------------------------------------------------------------------------
        //函數(shù)名稱(chēng): UART_Init()
        //函數(shù)功能: 初始化函數(shù),在系統(tǒng)時(shí)鐘為11.059MHZ時(shí),設(shè)定波特率為9600bit/s
        //其他說(shuō)明: 接收中斷允許,發(fā)送中斷禁止
        //--------------------------------------------------------------------------

        void UART_Init(void)
        {
        SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
        TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
        PCON |= 0x80 ; //SMOD=1;
        TH1 = 0xFA ; //Baud:9600 fosc=11.0592MHz
        ES=1;
        TR1 = 1 ; // timer 1 run
        EA=1;
        }


        //--------------------------------------------------------------------------
        //函數(shù)名稱(chēng): main(void)
        //函數(shù)功能: 主函數(shù)
        //其他說(shuō)明: 無(wú)
        //--------------------------------------------------------------------------

        void main(void)
        {
        UART_Init();
        while(1);
        }


        //--------------------------------------------------------------------------
        //函數(shù)名稱(chēng): Uart_SendData()
        //函數(shù)功能: 串口發(fā)送一個(gè)字節(jié)的數(shù)據(jù)
        //其他說(shuō)明: 此供中斷調(diào)用
        //--------------------------------------------------------------------------

        void Uart_SendData(uchar dat)
        {
        SBUF=dat; //寫(xiě)SBUF,開(kāi)始發(fā)送
        while(TI==0); //等待發(fā)送
        TI=0; //清發(fā)送標(biāo)志位
        }


        //--------------------------------------------------------------------------
        //函數(shù)名稱(chēng): INT_UartRcv()
        //函數(shù)功能: 串口接收中斷函數(shù)
        //其他說(shuō)明: 無(wú)
        //--------------------------------------------------------------------------

        void INT_UartRcv(void) interrupt 4
        {
        uchar Rcv;
        if(RI)
        {
        RI=0; //
        Rcv=SBUF;
        Uart_SendData(Rcv); //返回接收數(shù)據(jù),可以改為其他函數(shù)
        }
        }

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

        NO。2

        #i ncludereg52.h>
        #i nclude string.h>
        #define INBUF_LEN 4 //數(shù)據(jù)長(zhǎng)度
        unsigned char inbuf1[INBUF_LEN];
        unsigned char checksum,count3;
        bit read_flag= 0 ;
        void init_serialcomm( void )
        {
        SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
        TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
        PCON |= 0x80 ; //SMOD=1;
        TH1 = 0xF4 ; //Baud:4800 fosc=11.0592MHz
        IE |= 0x90 ; //Enable Serial Interrupt
        TR1 = 1 ; // timer 1 run
        EA=1;
        }

        //向串口發(fā)送一個(gè)字符


        上一頁(yè) 1 2 下一頁(yè)

        評(píng)論


        相關(guān)推薦

        技術(shù)專(zhuān)區(qū)

        關(guān)閉
        主站蜘蛛池模板: 东山县| 蒙城县| 冀州市| 崇仁县| 宁夏| 甘洛县| 彝良县| 监利县| 黄大仙区| 大渡口区| 东丽区| 崇礼县| 马鞍山市| 花垣县| 芒康县| 哈巴河县| 宜阳县| 高青县| 铁力市| 武宣县| 郎溪县| 维西| 米林县| 三明市| 平安县| 清流县| 栾城县| 新蔡县| 高平市| 岳阳县| 泾阳县| 牡丹江市| 思南县| 娱乐| 启东市| 济阳县| 五莲县| 汝南县| 上饶县| 兰考县| 栾川县|