初级会员
- 积分
- 141
- 金钱
- 141
- 注册时间
- 2017-3-19
- 在线时间
- 17 小时
|
3金钱
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
#include<delay us.h>
#define uint unsigned int
#define uchar unsigned char
sbit ds=P3^3;
sbit led1=P0^0;
sbit guan1=P2^7;
sbit guan2=P2^6;
sbit guan3=P2^5;
sbit guan4=P2^4;
uint shu[]={0Xc0,0Xf9,0Xa4,0Xb0,0X99,0X92,0X82,0Xf8,0X80,0X90};
uint shu1[]={0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10};
bit stuas;
int temp;
float f_temp;
uchar dat;
// ds18b20初始化
bit DS18b20_init(void)
{
ds=1;
_nop_();
ds=0;
Delay10us(65);
ds=1;
Delay10us(1);
while(!ds);
return stuas;
}
//写字节函数
void write_onebyte(uchar dat)
{
uchar i=0;
ds=1;
_nop_();
_nop_();
for(i=0;i<8;i++)
{
ds=0;
Delay10us(6);
ds=dat&0x01;
ds=1;
dat>>=1;
}
}
//读字节函数
uchar read_onebte(void)
{
uchar i=0,dat=0x00;
ds=1;
_nop_();
_nop_();
for(i=0;i<8;i++)
{
ds=0;
Delay6us();
ds=1;
_nop_();
_nop_();
_nop_();
dat>>=1;
if(ds)
{
ds |=0x80;
}
Delay10us(3);
ds=1;
}
return dat;
}
uint read_temp()
{
uchar a,b;
DS18b20_init();
write_onebyte(0xcc);
write_onebyte(0x44); //发出温度转化命令
DS18b20_init();
write_onebyte(0xcc);
write_onebyte(0xbe); //发出读温度命令
a=read_onebte(); //读高8位
b=read_onebte(); //读低8位
temp=(uint)a;
temp<<=8;
temp=temp | (uint)b;
f_temp=temp*0.0625;
temp=f_temp*10+0.5;
f_temp=f_temp+0.05;
return temp;
}
void display(uint t)
{
uint ge,shi,bai,qian;
ge=t%10;
shi=t/10%10;
bai=t/100%10;
qian=t/1000;
P1=shu[qian];
guan1=0;
Delay_ms(1);
guan1=1;
P1=0x00;
P1=shu[bai];
guan2=0;
Delay_ms(1);
guan2=1;
P1=0x00;
P1=shu1[shi];
guan3=0;
Delay_ms(1);
guan3=1;
P1=0x00;
P1=shu[ge];
guan4=0;
Delay_ms(1);
guan4=1;
P1=0x00;
}
void main()
{
DS18b20_init();
while(1)
{
if(stuas==0)
{
read_temp();
display(read_temp());
}
else
{
DS18b20_init();
led1==0;
}
}
}
|
|