sbit DQ =P3^7; //define communication port
//Delay function
void delay(unsigned int i)
{
while(i--);
}
//Init function
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ reset
delay(8); //Slight delay
DQ = 0; //MCU pulls DQ down
delay(80); //Precise delay is greater than 480us
DQ = 1; //Pull the bus high
delay(14);
x=DQ; //After a slight delay, if x=0, initialization is successful, if x=1, initialization fails
delay(20);
}
//Read a
byteReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; //Give pulse signaldat
>>=1;
DQ = 1; //Give pulse signalif
(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
//Write one byte
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
}
//Read temperatureReadTemperature
(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); //Skip the operation of reading the serial number and column
numberWriteOneChar(0x44); //Start temperature conversionInit_DS18B20
();
WriteOneChar(0xCC); //Skip the operation of reading the serial number and column
numberWriteOneChar(0xBE); //Read temperature registers, etc. (9 registers can be read in total) The first two are temperaturesa
=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625; //Merge the high and low bits of the temperaturet
= tt*10+0.5; //Round the result
return(t);
}
//Digital codes from 0 to 9 respectively. Note that this code is only suitable for the QQ microcontroller experiment board. For codes on other circuit boards, please refer to the above algorithm to calculate them yourself.
unsigned char code Num[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
//QQ MCU private function, may not work well on other circuit boards
//Display any characters on 8 digital tubes, the character content is determined by 8 parameters LED1--LED8 If the displayed character is empty, this digital tube will not be scanned to shorten the scanning time
ShowAny(unsigned char LED1,unsigned char LED2,unsigned char LED3,unsigned char LED4,unsigned char LED5,unsigned char LED6,unsigned char LED7,unsigned char LED8)
{
if(LED1) { P2=0xEF; P0=LED1; delay(100); }
if(LED2) { P2=0xDF; P0=LED2; delay(100); }
if(LED3) { P2=0xBF; P0=LED3; delay(100); }
if(LED4) { P2=0x7F; P0=LED4; delay(100); }
if(LED5) { P2=0xFE; P0=LED5; delay(100); }
if(LED6) { P2=0xFD; P0=LED6; delay(500); }
if(LED7) { P2=0xFB; P0=LED7; delay(500); }
if
(LED8) { P2=0xF7;
main()
{
unsigned int i=0;
while(1)
{
i=ReadTemperature(); //Read the current temperature
ShowAny(0,0,0,0,0,Num[i/100],Num[i/10%10] | 0x80,Num[i%10]); //Show the current temperature
}
}
Previous article:A design scheme of high-precision ultrasonic intelligent distance measurement system
Next article:Single chip ultrasonic sensor to measure distance
- Popular Resources
- Popular amplifiers
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- 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)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- DB HiTek Completes Global Shutter and SPAD Technology Development to Further Expand Image Sensors
- Celebrating the Golden 50th Anniversary of the Microprocessor
- iTOP-3399 development board Linux image burning (I)
- [Wireless serial port module quick selection guide] Communication frequency, chip, communication distance, power sensitivity, current air rate
- Check the internal working status and problems of the power supply
- EEWORLD University ---- Introduction to Deep Learning of Neural Networks
- FPGA introductory series experimental tutorial - key debounce control LED on and off.pdf
- ADC12 module of MSP430F149
- New Horizons to Pluto: Qorvo Helps Send Images from Pluto to Earth
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board]——RTC routine