打印

STC89C51,FreeModbus移植。搞过的人进来看看。

[复制链接]
6382|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
kangzp|  楼主 | 2013-4-14 20:54 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
沙发
mb4555| | 2013-4-15 15:46 | 只看该作者
确实太大了,我的Modbus,用STC89C52驱动,包括8位数码管扫描,设置界面,EEPROM的读写,
才4.4K的ROM,

使用特权

评论回复
评分
参与人数 1威望 +1 收起 理由
kangzp + 1
板凳
xtc超| | 2013-6-12 15:59 | 只看该作者
分享一下代码看看?能够优化?

使用特权

评论回复
地板
flypoppy| | 2013-6-12 17:45 | 只看该作者
还是很深的,值得学习

使用特权

评论回复
5
dong_abc| | 2013-6-12 17:57 | 只看该作者
FreeModbus包里有一堆demo ,没有8051的?

使用特权

评论回复
6
ruiboxu| | 2013-12-17 21:04 | 只看该作者
没代码,标题党

使用特权

评论回复
7
忧忧| | 2014-12-3 09:07 | 只看该作者
可以贴出代码么,我自己设置的好像不对,大神们看看

使用特权

评论回复
8
忧忧| | 2014-12-3 09:13 | 只看该作者
哪儿不对  求指正  我用的是90c516rd+
#include "includes.h"
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"

/* ----------------------- static functions ---------------------------------*/
static void prvvTIMERExpiredISR( void );

static u16 n;

tiime.c
------------------------------------------
BOOL
xMBPortTimersInit( USHORT usTim1Timerout50us )
{
       
        TMOD|= 0x01;
        TH0  = 0xFE;
        TL0  = 0x0C;  // timer sets at 50us
        IE   = 0X82;//T0ÖжÏÔÊÐí
        TR0  = 1;        //Æô¶¯T0
        TMOD &= ~0x08;          
        TMOD &= ~0x04;
  n = usTim1Timerout50us;

    return TRUE;
}



void
vMBPortTimersEnable(  )
{
    /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */
        TH0 = U16_HI(0-(F_MCU/12/20000)*n);
        TL0 = U16_LO(0-(F_MCU/12/20000)*n);
        TF0 = 0;        //
        ET0 = 1;        /
        TR0 = 1;  //
}

void
vMBPortTimersDisable(  )
{
/* Disable any pending timers. */
        ET0 = 0;
        TR0 = 0;
}

/* Create an ISR which is called whenever the timer has expired. This function
* must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
* the timer has expired.
*/
static void prvvTIMERExpiredISR( void )  interrupt 1
{
        // ( void )pxMBPortCBTimerExpired(  );²»ÄÜÖ±½Óµ÷ÓÃ
   if(eMBCurrentMode==MB_RTU)
    {
                xMBRTUTimerT35Expired();
         }
        else if(eMBCurrentMode==MB_ASCII)
        {
                xMBASCIITimerT1SExpired();
        }
}

void            
vMBPortTimersDelay( USHORT usTimeOutMS )
{
       
        usTimeOutMS = usTimeOutMS;
       
}


------------------------------------------------
serial.c
----------------------------------------------------




#include "port.h"

/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
//sbit EnCs=P3^6;  //485Ƭѡ
/* ----------------------- static functions ---------------------------------*/
static void prvvUARTTxReadyISR( void );
static void prvvUARTRxISR( void );

static u8 TxEnable;

/* ----------------------- Start implementation -----------------------------*/
void
vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
{
    /* If xRXEnable enable serial receive interrupts. If xTxENable enable
     * transmitter empty interrupts.
     */

    if(xRxEnable && xTxEnable)
                        {
                        TxEnable = 1;
                        REN = 1;//½ÓÊÕʹÄÜ
                        ES = 1;//ÖжÏʹÄÜ
                        //EnCs=1;        //ʹÄÜ485·¢ËÍƬѡ
                        }
                else if(!xRxEnable && xTxEnable)
                        {
                        TxEnable = 1;
                        REN = 0;//½ÓÊÕʹÄÜ
                        ES = 1;//ÖжÏʹÄÜ
                        //EnCs=1;
                  }
                else if(xRxEnable && !xTxEnable)
                        {
                        TxEnable = 0;
                        REN = 1;//½ÓÊÕʹÄÜ
                        ES = 1;//ÖжÏʹÄÜ
                        //EnCs=1;//ʹÄÜ485½ÓÊÜƬѡ
                  }
         else
                        {
                        TxEnable = 0;
                        REN = 0;//½ÓÊÕʹÄÜ
                        ES = 0;//ÖжÏʹÄÜ
                        //EnCs=0;   
                  }
}

BOOL
xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
{
        u16 bps;
        ucPORT = ucPORT;
        ucDataBits = ucDataBits;
        eParity = MB_PAR_NONE;
        bps = ulBaudRate;
       
        TMOD = 0x20;
        SCON = 0x50;
  //TH1  = 0xFE;
  //TL1  = TH1;
        PCON = 0x00;
        TMOD &= ~0x80;
        TMOD &= ~0x40;
        TMOD |= 0x20;
        TH1 = 0-(F_MCU/12/bps);       
       
        EA   = 1;//ÖжÏÔÊÐí
        ES   = 1;//´®ÐпÚÖжϠ  Ê¹ÄÜ
        TR1  = 1;//Æô¶¯T1¹¤×÷
        TxEnable = 1;
        return TRUE;
}

BOOL
xMBPortSerialPutByte( CHAR ucByte )
{
        /*·¢Ëͺ¯Êý*/
          
          SBUF = ucByte;
          
    return TRUE;
}

BOOL
xMBPortSerialGetByte( CHAR * pucByte )
{
        /*½ÓÊÕº¯Êý*/
       
          *pucByte = SBUF;
    return TRUE;
}

/* Create an interrupt handler for the transmit buffer empty interrupt
* (or an equivalent) for your target processor. This function should then
* call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
* a new character can be sent. The protocol stack will then call
* xMBPortSerialPutByte( ) to send the character.
*/
static void prvvUARTTxReadyISR( void )         
//×ÜÏß·¢ËͳÌÐòºó×Ô¶¯µ÷Óà       ¸Ä ÖжϷþÎñ
{

        if(eMBCurrentMode==MB_RTU){
                xMBRTUTransmitFSM();
        }else if(eMBCurrentMode==MB_ASCII){
                xMBASCIITransmitFSM();
        }
}

/* Create an interrupt handler for the receive interrupt for your target
* processor. This function should then call pxMBFrameCBByteReceived( ). The
* protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
* character.
*/
static void prvvUARTRxISR( void )
{               
        if(eMBCurrentMode==MB_RTU){         
                xMBRTUReceiveFSM();          
        }else if(eMBCurrentMode==MB_ASCII){
                xMBASCIIReceiveFSM();
        }
}

//´®¿ÚÖжÏ
void UartISR(void) interrupt 4
{
        if(RI)
                {
                 RI=0;
                 prvvUARTRxISR();
                       
          }
        if(TI)
                {
          TI=0;
                if(TxEnable)
                        {
                         prvvUARTTxReadyISR();
                  }
          }
}  

void            
vMBPortClose( void )
{
        /*close the TX and RX enable*/

}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

38

帖子

0

粉丝