#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char // From now on, unsigned char can be replaced by uchar
#define uint unsigned int // From now on, unsigned int can be replaced by uint
sbit LcdRs_P = P3^5; // RS pin of 1602 LCD
sbit LcdRw_P = P3^6; // RW pin of 1602 LCD
sbit LcdEn_P = P3^7; // EN pin of 1602 LCD
sbit DQ= P3^4; // Pin definition of temperature sensor
/************************************************************/
// Millisecond delay function, time is the number of milliseconds to delay
/********************************************************/
void DelayMs(uint time)
{
uint i,j;
for(i=0;i<time;i++)
for(j=0;j<112;j++);
}
/****************************************************** ********/
// Delay 15 microseconds
/************************************ ************************/
void Delay15us(void)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_() ;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_
(); _nop_();
_nop_()
; _nop_();
_nop_();
_nop_();
}
/************************************************************/
// 1602 LCD write command function, cmd is the command to be written
/********************************************************/
void LcdWriteCmd(uchar cmd)
{
LcdRs_P = 0;
LcdRw_P = 0;
LcdEn_P = 0;
P2=cmd;
DelayMs(2);
LcdEn_P = 1;
DelayMs(2);
LcdEn_P = 0;
}
/************************************************************/
// 1602 LCD write data function, dat is the data to be written
/********************************************************/
void LcdWriteData(uchar dat)
{
LcdRs_P = 1;
LcdRw_P = 0;
LcdEn_P = 0;
P2=dat;
DelayMs(2);
LcdEn_P = 1;
DelayMs(2);
LcdEn_P = 0;
}
/****************************************************************/
// 1602 LCD initialization function
/********************************************************/
void LcdInit()
{
LcdWriteCmd(0x38); // 16*2 display, 5*7 dot matrix, 8-bit data port
LcdWriteCmd(0x0C); // Turn on the display, do not display the cursor
LcdWriteCmd(0x06); // Add 1 to the address, and move the cursor right after writing data
LcdWriteCmd(0x01); // Clear the screen
}
/************************************************************/
// LCD cursor positioning function
/********************************************************/
void LcdGotoXY(uchar line,uchar column)
{
// First line
if(line==0)
LcdWriteCmd(0x80+column);
// Second line
if(line==1)
LcdWriteCmd(0x80+0x40+column);
}
/************************************************************/
// LCD output string function
/********************************************************/
void LcdPrintStr(uchar *str)
{
while(*str!='\0')
LcdWriteData(*str++);
}
/************************************************************/
// LCD output digital
/********************************************************/
void LcdPrintNum(uint num)
{
LcdWriteData(num/100+0x30); // Hundreds digit
LcdWriteData(num%100/10+0x30); // Tens digit
LcdWriteData(num%10+0x30); // Units digit
}
/************************************************************/
// Display temperature on LCD
/********************************************************/
void LcdPrintTemp(int temp)
{
if(temp<0)
{
LcdWriteData('-'); // minus sign
temp=0-temp; // negative number to positive number
}
if(temp>999)
{
LcdWriteData(temp/1000+0x30); // hundreds digit
}
LcdWriteData(temp%1000/100+0x30); // tens digit
LcdWriteData(temp%100/10+0x30); // ones digit
LcdWriteData('.'); // decimal point
LcdWriteData(temp%10+0x30); // one digit after the decimal
point LcdWriteData(0xdf); // Celsius symbol
LcdWriteData('C');
LcdWriteData(' ');
}
/****************************************************** ********/
// Reset DS18B20 (initialization)
/************************************ **********************/
void DS18B20_ReSet(void)
{
uchar i;
DQ=0;
i=240;
while(--i);
DQ =1;
i=30;
while(--i);
while(~DQ);
i=4;
while(--i);
}
/************************************************************/
// Write a byte to DS18B20
/********************************************************/
void DS18B20_WriteByte(uchar dat)
{
uchar j;
uchar btmp;
for(j=0;j<8;j++)
{
btmp=0x01;
btmp=btmp<<j;
btmp=btmp&dat;
if(btmp>0) // Write 1
{
DQ=0;//DQ=0
Delay15us();
DQ=1;
Delay15us(); Delay15us();
Delay15us();
Delay15us();
}
else // Write 0
{
DQ=0;
Delay15us();
Delay15us
();
Delay15us();
Delay15us();
DQ=1;
Delay15us();
}
}
}
/************************************************************/
//Read temperature value
/********************************************************/
int DS18B20_ReadTemp(void)
{
uchar j;
int b,temp=0;
DS18B20_ReSet(); // Generate reset pulse
DS18B20_WriteByte(0xcc); // Ignore ROM instruction
DS18B20_WriteByte(0x44); // Start temperature conversion instruction
DS18B20_ReSet(); // Generate reset pulse
DS18B20_WriteByte(0xcc); // Ignore ROM command
DS18B20_WriteByte(0xbe); // Read temperature command
for(j=0;j<16;j++) //Read the temperature quantity
{
DQ=0;
_nop_();
_nop_();
DQ=1;
Delay15us();
b=DQ;
Delay15us();
Delay15us();
Delay15us();
b=b<<j;
temp=temp|b;
}
temp=(temp*625/1); //Synthesize the temperature value and amplify it by 10 timesreturn
(temp); //Return the detected temperature value
}
/************************************************************/
// Main function
/************************************************************/
void main()
{
int temp; // Save the results measured by the temperature sensor
LcdInit(); // Execute LCD initialization
LcdGotoXY(0,0); // Position to row 0, column 0
LcdPrintStr(" temp="); // Row 0 displays " temp="
// LcdGotoXY(1,0); // Position to row 1, column 0
// LcdPrintStr(" dist= cm "); // Row 1 displays " dist= cm "
while(DS18B20_ReadTemp()==850) // Wait for temperature sensor initialization to complete
{
DelayMs(10);
}
while(1)
{
temp=DS18B20_ReadTemp(); // Get the temperature value of the temperature sensor
LcdGotoXY(0,7); // Position to row 0, column 7
LcdPrintTemp(temp); // Display the current temperature value
LcdGotoXY(1,7); // Cursor positioning
}
}
Here is the modified download code.
|