请问一下,我这个程序编译的时候,没有任何错误,逻辑上也行得通,之前没添加字符转换为数值的时候,
单片机接收到数据还可以返回数据的,但是加了后,就没反应了.protues里面用终端可以检测到数据,但是单片机死活没反应。
急啊!!搞了几天了......
#include "AT89X51.h"
#include "math.h"
#include "head.h"
int delay();
sbit RS=0xA0;
sbit RW=0xA1;
sbit E=0xA2;
sbit K1=P2^5;
sbit K2=P2^3;
sbit Key=P2^6;
char SpeedChar[]="SPEED(n/min):";
char StateChar[]="ZS_SPEED:";
char SPEED[3]="050";
char SPEED_R[3]="000";
char js[16];
char fss[16];
unsigned int RunSpeed=50;
unsigned int x=0;
//int n;
//unsigned int M1=0;
//unsigned int M2=0;
//unsigned int Z=10;
unsigned int i2=0;
unsigned int n1=0,n2=0;
unsigned char a,flag;
unsigned char fs,js_a,m,n,kk=0;
unsigned int ii;
unsigned int k=0;
void sendChar(uchar Value);
unsigned int fsws=0;
main()
{
TMOD=0x26;
EA=1;
TH0=0xff;
TL0=0xff;
ET0=1;
TR0=1;
IT0=1;
EX0=1;
//TH1 = 0xfd;
TH1 = 0xfd;
TL1 = 0xfd;
REN = 1;
SM0 = 0;
SM1 = 1;
EA = 1;
ES = 1;
TR1 = 1;
P2_5=0;
Key=1;
in
ti_lcd();
DoSpeed();
ShowState();
P1=0x01;
while(1){
fas();
if(K2==0){
clock(RunSpeed);
P2_4=P2_4^0x01;
//P0_0=P0_0^0x01;
}
}
}
void SpeedUp() interrupt 1
{
if(RunSpeed>=12)
RunSpeed=RunSpeed-2;
DoSpeed();
P1=0x01;
cmd_wr();
ShowState();
}
void SpeedDowm() interrupt 0
{
if(RunSpeed<=100)
RunSpeed=RunSpeed+2;
DoSpeed();
P1=0x01;
cmd_wr();
ShowState();
}
void ser_int (void) interrupt 4
{
RI = 0;
js[k]=SBUF;
flag=1;
if(js[k]=='#')
{
m=0;
for(ii=0;ii<k;ii++)
//m=(js[0]-48)*100+(js[1]-48)*10+(js[2]-48);
m=m*10+(js[k]-48);
k=0;
}
else k++;
}
void fas()
{
if(flag==1)
{
ES=0;
for(fsws=0;fsws<3;fsws++)
{
SBUF=SPEED[fsws];
while(!TI);
TI=0;
}
//SBUF=m;
ES=1;
flag=0;
}
js_a=js[k];
if(js_a=='Z'){P2_3=1;P2_5=1;} //停止
if(js_a=='A'){P2_3=0;} //启动
if(js_a=='C') //正转
{
unsigned char x_js=js_a;
if(x_js=='C')
{
P2_5=0;
}
}
if(js=='B') //反转
{
unsigned char y_js=js_a;
if(y_js=='B')
{
P2_5=1;
}
}
if(js_a=='Y') //减速
{
if(RunSpeed<=1000)
RunSpeed=RunSpeed+2;
DoSpeed();
}
if(js_a=='E') //加速
{
if(RunSpeed>=12)
RunSpeed=RunSpeed-2;
DoSpeed();
}
else {
m=RunSpeed;
DoSpeed();
}
}
int delay()
{
int a;
start:
RS=0;
RW=1;
E=0;
for(a=0;a<2;a++);
E=1;
P1=0xff;
if(P1_7==0)
return 0;
else
goto start;
}
void inti_lcd()
{
P1=0x38;
cmd_wr();
delay();
P1=0x01;
cmd_wr();
delay();
P1=0x0f;
cmd_wr();
delay();
P1=0x06;
cmd_wr();
delay();
P1=0x0c;
cmd_wr();
delay();
}
void cmd_wr()
{
RS=0;
RW=0;
E=0;
E=1;
}
void show_lcd(int i)
{
P1=i;
RS=1;
RW=0;
E=0;
E=1;
}
void ShowState()
{
int i=0;
while(SpeedChar[i]!=' ')
{
delay();
show_lcd(SpeedChar[i]);
i++;
}
delay();
P1=0x80 | 0x0d;
cmd_wr();
i=0;
while(SPEED[i]!=' ')
{
delay();
show_lcd(SPEED[i]);
i++;
}
delay();
P1=0xC0;
cmd_wr();
i=0;
while(StateChar[i]!=' ')
{
delay();
show_lcd(StateChar[i]);
i++;
}
delay();
P1=0xC0 | 0x0A;
cmd_wr();
i=0;
while(SPEED_R[i]!=' ')
{
delay();
show_lcd(SPEED_R[i]);
i++;
}
}
void clock(unsigned int Delay)
{ unsigned int i;
for(;Delay>0;Delay--)
for(i=0;i<124;i++);
if(Delay--){x++;}
}
/*void delay10ms(unsigned int xms10)
{
unsigned char i1,j1,k;
for(;xms10>0;xms10--)
for(i1=5;i1>0;i1--)
for(j1=4;j1>0;j1--)
for(k=248;k>0;k--);
void delay1s()
{
unsigned char h,i1,j1,k;
for(h=5;h>0;h--)
for(i1=4;i1>0;i1--)
for(j1=116;j1>0;j1--)
for(k=214;k>0;k--);
}
*/
void DoSpeed()
{
SPEED[0]=(1000*6/RunSpeed/100)+48;
SPEED[1]=1000*6/RunSpeed%100/10+48;
SPEED[2]=1000*6/RunSpeed%10+48;
SPEED_R[0]=(n/100)+48;
SPEED_R[1]=n%100/10+48;
SPEED_R[2]=n%10+48;
}
一周热门 更多>