*
Program effect: Read the temperature value through DS18B20 and display the corresponding temperature value
The program focuses on understanding the timing and delay time
*/
DS18DQ EQU P1.3 //P1.3 is equivalent to DS18DQ, which is easy to understand
SPEAKER EQU P2.0 //Drive the buzzer
TEMPH EQU 29H //Save the low 8 bits of the temperature read
TEMPL EQU 28H //Save the high 8 bits of the temperature read
DS18FLAG EQU 40H //Set the flag
ORG 0000H
LJMP START
ORG 0030H
START: MOV 30H,#00 //Set the initial value
MOV 31H,#00
MOV 32H,#00
START1: LCALL DS18_GF //Set the working mode of DS18B20
LCALL CONVERT_TEMPERATURE //Output conversion temperature command
LCALL DELAY750MS //Delay 750ms
LCALL GET_TEMPERATURE //Get temperature data
LCALL CAL_TEMPERATURE //Calculate temperature value
LCALL DISPLAY //Call the display to prevent the display from dropping
LJMP START1 //Infinite loop
//Initialize the subroutine
DS18_START:
PUSH 07H //Save the value of R7 to prevent changing the value of R7
SETB DS18DQ //Pull the data line high
NOP //Delay 1us
CLR DS18DQ //Pull the data line low
MOV R7,#0 //Delay about 512us
DJNZ R7,$
SETB DS18DQ //Pull the data line high
MOV R7,#40
DS18_START1: //Delay about 160us
JNB DS18DQ,DS18_START2 //Check if DS18B20 is working properly
DJNZ R7,DS18_START1
SETB DS18FLAG //Abnormal work
CLR SPEAKER //Drive the buzzer and alarm
DS18_START2:
CLR DS18FLAG
MOV R7,#100 //Delay month 200us
DJNZ R7,$
POP 07H //Restore the value of R7
RET
//Set DS18B20 working mode
DS18_GF:CLR DS18FLAG //Clear flag
LCALL DS18_START //Call initialization subroutine
MOV A,#0CCH //Jump ROM instruction
LCALL DS18_SEND //
MOV A,#4EH //Write data to RAM
LCALL DS18_SEND
MOV A,#75
LCALL DS18_SEND
MOV A,#0
LCALL DS18_SEND
MOV A,#7FH //Set working mode
LCALL DS18_SEND
CLR DS18DQ //Release bus
LCALL DELAY500US //Delay 500us
LOOP: LCALL CONVERT_TEMPERATURE //Call rotor program
MOV R5,#80
LOOP1: LCALL DISPLAY //Call display to prevent display dropout and wait for conversion to complete
DJNZ R5,LOOP1
LCALL GET_TEMPERATURE //Get conversion value
LCALL CAL_TEMPERATURE //Calculate temperature value
LJMP LOOP
RET[page]
//Output conversion temperature command
CONVERT_TEMPERATURE:
LCALL DS18_START //Call initialization subroutine
MOV A,#0CCH //Jump ROM instruction
LCALL DS18_SEND
MOV A,#44H //Temperature conversion
LCALL DS18_SEND
RET
//Read temperature data
GET_TEMPERATURE:
LCALL DS18_START //Call initialization subroutine
MOV A,#0CCH //Jump ROM instruction
LCALL DS18_SEND
MOV A,#0BEH //Data from RAM
LCALL DS18_SEND
LCALL DELAY60US
LCALL DS18_READ
MOV TEMPL,A //Store the lower eight bits of the obtained data in TEMPL
LCALL DS18_READ
MOV TEMPH,A //Store the upper eight bits of the obtained data in TEMPH
CLR DS18DQ //Reset. Otherwise, it will keep reading until
LCALL DELAY501US //Store the ninth byte temporarily
RET
//Calculate the temperature value for easy display
CAL_TEMPERATURE:
PUSH 07H //Store temporarily to prevent changing the data in R7
CLR C //Clear the carry flag
MOV R7,#04H //Number of cycles
EX2: MOV A,TEMPH // The functions of these statements are:
RRC A // The integer bit is in: TEMPL
MOV TEMPH,A // The integer bit is in: the high half of TEMPH
MOV A,TEMPL
RRC A
MOV TEMPL,A
DJNZ R7,EX2 //
MOV A,TEMPH //
RLC A //The functions of these statements are:
RLC A //Move the high half of TEMPH to the low four bits
RLC A
RLC A //
ANL A,#0FH //Shield the high four bits and keep the low four bits
MOV DPTR,#TAB_TEMP //Assign the first address of the value table with a decimal point
MOVC A,@A+DPTR
MOV 30H,A //Store the value temporarily for display
MOV A,TEMPL
CLR C
MOV B,#0AH
DIV AB //Separate the high and low four bits of the integer
MOV 32H,A //Store the tens digit temporarily
MOV A,B
ADD A,#10H
MOV 31H,A //Store the ones digit temporarily
POP 07H //Restore the value of R7
RET
//Write data subroutine
DS18_SEND:
PUSH 06H //Protect the value of Rn
PUSH 07H
//The following statements are "write pen and mouse"
SETB DS18DQ //The write timing is determined by the falling edge of DS18DQ
NOP //Delay 1us
MOV R6,#08H //Number of loops, one byte
DS18_SEND1:
CLR DS18DQ //Pull low to generate a falling edge
MOV R7,#07 //Delay about 15us
DJNZ R7,$
RRC A //Move the high bit of the data to be written to C
MOV DS18DQ,C //Then assign the value of C to DS18DQ
MOV R7,#30 //Delay time is about 60us, only about 45us is needed
DJNZ R7,$
SETB DS18DQ //Pull high to prepare, the next falling edge will be generated
DJNZ R6,DS18_SEND1 //Judge whether the number of loops is enough
POP 07H //Restore the data of Rn
POP 06H
RET
The full program is too long, please download it from here: http://www.51hei.com/ziliao/file/ds182051hei.asm
This program has been tested successfully.
Previous article:C51 program for adjustable time alarm clock
Next article:PWM control LED desk lamp program with infrared remote control
Recommended ReadingLatest update time:2024-11-16 19:59
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Single-chip microcomputer technology and application - electronic circuit design, simulation and production (edited by Zhou Runjing)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Making some preparations for the flag I set up on the forum this year (first time shopping at the ghost market)
- Open source scientific calculator based on STC32G
- FPGA---DDS Sine Wave Generator
- FPGA/DSP/GPU, accelerating radar signal processing
- Help! Can someone help me analyze a small circuit? Thank you!
- Wireless wifi good books
- Qinheng Trial_CH549 Initial Unpacking Report
- Disassembly: Huaqiangbei version of MagSafe magnetic wireless charging treasure
- How to implement deepcopy of MicroPython
- Several common types of pressure sensors