应该没什么问题的啊,就是不通讯 ! 程序如下:
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar idata InBuffer[11];
uchar idata revbuffer[5];
sbit ctrl01=P2^0;
sbit ctrl02=P2^1;
sbit ctrl03=P2^2;
sbit ctrl04=P2^3;
sbit ctrl05=P2^4;
sbit ctrl06=P2^5;
void sendanswer(uchar datalen);
void init_io();
uchar readaddr();
void main()
{
PCON=0x00;
EA=1;
ES=1;
TI=0;
RI=0;
REN=1;
IP=0x10;
SCON=0x50;
TCON=0x00;
TMOD=0x21;
TL1=0xfd;
TH1=0xfd;
while(1);
}
uchar readaddr()
{uchar myaddr,i;
P0=P0|0xf0;
i=P0;
i=~i;
myaddr=i+0xA0;
return myaddr;
}
void uart_com()interrupt 4 using 1
{ uchar temp;
uchar checkchar;
uchar CharN;
bit cmdend = 0;
uchar MeterN;
RI=0;
if(SBUF!=0x01)
{return;}
RI=0;
if (SBUF == 0xFF)
{
MeterN=0xff;
}
else if ((SBUF>=0xa0) && (SBUF<=0xb0))
{
MeterN=SBUF-0xa0;
}
else
{
return;
}
InBuffer[0]=0x01;
InBuffer[1]=SBUF;
RI=0;
cmdend = 0;
CharN = 2;
while(cmdend == 0)
{
if(RI==1)
{ RI = 0;
InBuffer[CharN] = SBUF;
cmdend = 0;
CharN++;
}
if( CharN>=10 ) {break;}
}
do
{
CharN=CharN-1;
}
while(InBuffer[CharN]!=0x17)
if(CharN<3){return;}
/* checkchar = 0;
for(temp=0;temp<=(CharN-2);temp++)
{
checkchar=checkchar+InBuffer[temp];
}
if (checkchar!=InBuffer[CharN-1])
{
return;
} */
sendanswer(2);
if(InBuffer[3]==0x30) {ctrl01=0;}
if(InBuffer[4]==0x30) {ctrl02=0;}
if(InBuffer[5]==0x30) {ctrl03=0;}
if(InBuffer[6]==0x30) {ctrl04=0;}
if(InBuffer[7]==0x30) {ctrl05=0;}
if(InBuffer[8]==0x30) {ctrl06=0;}
}
void sendanswer(uchar datalen)
{ uchar check_char,i ;
uint k=0;
revbuffer[0]=0x01;
revbuffer[1]=readaddr();
revbuffer[2]=0x06;
check_char=revbuffer[0]+revbuffer[1]+revbuffer[2];
revbuffer[datalen+1]=check_char;
revbuffer[datalen+2]=0x17;
if((TI==1)&& k<=(datalen+2) )
{ TI = 0;
ACC=revbuffer[k];
SBUF=ACC;
k++;
}
for(i=0;i<=datalen+2;i++)
{
revbuffer=0x00;
}
}
通讯规约:主机:0x01 + 控制板通讯地址 + 0x10 (命令)
+ 遥控信号1 (CTRL01)状态 0x30(低电平)/0x31(高电平)
+ 遥控信号2 (CTRL02)状态 0x30(低电平)/0x31(高电平)
+ 遥控信号3 (CTRL03)状态 0x30(低电平)/0x31(高电平)
+ 遥控信号4 (CTRL04)状态 0x30(低电平)/0x31(高电平)
+ 遥控信号5 (CTRL05)状态 0x30(低电平)/0x31(高电平)
+ 遥控信号6 (CTRL06)状态 0x30(低电平)/0x31(高电平)
+ 校验位 + 结束(0x17)
从机:0x01 + 控制板通讯地址 + 06H / 15H(06H表示接受正常,15H表示接受异常,)
+校验位+结束(0x17)
(0xFF为广播地址,从机无回答)
问题倒是很简单。不过无注释的代码看起来太痛苦。pass
能不能代码加点注释。现在应该没人看懂你的代码吧?就算能,也没时间去看你代码什么意思。
众位楼主,如果想要用单片机实现真彩液晶显示的话,可以跟我索取资料的。dwin99@live.cn