当设置下限为负数时 从0一下就到8.5多 设置不成负数 求大神帮忙找一下错误 我完全是小白一个。程序如下
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit d1=P2^2;
sbit d2=P2^1;
sbit d3=P2^0;
sbit d4=P2^3;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit beep=P3^3;
sbit DS=P1^2;
sbit ACC_7=ACC^7;
uint count=1000,alarm=300;
uchar shu;
uchar shi,fen,ri,yue,nian,xq,miao,ss;
uint temp;
uchar flag;
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90};
uchar code tab1[]={0x40,0x79,0x24,0x30,
0x19,0x12,0x02,0x78,0x00,0x10};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=120;y>0;y--);
}
void dsreset()
{
uint i;
DS=0;
i=103;
while(i>0)i--;
DS=1;
i=4;
while(i>0)i--;
}
uchar tmpread()
{
uchar j,k,dat;
uint i;
for(j=1;j<=8;j++)
{
DS=0;i++;
DS=1;i++;i++;
k=DS;
i=8;while(i>0)i--;
dat=(k<<7)|(dat>>1);
}
return(dat);
}
void tmpwritebyte(uchar dat)
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if(testb)
{
DS=0;
i++;i++;
DS=1;
i=8;while(i>0)i--;
}
else
{
DS=0;
i=8;while(i>0)i--;
DS=1;
i++;i++;
}
}
}
void tmpchange()
{
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0x44);
}
uint tmp()
{
float tt;
uchar a,b;
dsreset();
delay(1);
tmpwritebyte(0xcc);
tmpwritebyte(0xbe);
a=tmpread();
b=tmpread();
temp=b;
temp<<=8;
temp=temp|a;
if(b>127)
{
flag=1;
ss=flag;
temp=~temp+1;
}
tt=temp*0.0625;
temp=tt*10+0.5;
return(temp);
}
void displayTemp(uint temp)
{
uchar ge,shi,bai,qian,ser;
d1=0;
d1=0;
d3=0;
d4=0;
dsreset();
ser=temp/10;
SBUF=ser;
qian=temp/1000;
bai=temp/100%10;
shi=temp/10%10;
ge=temp%10;
if(flag==1)
{
flag=0;
P0=0xbf;
d1=1;
delay(2);
d1=0;
}
if(qian!=0)
{
P0=tab[qian];
d1=1;
delay(2);
d1=0;
}
else
{
P0=0xff;
d1=1;
delay(2);
d1=0;
}
if(temp>99)
{
P0=tab[bai];
d2=1;
delay(2);
d2=0;
}
P0=tab1[shi];
d3=1;
delay(2);
d3=0;
P0=tab[ge];
d4=1;
delay(2);
d4=0;
}
uint keyscan()
{
if(key1==0)
{
delay(5);
if(key1==0)
{
while(!key1);
shu++;
}
}
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
count=count+10;
if(shu==2)
{
alarm+=10;
}
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
count=count-10;
if(shu==2);
{
alarm-=10;
}
}
}
return(count);
}
void main()
{
beep=0;
delay(10);
while( 1 )
{
tmpchange();
if(shu==0)
{
displayTemp(tmp( ));
}
keyscan();
if(shu==1)
{
displayTemp(count);
}
if(shu==2)
{
displayTemp(alarm);
}
if(shu==3)
{
shu=0;
}
if(temp>count||(temp<alarm))
{
beep=0;
}
else
beep=1;
}
}
|