在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于51單片機(jī)的MODBUS通信C語(yǔ)言程序(從機(jī))

            基于51單片機(jī)的MODBUS通信C語(yǔ)言程序(從機(jī))

            作者: 時(shí)間:2016-11-28 來(lái)源:網(wǎng)絡(luò) 收藏
            #include "reg52.h"
            typedef unsigned char uint8;
            typedef unsigned int uint16;
            typedef unsigned long uint32;
            #define TIMER_HIGHT 0xf8
            #define TIMER_LOW 0xcd
            sbit bRunLED =P3^4; //運(yùn)行指示燈
            sbit bWatchDog=P1^7; //看門狗復(fù)位
            sbit b485Send= P1^3; //75LBC184 發(fā)送接收控制
            uint32 dwTickCount,dwIntTick; //時(shí)鐘
            uint8 idata sendBuf[16],receBuf[16]; //發(fā)送接收緩沖區(qū)
            uint8 idata checkoutError; // ==2 偶校驗(yàn)錯(cuò)
            uint8 idata receTimeOut; //接收超時(shí)
            uint8 idata c10ms; //10ms 計(jì)時(shí)
            bit b1ms,bt1ms,b10ms,bt10ms,b100ms,bt100ms; //定時(shí)標(biāo)志位
            // 串行中斷程序
            void commIntProc() interrupt 4
            {
            if(TI)
            {
            TI = 0;
            if(sendPosi < sendCount)
            {
            sendPosi++;
            ACC = sendBuf[sendPosi];
            TB8 = P; //加上校驗(yàn)位
            SBUF = sendBuf[sendPosi];
            }
            else
            {
            b485Send = 0; //發(fā)送完后將485置于接收狀態(tài)
            receCount = 0; //清接收地址偏移寄存器
            checkoutError = 0;
            }
            }
            else if(RI)
            {
            RI = 0;
            receTimeOut = 10; //通訊超時(shí)值
            receBuf[receCount] = SBUF;
            ACC = receBuf[receCount];
            if(P != RB8)
            checkoutError = 2; //偶校驗(yàn)出錯(cuò)
            receCount++; //接收地址偏移寄存器加1
            receCount &= 0x0f; //最多一次只能接收16個(gè)字節(jié)
            }
            } // void CommIntProc()
            //定時(shí)器0 1ms 中斷
            void timer0IntProc() interrupt 1
            {
            TL0 = TIMER_LOW;
            TH0 = TIMER_HIGHT;
            dwIntTick++;
            bt1ms = 1;
            c10ms++;
            if(c10ms >= 10)
            {
            c10ms = 0; //10ms計(jì)時(shí)器清零
            bt10ms = 1;
            }
            } // void Timer0IntProc()
            //外部中斷0
            void intEx0Proc(void) interrupt 0
            {
            }
            //計(jì)數(shù)器1中斷
            void counter1IntProc(void) interrupt 3 using 1
            {
            }
            //定時(shí)處理
            void timeProc(void)
            {
            static uint8 c200ms;
            bWatchDog = ~ bWatchDog; //看門狗取反
            b1ms = 0;
            b10ms = 0;
            b100ms = 0;
            ET0 = 0;
            dwTickCount = dwIntTick;
            ET0 = 1;
            if(bt1ms)
            {
            bt1ms = 0;
            b1ms = 1;
            if(receTimeOut>0)
            {
            receTimeOut--;
            if(receTimeOut==0 && receCount>0) //判斷通訊接收是否超時(shí)
            {
            b485Send = 0; //將485置為接收狀態(tài)
            receCount = 0; //將接收地址偏移寄存器清零
            checkoutError = 0;
            }
            }
            }
            if(bt100ms)
            {
            bt100ms = 0;
            b100ms = 1;
            }
            if(bt10ms) //判斷中斷10ms標(biāo)志位是否1
            {
            bt10ms = 0; //清中斷10ms標(biāo)志位
            b10ms = 1;
            c200ms++; //200ms計(jì)時(shí)器加1
            if(c200ms >= 20) //判斷是否計(jì)時(shí)到200ms
            {
            c200ms = 0; //清200ms計(jì)時(shí)器
            bRunLED = ~bRunLED; //取反運(yùn)行指示燈
            }
            }
            } // void TimerProc(void)
            //初始化串口
            void initUart(void)
            {
            //T2 用于波特率 9600
            T2CON = 0x30;
            RCAP2H = 0xff;
            RCAP2L = 0xb8;
            TR2 = 1;
            //偶校驗(yàn)
            SCON = 0xd0;
            PCON = 0;
            ES = 1;
            }//void initUart(void)
            //初始化中斷
            void initInt(void)
            {
            TMOD = 0x51;
            TH0 = TIMER_HIGHT;
            TL0 = TIMER_LOW;
            TR0 = 1;
            ET0 = 1;
            TH1 = 0; //9600
            TL1 = 0;
            TR1 = 0; //定時(shí)器1用于計(jì)數(shù)定時(shí)器2用于波特
            ET1 = 1;
            IT0 = 1;
            IT1 = 1;
            EX0 = 0;
            PX0 = 1;
            EX1 = 0;
            initUart();
            EA = 1;
            } // void initInt(void)
            //初始化
            void initProg(void)
            {
            initInt();
            b485Send = 0;
            }
            void main(void)
            {
            initProg();
            while(1)
            {
            timeProc();
            checkComm0Modbus();
            }
            }



            評(píng)論


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

            關(guān)閉