Ds18b20

ds18b20检测温度,显示在数码管上

#include <reg52.h>
#include <stdio.h>


#include <intrins.h>

typedef unsigned int u16;
typedef unsigned char u8;
typedef unsigned int uint;
typedef unsigned char uchar;

sbit DSPORT = P3^7;

#define LED_CHOOSE	P2
#define LED_VALUE	P0
sbit le = P1^0;

sbit LA = P2^2;
sbit LB = P2^3;
sbit LC = P2^4;

u8 value_array[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77,
0x39, 0x5e, 0x79, 0x71, 0x00, 0x80}; //0x00为无显示,0x80为小数点

void delay(int i)
{
	while (i--);
}

void index_choose(u8 index)
{
	switch (index)
	{
		case 0:
			LA = 0;
			LB = 0;
			LC = 0;
			break;
		case 1:
			LA = 1;
			LB = 0;
			LC = 0;
			break;
		case 2:
			LA = 0;
			LB = 1;
			LC = 0;
			break;
		case 3:
			LA = 1;
			LB = 1;
			LC = 0;
			break;
		case 4:
			LA = 0;
			LB = 0;
			LC = 1;
			break;
		case 5:
			LA = 1;
			LB = 0;
			LC = 1;
			break;
		case 6:
			LA = 0;
			LB = 1;
			LC = 1;
			break;
		case 7:
			LA = 1;
			LB = 1;
			LC = 1;
			break;
		default:
			break;
	}
}

u8 cal_num_bit(int num)
{
	if (0 != (num / 10000))
	{
		return 5;
	}

	if (0 != num / 1000)
	{
		return 4;
	}

	if (0 != num / 100)
	{
		return 3;
	}

	if (0 != num / 10)
	{
		return 2;
	}

	if (0 != num / 1)
	{
		return 1;
	}

	return 0;
}

/*
在数码管上显示数字,最多显示两位小数, 显示数字最大不超过:655.35
类似:50.1  40  66.66  72.1
*/
//#define FLOAT_SUPPORT (1)
#if defined(FLOAT_SUPPORT)
void nixue_tube_show_num(float num)
{
}
#else
void show_num_case1(int num)
{
	index_choose(0);
	LED_VALUE = value_array[num];
	delay(100);
	LED_VALUE = 0x0;
}

void show_num_case2(int num)
{
	u8 i = 0;
	u8 high = num / 10;
	u8 low = num % 10;
	u8 array[2] = {0};
	array[0] = low;
	array[1] = high;

	for (i=0; i<sizeof(array)/sizeof(array[0]); i++)
	{
		index_choose(i);
		LED_VALUE = value_array[array[i]];
		delay(100);
		LED_VALUE = 0x0;
	}
}

void show_num_case3(int num)
{
	u8 i = 0;
	u8 hundred = num / 100;
	u8 high = num % 100 / 10;
	u8 low = num % 10;
	u8 array[3] = {0};
	array[0] = low;
	array[1] = high;
	array[2] = hundred;

	for (i=0; i<sizeof(array)/sizeof(array[0]); i++)
	{
		index_choose(i);
		LED_VALUE = value_array[array[i]];
		delay(100);
		LED_VALUE = 0x0;
	}
}

void show_num_case4(int num)
{
	u8 i = 0;
	u8 thousand = num / 1000;
	u8 hundred = num % 1000 / 100;
	u8 high = num % 100 / 10;
	u8 low = num % 10;
	u8 array[4] = {0};
	array[0] = low;
	array[1] = high;
	array[2] = hundred;
	array[3] = thousand;

	for (i=0; i<sizeof(array)/sizeof(array[0]); i++)
	{
		index_choose(i);
		LED_VALUE = value_array[array[i]];
		delay(100);
		LED_VALUE = 0x0;
	}
}

