/*DS18B20温度计*/
ORG 0000H
LJMP MAIN
ORG 0030H
MAIN: MOV R0, #00H /*复位标志*/
MOV 20H,#00H /*存低字节*/
MOV 21H,#00H /*存高字节*/
DS18B20_TEMP3 EQU 22H
LOOP3: ACALL DS18B20_INIT
CJNE R0,#01H,$
ACALL DS18B20_DISP
MOV A,#0CCH
ACALL DS18B20_WHITE
MOV A,#44H
ACALL DS18B20_WHITE
MOV R6,#170 /*延时500MS*/
LOOP4: ACALL DS18B20_DISP
DJNZ R6,LOOP4
ACALL DS18B20_INIT
CJNE R0,#01H,$
MOV A,#0CCH
ACALL DS18B20_WHITE
MOV A,#0BEH
ACALL DS18B20_WHITE
ACALL DS18B20_READ
ACALL DS18B20_HCD
ACALL DS18B20_DISP
AJMP LOOP3
DS18B20_INIT: SETB P2.7
CLR P2.7
ACALL DELAY500uS /*>=480uS*/
SETB P2.7
ACALL DELAY45uS /*>=15uS*/
JB P2.7, $
MOV R0,#01H
ACALL DELAY500uS
RET
DS18B20_WHITE:MOV R1,#08H
SETB P2.7
CLR C
LOOP: CLR P2.7
ACALL DELAY15uS /*>=15uS*/
NOP
NOP
RRC A
MOV P2.7,C
ACALL DELAY45uS /*>=45uS*/
NOP
NOP
SETB P2.7
NOP /*>=1uS*/
NOP
DJNZ R1,LOOP
SETB P2.7
NOP
RET
DS18B20_READ: MOV R2,#08H
CLR A
LOOP2: CLR C
CLR P2.7 /*>1uS*/
NOP
NOP
SETB P2.7
ACALL DELAY10uS /*15uS以内*/
MOV C,P2.7
RRC A
ACALL DELAY45uS /*>=60uS*/
NOP
DJNZ R2,LOOP2
MOV 20H,A /*低字节*/
MOV R2,#08H
LOOP1: CLR C
CLR P2.7 /*>1uS*/
NOP
NOP
SETB P2.7
ACALL DELAY10uS /*15uS以内*/
MOV C,P2.7
RRC A
ACALL DELAY45uS /*>=60uS*/
NOP
DJNZ R2,LOOP1
MOV 21H,A /*高字节*/
RET
DS18B20_HCD: MOV A,20H
ANL A,#0F0H
SWAP A
MOV 20H,A /*低字节低四位丢掉*/
MOV A,21H
ANL A,#0FH /*高字节高四位丢掉*/
SWAP A
ORL A,20H
MOV DS18B20_TEMP3,A /*合并*/
MOV A,DS18B20_TEMP3
MOV B,#10
DIV AB
MOV 23H,B /*个位*/
MOV B,#10
DIV AB
MOV 24H,B /*十位*/
MOV 25H,A /*百位*/
MOV A,20H /*小数位*/
ANL A,#0FH
MOV B,#10
MUL AB
MOV B,#10H
DIV AB
MOV 26H,A
RET
DS18B20_DISP: MOV P0,#0C6H /*显示C单位*/
CLR P3.7
ACALL DELAY1MS
SETB P3.7
MOV A,26H /*小数位*/
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
CLR P3.6
ACALL DELAY1MS
SETB P3.6
MOV P0,#7FH /*显示小数点*/
CLR P3.5
ACALL DELAY1MS
SETB P3.5
MOV A,23H
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
CLR P3.4
ACALL DELAY1MS
SETB P3.4
MOV A,24H
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
CLR P3.3
ACALL DELAY1MS
SETB P3.3
MOV A,25H
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
CLR P3.2
ACALL DELAY1MS
SETB P3.2
RET
TABLE: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H /*0-9*/
DELAY500uS: MOV R4,#250
DJNZ R4,$
RET
DELAY15uS: MOV R4,#7
DJNZ R4,$
RET
DELAY10uS: MOV R4,#5
DJNZ R4,$
RET
DELAY45uS: MOV R4,#22
DJNZ R4,$
RET
DELAY1MS: MOV R4, #50 /*约1MS*/
D3: MOV R5, #10
DJNZ R5, $
DJNZ R4, D3
RET
END
文章评论(0条评论)
登录后参与讨论