新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 基于51單片機的MODBUS通信C語言程序(從機)

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

        作者: 時間:2016-11-28 來源:網絡 收藏
        #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; //運行指示燈
        sbit bWatchDog=P1^7; //看門狗復位
        sbit b485Send= P1^3; //75LBC184 發送接收控制
        uint32 dwTickCount,dwIntTick; //時鐘
        uint8 idata sendBuf[16],receBuf[16]; //發送接收緩沖區
        uint8 idata checkoutError; // ==2 偶校驗錯
        uint8 idata receTimeOut; //接收超時
        uint8 idata c10ms; //10ms 計時
        bit b1ms,bt1ms,b10ms,bt10ms,b100ms,bt100ms; //定時標志位
        // 串行中斷程序
        void commIntProc() interrupt 4
        {
        if(TI)
        {
        TI = 0;
        if(sendPosi < sendCount)
        {
        sendPosi++;
        ACC = sendBuf[sendPosi];
        TB8 = P; //加上校驗位
        SBUF = sendBuf[sendPosi];
        }
        else
        {
        b485Send = 0; //發送完后將485置于接收狀態
        receCount = 0; //清接收地址偏移寄存器
        checkoutError = 0;
        }
        }
        else if(RI)
        {
        RI = 0;
        receTimeOut = 10; //通訊超時值
        receBuf[receCount] = SBUF;
        ACC = receBuf[receCount];
        if(P != RB8)
        checkoutError = 2; //偶校驗出錯
        receCount++; //接收地址偏移寄存器加1
        receCount &= 0x0f; //最多一次只能接收16個字節
        }
        } // void CommIntProc()
        //定時器0 1ms 中斷
        void timer0IntProc() interrupt 1
        {
        TL0 = TIMER_LOW;
        TH0 = TIMER_HIGHT;
        dwIntTick++;
        bt1ms = 1;
        c10ms++;
        if(c10ms >= 10)
        {
        c10ms = 0; //10ms計時器清零
        bt10ms = 1;
        }
        } // void Timer0IntProc()
        //外部中斷0
        void intEx0Proc(void) interrupt 0
        {
        }
        //計數器1中斷
        void counter1IntProc(void) interrupt 3 using 1
        {
        }
        //定時處理
        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) //判斷通訊接收是否超時
        {
        b485Send = 0; //將485置為接收狀態
        receCount = 0; //將接收地址偏移寄存器清零
        checkoutError = 0;
        }
        }
        }
        if(bt100ms)
        {
        bt100ms = 0;
        b100ms = 1;
        }
        if(bt10ms) //判斷中斷10ms標志位是否1
        {
        bt10ms = 0; //清中斷10ms標志位
        b10ms = 1;
        c200ms++; //200ms計時器加1
        if(c200ms >= 20) //判斷是否計時到200ms
        {
        c200ms = 0; //清200ms計時器
        bRunLED = ~bRunLED; //取反運行指示燈
        }
        }
        } // void TimerProc(void)
        //初始化串口
        void initUart(void)
        {
        //T2 用于波特率 9600
        T2CON = 0x30;
        RCAP2H = 0xff;
        RCAP2L = 0xb8;
        TR2 = 1;
        //偶校驗
        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; //定時器1用于計數定時器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();
        }
        }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 宁津县| 福建省| 和静县| 胶南市| 平顶山市| 慈利县| 怀来县| 和硕县| 金乡县| 辽中县| 新民市| 抚顺市| 含山县| 舞阳县| 韩城市| 喀喇| 富平县| 苗栗县| 乐山市| 乌什县| 贺州市| 措美县| 大新县| 广州市| 崇明县| 中卫市| 新乡县| 苏州市| 西畴县| 凤山县| 侯马市| 南召县| 陵水| 古田县| 车致| 寿宁县| 莱芜市| 谢通门县| 榆社县| 大余县| 桃江县|