新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 模擬串口的實現(xiàn)單片機IO口

        模擬串口的實現(xiàn)單片機IO口

        作者: 時間:2016-11-19 來源:網(wǎng)絡(luò) 收藏
        用EBOX向我們的電力線調(diào)試模塊發(fā)數(shù)據(jù),但是要有一個控制接口,但是EBOX沒有提供,所有只好用MCU來做伺服器。

        一般的單片機都只有一個串口,所以必須模擬一個出來。

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

        /*

        sbit TXD1= P1^4;//define p14 as the analog transmit port

        sbit RXD1= P1^2;//define p12 as the analog recieve port

        */

        #include "REG52.H"

        #define uint unsigned int

        #define uchar unsigned char

        #include

        void WaitTF0(void);

        void TIMEINI(void)//counter initial

        {

        TMOD=0x02;

        TH0=0xA0;、//9600 BPS

        TL0=TH0;

        TR0=1;

        TF0=0;

        }

        void WByte(uchar input)//send function

        {

        uchar i=8;

        TR0=1;

        TXD1=0;//begin bit

        WaitTF0();

        //send the Byte

        while(i--)

        {

        TXD1=input&0x01;//send Byte from low bit to high bit

        WaitTF0();

        input=input>>1;//right shift

        }

        TXD1=1;//stop bit

        WaitTF0();

        TR0=0;//stop the counter

        }

        uchar RByte()//the recieve function

        {

        uchar Output=0;

        uchar i=8;

        while(RXD1);

        i=i;

        i=i;

        i=i;

        i=i;

        i=i;

        i=i;

        i=i;

        TR0=1;//計數(shù)器開始工作

        WaitTF0();

        while(i--)//接收8位數(shù)據(jù)位

        {

        Output>>=1;

        if(RXD1)Output=Output|0x80;//recieve from the high bit to low bit

        WaitTF0();

        }

        TR0=0;

        returnOutput;

        }

        void WaitTF0(void)//check the counter

        {

        while(!TF0);

        TF0=0;

        }



        關(guān)鍵詞: 模擬串口單片機IO

        評論


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

        關(guān)閉
        主站蜘蛛池模板: 翁源县| 沙田区| 个旧市| 五家渠市| 永嘉县| 上犹县| 新郑市| 子洲县| 吉水县| 岳池县| 石河子市| 视频| 涿州市| 寿光市| 修水县| 增城市| 芜湖市| 鸡西市| 屯留县| 曲周县| 芒康县| 永城市| 搜索| 清原| 南召县| 嘉兴市| 措勤县| 柘荣县| 游戏| 仲巴县| 福建省| 称多县| 建湖县| 赤水市| 本溪市| 新干县| 新野县| 新田县| 镇巴县| 营山县| 靖西县|