|
c51接收部分一接收像死机了一样!我在串口助手中发送的数据为:02000102
节点号为2,
一发送本来显示的界面为:
1.输入开始
2.节点设置
请选择:
可是一发送,在第0行0列显示请选择*
按键也没有反应,像列了一样?郁闷死了?
#include
#include
#include
//51 Receive Protocol: |节点号|长度|数据
#define uchar unsigned char
#define uint unsigned int
#define keyborad P0
#define MAXLEN 6 //最大位数
#define MAXLEN_STATION 3 //最大的节点长度
#define True 1
#define False 0
#define READY 0x66 //握手信号
#define BUSY 0x15 //忙信号
//#define ERROR 0xf0
//#define SUCCESS 0xff
#define BUFFER_LEN 10 //缓冲区长度
//0x41=A,0x46=F
uchar digits[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45};
uint StationID; //地址号
uchar StationH,StationL; //高8位,低8位
//uchar addrH,addrL;
uchar station[MAXLEN_STATION];
uchar WorkNo[MAXLEN]={10,10,10,10,10,10};
uchar Qty[MAXLEN]={10,10,10,10,10,10};
uchar inputbuf[MAXLEN]={10,10,10,10,10,10};
uchar sendbuffer[BUFFER_LEN]; //发送缓存
uchar ReceiveBuf[BUFFER_LEN]={10,10,10,10,10,10}; //接收缓存
uchar len; //个数
uchar ipoint; //输入指针
uchar tmp;
uchar Status_Key=0; //上次按键
uchar groupmenu; //菜单组别
bit Write_Success;
bit IsWorkNo;
bit IsQty;
sbit LED=P2^5;
sbit wela=P2^7;
sbit dula=P2^6;
uchar key_press();
uchar key_scan();
void init_serial(); //串口初始化
void delaymain(uint ms);
uchar CheckNodeLen(uchar *s);
uchar CheckWorkNoLen(uchar *s);
void clrinputbuf(void);
void displaymenu(uchar level);
//void senddata(uchar *buffer,uchar length);//发送数据
void IntToArray(uint num,uchar *array);
uint ArrayToInt(uchar array[]);
uchar nodeid[3];
//把整数转换为单个数字并存于数组
void IntToArray(uint num,uchar *array)
{
// uchar tmp;
uchar A;//商数
uchar B;//余数
A=num;
tmp=num;
while(A>0)
{
A=tmp / 10;
B=tmp % 10;
*array=B;
array++;
tmp=A;
}
}
//把数组中的数转为数字
uint ArrayToInt(uchar array[])
{ uchar i;
uint id;
id=0;
for(i=0;i
id=id*10+array;
return id;
}
void senddata()
{ uchar i;
i=0;
tmp=BUSY;
while(tmp!=READY)
{
SBUF=READY; //发送握手信号
while(TI==0);
TI=0;
RI=0;
while(RI==0); //等待接收完成应答
tmp=SBUF;
RI=0;
}
for(i=0;i
{
SBUF=station;
while (TI==0);
TI=0;
}
for(i=0;i
{
SBUF=WorkNo;
while(TI==0);
TI=0;
}
for(i=0;i
{
if ( Qty!=10 )
{
SBUF=Qty;
while(TI==0);
TI=0;
}
}
}
void init_serial()
{
// SCON: |SM0|SM1|SM2|REN|TB8|RB8|TI|RI
SCON=0x50;//串口工作方式2
ES=1;//开串口中断
//PCON: |SMOD| | | | | | |
PCON=0x00;//波特率 9600
//TMOD:TMOD: |GATE|C/T|M1|M0|GATE|C|M1|M0
TMOD=0x20;//定时器T1工作方式2
TH1=0xfd;//初值
//TL1=0xfd;
//控制寄存器TCON: |TF1|TR1|TF0|TR0
TR1=1;//开定时器
EA=0; //关总中断
}
void displaymenu(uchar level)
{
if (level==0)
{
lcm_clr();
LCD_set_xy(1,1);
lcm_w_word("1.输入开始");
LCD_set_xy(2,1);
lcm_w_word("2.节点设置");
LCD_set_xy(3,1);
lcm_w_word("请选择:");
groupmenu=0;
}
if (level==11)
{
lcm_clr();
LCD_set_xy(1,0);
lcm_w_word("A-工单号 B-PCS");
groupmenu=11;
}
if (level==21)
{
lcm_clr();
LCD_set_xy(1,0);
lcm_w_word("旧节点号:");
groupmenu=21;
}
}
void clrinputbuf(void)
{
uchar i;
for (i=0;i
{inputbuf=10;}
ipoint=0;
}
void delaymain(uint ms)
{
uint i,j;
for (j=0;j
for (i=0;i<120;i++);
}
void ClrWorkNo()
{
uchar i;
for(i=0;i<6;i++)
{ WorkNo=10;}
}
void clrQty()
{
uchar i;
for(i=0;i<6;i++)
{ Qty=10;}
}
//检查数据是否有效
uchar CheckQtyLen(uchar array[])
{
uchar len,i;
len=0;
i=0;
for(i=0;i
{
if (array!=10) { len++;break;}
}
if ( len>0 ) return True;
else return False;
}
//检查工单号长度是否为6位
uchar CheckWorkNoLen(uchar array[])
{
uchar len, i;
i=0;
len=0;
for(i=0;i<6;i++)
{ if (array==10)
{ break; }
len=len+1;
}
//write_data(digits[len]);
if (len==6) return True;
else return False;
}
//检查节点长度是否为3位
uchar CheckNodeLen(uchar *s)
{
uchar i;
i=0;
while(*s != 10)
{
s++;
i++;
}
//write_data(i+0x30);
if (i<3 || i>3 ) return False;
else return True;
}
//判断是否有键按下
uchar key_press()
{
uchar key;
keyborad=0xf0;
key=keyborad;
key=key&0xf0;
if(key==0xf0)
{
return 0;
}
else
{
return 1; //有键被按下
}
}
//键盘扫描 板载低四位行扫,高四位列扫
uchar key_scan()
{
uchar key;
if(key_press()) //是否有键按下
{
//P3=0xfe;
//key=P3;
keyborad=0xfe;
key=keyborad;
switch (key)
{
case 0xee:return 1;
case 0xde:return 2;
case 0xbe:return 3;
case 0x7e:return 10;
}
keyborad=0xfd;
key=keyborad;
switch (key)
{
case 0xed:return 4;
case 0xdd:return 5;
case 0xbd:return 6;
case 0x7d:return 11;
}
keyborad=0xfb;
key=keyborad;
switch (key)
{
case 0xeb:return 7;
case 0xdb:return 8;
case 0xbb:return 9;
case 0x7b:return 12; //C
}
keyborad=0xf7;
key=keyborad;
switch (key)
{
case 0xe7:return 15; //*
case 0xd7:return 0; //0
case 0xb7:return 14; //# --E
case 0x77:return 13; //D
}
} //if end
}
/*
//接收中断
void UART_Routine(void) interrupt 4
{
addrL=SBUF;
receive(ReceiveBuf);
}
*/
|
|