📄 ds18b20 c51源代码.htm
字号:
<TD colSpan=2>
<TABLE style="WORD-BREAK: break-all" cellSpacing=5 cellPadding=0
width=740 align=center border=0>
<TBODY>
<TR>
<TD vAlign=top height=200>
<TABLE cellSpacing=0 cellPadding=10 align=left border=0>
<TBODY>
<TR>
<TD></TD></TR></TBODY></TABLE>
<P>ds18b20 c51源代码 作者[qlpic]</P>
<P>--- 摘自《C51BBS单片机C语言论坛》--- <BR>#include
"reg51.h"<BR>#include "INTRINS.H"<BR>#include "LCD.h"</P>
<P>#define CLR_RI (RI=0)<BR>#define CLR_TI
(TI=0)<BR>
<BR>unsigned char code ID[2][8]={
0x28,0x1D,0x25,0x1D,0x00,0x00,0x00,0x80,<BR>
0x28,0x0e,0x9e,0x1c,0x00,0x00,0x00,0x32};<BR>unsigned char
currSensorNo=0;</P>
<P>sbit TMDAT = P1^7;<BR>sbit
RUN_LED = P1^6;</P>
<P>/*------------------------------------------------<BR>*<BR>*------------------------------------------------*/<BR>void
serial_initial()<BR>{<BR>
TMOD=0X20;<BR>
SCON=0X50;<BR>
PCON=0X00;<BR>
TL1=TH1=0XE8;<BR> TR1=1;<BR>}</P>
<P>/*------------------------------------------------<BR>*<BR>*------------------------------------------------*/<BR>void
send(unsigned char count,unsigned char SEND_Buf[])<BR>{
<BR>unsigned char i;
<BR> for(i=0;i<count;i++)<BR> {<BR> SBUF=SEND_Buf[i];<BR> while(!TI);<BR> CLR_TI;<BR> }<BR>}</P>
<P>/*----------------------------------------------------<BR>*
delay N
ms<BR>*----------------------------------------------------*/<BR>void
Delay_ms (unsigned int Nms )<BR>{<BR>unsigned char
i;<BR> while(Nms--)<BR> for(i=0; i<125; i++)
;<BR>}</P>
<P>/*----------------------------------------------------<BR>*
delay N
count<BR>*----------------------------------------------------*/<BR>void
Delay_Count (unsigned char Count )<BR>{<BR>
while(Count>0) Count--;<BR>}</P>
<P>/*----------------------------------------------------<BR>*
start Reset
Pulse<BR>----------------------------------------------------*/<BR>void
tmreset(void)<BR>{<BR>
TMDAT=0;<BR> Delay_Count(103);</P>
<P> TMDAT=1;<BR>
Delay_Count(4);<BR>}</P>
<P>/*----------------------------------------------------<BR>*
ACK<BR>*----------------------------------------------------*/<BR>void
tmpre(void)<BR>{<BR>
while(TMDAT);<BR>
while(~TMDAT);<BR> Delay_Count(4);<BR>}</P>
<P>/*----------------------------------------------------<BR>*
Read a bit from
1820<BR>*----------------------------------------------------*/<BR>bit
tmrbit(void)<BR>{<BR> int
i=0;<BR> bit dat;<BR>
TMDAT=0;i++;<BR>
TMDAT=1;i++;i++;<BR> dat =
TMDAT;<BR>
Delay_Count(8);<BR> return dat;<BR>}</P>
<P>/*----------------------------------------------------<BR>*
Read a byte from
1820<BR>*----------------------------------------------------*/<BR>unsigned
char tmrbyte(void)<BR>{<BR>unsigned char i,j,dat=0;</P>
<P>
for(i=1;i<=8;i++)<BR>
{<BR>
j=tmrbit();<BR>
dat=(j<<7)|(dat>>1);<BR>
}<BR> return dat;<BR>}</P>
<P>/*----------------------------------------------------<BR>*
Read a byte from
1820<BR>*----------------------------------------------------*/<BR>void
tmwbyte(unsigned char dat)<BR>{<BR> signed
char i=0;<BR> unsigned char
j;<BR> bit testb;</P>
<P>
for(j=1;j<=8;j++)<BR>
{<BR> testb=dat
& 0x01;<BR> dat
= dat>>1;<BR>
if(testb)<BR>
{<BR>
TMDAT=0;<BR>
i++;i++;<BR>
TMDAT=1;<BR>
Delay_Count(8);<BR>
}<BR>
else<BR>
{<BR>
TMDAT=0;<BR>
Delay_Count(8);<BR>
TMDAT=1;<BR>
i++;i++;<BR>
}<BR> }<BR>}</P>
<P>/*----------------------------------------------------<BR>*
send convert command to
1820<BR>*----------------------------------------------------*/<BR>void
tmstart(void)<BR>{<BR>unsigned char
i;<BR> tmreset();<BR>
tmpre();<BR> Delay_ms(1);<BR>/*
tmwbyte(0xcc);*/<BR>
tmwbyte(0x55);<BR>
for(i=0;i<8;i++)<BR>
tmwbyte(ID[currSensorNo][i]);<BR>
tmwbyte(0x44);<BR>}</P>
<P><BR>/*----------------------------------------------------<BR>*
Read tempreature from
1820<BR>*----------------------------------------------------*/<BR>unsigned
int tmrtemp_all(void)<BR>{<BR>unsigned char a,b;<BR>unsigned
int y3;<BR>unsigned char i;</P>
<P> tmreset();<BR>
tmpre();<BR>
Delay_ms(1);<BR>
/*tmwbyte(0xcc);*/<BR>
tmwbyte(0x55);<BR>
for(i=0;i<8;i++)<BR>
tmwbyte(ID[currSensorNo][i]);<BR>
<BR> tmwbyte(0xbe);<BR> a
= tmrbyte();<BR> b =
tmrbyte();<BR> y3 = ((unsigned int)b)
<< 8;<BR> </P>
<P> return ((y3+a) & 0x7ff) ;<BR>}</P>
<P>/*----------------------------------------------------<BR>*
Start Test tempreature,
All<BR>*<BR>*----------------------------------------------------*/<BR>void
Display_AllTemp(void )<BR>{<BR>unsigned int last;<BR>unsigned
char i,Dot;</P>
<P> RUN_LED=0;<BR> Part=0;<BR> LED_DISPLAY();<BR> Delay_ms(1);</P>
<P> tmstart();</P>
<P> Delay_ms(800);<BR> last=tmrtemp_all();<BR> RUN_LED=1;</P>
<P> Dot= (last & 0x0f)*6.25 ;<BR> Digit[0]=
Dot%10;<BR> Digit[1]=Dot/10;</P>
<P> last=(last>>4) & 0x7f ;<BR> if( (last
== 0x7f) ) //
erro<BR> {<BR>
for(i=0;i<5;i++) Digit[i]=16;
<BR> }<BR>
else<BR> {<BR> for(i=2;i<5;i++)<BR> {<BR> Digit[i]
= last %10;<BR> last =
last/10;<BR> }<BR> if(Digit[4]==0)Digit[4]=17;<BR> Part=1;
<BR> }<BR> Digit[5]=currSensorNo;</P>
<P>
<BR> LED_DISPLAY();</P>
<P>}</P>
<P>/*----------------------------------------------------<BR>*<BR>*----------------------------------------------------*/<BR>/*
void Read_Id()<BR> {<BR> unsigned char
i,id[8];<BR>
tmreset();<BR>
tmpre();<BR>
Delay_ms(1);<BR> tmwbyte(0x33);</P>
<P> for(i=0;i<8;i++)<BR>
id[i]=tmrbyte();<BR>
send(8,id);<BR>}*/</P>
<P>/*----------------------------------------------------<BR>*<BR>*
Main<BR>*----------------------------------------------------*/<BR>void
main(void)<BR>{<BR>unsigned char id[8]={1,2,3,4,5,6,7,8};</P>
<P> serial_initial();<BR> send(8,id);<BR>
for(;;)<BR>
{<BR> RUN_LED=!RUN_LED;<BR>/*
Read_Id();*/<BR> TX_C =!TX_C
;<BR> currSensorNo=(currSensorNo==1)?0:1;<BR> Display_AllTemp();<BR>
/*Change();*/<BR> send(6,Digit);<BR> Delay_ms(1000);<BR>
_nop_();<BR>
}<BR>}<BR> <BR> <BR>Copyright? 2001-2004, 晓奇工作室
E-Mail:info@xiao-qi.com
<BR></P></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD colSpan=2>
<TABLE
style="BORDER-TOP: #6687ba 1px solid; BORDER-BOTTOM: #6687ba 1px solid"
height=50 cellSpacing=5 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD>
<LI>上一篇文章: <A
title="文章标题:1-wire总线接口程序 (AVR源程序) 作 者:佚名 更新时间:2005-9-28 20:34:58 点击次数:584"
href="http://www.mcu123.com/new/Article_Show.asp?ArticleID=63">1-wire总线接口程序 (AVR源程序)</A>
<BR>
<LI>下一篇文章: <A
title="文章标题:数字电路、单片机的抗干扰设计 作 者:佚名 更新时间:2005-9-28 20:47:27 点击次数:598"
href="http://www.mcu123.com/new/Article_Show.asp?ArticleID=65">数字电路、单片机的抗干扰设计</A>
</LI></TD></TR></TBODY></TABLE></TD></TR>
<TR class=tdbg_leftall align=right>
<TD colSpan=2>【<A
href="http://www.mcu123.com/new/Article_Comment.asp?ArticleID=64"
target=_blank>发表评论</A>】【<A
href="http://www.mcu123.com/new/SendMail.asp?ArticleID=64"
target=_blank>告诉好友</A>】【<A
href="http://www.mcu123.com/new/Article_Print.asp?ArticleID=64">打印此文</A>】【<A
href="javascript:window.close();">关闭窗口</A>】</TD></TR>
<TR>
<TD>
<SCRIPT type=text/javascript><!--
google_ad_client = "pub-7844362911365004";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "CCCCCC";
google_color_bg = "FFFFFF";
google_color_link = "000000";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -