#include<reg51.h>
#include<ds18b20.c>
code unsigned char seven_seg[]= {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char bit_scan[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char display_num[8];
unsigned char cp0,cp1;
unsigned int temp_num;
unsigned int temp1,temp2;
sbit le = P1^0;
void display_dat(void)
{
display_num[0] = seven_seg[0];
display_num[1] = seven_seg[8];
display_num[2] = seven_seg[0];
display_num[3] = seven_seg[1];
if((temp_dat & 0xf800)< 0xf800)
{
temp_num = temp_dat & 0x0fff;
temp2 = (temp_num & 0x000f) * 0.625;
temp1 = temp_num >> 4;
display_num[4] = 0xff;
display_num[5] = seven_seg[temp1 / 10];
display_num[6] = seven_seg[temp1 % 10] & 0x7f;
display_num[7] = seven_seg[temp2];
}
else
{
temp_num = ~temp_dat + 1;
temp2 = (temp_num & 0x000f) * 0.625 + 0.5;;
temp1 = temp_num >> 4;
display_num[4] = 0xbf;
display_num[5] = seven_seg[temp1 / 10];
display_num[6] = seven_seg[temp1 % 10] & 0x7f;
display_num[7] = seven_seg[temp2];
}
}
void latch(void){le = 1;le = 0;}
void timer0_isr(void) interrupt 1
{
TH0 = (65536 - 2000) / 256;
TL0 = (65536 - 2000) % 256;
P0 = 0xff;
P0 = bit_scan[cp1];latch();P0 = display_num[cp1];
cp1++;
if(cp1 >= 8)cp1 = 0;
cp0++;
}
void timer0_init(void)
{
TMOD = 0x01;
TH0 = (65536 - 2000) / 256;
TL0 = (65536 - 2000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main(void)
{
timer0_init();
while(1)
{
display_dat();
if(cp0 == 200)
{
cp0 = 0;
read_temp_da18b20();
}
}
}
unsigned int temp_dat,temp_num;
sbit dq = P1^2;
void delay(unsigned int x){while(x--);}
void ds18b20_init(void)
{
unsigned char i = 255;
dq = 1;
dq = 0;delay(80);
dq = 1;
while(dq & i--);
delay(20);
}
void write_ds18b20(unsigned char x)
{
unsigned char i;
for(i = 0;i < 8;i++)
{
dq = 1;
dq = 0;
dq = (bit)(x & 0x01);
delay(10);
dq = 1;
x = x >> 1;
}
delay(8);
}
unsigned char read_ds18b20(void)
{
unsigned char i,j;
for(i = 0;i < 8;i++)
{
dq = 1;
dq = 0;
j = j >> 1;
dq = 1;
if(dq)j = j | 0x80;
delay(10);
}
return(j);
}
void read_temp_da18b20(void)
{
unsigned int i,j;
ds18b20_init();
write_ds18b20(0xcc);
write_ds18b20(0x44);
ds18b20_init();
write_ds18b20(0xcc);
write_ds18b20(0xbe);
i = read_ds18b20();
j = read_ds18b20();
j = j << 8;
temp_dat = i | j;
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/Aaa13507646634/article/details/143998793
|