/*ds18b20温度传感器p3.4作为数据传输口 p1.0~3控制显示led的位选 共阳极 经过pnp后 低电平有效p0口是led的字形码晶振:11.0592m设计者:陈仲库设计日期:2006年3月29日*/#include//#include //#include //#include sbit dq= p3^4; sbit p13= p1^3; sbit p12= p1^2; sbit p11= p1^1; sbit p10= p1^0; int flag; unsigned char disp1, disp2,disp3,disp0; unsigned char code ch[]={0xc0,0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82,0xf8,0x80,0x90}; /*0~9*/ void delay(int us) { int s; for ( s=0; s void disp() { p0 = 0xff; p0 = disp3; p13=0; delay(150); p13=1; p0 = 0xff; p0= disp2; p12=0; delay(150); p12=1; p0 = 0xff; p0 = disp1; p11=0; delay(150); p11=1; p0 = 0xff; p0 = disp0; p10=0; delay(150); p10=1; } void rst(void) { dq = 1; delay(2); dq = 0; delay(30); //精确延时 480~960us dq = 1; delay(8); } unsigned int read(void) { int i=0; unsigned int u=0; for (i=0;i<16;i++) { dq=0; u>>=1; dq=1; if(dq) u|=0x8000; delay(4); } return (u); } void write(unsigned char ku) { int i=0; for (i=0;i<8;i++) { dq=0; dq =ku&0x01; delay(3); dq=1; ku>>=1; } } void read0(void) { int t1,t2, t0; unsigned int tp; unsigned int lsb; rst(); write(0xcc); write(0x44); rst(); write(0xcc); write(0xbe); tp=read(); lsb=(unsigned int)(tp*6.25); // t0=lsb/1000; disp0 = ch[t0]; //第1位,最高位p1.2 if(!t0) disp0 = 0xff; t2= lsb%1000; t1=t2/100; disp1= ch[t1]&0x7f; t1=t2%100; t2=t1/10; disp2=ch[t2]; //第3位,最低位p1.2 disp3=ch[t1%10];//第4位,最低位p1.3 } void main(void) { while(1) { read0(); disp(); } }