【开源】STM32HAL库移植Arduino OneWire库驱动DS18B20和MAX31850

项目开源链接

github主页 https://github.com/snqx-lqh
本项目github地址 https://github.com/snqx-lqh/STM32F103C8T6HalDemo
作者 VX Qinghua-Li7

📖 欢迎交流 如果开源的代码对你有帮助,希望可以帮我点个赞👍和收藏

项目说明

最近在做一个项目的时候,需要用到Max31850去读取PT100的温度值,使用的模块如下

由于店家给的资料只有Arduino的,然后我就把Arduino库中的OneWire库修改成了STM32可移植的C语言代码,使用函数指针面向对象设计,使得代码移植性更强。具体的操作以及演示我也有在B站进行讲解,DS18B20和MAX31850一样使用的单总线协议,所以可以共同使用这个代码。

【开源】STM32F103读取DS18B20温度(移植Arduino的OneWire库)

移植讲解

如果想要使用我移植好的OneWire库,首先需要修改u_one_wire.c文件开头的宏定义。需要用户提供延时微秒的函数,以及使能和失能单片机中断的函数。

c 复制代码
#include "u_one_wire.h"

/**************用户处理的区域*****************/
#include "delay.h"

#define noInterrupts()         __disable_irq();  //失能单片机中断
#define interrupts()           __enable_irq();   //使能单片机中断
#define one_wire_delay_us      DWT_Delay_us
/********************************************/

然后在max_31850.c,也就是传感器处理文件中,建立一个one_wire的对象,并且实现对象中的方法。主要是设置引脚方向,引脚电平和读取引脚的电平,我这里由于是使用的HAL库,CubeMX将我的GPIO以及初始化了,所以这部分就不用管了,但是如果使用标准库的话,也要添加引脚初始化。

c 复制代码
/***************    用户处理区域    ****************/

#define max31850_delay_ms DWT_Delay_ms

static uint8_t gpio_init(void)
{
	return 0;
}

static uint8_t set_pin_dir(one_wire_dir_t one_wire_dir)
{
	if(one_wire_dir == ONE_WIRE_DIR_IN)
	{
		GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=8<<(4*0);
	}else if(one_wire_dir == ONE_WIRE_DIR_OUT)
	{
		GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=3<<(4*0);
	}
	return 0;
}

static  uint8_t set_pin_level(uint8_t level)
{
	if(0 == level)
	{
		GPIOA->BRR  = GPIO_PIN_0;
	}else if(1 == level)
	{
		GPIOA->BSRR = GPIO_PIN_0;
	}
	return 0;
}

static uint8_t read_pin_level(void)
{
	uint8_t read_pin ;
	read_pin = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
	return read_pin;
}

one_wire_dev_t max31850={
	.init = gpio_init,
	.set_pin_dir = set_pin_dir,
	.set_pin_level= set_pin_level,
	.read_pin_level= read_pin_level,
};

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

最后在使用的时候,就可以参考Arduino中的步骤,先要定义一个初始化的函数,然后开始地址扫描、校验、复位、选地址、转换、读取数据等一系列操作。

c 复制代码
/**
  * @brief   初始化max31850,包含引脚初始化
  * @param   
  * @retval   
 **/
void max31850_init()
{
	 one_wire_begin(&max31850);
}
/**
  * @brief   读取max31850的温度,带扫描地址
  * @param   celsius:摄氏度 fahrenheit:华氏温度
  * @retval  
 **/
