Temperature detection system using microcontroller Essay,...

A very simple electronic thermometer made with DS18B20.

#include <reg51.h>

#include \"AscLed.h\"

#include <intrins.h>

#include <stdio.h>

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

#define Seck (500/TK) //Factor of the main program in 1 second

#define OffLed (Seck*5*60) //Automatic shutdown of the Time 5 minutes!

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

#if (FHz==0)

#define NOP_2uS_nop_()

#else

#define NOP_2uS_nop_();_nop_()

#endif

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

#define SkipK 0xcc //skip command

# define ConvertK 0x44 //convert command

#define RdDs18b20K 0xbe //read temperature command

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

extern LedOut (void);

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

sbit PNP1=P3^4;

sbit PNP2=P3^5;

sbit BEEP=P3^2;

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

#defineDQ PNP2 //original PNP2 BEEP

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

static unsigned char Power=0;

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

union{

unsigned char Temp[2]; //single byte temperature

unsigned int Tt; //2-byte temperature

}T;

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

typedef struct{

unsigned char Flag; //positive flag 0; 1==" negative

unsigned char WenDu; //temperature integer

unsigned int WenDuDot; //temperature decimals enlarged by 10,000

}WENDU;

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

WENDU WenDu;

unsigned char LedBuf[3];

//----------------------------------

// Function: 10us level delay

// n=1===> 6Mhz=14uS 12MHz=7uS

//----------------------------------

void Delay10us(unsigned char n){

do{

#if (FHz==1)

NOP_2uS;NOP_2uS;

#endif

}while(--n);

}

//--------------------------------- --

//Function: write 18B20

//-----------------------------------

void Write_18B20(unsigned char n){

unsigned char i;

for(i=0;i<8;i++){

DQ=0;

Delay10us(1);//delay 13us or so

DQ=n & 0x01;

n=n>>1;

Delay10us(5);//delay 50us or more

DQ=1;

}

}

//------------------------------------

// Function: Read 18B20

//----------------------- -------------

unsigned char Read_18B20(void){

unsigned char i;

unsigned char temp;

for(i=0;i<8;i++){

temp=temp>>1;

DQ=0;

NOP_2uS;//delay 1us

DQ=1;

NOP_2uS;NOP_2uS;//delay 5us

if(DQ==0){

temp=temp&. amp;0x7F;

}else{

temp=temp|0x80;

}

Delay10us(5);//delay 40us

DQ=1;

}

return temp;

}

//-----------------------------------

void Init (void){

DQ=0;

Delay10us(45);//delay 500us

DQ=1;

Delay10us(9);//delay 90us

if(DQ){ //0001 1111b=1f

Power =0; //fail 0

}else{

Power++;

DQ=1;

}

}

< p>//----------------------------------

void Skip(void){

Write_18B20(SkipK);

Power++;

}

//------------ ----------------------

void Convert (void){

Write_18B20(ConvertK);

Power++;

}

// ______________________________________

void Get_Ds18b20L (void){

T.Temp[1]=Read_18B20(); //Read the low bit

Power++;

}

//______________________________________

void Get_Ds18b20H (void){

T.Temp[0]=Read_18B20(); //read high bit

Power++;

}

//------------------------------------

// Normalize to floating point

// sssss111;11110000

// sssss111;1111(0.5,0.25,0.125, 0.0625)

// ------------------------------------

void ReadTemp (void){

unsigned char i;

unsigned intF1=0;

char j=1;

code int Code_F[]={6250,1250,2500,5000};

WenDu.Flag=0;

if (T.Temp[0] >0x80){ //negative temperature

T.Tt =~T.Tt+1; // Invert +1 = Source?+Sign S

WenDu.Flag=-1;

}

T.Tt <<= 4; // Shift left by 4 bits

WenDu.WenDu=T.Temp[0]; // Temperature integer

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

T.Temp[1]>>=4;

//---------------------------

for (i=0;i<4;i++){ //calculate decimal places< /p>

F1 +=(T.Temp[1] & 0x01)*Code_F;

T.Temp[1]>>=1;

}

WenDu.WenDuDot=F1; //Temperature decimal

Power=0;

}

//----------------------------------

void Delay1S (void){

static unsigned int i=0;

if (++i==Seck) {i=0ower++;}

}

//----------------------------------

void ReadDo (void){

Write_18B20(RdDs18b20K);

Power++;

}

< p>/**********************************

Function Pointer Definition

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

code void (code *SubTemp[])()={

Init,Skip,Convert,Delay1S,Init,Skip,ReadDo,Get_Ds18b20L,

Get_Ds18b20H,ReadTemp

};

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

void GetTemp(void){

(*SubTemp[Power])();

}

//---------------------------------------------- -----

/// Display the temperature with the decimal point enlarged by 10000.

void GetBcd(void){

LedBuf[0]=WenDu.WenDu / 10;

LedBuf[1]=WenDu.WenDu % 10 +DotK;

< p> LedBuf[2]=(WenDu.WenDuDot/1000)%10;

if(LedBuf[0]==0)LedBuf[0]=Black;

if(WenDu.Flag==0) return;

if(LedBuf[0] ! =Black){

LedBuf[2]=LedBuf[1];

LedBuf[1]=LedBuf[0];

LedBuf[0]=Led_Pol; //'-'

}else{

LedBuf[0]=Led_Pol. //'-'

}

}

/*

//---------------------------------------------------

void JbDelay (void){

static long i;

if (++i>=OffLed){

P1=0xff;

P2=0xff;

PCON=0x02;

}

}

*/

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

Main program start

1:2002_10_1 design, using DS18B20 measurement

2:Using function array to read DS18B20.LED digital tube display is normal!

3:Change FHz can work with 6,12MHz!

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

code unsigned char Stop[3] _at_ 0x3b;

void main (void){

P1=0xff;

WenDu.WenDu=0;

while (1){

GetTemp();

GetBcd();

// JbDelay();

LedOut();

}

}< /p>

Copy the code

20091012_8b1ef92155560c13b5807ZmoDVSacjwD[1].jpg (12 KB)

Uploaded on 2009-10-21 23:21

Download times:0