栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【蓝桥杯单片机学习】第十节 --- 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< 
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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/856290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号