📄 18b20.c
字号:
#include <reg51.h>
#include <intrins.h>
#include"7219.h"
#define uint unsigned int
#define uchar unsigned char
uint qian,bai,shi,ge;
float temps;//距离的个位、十位、百位
uchar k[]={0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b};
sbit temp=P0^6;
bit yes;
void delay1820(uchar w)
{
unsigned char k;
for(k=w;k>0;k--)
{}//延时计算方法 w*8+19
}
void delay13us1820()
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
void init1820()
{
temp=0;
delay1820(90);
temp=1;
delay13us1820();
while(temp);
delay1820(4);
temp=1;
delay1820(60);
}
void write1820(uchar wdata)
{
char i,t;
for(i=8;i>0;i--)
{
t=wdata&0x01;
temp=0;
_nop_();
_nop_();
if(t)
temp=t;
delay1820(6);
temp=1;
wdata=wdata>>1;
delay1820(3);
}
}
uchar read1820()
{
char i;
uchar dat;
for(i=8;i>0;i--)
{
dat>>=1;
temp=0;
_nop_();
temp=1;
delay13us1820();
if(temp)
dat|=0x80;
temp=1;
delay1820(6);
}
return(dat);
}
void max7219show()
{
Write(0x04,k[ge]);
Write(0x03,k[shi]);
Write(0x02,k[bai]);
Write(0x01,k[qian]);
}
void readtemp()
{
uint templ,temph;
init1820();
write1820(0xcc);
write1820(0x44);
init1820();
write1820(0xcc);
write1820(0xbe);
templ=read1820();
temph=read1820();
//templ=(templ>>4)&0x0f;
temph=(temph<<8);//&0x07;
temps=templ|temph;
temps=temps*0.0625;
temps=temps*100;
}
void work(void)
{ uint value;
value=temps;
qian=value/1000;
bai=(value-qian*1000)/100;
shi=(value-qian*1000-bai*100)/10;
ge=value-qian*1000-bai*100-shi*10;
}
void main()
{
init7219();
while(1)
{
readtemp();
work();
max7219show();
delay1820(50000);
delay1820(50000);
delay1820(50000);
delay1820(50000);
delay1820(50000);
delay1820(50000);
delay1820(50000);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -