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

📄 ds18b20.c

📁 基于STC单片机的发酵塔温控装置的控制代码.
💻 C
字号:
//*************************************************************************************************
//  Module Name :  DS18B20.C 
//  CreateDate  :  2006-07-13 
//  ModifData   :  2006-07-13 
//  Description :  For STC MCU 
//  Author      :  Explorer01 
//  Version     :  V1.0  
//*************************************************************************************************
// 
//-------------------------------------------------------------------------------------------------
// Includes 
//-------------------------------------------------------------------------------------------------

#include <STC51.H>
#include "DS18B20.H"

//-------------------------------------------------------------------------------------------------
// Global CONSTANTS
//-------------------------------------------------------------------------------------------------
// 
unsigned int TEMPER;

//=================================================================================================
// Functions
//=================================================================================================
//-------------------------------------------------------------------------------------------------
// Delay, Fosc = 11.0592MHz 
void mDelay( unsigned int msecond )
{
	while( msecond-- );
}

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// Send a byte to DS18B20 
void DS18B20Write( unsigned char datum )
{
	unsigned char i;
	
	for( i=0; i<8; i++ ) 
	{
		// 60us -- 120us 
		DS18B20_DQ = 0;
		mDelay( 5 );						// 1us -- 15us 
		DS18B20_DQ = datum & 0x01;
		mDelay( 80 );						// Delay : Master write '0'/'1' slot : 60us -- 120us 
		DS18B20_DQ = 1;
		datum    >>= 1;
	}
}

//-------------------------------------------------------------------------------------------------
// Read a byte from DS18B20 
unsigned char DS18B20Read( void )
{
	unsigned char i, datum=0;
	
	for( i=0; i<8; i++ )
	{
		// 60us -- 120us 
	    DS18B20_DQ = 0;
		datum    >>= 1;
		mDelay( 5 );						// 1us -- 15us 
		DS18B20_DQ = 1;
		mDelay( 5 );						// <= 15us 
		if( DS18B20_DQ ) datum |= 0x80;
		mDelay( 60 );						// Delay : Master read '0'/'1' slot : 60us -- 120us 
		DS18B20_DQ = 1;
	}
	
	return( datum );
}

//=================================================================================================
//=================================================================================================
// 
//-------------------------------------------------------------------------------------------------
// DS18B20 Initialization Procedure 
void DS18B20RST( void )
{
	DS18B20_DQ = 0;
	mDelay( 960 );							// Master Tx "reset puls" : (480us -- 960us) 
	DS18B20_DQ = 1;
	// Master Rx : >= 480us 
	mDelay( 100 );							// DS18B20 wait : 15us -- 60us 
	if( DS18B20_DQ ) mDelay( 150 ); 
	mDelay( 500 ); 
}

//-------------------------------------------------------------------------------------------------
// Get the temperature 
void DS18B20Temperature( void )
{
	unsigned char TEMPER_L,TEMPER_H;
	unsigned int  temp;
	
	DS18B20RST( ); 
	DS18B20Write( 0xCC );					// Skip ROM 
	DS18B20Write( 0xBE );					// Read Scratchpad 
	
	TEMPER_L = DS18B20Read( );				// LSB 
	TEMPER_H = DS18B20Read( );				// MSB 
	
	temp   = TEMPER_H;
	temp <<= 8;
	temp  |= TEMPER_L;

	TEMPER = temp;
	
//	TEMPER  = temp/2*125;					// Temperatre regulate 
//	TEMPER += temp%2*62;
	
	DS18B20RST( ); 
	DS18B20Write( 0xCC );					// Skip ROM 
	DS18B20Write( 0x44 );					// Start next temperature convertion  
}

⌨️ 快捷键说明

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