Current location - Education and Training Encyclopedia - Graduation thesis - Using 805 1 single chip microcomputer to collect and display temperature (LED digital tube), how to write it in C language? Give a routine
Using 805 1 single chip microcomputer to collect and display temperature (LED digital tube), how to write it in C language? Give a routine
# include & ltreg52.h & gt// contains header files, which generally do not need to be changed. The header file contains definitions of special function registers.

# include & ltstdio.h & gt

# contains "18b20.h"

# contains "1602.h"

# contains "delay.h"

Bit ReadTempFlag// defines the read timestamp.

void Init _ timer 0(void); //Timer initialization

/* -

Serial communication initialization

- */

void UART_Init(void)

{

SCON = 0x 50; // SCON: mode 1, 8-bit UART, enabling reception.

TMOD | = 0x 20; //tmod: timer 1, mode 2, 8-bit overload.

th 1 = 0x FD; // TH 1: reload the crystal oscillator with a baud rate of 9600+0 1.0592mhz.

tr 1 = 1; // TR 1: timer 1 on.

//EA = 1; //Open host interrupt

//ES = 1; //Open serial port interrupt

TI = 1;

}

/* -

major function

- */

Invalid master (invalid)

{

Internal temperature;

Float temperature;

Char display temperature [16]; //Defines the display area temporary storage array.

LCD _ Init(); //Initialize the liquid crystal

Delems (20); //Delay helps to stabilize.

LCD _ Clear(); //Clear the screen

init _ timer 0();

UART _ Init();

LCD _ User _ Chr(); //Write custom characters

LCD_Write_String(0,0,“ www.doflye.net”);

LCD_Write_Char( 13, 1,0x 0 1); //Write the upper right corner of the temperature.

LCD_Write_Char( 14, 1,' C '); //write the character c

While (1) // main loop

{

if(ReadTempFlag== 1)

{

ReadTempFlag = 0;

temp = read temperature();

Temperature = (floating point) temp * 0.0625

Sprintf(displaytemp, "Temp% 7.3f", temperature); //Print the temperature value

LCD_Write_String(0, 1,display temp); //Display the second line

}

}

}

/* -

Timer initialization subroutine

- */

void Init_Timer0(void)

{

TMOD | = 0x 0 1; //Use 1 and 16-bit timers, and the "|" symbol is not affected when multiple timers are used.

//TH0 = 0x 00; //Given the initial value

//TL0 = 0x 00;

EA = 1; //Total interrupt open

ET0 = 1; //Timer interrupt open

TR0 = 1; //Timer switch on

}

/* -

Timer interrupt subroutine

- */

Invalid timer 0_isr (invalid) interrupt 1

{

Static unsigned integer;

TH0 =(65536-2000)/256; //Reassign the value of 2 milliseconds.

TL0 =(65536-2000)% 256;

num++;

if(num==300) //

{

num = 0;

ReadTempFlag = 1; //Read the mark position 1

}

}

/* -

Name: LCD 1602

Forum: www.doflye.net

Written by Shifang.

Date: May 2009

Modification: None

Content:

Pin definition is as follows:1-VSS 2-vdd 3-v04-RS 5-r/w 6-e 7-14db0-db715-bla16-blk.

- */

# contains "1602.h"

# contains "delay.h"

sbit RS = p2^4; //Define the port

sbit rw = p2^5;

sbit en = p2^6;

# Define RS_CLR RS=0

#define RS_SET RS= 1

# define RW_CLR RW=0

#define RW_SET RW= 1

#define EN_CLR EN=0

#define EN_SET EN= 1

# Define data port P0

/* -

Busy decision function

- */

Bit LCD_Check_Busy(void)

{

Data port = 0xFF

RS _ CLR

RW _ SET

EN _ CLR

_ nop _();

EN _ SET

Return (bit) (data port & amp0x80);

}

/* -

Write command function

- */

Void LCD_Write_Com (unsigned character Com)

{

//while(LCD _ Check _ Busy()); //Just wait when you are busy.

Delems (5);

RS _ CLR

RW _ CLR

EN _ SET

DataPort = com

_ nop _();

EN _ CLR

}

/* -

Write data function

- */

Void LCD_Write_Data (unsigned character data)

{

//while(LCD _ Check _ Busy()); //Just wait when you are busy.

Delems (5);

RS _ SET

RW _ CLR

EN _ SET

DataPort= data;

_ nop _();

EN _ CLR

}

/* -

Clear screen function

- */

void LCD_Clear(void)

{

LCD _ Write _ Com(0x 0 1);

Delems (5);

}

/* -

Write string function

- */

Void LCD_Write_String (unsigned character x, unsigned character y, unsigned character *s)

{

If (y == 0)

{

LCD _ Write _ Com(0x 80+x); //Indicates the first line.

}

other

{

LCD _ Write _ Com(0xc 0+x); //indicates the second line.

}

while (*s)

{

LCD _ Write _ Data(* s);

s++;

}

}

/* -

Write character function

- */

Void LCD_Write_Char (unsigned character x, unsigned character y, unsigned character data)

{

If (y == 0)

{

LCD _ Write _ Com(0x 80+x);

}

other

{

LCD _ Write _ Com(0xc 0+x);

}

LCD_Write_Data (data);

}

/* -

Initialization function

- */

void LCD_Init(void)

{

LCD _ Write _ Com(0x 38); /* Display mode setting */

Delems (5);

LCD _ Write _ Com(0x 38);

Delems (5);

LCD _ Write _ Com(0x 38);

Delems (5);

LCD _ Write _ Com(0x 38);

LCD _ Write _ Com(0x 08); /* Display off */

LCD _ Write _ Com(0x 0 1); /* Display a clear screen */

LCD _ Write _ Com(0x 06); /* Display cursor movement settings */

Delems (5);

LCD _ Write _ Com(0x0C); /* Display opening and cursor setting */

}

