#include "reg52.h"
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
Keywords:MCU
Reference address:#singlechip#ds18b20 unit conversion 30 degree alarm
#define uchar unsigned char
#define uint unsigned int
sbit DSPORT=P3^7;
sbit key3 = P3^2;
sbit beep = P1^0;
uchar flag;
int tmp;
float t;
uint warn_d = 300; // upper temperature limit, the temperature is the value after multiplying by 10
uchar code table[] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
void Delay1ms(unsigned int y)
{
unsigned int x;
for(y;y>0;y--)
for(x=110;x>0;x--);
}
unsigned char Ds18b20Init()
{
unsigned int i;
DSPORT=0; //Pull the bus down for 480us~960us
i=70;
while(i--); //delay 642us
DSPORT=1; //Then pull the bus high. If DS18B20 responds, it will pull the bus low after 15us~60us
i=0;
while(DSPORT) //Wait for DS18B20 to pull the bus low
{
i ;
if(i>5000) //wait>5MS
return 0; // Initialization failed
}
return 1; // Initialization successful
}
void Ds18b20WriteByte(unsigned char dat)
{
unsigned int i,j;
for(j=0;j<8;j )
{
DSPORT=0; //Pull the bus down for 1us before writing each bit of data
i ;
DSPORT=dat&0x01; //Then write a data, starting from the lowest bit
i=6;
while(i--); //Delay 68us, duration at least 60us
DSPORT=1; //Then release the bus, at least 1us is needed for the bus to recover before writing the second value
dat>>=1;
}
}
unsigned char Ds18b20ReadByte()
{
unsigned char byte,bi;
unsigned int i,j;
for(j=8;j>0;j--)
{
DSPORT=0; //Pull the bus down for 1us first
i ;
DSPORT=1; //Then release the bus
i ;
i ; // Delay 6us to wait for data to stabilize
bi=DSPORT; //Read data, starting from the lowest bit
byte=(byte>>1)|(bi<<7);
i=4; //After reading, wait 48us before reading the next number
while(i--);
}
return byte;
}
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //Skip ROM operation command
Ds18b20WriteByte(0x44); //temperature conversion command
}
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //Skip ROM operation command
Ds18b20WriteByte(0xbe); //Send the command to read temperature
}
int Ds18b20ReadTemp()
{
int temp=0;
unsigned char tmh,tml;
Ds18b20ChangTemp(); //Write the conversion command first
Ds18b20ReadTempCom(); //Then wait for the conversion to complete and then send the temperature reading command
tml=Ds18b20ReadByte(); //Read the temperature value, a total of 16 bits, read the low byte first
tmh=Ds18b20ReadByte(); // read high byte again
temp=tmh;
temp<<=8;
temp|=tml;
return temp;
}
void Display(int temp) //Display
{
unsigned char datas[] = {0, 0, 0, 0, 0}; //define array
float tp;
if(temp< 0) //When the temperature value is negative
{
//Because the temperature read is the complement of the actual temperature, subtract 1 and then invert it to get the original code
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*10 0.5;
//Leave two decimal points and *100. 0.5 is rounded because the decimal point is rounded when the C language floating point number is converted to an integer.
//The numbers behind are automatically removed, regardless of whether they are greater than 0.5. After 0.5, the numbers greater than 0.5 are incremented, and the numbers less than 0.5 are incremented.
// Calculate and add 0.5, or after the decimal point.
}
else
{
tp=temp; //Because the data processing has a decimal point, the temperature is assigned to a floating point variable
//If the temperature is positive, then the original code of the positive number is the complement code itself
temp=tp*0.0625*10 0.5;
//Leave two decimal points and *100. 0.5 is rounded because the decimal point is rounded when the C language floating point number is converted to an integer.
//The numbers behind are automatically removed, regardless of whether they are greater than 0.5. After 0.5, the numbers greater than 0.5 are incremented, and the numbers less than 0.5 are incremented.
// Calculate and add 0.5, or after the decimal point.
}
datas[0] = temp / 100;
datas = temp % 100 / 10;
datas = temp % 10;
P0=0xff;
P0 = table[datas[0]];
P2 = 0xfd;
Delay1ms(1);
P0=0xff;
P0 = table[datas ] & 0x7f;
P2 = 0xfb;
Delay1ms(1);
P0=0xff;
P0 = table[datas ];
P2 = 0xf7;
Delay1ms(1);
}
void Exter_Init()
{
EX0=1; //Open external interrupt
EA=1; //Open the general interrupt
IT0=0; //Low level trigger
EX0 = 1;
IT0 = 1;
}
void main()
{
Exter_Init();
beep = 1;
while(1)
{
if(flag%2 == 1) // odd number of key presses, display Fahrenheit
{
t = Ds18b20ReadTemp(); //Fahrenheit = (Celsius*9)/5 32
t = t*0.0625*10 0.5;
tmp = t*1.8 320;
P0=0xff;
P0 = table[15];
P2 = 0xfe;
Delay1ms(1);
P0=0xff;
P0 = table[tmp/100];
P2 = 0xfd;
Delay1ms(1);
P0=0xff;
P0 = table[tmp0/10] & 0x7f;
P2 = 0xfb;
Delay1ms(1);
P0=0xff;
P0 = table[tmp];
P2 = 0xf7;
Delay1ms(1);
}
else //Even number of key presses, display degrees Celsius
{
t = Ds18b20ReadTemp();
tmp = t*0.0625*10 0.5;
Display(Ds18b20ReadTemp());
if(tmp >= warn_d) //If the temperature exceeds the limit, the buzzer will sound an alarm
{
beep = 0;
Delay1ms(1);
beep = 1;
Delay1ms(1);
}
else beep = 1;
}
}
}
void exter0() interrupt 0
{
Delay1ms(5); //Debounce
while(!key3);
flag ;
}
Previous article:51 MCU expands external RAM
Next article:#Single-chipcomputer#Matrix keyboard-electronic piano
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
Guess you like
- Award-winning live broadcast: Application of Infineon gate driver chips and corresponding solutions of Avnet are now open for registration...
- Characterization of Integrated RF Hardware in 5G Applications
- How does TTL inverter work?
- Giant microbit (giga:bit)
- Renesas CPK-RA6M4 Development Board Review + Unboxing and Building Environment
- Has anyone used the ionizing radiation sensor module? Discuss some technical issues
- RS-485 communication interface encoder
- FPGA Implementation of Fully Parallel FFT
- RT-Thread has been ported to W600
- MakeCode now supports STM103