单片机8通道自动温度检测系统仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- CLR P3.7
- MAIN:MOV 38H,#00H
- MOV 39H,#00H
- MOV 3AH,#00H
- MOV 3BH,#00H
- LCALL GET_TEMPER
- LCALL TEMPRRO
- MOV P2,#06H
- LCALL PRO
- ;------------------------------------------------------
- MOV 38H,#00H
- MOV 39H,#00H
- MOV 3AH,#00H
- MOV 3BH,#00H
- LCALL GET_TEMPER2
- LCALL TEMPRRO
- MOV P2,#5BH
- LCALL PRO
- ;------------------------------------------------------
- MOV 38H,#00H
- MOV 39H,#00H
- MOV 3AH,#00H
- MOV 3BH,#00H
- LCALL GET_TEMPER3
- LCALL TEMPRRO
- MOV P2,#4FH
- LCALL PRO
- ;------------------------------------------------------
- MOV 38H,#00H
- MOV 39H,#00H
- MOV 3AH,#00H
- MOV 3BH,#00H
- LCALL GET_TEMPER4
- LCALL TEMPRRO
- MOV P2,#66H
- LCALL PRO
- ;------------------------------------------------------
- MOV 38H,#00H
- MOV 39H,#00H
- MOV 3AH,#00H
- MOV 3BH,#00H
- LCALL GET_TEMPER5
- LCALL TEMPRRO
- MOV P2,#6DH
- LCALL PRO
- ;------------------------------------------------------
- MOV 38H,#00H
- MOV 39H,#00H
- MOV 3AH,#00H
- MOV 3BH,#00H
- LCALL GET_TEMPER6
- LCALL TEMPRRO
- MOV P2,#7DH
- LCALL PRO
- ;------------------------------------------------------
- MOV 38H,#00H
- MOV 39H,#00H
- MOV 3AH,#00H
- MOV 3BH,#00H
- LCALL GET_TEMPER7
- LCALL TEMPRRO
- MOV P2,#07H
- LCALL PRO
- ;------------------------------------------------------
- MOV 38H,#00H
- MOV 39H,#00H
- MOV 3AH,#00H
- MOV 3BH,#00H
- LCALL GET_TEMPER8
- LCALL TEMPRRO
- MOV P2,#7FH
- LCALL PRO
- LJMP MAIN
-
- ;==============================================CHL1
- GET_TEMPER:
- SETB P1.0H ; 定时入口
- BCD: LCALL INIT_1820
- JB 00H,S22
- LJMP BCD ; 若DS18B20不存在则返回
- S22: LCALL DELAY1
- MOV A,#0CCH ; 跳过ROM匹配------0CC命令
- LCALL WRITE_1820
- MOV A,#44H ; 发出温度转换命令
- LCALL WRITE_1820
- NOP
- LCALL DELAY1
- CBA:LCALL INIT_1820
- JB 00H,ABC
- LJMP CBA
- ABC:LCALL DELAY1
- MOV A,#0CCH ; 跳过ROM匹配
- LCALL WRITE_1820
- MOV A,#0BEH ; 发出读温度命令
- LCALL WRITE_1820
- LCALL READ_18200 ;READ_1820
- RET
- ;==============================
- READ_1820:
- MOV R2,#8
- RE1:
- CLR C
- SETB P1.0H
- NOP
- NOP
- CLR P1.0H
- NOP
- NOP
- NOP
- SETB P1.0H
- MOV R3,#7
- DJNZ R3,$
- MOV C,P1.0H
- MOV R3,#23
- DJNZ R3,$
- RRC A
- DJNZ R2,RE1
- RET
- ;=============================
- WRITE_1820:
- MOV R2,#8
- CLR C
- WR1:
- CLR P1.0H
- MOV R3,#6
- DJNZ R3,$
- RRC A
- MOV P1.0H,C
- MOV R3,#23
- DJNZ R3,$
- SETB P1.0H
- NOP
- DJNZ R2,WR1
- SETB P1.0H
- RET
- ;=======================================
- READ_18200:
- MOV R4,#2 ; 将温度高位和低位从DS18B20中读出
- MOV R1,#31H ; 低位存入30H(TEMPER_L),高位存入31H(TEMPER_H)
- RE00:
- MOV R2,#8
- RE01:
- CLR C
- SETB P1.0H
- NOP
- NOP
- CLR P1.0H
- NOP
- NOP
- NOP
- SETB P1.0H
- MOV R3,#7
- DJNZ R3,$
- MOV C,P1.0H
- MOV R3,#23
- DJNZ R3,$
- RRC A
- DJNZ R2,RE01
- MOV @R1,A
- DEC R1
- DJNZ R4,RE00
- RET
- ;=======================================
- INIT_1820:
- SETB P1.0H
- NOP
- CLR P1.0H
- MOV R0,#250
- TSR1:
- DJNZ R0,TSR1 ; 延时520us
- SETB P1.0H
- MOV R0,#25H ;96US-25H
- TSR2:
- DJNZ R0,TSR2
- JNB P1.0H,TSR3;如D0为低则复位成功,DS18B20存在
- LJMP TSR4 ; 延时
- TSR3:
- SETB 00H ; 置标志位,表示DS1820存在
- LJMP TSR5
- TSR4:
- CLR 00H ; 清标志位,表示DS1820不存在
- LJMP TSR7
- TSR5:
- MOV R0,#06BH ;200US
- TSR6:
- DJNZ R0,TSR6 ; 延时
- TSR7:
- SETB P1.0H ;拉高总线
- RET
- ;===============================
- GET_TEMPER2:
- SETB P1.1H ; 定时入口
- BCD2: LCALL INIT_18202
- JB 00H,S222
- LJMP BCD2 ; 若DS18B20不存在则返回
- S222: LCALL DELAY1
- MOV A,#0CCH ; 跳过ROM匹配------0CC命令
- LCALL WRITE_18202
- MOV A,#44H ; 发出温度转换命令
- LCALL WRITE_18202
- NOP
- LCALL DELAY1
- CBA2:LCALL INIT_18202
- JB 00H,ABC2
- LJMP CBA2
- ABC2:LCALL DELAY1
- MOV A,#0CCH ; 跳过ROM匹配
- LCALL WRITE_18202
- MOV A,#0BEH ; 发出读温度命令
- LCALL WRITE_18202
- LCALL READ_182002 ;READ_1820
- RET
- ;==============================
- READ_18202:
- MOV R2,#8
- RE12:
- CLR C
- SETB P1.1H
- NOP
- NOP
- CLR P1.1H
- NOP
- NOP
- NOP
- SETB P1.1H
- MOV R3,#7
- DJNZ R3,$
- MOV C,P1.1H
- MOV R3,#23
- DJNZ R3,$
- RRC A
- DJNZ R2,RE12
- RET
- ;=============================
- WRITE_18202:
- MOV R2,#8
- CLR C
- WR12:
- CLR P1.1H
- MOV R3,#6
- DJNZ R3,$
- RRC A
- MOV P1.1H,C
- MOV R3,#23
- DJNZ R3,$
- SETB P1.1H
- NOP
- DJNZ R2,WR12
- SETB P1.1H
- RET
- ;=======================================
- READ_182002:
- MOV R4,#2 ; 将温度高位和低位从DS18B20中读出
- MOV R1,#31H ; 低位存入30H(TEMPER_L),高位存入31H(TEMPER_H)
- RE002:
- MOV R2,#8
- RE012:
- CLR C
- SETB P1.1H
- NOP
- NOP
- CLR P1.1H
- NOP
- NOP
- NOP
- SETB P1.1H
- MOV R3,#7
- DJNZ R3,$
- MOV C,P1.1H
- MOV R3,#23
- DJNZ R3,$
- RRC A
- DJNZ R2,RE012
- MOV @R1,A
- DEC R1
- DJNZ R4,RE002
- RET
- ;=======================================
- INIT_18202:
- SETB P1.1H
- NOP
- CLR P1.1H
- MOV R0,#250
- TSR12:
- DJNZ R0,TSR12 ; 延时520us
- SETB P1.1H
- MOV R0,#25H ;96US-25H
- TSR22:
- DJNZ R0,TSR22
- JNB P1.1H,TSR32;如D0为低则复位成功,DS18B20存在
- LJMP TSR42 ; 延时
- TSR32:
- SETB 00H ; 置标志位,表示DS1820存在
- LJMP TSR52
- TSR42:
- CLR 00H ; 清标志位,表示DS1820不存在
- LJMP TSR72
- TSR52:
- MOV R0,#06BH ;200US
- TSR62:
- DJNZ R0,TSR62 ; 延时
- TSR72:
- SETB P1.1H ;拉高总线
- RET
- ;===============================
- GET_TEMPER3:
- SETB P1.2H ; 定时入口
- BCD3: LCALL INIT_18203
- JB 00H,S223
- LJMP BCD3 ; 若DS18B20不存在则返回
- S223: LCALL DELAY1
- MOV A,#0CCH ; 跳过ROM匹配------0CC命令
- LCALL WRITE_18203
- MOV A,#44H ; 发出温度转换命令
- LCALL WRITE_18203
- NOP
- LCALL DELAY1
- CBA3:LCALL INIT_18203
- JB 00H,ABC3
- LJMP CBA3
- ABC3:LCALL DELAY1
- MOV A,#0CCH ; 跳过ROM匹配
- LCALL WRITE_18203
- MOV A,#0BEH ; 发出读温度命令
- LCALL WRITE_18203
- LCALL READ_182003 ;READ_1820
- RET
- …………
- …………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
DS18B20.rar
(59.8 KB, 下载次数: 10)
|