/* -

Set two custom characters, and the address of the custom characters in LCD 1602 is 0x00-0x07.

You can define 8 characters.

Here we set the custom character to 0x00 (000).

The other is placed at position 0x00 1 (00 1).

- */

void Lcd_User_Chr(void)

{//First custom character

LCD _ Write _ Com(0x 40); //"0 1 000 000 "line 1 address (D7D6 is in the form of an address setting command? D5D4D3 is the character storage location (0-7), and D2D 1D0 is the character line address (0-7)).

LCD _ Write _ Data(0x 00); //"XXX 1 11"line1data (D7D6D5 is XXX, which means any number (generally 000), and D4D3D2D 1D0 is character line data (/kloc

LCD _ Write _ Com(0x 4 1); //"0 1 000 00 1 "Address in line 2

LCD _ Write _ Data(0x 04); //"XXX 1000 1 "data in line 2

LCD _ Write _ Com(0x 42); //"0 1 000 0 10 "third line address

LCD _ Write _ Data(0x0e); //"XXX10101"Line 3 data

LCD _ Write _ Com(0x 43); //The address is in the fourth line of "0 1 000 0 1 1"

LCD _ Write _ Data(0x0e); //"XXX 1000 1 "Line 4 data

LCD _ Write _ Com(0x 44); //"0 1 000 100 "5th line address

LCD _ Write _ Data(0x0e); //"XXX1111"data is in line 5.

LCD _ Write _ Com(0x 45); //"01000101"Line 6 address

LCD _ Write _ Data(0x 1f); //"XXX 0 10 10 "Line 6 data

LCD _ Write _ Com(0x 46); //"01000110" Line 7 Address

LCD _ Write _ Data(0x 04); //"XXX1111"Line 7 data

LCD _ Write _ Com(0x 47); //"01000111"Row 8 address

LCD _ Write _ Data(0x 00); //"XXX 00000 "Line 8 data

//The second custom character

LCD _ Write _ Com(0x 48); //The address of "0 1 001000" is in line1

LCD _ Write _ Data(0x 03); //"XXX 0000 1 "line 1 data

LCD _ Write _ Com(0x 49); //The address is in the second line of "0 1 00 1 00 1"

LCD _ Write _ Data(0x 03); //"XXX11011"data is in line 2.

LCD _ Write _ Com(0x4a); //"01001010" 3rd line address

LCD _ Write _ Data(0x 00); //"XXX1110/"data in the third line.

LCD _ Write _ Com(0x4b); //The address of "0100101"is in the fourth line.

LCD _ Write _ Data(0x 00); //"XXX11001"Line 4 data

LCD _ Write _ Com(0x4c); //"01001100" fifth line address

LCD _ Write _ Data(0x 00); //"XXX1110/"data is in line 5.

LCD _ Write _ Com(0x4d); //"0100110/"Address in line 6.

LCD _ Write _ Data(0x 00); //"XXX11011"Line 6 data

LCD _ Write _ Com(0x4e); //Address of line 7 of "0100110"

LCD _ Write _ Data(0x 00); //"XXX 0000 1 "Line 7 data

LCD _ Write _ Com(0x4f); //"010011"Address on the 8th line.

LCD _ Write _ Data(0x 00); //"XXX 00000 "Line 8 data

}

# contains "delay.h"

# contains "18b20.h"

/* -

18b20 initialization

- */

Bit Init_DS 18B20 (null)

{

Bit dat = 0;;

DQ = 1; //DQ reset

delayus 2x(5); //A little delayed

DQ = 0; //Single chip microcomputer pulls down DQ.

dela Yus 2x(200); //The exact delay is greater than 480us and less than 960us.

dela Yus 2x(200);

DQ = 1; //Pull the bus high

Drayus 2x (50); // 15~60us and then receive 60-240us pulse.

dat = DQ; //If x=0, initialization succeeds; if x= 1, initialization fails.

Drayus 2x (25); //slightly delayed return

Return dat

}

/* -

Read a byte

- */

Unsigned character ReadOneChar(void)

{

Unsigned character I = 0;;

Unsigned character dat = 0;;

for(I = 8; I>0; I-)

{

DQ = 0; //Give a pulse signal

dat & gt& gt= 1;

DQ = 1; //Give a pulse signal

if(DQ)

dat | = 0x80

Drayus 2x (25);

}

return(dat);

}

/* -

Write a byte

- */

Void WriteOneChar (unsigned character data)

{

Unsigned character I = 0;;

for(I = 8; I>0; I-)

{

DQ = 0;

DQ = date and time; 0x 0 1;

Drayus 2x (25);

DQ = 1;

dat & gt& gt= 1;

}

Drayus 2x (25);

}

/* -

Reading temperature

- */

Unsigned integer read temperature (void)

{

Unsigned character a = 0;;

Unsigned int b = 0;;

Unsigned integer t = 0;;

init _ ds 18b 20();

WriteOneChar(0x cc); //Skip reading serial number and column number.

WriteOneChar(0x 44); //Start temperature conversion

delay ms( 10);

init _ ds 18b 20();

WriteOneChar(0x cc); //Skip reading serial number and column number.

WriteOneChar(0x be); //Read the temperature register (* * * can read 9 registers), the first two are the temperature.

a = ReadOneChar(); //Low position

b = ReadOneChar(); //high position

b & lt& lt=8;

t = a+b;

Return (t);

}