求一个51单片机 用DS18B20芯片实现 的温控程序

基于AT89S51单片机的数字温度测量及显示系统设计

程序

DI EQU P3.3

DO EQU P3.4

CLK EQU P3.5

CS EQU P3.6 ; LCD端口定义

D2RS EQU P2.7

D2RW EQU P2.6

D2E EQU P2.5

KEYPORT EQU P1 ; DS18B20端口定义

TEMPER_L EQU 36H

TEMPER_H EQU 35H

TEMPER_NUM EQU 38H

FLAG1 BIT 00H

DQ BIT P2.4

ORG 0000H

LJMP MAIN

ORG 0003H

JMP INT00

ORG 0038H

MAIN: MOV SP,#60H

SETB P2.0

SETB P2.1

SETB P2.2

SETB EA

SETB EX0

SETB P2.0

SEETB P2.1

SETB P2.2

MOV R0,#01H ;清屏并置地址计数器AC为0

LCALL DIS_CMD _WRT

MOV R0,#38H ;8位数据接口,双行显示,5*7点阵

LCALL DIS_CMD _WRT

CALL DIS_CUR_OFF

MOV 42H,#20

MOV 43H,#32

XIAN: LCALL GET_TEMPER

LCALL DISP

LCALL DELAY43MS

MOV A,TEMPER_NUM

SUBB A,42H

JC ZZZL

MOV A,TEMPER_NUM

SUBB A,43H

JNC ZZZ2

SETB P2.0

SETB P2.1

SETB P2.2

JMP XIAN

ZZZL: CLR P2.0

CLR P2.2

JMP XIAN

ZZZ2: CLR P2.0

CLR P2.1

JMP XIAN

INT0: ;扫描键盘程序

LCALL ASK

SAO: CLR 01H

LCALL KEY

JNB 01H,SAO

CJNE A,#10,PAN

LCALL ANSW

RETI

PAN: CJNE A,#12,SAO

RETI ;显示函数部分,可供调用

DIS_CUR_OFF: MOV R0,#0CH

LCALL DIS_CMD_WRT

RET

DIS_CUR_ON: MOV R0,#0EH

LCALL DIS_CMD_WRT

RET

CHK_BUSY_FLG: MOV P0,#0FFH

CLR D2RS

NOP

NOP

NOP

LCALL DISPLAY_RD

JB ACC.7,CHK_BUSY_FLG

RET

CLEAR_DIS: MOV R0,#01H

LCALL DIS_CMD_WRT

RET

DIS_CMD_WRT: LCALL CHK_BUSY_FLG

MOV P0,R0

CLR D2RS

NOP

NOP

NOP

LCALL DISPLAY_WRT

RET

DIS_DATA_WRT: LCALL CHK_BUSY_FLG

CJNE A,#10H,DIS_DAT_WRT1

MOV P0,#0C0H ;1100,00000行起始地址为40

CLR D2RS

NOP

NOP

NOP

LCALL DISPLAY_WRT

DIS_DAT_WRT1: MOV P0,R0

SETB D2RS

NOP

NOP

NOP

LCALL DISPLAY_WRT

RET

DISPLAY_WRT: CLR D2RW

NOP

NOP

NOP

SETB D2E

NOP

NOP

NOP

CLR D2E

NOP

NOP

NOP

RET

DISPLAY_RD: SETB D2RW

NOP

NOP

NOP

SETB D2E

NOP

NOP

NOP

MOV A,P0

NOP

NOP

NOP

CLR D2E

NOP

NOP

NOP

RET

DIS_DATA_RD: LCALL CHK_BUSY_FLG

MOV P0,#0FFH

SETB D2RS

LCALL DISPLAY_RD

RET ;键盘程序,出口:A为按键值 01H:0无键按下 1 有键按下

KEY: MOV KEYPORT,#0FH

MOV A,KEYPOR

CJNE A,#0FH,KEYDOWN

MOV A,#0FFH

RET

KEYDOWN: LCALL DELAY43MS

MOV KEYPORT,#0FH

MOV A,KEYPORT

CJNE A,#0FH,KEYDOWN_YES

MOV A,#0FFH

RET

KEYDOWN _YES: MOV B,A

MOV KEYPORT,#0F0H

MOV A,KEYPORT

ORL A,B

PUSH 30H

MOV 30H,A

MOV R3,#10H

MOV DPTR,#KEYVALUE

NEXT_KEY: MOV A,R3

MOVC A,@a+dptr

CJNE A,30h,NEXTKEYVALUE

DEC R3

POP 30H

WAITKEY_F: MOV KEYPORT,#0FH

MOV A,KEYPORT

CJNE A,#0FH, WAITKEY_F

MOV A,R3

SETB 01H

RET

NEXTKEYVALUE: DJNZ R3,NEXT_KEY

DEC R3

POP 30H

WAITKEY_FREE: MOV KEYPORT,#0FH

MOV A,KEYPORT

CJNE A,#0FH, WAITKEY_FREE

MOV A,R3

SETB 01H

RET

KEYVALUE: DB 0FFH,7EH,7DH,7BH,77H,0BEH,0BDH,0BBH,0B7H,0DEH,0DDH,0DBH,0D7H,0EEH,0EDH,0EBH,0E7H

DELAY43MS: PUSH A

MOV A,R3

PUSH A

MOV A,R2

PUSH A

MOV R3,#43

DELAY: MOV R2,#0FAH

LOOP: NOP ;内层循环为1MS

NOP

DJNZ R2,LOOP

DJNZ R3, DELAY

POP A

MOV R2,A

POP A

MOV R3,A

POP A

RET ;确认是否修改温度设定

ASK: MOV R0,#01H ;清屏并置地址计数器AC为0

LCALL DIS_CMD_WRT

MOV 40H,#0

ZDZ: MOV DPTR,#LINE

MOV A,40H

MOVC A,@A+DPTR

MOV R0,A

LCALL DIS_DATA_WRT

INC 40H

MOV A,40H

MOVC A,@A+DPTR

CJNE A,#00H,ZDZ

RET

LINE: DB " ARE YOU SURE

CHANGE T(Y/N)?",00H

ANSW: MOV R6,42H

MOV R7,43H

MOV R0,#01H

LCALL DIS_CMD_WRT

MOV R0,#44H

CALL DIS_DATA_WRT

MOV R0,#6FH

CALL DIS_DATA_WRT

MOV R0,#77H

CALL DIS_DATA_WRT

MOV R0,#6EH

CALL DIS_DATA_WRT

MOV R0,#3AH

CALL DIS_DATA_WRT

MOV 40H,#0

CLR 00H

JMP SAO1

GAI: MOV R0,#0FEH

CALL DIS_DATA_WRT

MOV R0,#55H

CALL DIS_DATA_WRT

MOV R0,#70H

CALL DIS_DATA_WRT

MOV R0,#3AH

CALL DIS_DATA_WRT

MOV 40H,#0

SAO1: CLR 01H

LCALL KEY

JNB 01H,SAO1

MOV R1,A

MOV B,#10

DIV AB

JZ NEXT2

MOV A,A1

CJNE A,#11,XU

JB 00H,DOWN

MOV 42H,40H

CPL 00H

JMP GAI

DOWN: MOV 43H,40H

RETI

XU: CJNE A,#12,SAO1

MOV 42H,R6

MOV 43H,R7

RETI

NEXT2: MOV A,R1

ADD A,#30H

MOV R0,A

LCALL DIS_DATA_WRT

MOV A,R1

XCH A,40H

MOV B,#10

MUL AB

NOP

CLR DQ

CLR C

WR1: CLR DQ

MOV R3,#6

DJNZ R3,$

RR R4,#2 R1,#36H ; 低位存入36H(TEMPER_L),高位存入35H(TEMPER_

NOP

NOP

SETB DQ

MOV R3,#7

DJNZ R3,$

MOV C,DQ

MOV R3,#23

DJNZ R3,$

RRC A

DJNZ R2,RE01

MOV @R1,A

DEC R1

DJNZ R4,RE00

RET ; 将从DS18B20中读出的温度数据进行转换

TEMPER_COV: MOV A,#0f0H

ANL A,TEMPER_L ; 舍去温度低位中小数点后的四位温度数值

SWAP A

MOV TEMPER_NUM,A

MOV A,TEMPER_L

JNB ACC.3,TEMPER_COV1 ; 四舍五入去温度值

INC TEMPER_NUM

TEMPER_COV1:MOV A,TEMPER_H

ANL A,#07H

SWAP A

ORL A,TEMPER_NUM

MOV TEMPER_NUM,A ; 保存变换后的温度数据

RET ;AD0832:

SETB DI

SETB DO

SETB CLK

CLR CS

NOP

SETB CS

CLR CLK

CLR CS

CALL DELAY1

SETB DI

SETB CLK

NOP

CLR CLK

SETB DI

SETB CLK

NOP

CLR CLK

CLR DI

SETB CLK

NOP

CLR CLK

NOP

SETB CLK

NOP

CLR CLK

NOP

SETB CLK

MOV C,DO

CLR CLK

RLC A;7

SETB CLK

MOV C,DO

CLR CLK

RLC A 6

SETB CLK

MOV C,DO

CLR CLK

RLC A ;5

SETB CLK

MOV C,DO

CLR CLK

RLC A ;4

SETB CLK

MOV C,DO

CLR CLK

RLC A ;3

SETB CLK

MOV C,DO

CLR CLK

RLC A ;2

SETB CLK

MOV C,DO

CLR CLK

RLC A ;1

SETB CLK

MOV C,DO

CLR CLK

RLC A ;0

SETB CLK

NOP

CLR CLK ;1

NOP

SETB CLK

NOP

CLR CLK ;2

NOP

SETB CLK

NOP

CLR CLK ;3

NOP

SETB CLK

NOP

CLK ;4

NOP

SETB CLK

NOP

CLR CLK ;5

NOP

SETB CLK

NOP

CLR CLK ;6

NOP

SETB CLK

NOP

CLR CLK ;7

NOP

SETB CLK

NOP

CLR CLK

NOP

SETB CLK

NOP

CLR CLK

NOP

CALL DELAY1

SETB CS

RET

DELAY1: MOV R7,#10

DELAY2: NOP

NOP

DJNZ R7,DELAY2

RET