/***********************************************
** Experiment name: 51 single chip microcomputer experimental board temperature measurement (DS18B20)
** Creator: Xie Junyang
** Description: P3.7 port input, output data.
**------------------------------------------------------------------
** CPU: MCS-51
** Language: Assembly
** Fosc=12M
The simulation diagram of this experiment
***************************************************/
GE_BIT EQU 30H
SHI_BIT EQU 31H
BAI_BIT
EQU 32H DI_8BIT
EQU 33H GAO_8BIT EQU 34H
DQ EQU P3.7
ORG 0000H
LJMP START
ORG 0020H
START:
MOV SP,#60H
LCALL ZHUANHUAN ;Call the temperature reading subroutine
LCALL CHULI
LCALL DISPLAY ;Call the digital tube display subroutine
LJMP START
;-------------------------------------------------
;This is the DS18B20 reset initialization subroutine
INIT_1820:
SETB DQ
NOP
CLR DQ ;The host sends a reset low pulse with a delay of 537 microseconds
MOV R1,#3
DU_1:
MOV R0,#107
DJNZ R0,$
DJNZ R1,DU_1
SETB DQ ;Then pull up the data line
NOP
NOP
NOP
MOV R0,#25H
DU_2:JNB DQ,DU_3 ;Wait for DS18B20 to respond
DJNZ R0,DU_2
LJMP DU_4 ;Delay
DU_3:SETB F0 ;Set the flag bit, indicating that DS1820 exists
LJMP DU_5
DU_4:CLR F0 ;Clear the flag bit, indicating that DS1820 does not exist
LJMP DU_7
DU_5:MOV R0,#117
DU_6:DJNZ R0,DU_6 ;The timing requires a delay of some time
DU_7:SETB DQ
RET [page]
;-------------------------------------------------
;Write the subroutine of DS18B20 (with specific timing requirements)
WRITE_1820:MOV R2,#8 ;A total of 8 bits of data
CLR C
WR1:CLR DQ
MOV R3,#6
DJNZ R3,$
RRC A
MOV DQ,C
MOV R3,#23
DJNZ R3,$
SETB DQ
NOP
DJNZ R2,WR1
SETB DQ
RET
;-------------------------------------------------
;Program to read DS18B20, read two bytes of temperature data from DS18B20
READ_1820:MOV R4,#2 ;Read the high and low bits of temperature from DS18B20
MOV R1,#DI_8BIT ;Store the low bit in DI_8BIT and the high bit in GAO_8BIT
RE0:MOV R2,#8 ;There are 8 bits of data in total
RE1:CLR C
SETB DQ
NOP
NOP
CLR DQ
NOP
NOP
NOP
SETB DQ
MOV R3,#9
RE2:DJNZ R3,RE2
MOV C,DQ
MOV R3,#23
RE3:DJNZ R3,RE3
RRC A
DJNZ R2,RE1
MOV @R1,A
INC R1
DJNZ R4,RE0
/* DEC R1
MOV A,GAO_8BIT
XCHD A,@R1
XCH A,@R1
MOV GAO_8BIT,A
MOV A,@R1
SWAP A
MOV @R1,A */
RET
;-------------------------------------------------
;Read the converted temperature value
ZHUANHUAN:
//SETB DQ
LCALL INIT_1820 ;Reset DS18B20 first
JB F0,ZH1
RET ;Judge whether DS1820 exists? If DS18B20 does not exist, return to
ZH1: MOV A,#0CCH ;Skip ROM match
LCALL WRITE_1820
MOV A,#44H ;Issue temperature conversion command
LCALL WRITE_1820
LCALL DISPLAY ;Wait for AD conversion to end, 750 microseconds for 12 bits
LCALL INIT_1820 ;Reset before preparing to read temperature
MOV A,#0CCH ;Skip ROM match
LCALL WRITE_1820
MOV A,#0BEH ;Issue temperature read command
LCALL WRITE_1820
LCALL READ_1820
RET
;-------------------------------------------------
;Data processing subroutine
CHULI:
MOV P0,GAO_8BIT
MOV P2,DI_8BIT
MOV A,GAO_8BIT
JB ACC.7,FU
MOV A,DI_8BIT
MOV B,#16
DIV AB
MOV 35H,A ;Shift the high four bits of DI_8BIT right by four bits and store them in 35H (temperature value)
MOV A,B ;Multiply the low four bits of TEMPER_L by 10/16 to get the decimal point.
MOV B,#10
MUL AB
MOV B,#16
DIV AB
MOV 36H,A ;Store the decimal point in 36H
MOV A,GAO_8BIT ;Store the high eight digits in TEMPER_H, weight 16
MOV B,#16
MUL AB
ADD A,35H ;Store the integer part of the temperature value in 35H
MOV B,#10
DIV AB
MOV GE_BIT,B ;Store the ones digit in 30H
MOV B,#10 ;
DIV AB ;
MOV SHI_BIT,B ;Store the tens digit in 31H
MOV B,#10 ;
DIV AB ;
MOV BAI_BIT,B ;Store the hundreds digit in 32H
MOV A,GAO_8BIT
MOV 37H,#10H ;
JB ACC.7,EXIT
MOV 37H,#00H
SJMP EXIT
FU:
MOV A,DI_8BIT
CPL A
ADD A,#1
MOV B,#16
DIV AB
MOV 35H,A ; Shift the high 4 bits of DI_8BIT right by 4 bits and store them in 35H (temperature value)
MOV A,B ; Multiply the low 4 bits of TEMPER_L by 10/16 to get the decimal place.
MOV B,#10
MUL AB
MOV B,#16
DIV AB
// MOV 36H,A ; Store the decimal place in 36H
// MOV A,GAO_8BIT ;Store the high 8 digits in TEMPER_H, weight 16
MOV B,#16
MUL AB
ADD A,35H ;Store the integer part of the temperature value in 35H
MOV B,#10
DIV AB
MOV GE_BIT,B ;Store the ones digit in 30H
MOV B,#10 ;
DIV AB ;
MOV SHI_BIT,B ;Store the tens digit in 31H
MOV B,#10 ;
DIV AB ;
MOV BAI_BIT,B ;Store the hundreds digit in 32H
MOV A,GAO_8BIT
MOV 37H,#10H ;
JB ACC.7,EXIT
MOV 37H,#00H
SJMP EXIT
EXIT:RET [page]
;-------------------------------------------------
;Note: This ds18b20 temperature measurement experiment was conducted on the 51hei microcontroller experiment board http://www.51hei.com. The test was OK. The following is the data display subroutine
CL0:INC A
AJMP CL1
CHULI:MOV A,DI_8BIT
MOV B,#16
DIV AB
JB B.3,CL0
CL1:MOV 35H,A ;Shift the high four bits of DI_8BIT right by four bits and store them in 35H (temperature value)
MOV A,B ;Multiply the low four bits of TEMPER_L by 10/16 to get the first decimal place.
MOV B,#10
MUL AB
MOV B,#16
DIV AB
MOV 36H,A ;Store the last decimal digit in 36H
MOV A,GAO_8BIT ;Store the high 8 digits in TEMPER_H, weight 16
MOV B,#16
MUL AB
ADD A,35H ;Store the integer part of the temperature value in 35H
MOV B,#10
DIV AB
MOV GE_BIT,B ;Store the ones digit in 30H
MOV B,#10 ;
DIV AB ;
MOV SHI_BIT,B ;Store the tens digit in 31H
MOV B,#10 ;
DIV AB ;
MOV BAI_BIT,B ;Store the hundreds digit in 32H
MOV A,GAO_8BIT
MOV 37H,#10H ;
JB ACC.7,EXIT
MOV 37H,#00H
EXIT: RET */
DISPLAY:MOV DPTR,#TABLE
MOV R0,#4
XUN:MOV R1,#250 ;Display 1000 times
HUAN:MOV A,#10 //Display '-'
MOVC A,@A+DPTR
MOV P1,A
CLR P3.0
LCALL DELAY1MS
SETB P3.0
MOV A,#10 //Display '-'
MOVC A,@A+DPTR
MOV P1,A
CLR P3.1
LCALL DELAY1MS
SETB P3.1
MOV A,BAI_BIT //Display temperature in hundreds digit
MOVC A,@A+DPTR
MOV P1,A
CLR P3.2
LCALL DELAY1MS
SETB P3.2
MOV A,SHI_BIT //Display temperature in tens digit
MOVC A,@A+DPTR
MOV P1,A
CLR P3.3
LCALL DELAY1MS
SETB P3.3
MOV A,GE_BIT //Display temperature units
MOVC A,@A+DPTR
MOV P1,A
CLR P3.4
LCALL DELAY1MS
SETB P3.4
MOV A,#11 //Display \'C\'
MOVC A,@A+DPTR
MOV P1,A
CLR P3.5
LCALL DELAY1MS
SETB P3.5
MOV A,#10 //Display '-'
MOVC A,@A+DPTR
MOV P1,A
CLR P3.6
LCALL DELAY1MS
SETB P3.6
/* MOV A,#10 //Display '-'
MOVC A,@A+DPTR
MOV P1,A
CLR P3.7
LCALL DELAY1MS
SETB P3.7 */
DJNZ R1,HUAN ;250 endless loops
DJNZ R0,XUN ;4 endless loops of 250 times
RET
DELAY10MS:MOV R7,#1 //Delay 10MS subroutine
L1:NOP
DJNZ R7,L1
DELAY1MS:RET
TABLE:DB 3FH ;0
DB 06H ;1
DB 5BH ;2
DB 4FH ;3
DB 66H ;4
DB 6DH ;5
DB 7DH ;6
DB 07H ;
7 DB 7FH
;8
DB 6FH ;9
DB 01000000B ;-
DB 39H ;C
END
Previous article:51 single chip microcomputer frequency meter program (4 digits without decimal point)
Next article:Single chip microcomputer drives 16*16 dot matrix LED Chinese character display C51 program
Recommended ReadingLatest update time:2024-11-16 15:03
- 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
- Terasic C5P FPGA development board and Intel Up2 Squared Grove development kit released
- EEWORLD University - Introduction to Switching Power Supply Circuit Design
- 【ST NUCLEO-H743ZI Review】(3) First experience with UART3 testing
- DIY-Small Stool (First Work)
- Simple self-start
- MSP430F149 minimum system circuit diagram
- Calculating sound decibels
- Urgent! The device just came out yesterday, and today my teammates ran away!
- Byte alignment for 32-bit embedded systems
- Layman asks questions about ZigBee