导航: 老古网老古论坛XMOS公共讨论区XMOS开源项目区单片机程序设计嵌入式系统广告区域
→c51

* 40267: 1820读不出来啊?快来救命啊,

   hcphs 
hcphs发表的帖子 

 c51
void init_18b20 (void)
{
  dq=1;
  _nop_();
  dq=0;
  delay(90);            //delay 530 uS
  dq=1;
  delay(6);            //delay 100 uS
  if(dq==0)
     flag=1;            //detect 1820 success!
  else
     flag=0;             //detect 1820 fail!
  delay(20);
  dq=1;
}
//
void write (uchar wr)
{
  uchar i;
  for (i=0;i  <8;i++)
   {
     dq=0;
     _nop_();
     dq=wr&0x01;
     delay(5);            //delay 45 uS
     dq=1;
     wr>  >  =1;
   }
}
//
uint read_word (void)
{
  uchar i;
  uint u=0;
  for(i=0;i  <16;i++)
   {
     dq=0;
     u>  >  =1;
     dq=1;
     if(dq==1)
        u|=0x8000;
     delay (4);
   }
  return(u);
}
/*
uchar read_byte (void)
{
  uchar i,u=0;
  for(i=0;i  <8;i++)
   {
     dq=0;
     u>  >  =1;
     dq=1;
     if(dq==1)
        u|=0x80;
     delay (4);
   }
  return(u);
}
//
void config (void)
{
  if(flag)
   {
     write (0xcc);        //skip rom
     write (0x4e);        //write scratchpad
     write (0x19);        //th=00
     write (0x1a);        //tl=00
     write (0x5f);           //set 11 bit (0.125)
     init_18b20();
     write (0xcc);        //skip rom
     write (0x48);        //copy
     init_18b20();
     write (0xcc);        //skip rom
     write (0xb8);        //recall
     delay (254);
     delay (250);
     init_18b20();
     write (0xcc);
     write (0xbe);
     one=read_word();
     two=read_word();
     three=read_word();
   }   */
}

void get_temp (void)
{
  uint bdata tem;
  init_18b20();
  if (flag)
   {  EA=0;
     write (0xcc);            //skip rom
     write (0x44);            //temp convert
     init_18b20 ();
     write (0xcc);            //skip rom
     write (0xbe);            //read temp
     tem=read_word();             //read
     if(tem  <0x8000)
       temp=tem*0.0625;            //temperature>  =0
     else
       temp=tem*(-0.0625);
        EA=1;           //temperature  < 0
     //write (0xec);               //search alarm
   }
}
//
void delay (uchar us)
{
  while (us--);
}
main()
{
while(1)
{get_temp();
}
}

发表时间:2003年8月17日22:17:40

  
回复该帖

本主题共有 2 帖,分页:>>>>>该主题的所有内容[2]条

 *树形目录 只列出部分跟帖的标题以及简单的摘要信息 该主题的部分跟帖如下:

[上一篇帖子]:*.plm可以用支持plm语言的单片机编程工具来打开,具体请另行搜索
[下一篇帖子]:*.s01用tan33[quote][b]以下是引用[i]hhblxm在2003-8-15