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

📄 temp.c

📁 温度传感器DS18B20的读写程序
💻 C
字号:
#include "iodefine.h"
#include "device.h"
#include "delays.h"

void temp_reset(void)
{
	unsigned char bresult=1;
	do{
		TEMP_RLS;
		TEMP_PULL;
		DELAY_US(500);	
		TEMP_RLS;
		DELAY_US(60);
		bresult=TEMP_PORT;
		DELAY_US(240);		
	}while(bresult);
	DELAY_US(480);
}

void temp_write_char(char val)
{	
	char i,bitval;
	bitval = val&0x01;
	for(i=0;i<8;i++){
		TEMP_PULL;
		DELAY_US(15);
		if(val&0x01) TEMP_RLS;
		DELAY_US(45);
		TEMP_RLS;
		DELAY_US(1);
		val>>=1;
	}
}

unsigned char temp_read_char(void)
{
	unsigned char i,result=0;
	TEMP_RLS;
	for(i=0;i<8;i++){
		result>>=1;
		TEMP_HLD;
		DELAY_1US;
		TEMP_RLS;
		DELAY_US(15);		
		if(TEMP_PORT) result|=0x80;
		DELAY_US(45);
	}
	return result;	
}

void temp_get(void)
{
	temp_reset();			//convert
	temp_write_char(0xcc);	
	temp_write_char(0x44);
}

float temp_result(char* hibyte,char* lobyte)
{
	float result=0;
	int t=0;
	do{						//wait convertion to end
		DELAY_US(45);
		TEMP_HLD;
		DELAY_1US;
		TEMP_RLS;
		DELAY_US(15);
	}while(!TEMP_PORT);	
	
	temp_reset();		
	temp_write_char(0xcc);
	temp_write_char(0xbe);
	*lobyte=temp_read_char();
	*hibyte=temp_read_char();

	t=*hibyte;
	t<<=8;
	t|=*lobyte;
	result=t*0.0625;
	return result;
}

⌨️ 快捷键说明

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