int get_max31850_temp(float *celsius,float *fahrenheit)
{
    uint8_t i;
	uint8_t present = 0;
	uint8_t temptype;
	uint8_t data[12];
	uint8_t addr[8];
	
    if(celsius == NULL || fahrenheit == NULL)
        return -1;

    if ( !one_wire_search(&max31850,addr,true)) 
	{
        one_wire_reset_search(&max31850);
        max31850_delay_ms(250);
        return -2;
    }

    if (crc8(addr, 7) != addr[7]) {
        return -3;
    }

    // the first ROM byte indicates which chip
    switch (addr[0]) {
    case 0x10:
        temptype = TYPE_DS18S20;
        break;
    case 0x28:
        temptype = TYPE_DS18B20;
        break;
    case 0x22:
        temptype = TYPE_DS18S22;
        break;
    // ADDED SUPPORT FOR MAX31850!
    case 0x3B:
        temptype = TYPE_MAX31850;
        break;
    default:
        return -4;
    }

    one_wire_reset(&max31850);
    one_wire_select(&max31850,addr);
    one_wire_write(&max31850,0x44, 1);        // start conversion, with parasite power on at the end

    max31850_delay_ms(1000);     // maybe 750ms is enough, maybe not
    // we might do a ds.depower() here, but the reset will take care of it.

    present = one_wire_reset(&max31850);
    one_wire_select(&max31850,addr);
    one_wire_write(&max31850,0xBE,0);         // Read Scratchpad

    for ( i = 0; i < 9; i++) {           // we need 9 bytes
        one_wire_read(&max31850,&data[i]);
    }

    // Convert the data to actual temperature
    // because the result is a 16 bit signed integer, it should
    // be stored to an "int16_t" type, which is always 16 bits
    // even when compiled on a 32 bit processor.
    int16_t raw = (data[1] << 8) | data[0];
    if (temptype == TYPE_DS18S20) {
        raw = raw << 3; // 9 bit resolution default
        if (data[7] == 0x10) {
            // "count remain" gives full 12 bit resolution
            raw = (raw & 0xFFF0) + 12 - data[6];
        }
    } else if (temptype == TYPE_MAX31850) {
        printf("--------------------------------\r\n");
        if (raw & 0x01) {
            return -4;
        }
    } else {
        uint8_t cfg = (data[4] & 0x60);
        // at lower res, the low bits are undefined, so let's zero them
        if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
        else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
        else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
         default is 12 bit resolution, 750 ms conversion time
    }
    *celsius = (float)raw / 16.0;
    *fahrenheit = *celsius * 1.8 + 32.0;
	return 0;
}

还可以跳过扫描阶段,直接获取数据,这种方式比较适合单节点的获取。就是电路上只连接了一个DS18B20或者MAX31850。

c 复制代码
/**
  * @brief   读取max31850的温度,跳过地址扫描
  * @param   celsius:摄氏度 fahrenheit:华氏温度
  * @retval  
 **/
int get_max31850_temp_skiprom(float *celsius,float *fahrenheit)
{
    uint8_t i;
	uint8_t present = 0;
	uint8_t temptype;
	uint8_t data[12];
	uint8_t addr[8];
	
    if(celsius == NULL || fahrenheit == NULL)
        return -1;
	
    one_wire_reset(&max31850);
    one_wire_skip(&max31850);
    one_wire_write(&max31850,0x44, 1);        // start conversion, with parasite power on at the end

    max31850_delay_ms(1000);     // maybe 750ms is enough, maybe not
    // we might do a ds.depower() here, but the reset will take care of it.

    present = one_wire_reset(&max31850);
    one_wire_skip(&max31850);
    one_wire_write(&max31850,0xBE,0);         // Read Scratchpad
    for ( i = 0; i < 9; i++) {                // we need 9 bytes
        one_wire_read(&max31850,&data[i]);
    }
    int16_t raw = (data[1] << 8) | data[0];
	if (raw & 0x01) {
		return -4;
	}
    *celsius = (float)raw / 16.0;
    *fahrenheit = *celsius * 1.8 + 32.0;
	return 0;
}
相关推荐
猫头虎7 小时前
如何查看局域网内IP冲突问题?如何查看局域网IP环绕问题?arp -a命令如何使用?
网络·python·网络协议·tcp/ip·开源·pandas·pip
SundayBear8 小时前
基于STM32的RT-Thread移植
单片机·嵌入式硬件
thubier(段新建)8 小时前
gitee中的一些开源项目整理20251010
开源
R6bandito_9 小时前
STM32中printf的重定向详解
开发语言·经验分享·stm32·单片机·嵌入式硬件·mcu
清风6666669 小时前
基于单片机的元胞自动机仿真系统设计
单片机·嵌入式硬件·毕业设计·课程设计
千千道10 小时前
利用keil +RASC给瑞萨RA8D1编译烧写程序
单片机·嵌入式硬件·mcu·物联网
武文斌7710 小时前
项目学习总结:LVGL图形参数动态变化、开发板的GDB调试、sqlite3移植、MQTT协议、心跳包
linux·开发语言·网络·arm开发·数据库·嵌入式硬件·学习
充哥单片机设计10 小时前
【STM32项目开源】基于STM32的智能家居安防系统
stm32·嵌入式硬件·智能家居
点灯小铭11 小时前
基于单片机的N型热电偶PID锅炉温度控制系统
单片机·嵌入式硬件·毕业设计·课程设计
GilgameshJSS11 小时前
STM32H743-ARM例程15-RTC
c语言·arm开发·stm32·实时音视频