一个IO上挂接多个DS18B20_ds18b20多个挂载

本文介绍了如何在一个IO口上挂接多个DS18B20温度传感器,包括DS18B20的在线检查、读写操作以及二叉树遍历获取ROM代码的方法,展示了如何处理总线上各器件的ID,提供了完整的嵌入式程序代码片段。
摘要由CSDN通过智能技术生成

* @retval None
* @author PWH
* @date 2022/10
*/
static bool DS18B20_Online_Check(PORT_TypeDef PORTx, PIN_TypeDef PINx)
{
uint32_t wait = 0;

DS18B20\_PIN\_MODE\_OUT(PORTx, PINx);
PORT\_SetBit(PORTx, PINx);
DELAY\_US(10);
PORT\_ClrBit(PORTx, PINx);
DELAY\_US(700);
PORT\_SetBit(PORTx, PINx);

DS18B20\_PIN\_MODE\_IN(PORTx, PINx);
DELAY\_US(30);
if (!DS18B20\_PIN\_GET(PORTx, PINx))	//有应答
{
    UserTimer\_Reset(&wait);
    while (!DS18B20\_PIN\_GET(PORTx, PINx) && UserTimer\_Read(&wait) < 2);	//等待应答信号结束,最长等待1~2ms
    if (DS18B20\_PIN\_GET(PORTx, PINx))
        return true;
    else
        return false;
}
else
{
    return false;
}

}

/**
* @brief
* @note
* @param None
* @retval None
* @author PWH
* @date 2022/10
*/
static void DS18B20_Write(PORT_TypeDef PORTx, PIN_TypeDef PINx, uint8_t val)
{
uint8_t i;

DS18B20\_PIN\_MODE\_OUT(PORTx, PINx);
PORT\_SetBit(PORTx, PINx);

for (i = 0; i < 8; i++)
{
    PORT\_SetBit(PORTx, PINx);
    DELAY\_US(4);
    PORT\_ClrBit(PORTx, PINx);
    DELAY\_US(1);
    if (val & 0x01)
        PORT\_SetBit(PORTx, PINx);
    else
        PORT\_ClrBit(PORTx, PINx);
    DELAY\_US(66);
    val >>= 1;
}

PORT\_SetBit(PORTx, PINx);

}

/**
* @brief 读2个bit
* @note
* @param None
* @retval None
* @author PWH
* @date 2022/10
*/
static uint8_t DS18B20_Read2Bit(PORT_TypeDef PORTx, PIN_TypeDef PINx)
{
uint8_t i;
uint8_t val = 0;

DS18B20\_PIN\_MODE\_OUT(PORTx, PINx);
PORT\_SetBit(PORTx, PINx);

for (i = 0; i < 2; i++)
{
    val <<= 1;
    DS18B20\_PIN\_MODE\_OUT(PORTx, PINx);
    PORT\_SetBit(PORTx, PINx);
    DELAY\_US(4);
    PORT\_ClrBit(PORTx, PINx);
    DELAY\_US(1);
    PORT\_SetBit(PORTx, PINx);
    DS18B20\_PIN\_MODE\_IN(PORTx, PINx);
    DELAY\_US(5);
    if (DS18B20\_PIN\_GET(PORTx, PINx))
        val |= 0x01;
    DELAY\_US(60);
}

DS18B20\_PIN\_MODE\_OUT(PORTx, PINx);
PORT\_SetBit(PORTx, PINx);

return val;

}
/**
* @brief
* @note
* @param None

  • 16
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值