基于stc15f2k60s2芯片单片机编程(DS18B20测温度)

main.c

#include <stc15f2k60s2.h>
#include "ds18b20.h"
#include  <intrins.h>
unsigned char code abile[10]={0xc0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};
unsigned int disp[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
void Timer0Init();
void shumaguanxianshi();
void display();
unsigned char i=0;
int temp;
void Delay2ms();
void  main()
{
	P2=(P2&0X1F|0Xa0);
	P0=0X00;
	P2&=0X1F;

	Timer0Init();
	while(1)
	{
       
		  temp=Ds18b20ReadTemp();
       display();
			disp[4]=abile[temp % 10000 / 1000];
			disp[5]=abile[temp % 1000 / 100]&0x7f;
      disp[6]=abile[temp % 100/10];
		  disp[7]=abile[temp % 10];
		  Delay2ms();

	}
}
void Timer0Init(void)		//[email protected]
{
	AUXR |= 0x80;		//?????1T??
	TMOD &= 0xF0;		//???????
	TL0 = 0x9A;		//??????
	TH0 = 0xA9;		//??????
	TF0 = 0;		//??TF0??
	TR0 = 1;		//???0????
	ET0=1;
	EA=1;
}
void DigDisplay() interrupt 1
{
shumaguanxianshi();
		
}
void  shumaguanxianshi()
{	
  P2=(P2&0X1F|0XE0);
	P0=0XFF;
	P2&=0X1F;
	
	P2=(P2&0X1f|0xc0);
	P0=(1<<i);
	P2&=0X1F;
	
	P2=(P2&0X1F|0XE0);
	P0=disp[i];
	P2&=0X1F;
	
	if(++i==8)
	{
	i=0;
	}
	}
void display()
{
	float tp;
		if(temp<0)
 {
   temp=~temp;
	 temp=temp-1;
	 tp=temp;
	 temp=tp*0.0625*100+0.5;
 }
 else
 {
	 tp=temp;
	 temp=tp*0.0625*100+0.5;
 }
}
void Delay2ms()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	i = 22;
	j = 128;
	do
	{
		while (--j);
	} while (--i);
}

DS18B20.C

#include"DS18B20.h"
/*******************************************************************************

## 延时1ms

*******************************************************************************/

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

## 初始化。初始化成功返回1,失败返回0.

*******************************************************************************/

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

/*******************************************************************************
   

## 向DSB1802写入一个字节

*******************************************************************************/

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

## 读取一个字节

*******************************************************************************/


unsigned char Ds18b20ReadByte()
{
	unsigned char byte,bi;
	unsigned int i,j;
	for(j=8;j>0;j--)
	{
		DSPORT=0;         //先将总线拉低
		i++;
		DSPORT=1;           //释放总线
		i++;
		i++;                 //延时6us等待数据稳定
		bi=DSPORT;	    //读取数据从最低位开始读取

		byte=(byte>>1)|(bi<<7);						  
		i=50;		             //读取完之后等待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;
	unsigned char gao,di;
	Ds18b20ChangTemp();			 	 //先写入转换命令 
	Ds18b20ReadTempCom();			  //然后等待转换完后发送读取温度命令
	di=Ds18b20ReadByte();		     //读取温度值共16位,先读低字节
	gao=Ds18b20ReadByte();		  //再读高字节
	temp=gao;
	temp<<=8;
	temp|=di;
	return temp;
}

DS18B20.H

#ifndef __DS18B20_H_
#define __DS18B20_H_


#include <STC15F2K60S2.h>
#include "intrins.h"
sbit DSPORT=P1^4;
void Delay1ms();
void Delay100ms();
unsigned char Ds18b20Init();
void Ds18b20WriteByte(unsigned  char dat);
unsigned char Ds18b20ReadByte();
void Ds18b20ChangTemp();
void Ds18b20ReadTempCom();
int Ds18b20ReadTemp();
#endif
发布了10 篇原创文章 · 获赞 1 · 访问量 433

猜你喜欢

转载自blog.csdn.net/lymtics1111/article/details/103356034