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

📄 two_ds1820_lcd_write.c

📁 89C52单片机12M晶振下DS18B20温度传感器测量温度应用子程序源码
💻 C
字号:





//**********************************************************************//
//			                    头文件区                                //
//**********************************************************************//

#include <reg52.h>					/*单片机内部寄存器*/

#include <intrins.h>				/*内部函数调用,如_nop_() 、_crol_()、cror();*/

#include <absacc.h>	    			/*绝对地址访问,如XBYTE[0x0fff0]*/

#include <LCD1602_packet.h>	    			/*LCD 的初始化*/

#define pot 10
unsigned char disp[17]="temperature is:  ";
unsigned char disp_number[]="0123456789.";
unsigned char buffer[17]="                 ";  
//unsigned char temp[10];

unsigned char ROM_0_code[]={0x28,0x25,0xb3,0x84,0x00,0x00,0x00,0x95};
unsigned char ROM_1_code[]={0x28,0x50,0xe2,0x84,0x00,0x00,0x00,0xbe};

/*--------------------------------------------------------------------------
DS1820.H


--------------------------------------------------------------------------*/
sbit TSOR = P1^3;


//**********************************************************************//
//			                延时子程序                                 //
//**********************************************************************//
/*void delay(unsigned int d)
{
	while(d--);
}
*/
void delay100ms(void)
{
	unsigned char i,j,k;
	for(i=0;i<8;i++)
		for(j=0;j<25;j++)
			for(k=0;k<250;k++);
}

void delay15us(void)
{
	unsigned char i;
	for(i=0;i<8;i++);
}

void delay60us(void)
{
	unsigned char i;
	for(i=0;i<30;i++);
}

void write_0_DS(void)//写 bit 0
{//写数据位0:置总线为低电平并保持至少15us,
//然后保持低电平15us~45us等待从端对电平采样,最后拉高电平完成写操作
	TSOR=1;
	TSOR=0;
	delay15us();
	delay15us();
	delay15us();
	delay15us();
	delay15us();

	TSOR=1;

	_nop_();
	_nop_();
}

void write_1_DS(void)//写 bit 1
{//写数据位1:置总线为低电平并保持1us~15us,
//然后拉高电平并保持15us~45us等待从端对电平采样,最后完成写操作
	TSOR=1;
	TSOR=0;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();

	TSOR=1;

	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	delay15us();
	delay15us();
}


bit read_DS(void)//读取数据位
{//读数据位:置总线为低电平并保持至少1us,然后拉高电平保持1us,
//在15us内采样总线电平获得数据,延时45us完成读操作
	bit b;

	TSOR=1;
	TSOR=0;
	_nop_();
	_nop_();
	_nop_();

	TSOR=1;

	_nop_();
	_nop_();
	_nop_();

	b=TSOR;

	delay15us();
	delay15us();
	delay15us();
	_nop_();
	_nop_();

	return b;
}

void reset_DS(void)//复位总线
{//总线复位:置总线为低电平并保持480us,然后拉高电平,
//等待从端重新拉低电平作为响应,则总线复位完成
	unsigned char i;

	TSOR=1;
	TSOR=0;
	for(i=0;i<9;i++)
	delay60us();
	TSOR=1;

	while(TSOR);//程序的关键,总线控制器发出(TX)一个复位脉冲(一个最少480us的
	//低电平信号),然后释放总线,进入接收状态(RX)。单线总线由4.7K上拉电阻拉到
	//到高电平。探测到I/0引脚上的上升沿后,DS182015等待15~60us,然后发出存在脉冲
	//(一个60~240us的低电平信号。

	for(i=0;i<3;i++)
	delay60us();	
}

void write_byte_DS(unsigned char byte)//写一个字节(byte)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		if(byte & 0x01==0x01)		//最低bit是否为0?
			write_1_DS();
		else 
			write_0_DS();
		byte=byte>>1;
	}
}

unsigned char read_byte_DS(void)	//读一个字节(byte)
{
	unsigned char i,j=0;
	bit b;

	for(i=0;i<8;i++)
	{
		b=read_DS();				//读取1bit
		if(b)						//为1否?
			j+=1;
		j=_cror_(j,1);				//循环右移1位
	}
	return j;
}

