⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 18b20p0.c

📁 当使用大量18b20的时候
💻 C
字号:
//P0的Pin脚置1 Pin为脚所在地址。如第3脚就是0x04
void Set1toP0(void)
{
  P0|=Pin;
}
/**************************************************************/

//P0的Pin脚置0,Pin为脚所在地址。如第3脚就是0x04
void Set0toP0(void)
{
  P0&=~Pin;
}
/********************************************************/

//检查P0的Pin脚电平,为0返回0,为1返回非0
uchar CheckP0Level(void)
{
  return(P0&Pin);
}
/**************************************************/

//18B20P0初始化
void Init18b20P0 (void)
{
  flag=0;
    WorR=1;
  _nop_();

  Set1toP0();
    _nop_();
  Set0toP0();
  Delay600us(); //delay 600 uS
  Set1toP0();
  _nop_();
    WorR=0;
  _nop_();

  Delay80us(); //delay 80 uS
  if(!CheckP0Level())  flag = 1; //detect 1820 success!
  else flag = 0; //detect 1820 fail!
  Delay80us(); 
  Delay45us();
    WorR=1;
  _nop_();

  Set1toP0();
}

/*************************************************************/


//单字节写入P0上18b20子程序
void WriteByteP0 (uchar wr) 
{
  uchar i;
    WorR=1;
  _nop_();

  for (i=0;i<8;i++)
  {
    Set0toP0();
    if(wr&0x01) Set1toP0();else Set0toP0();
    Delay20us(); //delay 45 uS 
    Set1toP0();
    wr >>= 1;
  }
}
/***************************************/

//从P0上18b20读取单字节子程序
uchar ReadByteP0 (void) 
{
  uchar i,u=0;
  for(i=0;i<8;i++)
  {
      WorR=1;
  _nop_();

    Set0toP0();
    u >>= 1;
    Set1toP0();
    _nop_();
      WorR=0;
  _nop_();

    if(CheckP0Level()) u |= 0x80;
    Delay45us();
    _nop_();
  }
  return(u);
}
/*************************************/

//从P0上18b20连续读取数个字节子程序
void read_bytesP0 (uchar j)
{
  for(i=0;i<j;i++)
  {
    *p = ReadByteP0();
    p++;
  }
}
/***********************************************/



//MatchRomP0命令,只有P0上8位ID相同的18b20才能响应以后的命令
void MatchRomP0(void)
{
  Init18b20P0();
  WriteByteP0(0x55);//发出MatchRomP0命令,后带8字节rom id
  for(i=0;i<8;i++)
  {
     WriteByteP0(ReceivdID_buff[i]);
  }
}
/********************************************************/

//读取P0上温度子程序
void GetTempP0 (void)
{
  Init18b20P0 ();
  MatchRomP0();
  WriteByteP0(0xbe); //read Temperature
  p = temp_buff;
  read_bytesP0(9);
  Delay5us();
  Delay5us();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -