ds18b20测量温度并显示到lcd1602上的汇编程序
时间: 2023-09-22 12:07:39 浏览: 198
以下是使用ds18b20测量温度并将其显示到lcd1602上的汇编程序:
```
;-----------------------DS18B20温度传感器-----------------------
;DS18B20温度传感器引脚定义
DS18B20_PORT EQU P3
DS18B20_PIN EQU 0
;LCD1602引脚定义
LCD1602_DATA_PORT EQU P0
LCD1602_RS EQU P2.0
LCD1602_RW EQU P2.1
LCD1602_EN EQU P2.2
;定义常量
DELAY_TIME EQU 500
;初始化DS18B20
INIT_DS18B20 PROC
MOV A, #0
MOV DS18B20_PORT, A
MOV A, #1
MOV DPTR, #DELAY_TIME
CALL DELAY
MOV A, #1
MOV DS18B20_PORT, A
MOV DPTR, #DELAY_TIME
CALL DELAY
RET
INIT_DS18B20 ENDP
;向DS18B20发送指令
SEND_DS18B20 PROC
MOV A, #0
MOV DS18B20_PORT, A
MOV A, #1
MOV DPTR, #DELAY_TIME
CALL DELAY
MOV A, #1
MOV DS18B20_PORT, A
MOV DPTR, #DELAY_TIME
CALL DELAY
RET
SEND_DS18B20 ENDP
;读取DS18B20的响应
READ_DS18B20 PROC
MOV A, DS18B20_PORT
ANL A, #01H
RET
READ_DS18B20 ENDP
;发送一个字节的数据
SEND_BYTE PROC BYTE_VAL
MOV R7, #08H
SEND_BYTE_LOOP:
MOV A, BYTE_VAL
RLC A
MOV DS18B20_PORT.0, C
MOV DPTR, #DELAY_TIME
CALL DELAY
MOV DS18B20_PORT.0, 1
DJNZ R7, SEND_BYTE_LOOP
RET
SEND_BYTE ENDP
;读取一个字节的数据
READ_BYTE PROC
MOV R7, #08H
MOV A, #0
READ_BYTE_LOOP:
RLC A
MOV DS18B20_PORT.0, 1
MOV DPTR, #DELAY_TIME
CALL DELAY
MOV C, DS18B20_PORT.0
JNC READ_BYTE_SKIP
SETB A.0
READ_BYTE_SKIP:
MOV DS18B20_PORT.0, 0
DJNZ R7, READ_BYTE_LOOP
RET
READ_BYTE ENDP
;读取温度
READ_TEMPERATURE PROC
CALL INIT_DS18B20
CALL SEND_DS18B20
CALL SEND_BYTE, #0CCH
CALL SEND_BYTE, #044H
CALL INIT_DS18B20
CALL SEND_DS18B20
CALL SEND_BYTE, #0CCH
CALL SEND_BYTE, #BEH
CALL READ_BYTE
MOV R6, A
CALL READ_BYTE
MOV R5, A
MOV A, R5
MOV B, R6
CPL A
CPL B
JZ READ_TEMPERATURE_SKIP
MOV A, #0FFH
READ_TEMPERATURE_SKIP:
RET
READ_TEMPERATURE ENDP
;显示温度到LCD1602
DISPLAY_TEMPERATURE PROC TEMPERATURE
MOV A, TEMPERATURE
ACALL CONVERT_TEMPERATURE
MOV LCD1602_RS, #0
MOV LCD1602_RW, #0
MOV LCD1602_DATA_PORT, #38H
MOV LCD1602_EN, #1
MOV DPTR, #DELAY_TIME
CALL DELAY
MOV LCD1602_EN, #0
MOV LCD1602_DATA_PORT, #0FH
MOV LCD1602_EN, #1
MOV DPTR, #DELAY_TIME
CALL DELAY
MOV LCD1602_EN, #0
MOV LCD1602_RS, #1
MOV LCD1602_RW, #0
MOV A, #80H
ADD A, #0
MOV LCD1602_DATA_PORT, A
MOV LCD1602_EN, #1
MOV DPTR, #DELAY_TIME
CALL DELAY
MOV LCD1602_EN, #0
MOV A, TEMPERATURE_HIGH
ACALL CONVERT_TEMPERATURE
MOV LCD1602_DATA_PORT, A
MOV LCD1602_EN, #1
MOV DPTR, #DELAY_TIME
CALL DELAY
MOV LCD1602_EN, #0
MOV A, TEMPERATURE_LOW
ACALL CONVERT_TEMPERATURE
MOV LCD1602_DATA_PORT, A
MOV LCD1602_EN, #1
MOV DPTR, #DELAY_TIME
CALL DELAY
MOV LCD1602_EN, #0
RET
DISPLAY_TEMPERATURE ENDP
;转换温度为可显示的字符
CONVERT_TEMPERATURE PROC
MOV R0, A
MOV R1, #10H
DIV AB, R1
MOV TEMPERATURE_HIGH, A
MOV A, B
MOV R1, #10
DIV AB, R1
ADD A, #30H
MOV TEMPERATURE_LOW, A
RET
CONVERT_TEMPERATURE ENDP
;延时函数
DELAY PROC
MOV R2, #0FFH
DELAY_LOOP:
DJNZ R2, $
MOV R3, #0FFH
DELAY_LOOP_1:
DJNZ R3, DELAY_LOOP_1
DJNZ DPTR, DELAY_LOOP
RET
DELAY ENDP
;主程序
MAIN:
MOV P0, #0
MOV P2, #0
CALL DISPLAY_TEMPERATURE, READ_TEMPERATURE
SJMP MAIN
```
需要注意的是,以上程序仅供参考,实际使用中可能需要进行适当的修改和调试。
阅读全文