大家好,我是第一次做 单片机(stc89c52)通过RS485与PLC通信,采用modbus协议;结果是通信不上,(呵呵,意料之中的),然后我就检查程序,反复研究了好几天都没个结果,我利用单片机闲置的一个管脚置高或低,来判断出现单片机程序执行的情况,结果发现单片机反复复位,如果不接485的A/B端,单片机不复位(我是这样判断复位的:在main()函数和while(1)之间加了单片机的闲置脚,1s高电平,1s低电平)。硬件连接比较简单,单片机通过485的A/B端连接到RS485/RS422,RS422连接但PLC.请大家帮着看看,是什么问题引起的,谢谢了啊。
#include <AT89X52.H>
#include <intrins.h>
#define Uint16 unsigned int
#define Uint32 unsigned long
#define uchar unsigned char
unsigned char temp8;
Uint16 temp16;
sbit led2=P1^3;
sbit led1=P1^4;
sbit reving=P1^6;
sbit rw=P1^0; //487收发控制端
/***
sbit SCL2=P1^1; //SCL2定义为P1口的第3位脚,连接ADC0831SCL脚
sbit SDA2=P1^2; //SDA2定义为P1口的第4位脚,连接ADC0831SDA脚
sbit CS2=P1^0; //CS2定义为P1口的第4位脚,连接ADC0831CS脚
***/
void InitUART(void); //串口初始化
void SendOneByte(unsigned); //串口发送数据
void mdproc(uchar); //接收数据处理
unsigned char ad0831read(void); //定义该函数为读取模数转换的数据
Uint16 Crc16(Uint16 *puchMsg, Uint16 usDataLen); //crc校验
uchar node;
uchar mtx[11],mrx[10]; //从机接收数据存放
uchar send101[9]={0x00,0x03,0x06,0x00,0x00,0x00,0x00,0x00,0x00}; //从机待发数据存放
uchar timercnt; //时间间隔
// modbus协议 定义 :
uchar byterev;
bit nodeok;
uchar revptr;
bit crcok;
uchar datalen;
bit procok;
Uint16 delaycnt;
/* CRC 高位字节值表 */
const Uint16 code auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC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xAD, 0x6D, 0xAF, 0x6F,
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
} ;
Uint16 Crc16(uchar *puchMsg, Uint16 usDataLen) //获取crc高8位和低8位
{
Uint16 uchCRCHi = 0xFF ; /* 高CRC字节初始化 */
Uint16 uchCRCLo = 0xFF ; /* 低CRC 字节初始化 */
Uint16 temp16;
Uint32 uIndex ; /* CRC循环中的索引 */
while (usDataLen--) /* 传输消息缓冲区 */
{
temp16=*puchMsg++;
uIndex = uchCRCHi ^ temp16 ; /* 计算CRC */
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ;
uchCRCLo = auchCRCLo[uIndex] ;
}
return (uchCRCHi << 8 | uchCRCLo) ;
}
void UARTInterrupt(void) interrupt 4 using 2 //串口中断处理函数
{
if(RI==1)
{
RI = 0;
byterev=SBUF;
mdproc(byterev);
}
else
TI = 0;
}
void mdproc(uchar b) //功能码03处理 ,如果地址正确 ,接收报文并crc检查 , crc检查正确 ,向plc主机发送数据
{ Uint16 temp16_1,temp16_2;
if(b==0x12)
{
datalen=7;
mrx[0]=b;
revptr=1;
}
else
{ mrx[revptr++]=b;
if(revptr==8)
{
temp16_1=Crc16(mrx,6);
temp16_2=mrx[6];
temp16_2<<=8;
temp16_2|=mrx[7];
if(temp16_1==temp16_2)
{ crcok=1;
led2=0;
}
}
}
}
void SendOneByte(unsigned char c)
{
SBUF = c;
while(!TI);
TI = 0;
}
void InitUART(void)//串口初始化 9.6kb/S
{
TMOD = 0x20; //T1方式2 (8位T/C,TL用于计数,当TL溢出时将TH中的值自动写入TL)
SCON = 0x50;//串口工作方式1,允许串口接收
TH1 = 256 - (11059200/12/32)/9600; //定时器初值高8位设置
TL1 = TH1; //定时器初值低8位设置
PCON = 0x00; //频率不加倍
ET1 = 0;
EA = 1;//开总中断
ES = 1;//开串口中断
TR1 = 1;//启动T1
}
void DelayM(unsigned int a){ //延时函数 1MS/次
unsigned char i;
while( --a != 0){
for(i = 0; i < 125; i++);
}
}
unsigned char ad0831read(void)//AD转换值
{
unsigned char i=0,tmp=0;
/****
SDA2=1;
CS2=0;
_nop_();
_nop_();
SCL2=0;
_nop_();
_nop_();
SCL2=1;
_nop_();
_nop_();
SCL2=0;
_nop_();
_nop_();
SCL2=1;
_nop_();
_nop_();
SCL2=0;
_nop_();
_nop_();
for(i=0;i<8;i++){
tmp<<=1;
if(SDA2)
tmp++;
SCL2=1;
_nop_();
_nop_();
SCL2=0;
_nop_();
_nop_();
}
CS2=1;
*****/
return tmp;
}
void main(void)
{
InitUART();
crcok=0;
nodeok=0;
timercnt=0x00;
datalen=0;
send101[0]=0x12; //从机地址
led2=0;
DelayM(1000);
led2=1;
DelayM(1000);
rw=0; //487收发控制端
while(1)
{/*
if(crcok==1) //接收数据处理时间完毕
{ reving=1;
led1=1;
send101[3]=0x01;
send101[4]=0x02;
send101[5]=ad0831read(); //获取 ADC 数据
for(i=0;i<9;i++){mtx=send101;}
temp16=Crc16(send101,9);
mtx[9]=(temp16>>8)&0x00ff;
mtx[10]=temp16&0x00ff;
rw=1; //487允许发送
for(i=0;i<11;i++)
{ temp8=mtx;
SendOneByte(temp8);
}
crcok=0;
rw=0; //487允许接收
reving=0; // 收到报文并回复时,点亮led
}*/
}
}
|