void show_num_case5(int num)
{
	u8 i = 0;
	u8 ten_thousand = num / 10000;
	u8 thousand = num % 10000 / 1000;
	u8 hundred = num % 1000 / 100;
	u8 high = num % 100 / 10;
	u8 low = num % 10;
	u8 array[5] = {0};
	array[0] = low;
	array[1] = high;
	array[2] = hundred;
	array[3] = thousand;
	array[4] = ten_thousand;
	
	for (i=0; i<sizeof(array)/sizeof(array[0]); i++)
	{
		index_choose(i);
		LED_VALUE = value_array[array[i]];
		delay(100);
		LED_VALUE = 0x0;
	}
}

/*
显示整数:最大不超过65535
*/
void nixue_tube_show_num(int num)
{
	u8 ret = -1;
	ret = cal_num_bit(num);
	if (0 != ret)
	{
		switch (ret)
		{
			case 1:
				show_num_case1(num);
				break;
			case 2:
				show_num_case2(num);
				break;
			case 3:
				show_num_case3(num);
				break;
			case 4:
				show_num_case4(num);
				break;
			case 5:
				show_num_case5(num);
				break;
			default:
				break;
		}
	}
}
#endif

void Delay1ms(int y)
{
	int x;
	for( ; y>0; y--)
	{
		for(x=110; x>0; x--);
	}
}

char Ds18b20Init()
{
	uchar i;
	DSPORT = 0;			 //将总线拉低480us~960us
	i = 70;	
	while(i--);//延时642us
	DSPORT = 1;			//然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
	i = 0;
	while(DSPORT)	//等待DS18B20拉低总线
	{
		Delay1ms(1);
		i++;
		if(i>5)//等待>5MS
		{
			return 0;//初始化失败
		}
	
	}
	return 1;//初始化成功
}

void Ds18b20WriteByte(uchar dat)
{
	uint i, j;

	for(j=0; j<8; j++)
	{
		DSPORT = 0;	     	  //每写入一位数据之前先把总线拉低1us
		i++;
		DSPORT = dat & 0x01;  //然后写入一个数据,从最低位开始
		i=6;
		while(i--); //延时68us,持续时间最少60us
		DSPORT = 1;	//然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
		dat >>= 1;
	}
}


uchar Ds18b20ReadByte()
{
	uchar byte, bi;
	uint i, j;	
	for(j=8; j>0; j--)
	{
		DSPORT = 0;//先将总线拉低1us
		i++;
		DSPORT = 1;//然后释放总线
		i++;
		i++;//延时6us等待数据稳定
		bi = DSPORT;	 //读取数据,从最低位开始读取
		/*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
		byte = (byte >> 1) | (bi << 7);						  
		i = 4;		//读取完之后等待48us再接着读取下一个数
		while(i--);
	}				
	return byte;
}

void  Ds18b20ChangTemp()
{
	Ds18b20Init();
	Delay1ms(1);
	Ds18b20WriteByte(0xcc);		//跳过ROM操作命令		 
	Ds18b20WriteByte(0x44);	    //温度转换命令
	//Delay1ms(100);	//等待转换成功,而如果你是一直刷着的话,就不用这个延时了
   
}

void  Ds18b20ReadTempCom()
{	

	Ds18b20Init();
	Delay1ms(1);
	Ds18b20WriteByte(0xcc);	 //跳过ROM操作命令
	Ds18b20WriteByte(0xbe);	 //发送读取温度命令
}

int Ds18b20ReadTemp()
{
	int temp = 0;
	uchar tmh, tml;
	Ds18b20ChangTemp();			 	//先写入转换命令
	Ds18b20ReadTempCom();			//然后等待转换完后发送读取温度命令
	tml = Ds18b20ReadByte();		//读取温度值共16位,先读低字节
	tmh = Ds18b20ReadByte();		//再读高字节
	temp = tmh;
	temp <<= 8;
	temp |= tml;
	return temp;
}

int get_temp(void)
{
	float tp;
	int temp = Ds18b20ReadTemp();
	tp = temp;
	temp = tp*0.0625;	
	return temp;
}



void main(void)
{
	while(1)
	{
		nixue_tube_show_num(get_temp());
		delay(500);		
	}	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值