void DS1820_system_Init(void)
{
	reset_DS();						//复位总线
	write_byte_DS(0xcc);			//忽略ROM匹配操作
	write_byte_DS(0x4e);			//设置写模式
	write_byte_DS(0x64);			//写温度上限TH为100度
	write_byte_DS(0x8a);			//写温度下限为负10度
	write_byte_DS(0x1f);			//写温度计配置寄存器
}


int get_temperature_0_DS1820(void)				//获得温度,2字节的数据分别放在MSB,LSB中
{
	unsigned char i;
 	unsigned char MSB=0,LSB=0;//温度值的整数部分,小数部分

	reset_DS();						//复位总线
	write_byte_DS(0x55);			//ROM匹配操作

	for(i=0;i<8;i++)
	{
		write_byte_DS(ROM_0_code[i]);
	}

	write_byte_DS(0x44);			//温度转换命令
	delay100ms();

	reset_DS();						//复位总线
	write_byte_DS(0x55);			//ROM匹配操作

	for(i=0;i<8;i++)
	{
		write_byte_DS(ROM_0_code[i]);
	}

	write_byte_DS(0xbe);			//读取寄存器

	LSB=read_byte_DS();				//读取第1字节
	MSB=read_byte_DS();				//读取第2字节
/*	read_byte_DS();					//读取第3字节
	read_byte_DS();					//读取第4字节
	read_byte_DS();					//读取第5字节
	read_byte_DS();					//读取第6字节
	read_byte_DS();					//读取第7字节
	read_byte_DS();					//读取第8字节
	read_byte_DS();					//读取第9字节*/

	MSB=MSB<<4;
	MSB+=(LSB&0xf0)>>4;				//获得温度值整数部分
	LSB=(LSB&0x0f) ? 5:0;			//获得温度值小数部分
	return (MSB*10+LSB);
}

int get_temperature_1_DS1820(void)				//获得温度,2字节的数据分别放在MSB,LSB中
{
	unsigned char i;
	unsigned char MSB=0,LSB=0;//温度值的整数部分,小数部分

	reset_DS();						//复位总线
	write_byte_DS(0x55);			//ROM匹配操作

	for(i=0;i<8;i++)
	{
		write_byte_DS(ROM_1_code[i]);
	}

	write_byte_DS(0x44);			//温度转换命令
	delay100ms();

	reset_DS();						//复位总线
	write_byte_DS(0x55);			//ROM匹配操作

	for(i=0;i<8;i++)
	{
		write_byte_DS(ROM_1_code[i]);
	}

	write_byte_DS(0xbe);			//读取寄存器

	LSB=read_byte_DS();				//读取第1字节
	MSB=read_byte_DS();				//读取第2字节
/*	read_byte_DS();					//读取第3字节
	read_byte_DS();					//读取第4字节
	read_byte_DS();					//读取第5字节
	read_byte_DS();					//读取第6字节
	read_byte_DS();					//读取第7字节
	read_byte_DS();					//读取第8字节
	read_byte_DS();					//读取第9字节
*/
	MSB=MSB<<4;
	MSB+=(LSB&0xf0)>>4;				//获得温度值整数部分
	LSB=(LSB&0x0f) ? 5:0;			//获得温度值小数部分
	return (MSB*10+LSB);
}


void main(void)
{

	LCD_system_Init ();

	DS1820_system_Init();

	while(1)
	{
		buffer[0]=disp_number[get_temperature_0_DS1820()/100];
		buffer[1]=disp_number[get_temperature_0_DS1820()/10%10];
		buffer[2]=disp_number[pot];
		buffer[3]=disp_number[get_temperature_0_DS1820()%10];

		buffer[7]=disp_number[get_temperature_1_DS1820()/100];
		buffer[8]=disp_number[get_temperature_1_DS1820()/10%10];
		buffer[9]=disp_number[pot];
		buffer[10]=disp_number[get_temperature_1_DS1820()%10];



		LCD_display(0,0,disp);
		LCD_display(1,2,buffer);
	}  
}

⌨️ 快捷键说明

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