⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 18b20_led.c

📁 ds18b20数码管显示
💻 C
字号:
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
/*------------------------------------------------*/
#define uint       unsigned int
#define uchar      unsigned char
//===========================================================
sbit DQ = P0^0;  //定义通信端口
//=========================codes==========================================
uchar code seg0[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
uchar code seg1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
//============================延时8us=======================================================
void Delay2(uchar i)
{
  while(--i);
  }
void Delay1(uint i)//延时
{
	while(i--);  
}
//===================================================
 void Init_18B20(void)
{
 DQ = 1;
  _nop_();
 DQ = 0;      //拉低数据线,准备Reset OneWire Bus;
 Delay2(125);  //延时510us,Reset One-Wire Bus.
 Delay2(125);
 DQ = 1;      //提升数据线;
Delay2(15);  //延时35us;
 while(DQ)     //等待Slave 器件Ack 信号;
 { _nop_(); }
Delay2(60);  //延时125us; 
 DQ = 1;      //提升数据线,准备数据传输;
}
//********************/
//******write1820****
void WriteAByte(uchar a)
{
    uchar i;
 for(i=0;i<8;i++)
 {if(a & 0x01)  //低位在前;
   {DQ = 0;    //结束Recovery time;
    _nop_();_nop_();_nop_();
    DQ = 1;   } //发送数据;
  else
   DQ = 0;    //结束Rec time;
    _nop_();_nop_();_nop_();  
    //DQ = 0;  } //发送数据;
 Delay2(30);  //等待Slave Device采样;
  DQ = 1;      //Recovery;
  _nop_();      //Recovery Time Start;
  a >>= 1;
  }

}
//*******read1820************
 unsigned char ReadAByte(void)
{
unsigned char i;
unsigned char tmp=0;
 DQ = 1;  
 _nop_();     //准备读;
 for(i=0;i<8;i++)
 {
  tmp >>= 1;    //低位先发;
  DQ = 0;      //Read init;
  _nop_();     //2ms;
  DQ = 1;      //必须写1,否则读出来的将是不预期的数据;
  Delay2(2);   //延时9us;
  _nop_();
  if(DQ)      //在12us处读取数据;
   tmp |= 0x80;
  Delay2(30);  //延时65us;
  DQ = 1;  
   _nop_();   //恢复One Wire Bus;
  }
 return tmp; 
 }
 //===============================================
void GetTemp(uchar Temp[2])
{
	uchar TempH,TempL,LSL4,LSH4,MSL3;//MSH5;
	bit byte = 0;
	Init_18B20();
	WriteAByte(0xcc);
	WriteAByte(0x44);
	//Delay(200);
	Init_18B20();
	WriteAByte(0xcc);
	WriteAByte(0xbe);
	TempL = ReadAByte();
	TempH = ReadAByte();
	LSL4 = TempL&0x0f;
	LSH4 = TempL&0xf0;
	LSH4 >>= 4;
	MSL3 = TempH&0x07;
	Temp[0] = (((LSL4&0x08)>>3)*0.5+((LSL4&0x04)>>2)*0.25+((LSL4&0x02)>>1)*0.125+(LSL4&0x01)*0.0625)*100;
    Temp[1] = LSH4 + MSL3*16;
   }
/*----------------------------------------------------------------*/

void main()
{
	uchar Temp[2],iL=0,jL=0,jH=0,iH=0,a=0,b=0,c=0,d=0;
	while(1){
	GetTemp(Temp);
	iH = Temp[1]/10;
	iL = Temp[1]%10;
	jH = Temp[0]/10;
    jL = Temp[0]%10;
	a=iH;b=iL;c=jH;d=jL;
	P2=0x01;
	P1=seg0[a];
	Delay2(2000);
	P2=0x02;
	P1=seg1[b];
	Delay2(2000);
	P2=0x04;
	P1=seg0[c];
	Delay2(2000);
	P2=0x08;
	P1=seg0[d];
   	Delay2(2000);
}

	}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -