您现在所在的是:

单片机论坛

回帖:0个,阅读:1902 [上一页] [1] [下一页]
923
cvssv
文章数:25
年度积分:50
历史总积分:923
注册时间:2004/8/12
发站内信
发表于:2004/8/13 12:48:00
#0楼
DS18B20的源程序,采用12位转换。
;***************


;Ds18b20_program 2003-1-3


;***************


TemperL EQU 8H


TemperH EQU TemperL+1


TemperDotL EQU TemperH+1


TemperDotH equ TemperDotL+1


TemperZer equ TemperDotH+1


Led1 equ TemperZer+1


led2 equ led1+1


led3 equ led2+1


DogL equ Led3+1


DogH equ DogL+1


SpData equ 22h





DogX equ 0 ;4Min





Ds18b20F BIT 00H



DQ BIT P3.7


led_1 bit p3.0


led_2 bit p3.1


led_3 bit p3.4





key_f bit p3.3


pnp1 bit p3.4


pnp2 bit p3.5








oRG 0000H; ;


sJMP start


ORG 0003H; ;


reti;


ORG 000BH ;


reti ;


ORG 0013H


reti ;


ORG 001BH ;


reti;aJMP T1 ;


ORG 0023H ;


RETI; AJMP RS_485 ;


;**************************************


start:


MOV R7,#7fh ;


MOV R0,#1 ;


CR: INC R0 ;


MOV @R0,#0 ;


djnz r7,cr ;CLR RAM


MOV SP,#SpData ;


;*****************


; MINI_Pragram


;*****************


; mov Dogl,#DogX


mov led1,#0fh


mov led2,#ff_led


mov led3,#ff_led


STAR_BI:


CALL GetTemper


CALL TemperDo


call led


djnz DogL ,Star_bi


; djnz DogH,Star_bi


mov p1,#0ffh


mov p3,#0ffh


mov Pcon,#02


sjmp Star_bi


;*******************


LED:


mov dptr,#led_asc


;***************************led1


mov a,led1


movc a,@a+dptr


call TurnAsc1


mov p1,a


clr led_1


call timer_1ms


setb led_1


;*************************led2


led_led2:


mov a,led2


movc a,@a+dptr


call TurnAsc


mov p1,a


clr led_2


call timer_1ms


setb led_2


;*************************led3


led_led3:


mov a,led3


movc a,@a+dptr


call TurnAsc


mov p1,a


clr led_3


call timer_1ms


setb led_3


led_end:


ret


;*************************


black equ 1Ch


l_led equ 11h


o_led equ 15h


u_led equ 1Bh


r_led equ 18h


QQ_led equ 1dh


p_led equ 16h


a_led equ 0ah


UU_led equ 1ah


s_led equ 19h


e_led equ 0eh


tt_led equ 1fh


ff_led equ 1eh


c_led equ 0ch


h_led equ 10h


b_led equ 0bh


d_led equ 0dh


f_led equ 0fh


nn_led equ 12h


n_led equ 13h


cc_led equ 2ah


oo_led equ 15h


rr_led equ 16h


dot_led equ 2bh


;"bafgecdh"


;*********************************************


; led=> / abcdefgh


led_asc:


db 11111100B;0


db 01100000B;1


db 11011010B;2


db 11110010B;3


db 01100110B;4


db 10110110B;5


db 10111110B;6


db 11100000B;7


db 11111110B;8


db 11110110B;9


DB 11101110B;a=>A


DB 00111110B;B=>B


DB 10011100B;C=>C


DB 01111010B;D=>D


DB 10011110B;E=>E


DB 10001110B;F=>F


DB 01101110B;H=>10H


DB 00011100B;L=>11H


DB 11101100B;N=>12H


db 00101010b;n=>13h


db 11111100b;O=>14h


dB 00111010B;o=>15H


DB 11001110B;P=>16H


DB 11100110B;Q=>17H


DB 00001010B;r=>18H


DB 10110110B;S=>19H


DB 01111100B;U=>1AH


DB 00111000B;u=>1BH


DB 00000000B;BLACK=>1CH


DB 11111111B;8.=>1DH


db 00000010b;-=>1Eh


db 00011110b;t=>1Fh


db 11111101B;0.=>20h


db 01100001B;1.


db 11011011B;2.


db 11110011B;3.


db 01100111B;4.


db 10110111B;5.


db 10111111B;6.


db 11100001B;7.


db 11111111B;8.


db 11110111B;9.=>29h


;************* add 2002.12.10


DB 11101111B;a.=>A


DB 00111111B;B.=>B


DB 10011101B;C.=>C


DB 01111011B;D.=>D


DB 10011111B;E.=>E


DB 10001111B;F.=>F


DB 01101111B;H.=>10H


DB 00011101B;L.=>11H


DB 11101101B;N.=>12H


db 00101011b;n.=>13h


db 11111101b;O.=>14h


dB 00111011B;o.=>15H


DB 11001111B;P.=>16H


DB 11100111B;Q.=>17H


DB 00001011B;r.=>18H


DB 10110111B;S.=>19H


DB 01111101B;U.=>1AH


DB 00111001B;u.=>1BH


DB 00000001B;BLACK.=>1CH


DB 11111111B;8.=>1DH


db 00000011b;-=>1Eh


db 00011111b;t=>1Fh


db ''li jie by 2002.12.24''


;*************************************


timer_1ms:


djnz b,$


djnz b,$


ret


;"bafgecdh"


;*********************************************


; led=> / abcdefgh ,bafgecdh


TurnAsc:


mov c,acc.0


mov b.0,c


mov c,acc.1


mov b.4,c


mov c,acc.2


mov b.5,c


mov c,acc.3


mov b.3,c


mov c,acc.4


mov b.1,c


mov c,acc.5


mov b.2,c


mov c,acc.6


mov b.7,c


mov c,acc.7


mov b.6,c


mov a,b


ret


;***************************


;abcdefgh=>bafhecdg


TurnAsc1:


mov c,acc.0


mov b.4,c


mov c,acc.1


mov b.0,c


mov c,acc.2


mov b.5,c


mov c,acc.3


mov b.3,c


mov c,acc.4


mov b.1,c


mov c,acc.5


mov b.2,c


mov c,acc.6


mov b.7,c


mov c,acc.7


mov b.6,c


mov a,b


ret


;******************


;读出转换后的温度值


GetTemper:


SkipDs18b20 equ 0cch


StartDs18b20 equ 44h


ReadDs equ 0beh


;***************************


SETB DQ ; 定时入口


call InitDs18b20


JB Ds18b20F,S22


ret


S22:call Delay64uS


MOV A,#SkipDs18b20 ; 跳过ROM匹配------0CC


call WriteByteDs1820


MOV A,#StartDs18b20 ; 发出温度转换命令


call WriteByteDs1820


call DELAY1s


call InitDs18b20


JB Ds18b20F,ABC


ret


ABC:call Delay64uS


MOV A,#SkipDs18b20 ; 跳过ROM匹配


call WriteByteDs1820


MOV A,#ReadDs ; 发出读温度命令


call WriteByteDs1820


call ReadDs18b20 ;READ_1820


RET


;------------------读DS18B20的程序,从DS18B20中读出一个字节的数据


ReadByteDs1820:


MOV R2,#8


RE1:


CLR C


SETB DQ


NOP


NOP


CLR DQ


NOP


NOP


NOP


SETB DQ


MOV R3,#7


DJNZ R3,$


MOV C,DQ


MOV R3,#23


DJNZ R3,$


RRC A


DJNZ R2,RE1


RET


;-------------------写DS18B20的程序


WriteByteDs1820:


MOV R2,#8


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


;-------------------读DS18B20的程序,从DS18B20中读出两个字节的温度数据


ReadDs18b20:


Call ReadByteDs1820


MOV TemperL,A


Call ReadByteDs1820


MOV TemperH,A


RET


;------------------重新写DS18B20暂存存储器设定值


RE_CONFIG:


JB Ds18b20F,RE_CONFIG1 ; 若DS18B20存在,转RE_CONFIG1


RET


RE_CONFIG1:


MOV A,#0CCH ; 发SKIP ROM命令


call WriteByteDs1820


MOV A,#4EH ; 发写暂存存储器命令


call WriteByteDs1820


MOV A,#00H ; TH(报警上限)中写入00H


;call WriteByteDs1820


MOV A,#00H ; TL(报警下限)中写入00H


;call WriteByteDs1820


MOV A,#7FH ; 选择12位温度分辨率


call WriteByteDs1820


RET


;------------------延时子程序


DELAY:


MOV R7,#00H


Dey1:


call led


DJNZ R7,Dey1


RET


Delay500uS:


MOV R6,#00H


DJNZ R6,$


RET


Delay64uS:


MOV R6,#20H


DJNZ R6,$


RET


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


;2^3,2^2,2^1,2^0,2^-1,2^-2,2^-3,2^4 ;L


;SSSSSSS2^6,2^5,2^4;H


;************************


TemperD


JB Ds18b20F,Do1


ret


do1:


clr a


mov TemperDotL,a


mov TemperDotH,a


mov TemperZer,a


;*****************


mov a,TemperH


jnb acc.7,ZhenData ;>0


;<0


mov a,TemperL


cpl a


add a,#01 ;cpl+1


mov TemperL,a


mov a,TemperH


cpl a


addc a,#0


mov TemperH,a


ZhenData:


call FenLiDot


call FenLiZer


call HexToLed


call Led


ret


;**********************


FenLiZer:


mov a,TemperL


jnb acc.4,Fz1


mov a,#01


add a,TemperZer


mov TemperZer,a


Fz1:


mov a,TemperL


jnb acc.5,Fz2


mov a,#02


add a,TemperZer


mov TemperZer,a


Fz2:


mov a,TemperL


jnb acc.6,Fz3


mov a,#04


add a,TemperZer


mov TemperZer,a


Fz3:


mov a,TemperL


jnb acc.7,Fz4


mov a,#08


add a,TemperZer


mov TemperZer,a


Fz4:


mov a,TemperH;!!!!


jnb acc.0,Fz5


mov a,#16


add a,TemperZer


mov TemperZer,a


Fz5:


mov a,TemperH


jnb acc.1,Fz6


mov a,#32


add a,TemperZer


mov TemperZer,a


Fz6:


mov a,TemperH


jnb acc.2,Fz7


mov a,#64


add a,TemperZer


mov TemperZer,a


Fz7:


ret


;****************************


FenLiDot:


mov a,TemperL


jnb acc.3,Fen1 ;2^-1


mov r5,#low(5000)


mov r4,#High(5000)


call Add2


Fen1:


jnb acc.2,Fen2 ;2^-2


mov r5,#low(2500)


mov r4,#High(2500)


call Add2


Fen2:


jnb acc.1,Fen3 ;2^-3


mov r5,#low(1250)


mov r4,#High(1250)


call Add2


Fen3:


jnb acc.0,Fen4 ;2^-4


mov r5,#low(625)


mov r4,#High(625)


call Add2


Fen4:


ret


;****************


;TemperDorHL+r4r5=r2r3


;*****************


Add2:


push a


mov a,TemperDotL


add a,r5


mov TemperDotL,a


mov a,TemperDotH


addc a,r4


mov TemperDotH,a


pop a


ret


;***********************


; D=>BCD_16


;***********************


bcd_16: clr a ;in:r3r2[16 bdata]


mov r4,a ;R6,r5,r4[5 bcddata]


mov r5,a


mov R6,a


mov R7,#10h


lp0: clr c


mov a,r2


rlc a


mov r2,a


mov a,r3


rlc a


mov r3,a


mov a,r4


addc a,r4


da a


mov r4,a


mov a,r5


addc a,r5


da a


mov r5,a


mov a,R6


addc a,R6


da a


mov R6,a


djnz R7,lp0


ret


;************************


HexToLed:


mov r3,TemperDotH


mov r2,TemperDotL


call Bcd_16 ;r3r2(in)=>r6r5r4(out)


mov a,r5


anl a,#0f0h


swap a


mov led3,a


mov a,r5


anl a,#0fh


cjne a,#05,He1


;>=0.5


He2:


inc led3


sjmp He3


He1:


jnc He2


;******* Zer To Bcd


He3:


;***************************


mov a,TemperZer


mov b,#10


div ab ;a.b


mov led1,a


jnz He4


mov led1,#black


He4:


mov a,b


add a,#20h ;Add Dot


mov led2,a


ret


;***************


Delay1S:


mov b,#130


Dela11:


push b


call Led


call Led


pop b


djnz b,Dela11


ret


;-------------------DS18B20初始化程序


InitDs18b20:


CLR DQ


MOV R7,#80h


DJNZ R7,$ ; 延时


SETB DQ


MOV R7,#25h ;


DJNZ R7,$


SETB Ds18b20F ; 置标志位,表示DS1820存在


JNB DQ,TSR5


CLR Ds18b20F ; 清标志位,表示DS1820不存在


Jmp TSR7


TSR5:


MOV R7,#06BH ;200US


DJNZ R7,$ ; 延时


TSR7:


SETB DQ


RET



[此贴子已经被思南于2004-8-15 23:17:41编辑过]

关于我们 | 联系我们 | 广告服务 | 本站动态 | 友情链接 | 法律声明 | 非法和不良信息举报

工控网客服热线:0755-86369299
版权所有 工控网 Copyright©2024 Gkong.com, All Rights Reserved

62.4004