Current location - Education and Training Encyclopedia - Graduation thesis - Paper on temperature control of single chip microcomputer
Paper on temperature control of single chip microcomputer
The electronic thermometer made of DS 18B20 is very simple.

# include & ltreg 5 1 . h & gt;

#include\"AscLed.h\ "

# include & ltintrins.h & gt

# include & ltstdio.h & gt

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

# definesec (500/tk)/1second coefficient of the main program.

#define OffLed (Seck*5*60) // The time for automatic shutdown is 5 minutes!

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

#if (FHz==0)

# define NOP_2uS_nop_ ()

# Otherwise

# define NOP _ 2uS _ NOP _(); _nop_()

#endif

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

# Define SkipK 0xcc // Skip command

#define ConvertK 0x44 // conversion command

# define RdDs 18b20K 0xbe // read temperature command.

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

extern led out(void);

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

sbit pnp 1=p3^4;

sbit pnp2=p3^5;

sbit beep=p3^2;

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

# defined qp PNP 2// original pnp2 beep

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

Power of static unsigned character = 0;

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

labour union

Unsigned character temp [2]; //Single byte temperature

Unsigned int TT;; //2 byte temperature

} T;

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

Typedef structure {

Unsigned character symbol; //Positive number flag 0; 1== "negative number

Unsigned char Du Wen; //temperature integer

Unsigned int WenDuDot///fractional temperature amplification 10000.

} Du Wen;

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

Du Wen Du Wen;

Unsigned character ledbuf [3];

// -

//function: 10us class delay.

//n = 1 = = = & gt; 6Mhz= 14uS 12MHz=7uS

// -

Void Delay 10us (unsigned character n) (

Do {

#if (FHz== 1)

NOP _ 2uSNOP _ 2uS

#endif

} while(-n);

}

// -

//function: write 18B20.

// -

Void Write_ 18B20 (unsigned character n) (

Unsigned character I;

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

DQ = 0;

delay 10 us( 1); //The delay is about 13us.

DQ = n & amp; 0x 0 1;

n = n & gt& gt 1;

delay 10us(5); //Delay exceeds 50us

DQ = 1;

}

}

// -

//function: read 18B20.

// -

Unsigned character read _18b20 (void) (

Unsigned character I;

Unsigned character temperature;

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

temp = temp & gt& gt 1;

DQ = 0;

NOP _ 2uS// delay 1us

DQ = 1;

NOP _ 2uSNOP _ 2uS// delay 5us

If (DQ==0){

Temperature = Temperature & amp0x7F

} Otherwise {

temp = temp | 0x80

}

delay 10us(5); //delay 40us

DQ = 1;

}

Return temperature;

}

// -

void Init (void){

DQ = 0;

delay 10us(45); //Delay 500us

DQ = 1;

delay 10us(9); //Delay 90us

if(DQ){//000 1 1 1 1 1b = 1f

Power = 0; //Failed 0

} Otherwise {

power++;

DQ = 1;

}

}

// -

Invalid Skip (Invalid) {

write _ 18b 20(SkipK);

power++;

}

// -

Void transformation (void)

write _ 18b 20(ConvertK);

power++;

}

//______________________________________

void Get_Ds 18b20L (void){

T.temp[ 1]= Read _ 18b 20(); //Read the low bit

power++;

}

//______________________________________

void Get_Ds 18b20H (void){

T.temp[0]= Read _ 18b 20(); //Read the high bit

power++;

}

// -

//Normalized to floating point number

//sssss 1 1 1; 1 1 1 10000

//sssss 1 1 1; 1 1 1 1(0.5,0.25,0. 125,0.0625)

// -

void ReadTemp (void){

Unsigned character I;

Unsigned intf1= 0;

char j = 1;

Code int Code _ F[]= { 6250 125025005000 };

Du Wen. flag = 0;

if(T . Temp[0]& gt; 0x80){ // Negative temperature

T.TT = ~ TT Tyrannosaurus rex+1; //Invert+1= source+symbol s

Du Wen. flag =- 1;

}

T.Tt <& lt= 4; //Move 4 bits to the left

Du Wen. Du Wen = temperature temperature [0]; //temperature integer

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

T.Temp[ 1]>& gt=4;

// -

for(I = 0; I<4; I++){ // Calculate the number of decimal places

f 1+=(T . Temp[ 1]& amp; 0x 0 1)* Code _ F;

T.Temp[ 1]>& gt= 1;

}

Du Wen. wendu dot = f 1; //Decimal of temperature

Power = 0;

}

// -

void Delay 1S (void){

Static unsigned int I = 0;;

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

}

// -

void ReadDo (void){

write _ 18 b20(rdds 18 b20k);

power++;

}

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

Function pointer definition

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

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

Init,Skip,Convert,Delay 1S,Init,Skip,ReadDo,Get_Ds 18b20L,

Get_Ds 18b20H, reading temperature

};

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

void GetTemp(void){

(* SubTemp[Power])();

}

// -

//Display temperature, with decimal point enlarged 10000.

void GetBcd(void){

Led buf[0]= Du Wen. Du Wen/10;

Led buf[ 1]= Du Wen. Du Wen%10+dotk;

LedBuf[2]= (Du Wen. wendu dot/ 1000)% 10;

if(led buf[0]= = 0)led buf[0]= Black;

If (Du Wen. Flag==0) returns;

if(LedBuf[0]! = black) (

led buf[2]= led buf[ 1];

led buf[ 1]= led buf[0];

Led buf[0]= Led _ Pol; //'-'

} Otherwise {

Led buf[0]= Led _ Pol; //'-'

}

}

/*

// -

void JbDelay (void){

Quietly grow me;

If (+++i>;; = off) {

p 1 = 0x ff;

P2 = 0x ff;

PCON = 0x 02;

}

}

*/

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

Main program start

1:2002 _10 _1designed and measured by DS 18B20.

2: read DS 18B20. LED with functional array. The LED digital tube shows normal!

3: FHz can work at 6 12 MHz!

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

code unsigned char Stop[3]_ at _ 0x3b;

Invalid primary (invalid) {

p 1 = 0x ff;

Du Wen. Du Wen = 0;

while ( 1){

GetTemp();

get BCD();

//JB delay();

LedOut();

}

}

Copy code

2009 10 12 _ 8b 1ef 92 155560 c 13b 5807 zmodvsacjwd[ 1]。 jpg ( 12 KB)

2009- 10-2 1 uploaded at 23: 2 1

Number of downloads: 0