蓝桥杯嵌入式扩展板——DS18B20

1.原理图

1
2

2.CUBEMX配置

DS18B20不需要去专门配置引脚

3.在Keil修改代码

(1)修改ds18b20_hal.c和ds18b20_hal.h代码

在ds18b20_hal.c的最后手动添加读取温度的代码:

//需要手动添加读的操作
double read_temperature(void)
{
   
    unsigned char h,l;
    double result;
    
    ow_reset();
    ow_byte_wr(0xcc);//跳过ROM
    ow_byte_wr(0x44);//开始转换温度
    
    ow_reset();
    ow_byte_wr(0xcc);//跳过ROM
    ow_byte_wr(0xbe);//读暂存器 
    
    l=ow_byte_rd();
    h=ow_byte_rd();
    
    result=((h<<8)+l)*0.0625;

        return result;
}

在ds18b20_hal.h中声明
声明

(2)修改main.c

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include 
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值