求大神帮我看看程序,我是用超声波模块测距,并将测得距离通过串口在组态王上显示...并且通过数码管显示出来。问题是按照这个程序,数码管不亮(这让我非常不解),且和组态王通信有问题,应该是定时器1中断时间的问题,不知道设置多少合适呀?
#include
#include
#define uchar unsigned char
#define uint unsigned int
//#define N 800
sbit SDATA_595 = P2^5; //串行移位输入
sbit SHcp_595 = P2^7; //移位寄存器时钟输入SHcp
sbit STcp_595 = P2^6; //存储寄存器时钟输入STcp
sbit fengmingqi=P2^0;
unsigned int time=0;
unsigned int timer=0;
unsigned int t=0;
//unsigned char z=100;
unsigned char posit=0;
unsigned long S=0;
bit flag =0;
unsigned char const discode[] ={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xBF,0xff/*-*/};
unsigned char const positon[3]={0x10,0x08,0x04};
unsigned char disbuff[4]={0,0,0,0,};
sbit RX=P1^2;
sbit TX=P1^3;
sbit Q1=P1^4;
sbit Q2=P1^0;
sbit Q3=P1^1;
sbit K1=P1^7;
sbit K2=P1^6;
sbit K3=P1^5;
sbit K4=P2^1;
sbit S1=P2^2;
sbit S2=P2^3;
unsigned char rec[50];
unsigned char code error[]={0x40,0x30,0x46,0x2a,0x2a,0x37,0x36,0x0d};
unsigned char send[]={0x40,0x30,0x46,0x30,0x31,0x00,0x00,0x00,0x00,0x0d};
unsigned char i;
unsigned char ath(unsigned char,unsigned char);
unsigned int hta(unsigned char);
//void uart(void);
void delay(uint);
/*-----------延时子程序---------*/
void delay(uint z)
{
uint f;
for(;z>0;z--)
for(f=100;f>0;f--);
}
void OUT_595(void)
{
STcp_595 = 0;
_nop_();
_nop_();
STcp_595=1; //上升沿将数据送到输出锁存器
_nop_();
_nop_();
STcp_595 = 0 ;
}
void WR_595(uchar x)
{
uchar temp;
uchar j;
temp = x;
SHcp_595 = 0;
for (j=0; j<8; j++)
{
temp = temp<<1;
SDATA_595 = CY;
SHcp_595 = 1; //上升沿发生移位
_nop_();
_nop_();
SHcp_595 = 0;
_nop_();
_nop_();
}
OUT_595();
}
/*void baojing()
{
uchar x,i;
x=0xfe;
fengmingqi=1;
for(i=0;i<8;i++)
{
WR_595(x);
delay(N);
x=(x<<1)|0x01;
delay(N);
}
}
*/
void Display(void) //扫描数码管
{
if(++posit>=3)
posit=0;
P3=positon[posit] ;
if(posit==1)
{
P0=(discode[disbuff[posit]])&0x7f;
}
else
{
P0=discode[disbuff[posit]];
}
}
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=130-(time*17)/100; //算出来是CM x10
if((S>=700)||flag==1) //超出测量范围显示“-”
{
flag=0;
disbuff[0]=10; //“-”
disbuff[1]=10; //“-”
disbuff[2]=10; //“-”
}
else
{
disbuff[0]=S%1000/100;
disbuff[1]=S%1000%100/10;
disbuff[2]=S%1000%100%10;
}
if(S<=55||S>=95)
{
WR_595(0x00);
fengmingqi=1;
}
else
{
WR_595(0xff);
fengmingqi=0;
}
if(K1==1&&K2==1&&S<=50){Q1=0;Q2=1;}
if(K1==1&&K2==0&&S<=50){Q2=0;Q1=1;}
if(K1==1&&S>=100){Q1=1;Q2=1;}
if(K1==1&&S1==0&&S2==1){Q3=0;t=1;}
if(K1==1&&S1==0&&S2==0&&t==1)Q3=0;
if(K1==1&&S1==0&&S2==0&&t==0)Q3=1;
if(K1==1&&S1==1&&S2==0){Q3=1;t=0;}
if(K1==0&&K3==1){Q1=1;Q2=1;}
if(K1==0&&K2==1&&K3==0){Q1=0;Q2=1;}
if(K1==0&&K2==0&&K3==0){Q1=1;Q2=0;}
if(K1==0&&K4==0)Q3=0;
if(K1==0&&K4==1)Q3=1;
}
/********************************************************/
void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
{
flag=1; //中断溢出标志
}
/********************************************************/
void zd3() interrupt 3 //T1中断用来每50MS查询串口数据发送和接收
{
unsigned char a,b;
unsigned int c,d,e=0;
unsigned char i=0;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
c=hta(S);
send[5]=c>>8;
send[6]=(unsigned char)c;
d=0;
for(c=1;c<7;c++)d^=send[c];
d=hta(d);
send[7]=d>>8;
send[8]=(unsigned char)d;
while(e!=1)
{
if(RI)
{
a=SBUF;
RI=0;
if(a==0x40)i=0;
rec=a;
i++;
if(a==0x0d)
{
if(ath(rec[1],rec[2])==15)
{
b=0;
for(a=1;a
if(b==ath(rec[i-3],rec[i-2]))
{
if((ath(rec[3],rec[4])&0x01)==0)
{
for(a=0;a<10;a++)
{
SBUF=send[a];
while(TI!=1);
TI=0;
}
}
}
else
{
for(a=0;a<8;a++)
{
SBUF=error[a];
while(TI!=1);
TI=0;
}
}
}
e=1;
}
}
else
{
TI=0;
}
}
/* timer++;
if(timer>=400)
{
timer=0;
TX=1; //400MS 启动一次模块
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TX=0;
}
*/
}
/*********************************************************/
unsigned char ath(unsigned char a,unsigned char b)
{
if(a<0x40)a-=0x30;
else if(a<0x47)a-=0x37;
else if(a<67)a-=0x57;
if(b<0x40)b-=0x30;
else if(b<0x47)b-=0x37;
else if(b<67)b-=0x57;
return((a<<4)+b);
}
unsigned int hta(unsigned char a)
{
unsigned int b;
b=a>>4;
a&=0x0f;
if(a<0x0a)a+=0x30;
else a+=0x37;
if(b<0x0a)b+=0x30;
else b+=0x37;
b=((b<<8)+a);
return b;
}
void main(void)
{
TMOD=0x11; //设T0为方式1,GATE=1;
TH0=0;
TL0=0;
TH1=(65536-50000)/256; //50MS定时
TL1=(65536-50000)%256;
ET0=1; //允许T0中断
ET1=1; //允许T1中断
TR1=1; //开启定时器
SCON=0x50;
BRT=0xfd;
AUXR=0x11;
EA=1; //开启总中断
RI=0;TI=0;
while(1)
{
while(!RX); //当RX为零时等待
TR0=1; //开启计数
while(RX); //当RX为1计数并等待
TR0=0; //关闭计数
Conut(); //计算
timer++;
if(timer>=400)
{
timer=0;
TX=1; //400MS 启动一次模块
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TX=0;
}
Display();
delay(2);
}
}本文来自论坛,点击查看完整帖子内容。