【STM32duino】使用库读取MAX6675芯片获取多路热电偶温度数据 + 原生实现HDC1080获取温湿度
编辑
              
              10
            
            
          2023-09-20
          
成品效果,截图的时候HDC1080 没接,实质使用是可以获取到的。

这是一篇水文x,忘记发了的。
MCU: Air32F103CBT6
实现代码
thermocouple.cpp
#include "thermocouple.h"
// V1.9.2 后从每个传感器使用单独的SPI变成共用SPI2
#define SPI2_SCK PB13
#define SPI2_MISO PB14
#define TEMP0CS PB12
#define TEMP1CS PB15
#define TEMP2CS PA8
#define HDC1080_ADDRESS 0x40
MAX6675 thermocouple0(SPI2_SCK, TEMP0CS, SPI2_MISO);
MAX6675 thermocouple1(SPI2_SCK, TEMP1CS, SPI2_MISO);
MAX6675 thermocouple2(SPI2_SCK, TEMP2CS, SPI2_MISO);
void readHDC1080(TwoWire& wire,float* temperature, float* humidity) {
  uint8_t data[4];
  uint16_t rawTemperature, rawHumidity;
  // 发送读取温湿度数据的命令
  wire.beginTransmission(HDC1080_ADDRESS);
  wire.write(0x00); // 温度和湿度寄存器的起始地址
  wire.endTransmission();
  // 延时一段时间,等待传感器完成测量
  delay(20);
  // 读取温湿度数据
  wire.requestFrom(HDC1080_ADDRESS, 4);
  for (int i = 0; i < 4; i++) {
    data[i] = wire.read();
  }
  // 将读取到的数据进行解析
  rawTemperature = (data[0] << 8) | data[1];
  rawHumidity = (data[2] << 8) | data[3];
  // 计算温度和湿度的实际值
  *temperature = (float)rawTemperature / 65536 * 165 - 40;
  *humidity = (float)rawHumidity / 65536 * 100;
}
thermocouple.h
#ifndef THERMOCOUPLE_H
#define THERMOCOUPLE_H
#include <max6675.h>
#include <Wire.h>
extern MAX6675 thermocouple0;
extern MAX6675 thermocouple1;
extern MAX6675 thermocouple2;
platformio.ini
这里使用的是通用的STM32的,记得装库x。
[env:genericSTM32F103CB]
platform = ststm32
board = genericSTM32F103CB
framework = arduino
upload_protocol = cmsis-dap
lib_deps = 
	adafruit/MAX6675 library@^1.1.0
main.cpp
#include <Arduino.h>
#include <Wire.h>
#include <vector>
#include "serial_utils.h"
#include "wire_utils.h"
#include "thermocouple.h"
#include "helpers.h"
#define LED1 PC13
void setup() {
  initSerial();
  initWire();
}
void loop() {    
  //GET ENV temperature, humidity
  float temperature, humidity;
  readHDC1080(Wire2,&temperature, &humidity);
  //sensor temperature & env temperature
  std::vector<float> numbers = {
    thermocouple0.readCelsius(),
    thermocouple1.readCelsius(), 
    thermocouple2.readCelsius(), 
    temperature, 
    humidity,
    //Addons
  };
  //printout
  const size_t bufferSize = 128;
  char formattedString[bufferSize];
  formatFloats(numbers, formattedString, bufferSize);
  std::string message = "DATA,";
  message += formattedString;
  Seriral1.println(message.c_str());
}
- 0
 - 0
 - 
              
              
  
分享