【蓝桥杯单片机学习】第十节 --- DS18B20(包含整数与小数)

 

 

 main.c

#include "STC15F2K60S2.h"
#include "onewire.h"
#define uchar unsigned char
#define uint unsigned int
#define Y0 P2=(P2&0X1F)|0X00;
#define Y4 P2=(P2&0X1F)|0X80;
#define Y5 P2=(P2&0X1F)|0Xa0;
#define Y6 P2=(P2&0X1F)|0Xc0;
#define Y7 P2=(P2&0X1F)|0Xe0;
/*************  本地常量声明    **************/
uchar code t_display[]={                       //标准字库
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
void sys_delay(uint t)
{
	while(t--);
}
void seg_one(uchar wei,dat)
{
	Y6; P0=0X01<<wei; Y0;
	Y7; P0=~t_display[dat]; sys_delay(500); P0=0XFF; Y0;
}
void main()
{
	uchar dispaly_wd;
	uint dispaly_wd_float;
	Y4; P0=0XFF; Y0;
	Y5; P0=0X00; Y0;
	Y6; P0=0X00; Y0;
	while(1)
	{
		dispaly_wd=rd_temperature();
		dispaly_wd_float=rd_float_temperature();
		seg_one(0,dispaly_wd/10);
		seg_one(1,dispaly_wd%10);
		seg_one(5,dispaly_wd_float/100%10);
		seg_one(6,dispaly_wd_float/10%10+32);
		seg_one(7,dispaly_wd_float%10);
	}
}

onewire.c

#include "onewire.h"

//单总线内部延时函数
void Delay_OneWire(unsigned int t)  
{
	t=t*10;
	while(t--);
}

//单总线写操作
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

//单总线读操作
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

//DS18B20初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}
unsigned char rd_temperature(void)//整数
{
	unsigned char temp_h,temp_l,temp;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay_OneWire(200);
	
	do
	{
		init_ds18b20();
		Write_DS18B20(0xcc);
		Write_DS18B20(0xbe);
		temp_l=Read_DS18B20();
		temp_h=Read_DS18B20();
	//	temp_l=temp_l&0xf0;  
	//	temp_h=temp_h&0x0f;
		temp=temp_h<<4|temp_l>>4;
	}while(temp==85);
	return temp;
}
unsigned int rd_float_temperature(void)//小数
{
	unsigned int temp_h,temp_l;
	float temp;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	Delay_OneWire(200);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	temp_l=Read_DS18B20();
	temp_h=Read_DS18B20();
	temp=temp_h<<8|temp_l;
	temp=temp*0.0625;
	temp=temp*10;
	return temp;
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

#include "STC15F2K60S2.h"

sbit DQ = P1^4;  
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
unsigned char rd_temperature(void);  
unsigned int rd_float_temperature(void);//小数

#endif

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值