• 大小: 113KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-24
  • 语言: 其他
  • 标签: RS485  

资源简介

RS485多机通信,Keil C51源程序,Proteus仿真!适合初学者与项目参考!

资源截图

代码片段和文件信息

//MAX485的使能转换时会出很麻烦的问题!!!
//通信协议:主机发地址信息,相应从机发地址应答信息(地址+0x80)
//          主机发命令信息,相应从机发命令应答信息(命令+0x80)
//          命令a(主发从收命令),命令号:0x0a,主机首先发数据数量N,紧接着发N个数据,最后发2字节CRC校验(高位在前)
//                                 从机无需应答,只需把数据连同检验存入接收缓冲区即可,接着进行校验,校验正确发肯定应答(0xaa)
//                                 否则发否定应答(0xee),主机若收到否定应答,则全部从发(从地址开始)
#include
#include

#define uchar unsigned char
#define uint  unsigned int

uchar*  receive_buffer_head=0xa0;   //接收数据缓冲区
uchar*  send_buffer_head=0x60;   //发送数据缓冲区
uchar*  current_receive_pointer;          //当前接收指针
uchar*  current_send_pointer;   //当前发送指针
uchar*  crc_head=0x80;
uchar receive_buffer_length;              //接收缓冲区数据长度
uchar send_buffer_length;       //发送缓冲区数据长度
uchar respon;                             //应答信息
uchar slave1adress=0x01;   //从机1地址
uchar slave2adress=0x02;   //从机2地址
uchar slave3adress=0x03;   //从机3地址
uchar crc_high;
uchar crc_low;
uint  crc;
uchar command_a=0x0a;

sbit sending=P2^0;                        //发送指示灯  
sbit receiveing=P2^1;                     //接收指示灯
sbit test=P2^2;
sbit send_enable=P2^7;                    //发送使能,只有发送时为高,其余时间为低
sbit receive_enable_inv=P2^6;             //接收使能,低电平有效




//***********************************************************
//                      延时函数,延时x毫秒
//***********************************************************
void DelayMS(uint ms)             
{
   uchar i;
   while(ms--)
   for(i=0;i<120;i++);
}



//***********************************************************
//                 写主机的发送缓冲区数据
//这里简单地往发送缓冲区写入a~z
//***********************************************************
void Write_Send_Buffer()
{  
   uchar i;
   uchar* temp_pointer;
   temp_pointer=send_buffer_head;
   for(i=0x00;i<0x1a;i++)
   {
    *temp_pointer=i+0x41;
 temp_pointer++;
   }  
}



//***********************************************************
//              RS-485初始化
//***********************************************************
void RS485_initial()
{
     sending=0;                     //发送指示灯  
     receiveing=0;                  //接收指示灯
 test=0;
     current_receive_pointer=receive_buffer_head;    //初始化当前的接收缓冲区指针指向接收缓冲区首地址
     current_send_pointer=send_buffer_head;          //初始化当前的发送缓冲区指针指向发送缓冲区首地址              
 receive_buffer_length=0;         //接收缓冲区数据长度初始化为0
     send_buffer_length=0;   //发送缓冲区数据长度初始化为0
 send_enable=1;                   //初始化发送有效
 receive_enable_inv=1;            //接收无效
 REN=0;                           //初始化为不允许接收
 P1=0x88;
  

}


//***********************************************************
//UART初始化
//***********************************************************
void UART_initial()
{
   EA=1;                        //开总中断
   ES=1;                        //开串行口中断
   ET1=1;                       //开定时计数器1中断
   SCON=0xd0;                 //设置串行方式,1101 0000 方式3,不允许多机,允许接收
   TMOD=0x20;                 //设置计数器方式,

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        477  2012-12-16 14:42  RS_485_多机通信\RS-485说明文档.txt

     文件     126001  2012-12-16 14:27  RS_485_多机通信\rs485.DSN

     文件      12569  2012-12-16 14:20  RS_485_多机通信\主机host\again_rs485

     文件       8898  2012-12-16 14:29  RS_485_多机通信\主机host\again_rs485.c

     文件       3129  2012-12-16 14:20  RS_485_多机通信\主机host\again_rs485.hex

     文件         39  2012-12-16 14:20  RS_485_多机通信\主机host\again_rs485.lnp

     文件      14396  2012-12-16 14:20  RS_485_多机通信\主机host\again_rs485.LST

     文件      14472  2012-12-16 14:20  RS_485_多机通信\主机host\again_rs485.M51

     文件      14185  2012-12-16 14:20  RS_485_多机通信\主机host\again_rs485.OBJ

     文件       4203  2012-12-16 14:20  RS_485_多机通信\主机host\again_rs485.plg

     文件      55227  2012-12-16 14:30  RS_485_多机通信\主机host\again_rs485.uvopt

     文件      13245  2012-12-13 21:58  RS_485_多机通信\主机host\again_rs485.uvproj

     文件      55230  2012-12-16 14:29  RS_485_多机通信\主机host\again_rs485_uvopt.bak

     文件      13241  2012-12-13 20:54  RS_485_多机通信\主机host\again_rs485_uvproj.bak

     文件      12568  2012-12-16 14:19  RS_485_多机通信\从机1\again_rs485_slave1

     文件      12280  2012-12-16 14:29  RS_485_多机通信\从机1\again_rs485_slave1.c

     文件       3319  2012-12-16 14:19  RS_485_多机通信\从机1\again_rs485_slave1.hex

     文件         53  2012-12-16 14:19  RS_485_多机通信\从机1\again_rs485_slave1.lnp

     文件      20101  2012-12-16 14:19  RS_485_多机通信\从机1\again_rs485_slave1.LST

     文件      14986  2012-12-16 14:19  RS_485_多机通信\从机1\again_rs485_slave1.M51

     文件      14070  2012-12-16 14:19  RS_485_多机通信\从机1\again_rs485_slave1.OBJ

     文件       4332  2012-12-16 14:19  RS_485_多机通信\从机1\again_rs485_slave1.plg

     文件      55252  2012-12-16 14:30  RS_485_多机通信\从机1\again_rs485_slave1.uvopt

     文件      13266  2012-12-13 21:58  RS_485_多机通信\从机1\again_rs485_slave1.uvproj

     文件      55254  2012-12-16 14:29  RS_485_多机通信\从机1\again_rs485_slave1_uvopt.bak

     文件          0  2012-12-13 20:54  RS_485_多机通信\从机1\again_rs485_slave1_uvproj.bak

     文件      11837  2012-12-16 14:19  RS_485_多机通信\从机2\again_rs485_slave2

     文件      11950  2012-12-16 14:19  RS_485_多机通信\从机2\again_rs485_slave2.c

     文件       3213  2012-12-16 14:19  RS_485_多机通信\从机2\again_rs485_slave2.hex

     文件         53  2012-12-16 14:19  RS_485_多机通信\从机2\again_rs485_slave2.lnp

............此处省略15个文件信息

评论

共有 条评论