希望好心人帮帮我- - 下面是源代码
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P1^7;
sbit key1=P1^3;
sbit key2=P1^4;
sbit dianji=P3^1;
float ff;
uint y3;
uchar shi,ge,xiaoshu,sheding=20,gaonum,dinum;
uchar code dispcode[]={ //段码
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code tablel[]={ //带小数点的段码
0xbf,0x86,0xdb,0xcf,
0xe6,0xed,0xfd,
0x87,0xff,0xef};
uchar dispbitcode[]={ //位选
0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
uchar dispbuf[8]={0,0,0,0,0,0,0,0};
void Delay(uint num)// 延时函数
{
while( --num );
}
void digitalshow(uchar a4,uchar a3,uchar a2,uchar a1,uchar a0)
{
dispbuf[0]=a0;
dispbuf[1]=a1;
dispbuf[2]=a2;
dispbuf[3]=a3;
dispbuf[4]=a4;
P2=0xff;
P0=dispcode[dispbuf[0]];
P2=dispbitcode[5];
Delay(1);
P2=0xff;
P0=dispcode[dispbuf[1]];
P2=dispbitcode[4];
Delay(1);
P2=0xff;
P0=dispcode[dispbuf[2]];
P2=dispbitcode[2];
Delay(1);
P2=0xff;
P0=tablel[dispbuf[3]];
P2=dispbitcode[1];
Delay(1);
P2=0xff;
P0=dispcode[dispbuf[4]];
P2=dispbitcode[0];
Delay(1);
}
void dmsec(uint count)
{
uint i; // 1ms 延时
while(count--)
{
for(i=0;i<125;i++){}
}
}
void tmreset(void)
{
DQ=0;
Delay(90); // 精确延时 大于 480us
DQ=1;
Delay(4); // 90,4 可以小范围变化
}
void tmpre(void)
{
while(DQ);
while(~DQ);
Delay(4);
}
bit tmrbit(void)
{
uint i;
bit dat;
DQ=0;
i++; // i++;大概 1us
DQ=1;
i++;
i++;
dat=DQ;
Delay(8);
return(dat);
}
uchar tmrbyte(void) //读一个比特
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=tmrbit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
void tmwbyte(uchar dat) //写一个比特
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1; // 从低位开始
if(testb) // Write 1
{
DQ=0; // 先拉低
i++;
i++; // >1us
DQ=1;
Delay(4);
}
else // Write 0
{
DQ=0;
Delay(4);
DQ=1;
i++;
i++; // 再拉高
}
}
}
void tmstart(void) //ds1820 开始转换
{
dmsec(1);
tmreset();
tmpre();
dmsec(1);
tmwbyte(0xcc); // skip rom
tmwbyte(0x44); // 转换
}
uchar tmrtemp(void) //读取温度
{
uchar a,b;
tmreset();
tmpre();
dmsec(1);
tmwbyte(0xcc); // skip rom
tmwbyte(0xbe); // 转换
a=tmrbyte(); // LSB 低 8 位
b=tmrbyte(); // MSB 高 8 位
y3=b;
y3<<=8;
y3=y3|a;
ff=y3*0.0625;
y3=ff*10+0.5;
return(y3);
}
void keyscan(void)
{
if(key1==0)
{
dmsec(5);
if(key1==0)
{
sheding++;
if(sheding==100)
sheding=20;
}
while(!key1);
}
else if(key2==0)
{
dmsec(5);
if(key2==0)
{
sheding--;
if(sheding==0)
sheding=20;
}
while(!key2);
}
}
void deal(uint tmp) //温度处理
{
if(tmp<=sheding)
{
gaonum=0;
dinum=4;
}
else if((tmp>sheding)&&(tmp<=(sheding+5)))
{
gaonum=1;
dinum=3;
}
else if((tmp>(sheding+5))&&(tmp<=(sheding+10)))
{
gaonum=2;
dinum=2;
}
else if((tmp>(sheding+10))&&(tmp<=(sheding+15)))
{
gaonum=3;
dinum=1;
}
else
{
gaonum=4;
dinum=0;
}
}
void dianjik() //电机控制
{
uchar q,i;
for(q=0;q<dinum;q++)
{
dianji=0;
digitalshow(shi,ge,xiaoshu,sheding/10,sheding%10);
for(i=255;i>0;i--)
{
digitalshow(shi,ge,xiaoshu,sheding/10,sheding%10);
}
}
for(q=0;q<gaonum;q++)
{
dianji=1;
digitalshow(shi,ge,xiaoshu,sheding/10,sheding%10);
for(i=255;i>0;i--)
{
digitalshow(shi,ge,xiaoshu,sheding/10,sheding%10);
}
}
}
void main(void)
{
uint last;
dianji=0;
tmstart();
dmsec(450); // 初始化 ds18b20
while(1)
{
tmstart(); // ds1820 开始转换
dmsec(2);
last=tmrtemp()+256; // 读取温度
shi=last/100;
ge=(last%100)/10;
xiaoshu=(last%100)%10;
keyscan();
dmsec(2);
deal(last/10);
dianjik();
}
}
|