求大神帮写一个温度计的单片机程序

图片说明
请给这个温度计写个程序,要求P3口三个按键有两个是调整温度上下线,一个是显示实时温度,当实时温度低于下限温度,蜂鸣器响,绿灯亮,高于上限温度。红灯亮,蜂鸣器响,p11红灯,p12绿灯,C语言来写

以前做的一个类似的东西,你可以参考下吧。毕竟这个要依赖硬件,我不好给你写
http://blog.csdn.net/nk_test/article/details/50375196

#include
#include "INTRINS.H"
typedef unsigned int uint;
typedef unsigned char BYTE; //DS18B20的数据口位P3.3
BYTE TPH; //存放温度值的高字节
BYTE TPL; //存放温度值的低字节
void DelayXus(BYTE n);
void DS18B20_Reset();
void DS18B20_WriteByte(BYTE dat);
BYTE DS18B20_ReadByte();
uint temp,temp0; //定义温度变量
int flag=0,mintemp=100,maxtemp=400;//定义温度上下设置标志位和上下限值
sbit DQ = P2^7; //温度传感器接口
sbit LEDred=P0^6; //报警LED接口
sbit LEDgreen=P0^7;

sbit key1=P2^0; //按键接口
sbit key2=P2^1;
sbit key3=P2^2;

sbit wei1=P3^4; //数码位选接口
sbit wei2=P3^5;
sbit wei3=P3^6;
sbit wei4=P3^7;

BYTE code table[]={ //码表
0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};
BYTE code table0[]={
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=110;y>0;y--);
}

void returntemp()
{
DS18B20_Reset(); //设备复位
DS18B20_WriteByte(0xCC); //跳过ROM命令
DS18B20_WriteByte(0x44); //开始转换命令
while (!DQ); //等待转换完成

DS18B20_Reset();                //设备复位
DS18B20_WriteByte(0xCC);        //跳过ROM命令
DS18B20_WriteByte(0xBE);        //读暂存存储器命令
TPL = DS18B20_ReadByte();       //读温度低字节
TPH = DS18B20_ReadByte();       //读温度高字节
temp0=TPH*256+TPL;
temp0=temp0*0.625;

}

/**************************************
延时X*10微秒(STC90C52RC@12M)
不同的工作环境,需要调整此函数
当改用1T的MCU时,请调整此延时函数
**************************************/
void DelayX0us(BYTE n)
{
while (n--)
{
nop();
nop();
}
}

/**************************************
复位DS18B20,并检测设备是否存在
**************************************/
void DS18B20_Reset()
{
CY = 1;
while (CY)
{
DQ = 0; //送出低电平复位信号
DelayX0us(48); //延时至少480us
DQ = 1; //释放数据线
DelayX0us(6); //等待60us
CY = DQ; //检测存在脉冲
DelayX0us(42); //等待设备释放数据线
}
}

/**************************************
从DS18B20读1字节数据
**************************************/
BYTE DS18B20_ReadByte()
{
BYTE i;
BYTE dat = 0;

for (i=0; i<8; i++)             //8位计数器
{
    dat >>= 1;
    DQ = 0;                     //开始时间片
    _nop_();                    //延时等待
    _nop_();
    DQ = 1;                     //准备接收
    _nop_();                    //接收延时
    _nop_();
    if (DQ) dat |= 0x80;        //读取数据
    DelayX0us(6);               //等待时间片结束
}

return dat;

}

/**************************************
向DS18B20写1字节数据
**************************************/
void DS18B20_WriteByte(BYTE dat)
{
char i;

for (i=0; i<8; i++)             //8位计数器
{
    DQ = 0;                     //开始时间片
    _nop_();                    //延时等待
    _nop_();
    dat >>= 1;                  //送出数据
    DQ = CY;
    DelayX0us(6);               //等待时间片结束
    DQ = 1;                     //恢复数据线
}

}
void keyscan() //按键扫描
{
if(flag==0) //判断是否不在上下线设置状态
temp=temp0; //显示正常温度值
if(flag==1) // //判断是否在下线设置状态
temp=mintemp;//显示最小设置温度
if(flag==2) //判断是否在上线设置状态
temp=maxtemp;//显示最大设置温度
if(key1==0)//判断温度设置键是否按下
{
delay(3);// 延时去抖
if(key1==0)//确定按下
{
flag++; //设置标志位加1
while(!key1); //松手检测
if(flag==3) //达到3清零
flag=0;
}
}
if(flag!=0)
{
if(key2==0)
{
delay(3);
if(key2==0)
{
switch(flag)
{
case 1: mintemp=mintemp+10;
if(mintemp>1250)
{
mintemp=0;

}
break;
case 2: maxtemp=maxtemp+10;
if(maxtemp>1250)
{
maxtemp=0;

}
break;

}
}
while(!key2);

}
if(key3==0)
{
delay(3);
if(key3==0)
{
switch(flag)
{
case 1: mintemp=mintemp-10;
if(mintemp<0)
{
mintemp=1250;

}
break;
case 2: maxtemp=maxtemp-10;
if(maxtemp<0)
{
maxtemp=1250;

}
break;

}
}
while(!key3);

}
}
}
void display()//显示函数
{
BYTE set,shi,ge,dian;//显示各位分开变量
if(flag==0) //正常显示即不在上下线设置状态
set=0xff; //第一位数码管不显示
if(flag==1) //下线设置
set=0xf7;
if(flag==2) //上线设置
set=0xfe;
shi=temp/100;
ge=temp/10%10;
dian=temp%10;
wei1=0;
wei2=0;
wei3=0;
wei4=0;

wei1=1;
P1=set;
delay(2);
wei1=0;

wei2=1;
P1=table[shi];
delay(2);
wei2=0;

wei3=1;
P1=table0[ge];
delay(2);
wei3=0;

wei4=1;
P1=table[dian];
delay(2);
wei4=0;
P1=0xff;

}
void baojing()
{
if(temp0>=maxtemp) //当温度高于设置温度最高值
{
LEDred=0; //红色LED发光
}
else
{
LEDred=1;

}
if(temp0<=mintemp) //当温度低于设置温度最低值
{
LEDgreen=0; //绿色LED发光
}
else
{
LEDgreen=1;
}
}
void main()
{
while(1)
{
returntemp(); //返回温度值
keyscan(); //按键扫描
display(); //显示
if(flag==0) //判断是否在正常显示下
baojing();//检查温度并报警

}
}