蓝桥杯物联网竞赛_STM32L071_10_温度传感器扩展模块

原理图:

温度传感器原理图:

蓝桥杯物联网竞赛_STM32L071_10_温度传感器扩展模块_第1张图片
其中芯片可以通过SCL和SDA引脚通过I2C通信向温度传感器指定地址获取温度的模拟量
再利用公式将模拟量转换成相应温度即可

实验板接口原理图:

蓝桥杯物联网竞赛_STM32L071_10_温度传感器扩展模块_第2张图片

模拟量转相应温度公式:

蓝桥杯物联网竞赛_STM32L071_10_温度传感器扩展模块_第3张图片

CubMx配置:

蓝桥杯物联网竞赛_STM32L071_10_温度传感器扩展模块_第4张图片

Keil配置:

Function:

#include "Function.h"
#include "oled.h"
#include "i2c.h"

void OLED_Write(unsigned char type, unsigned char data){
	unsigned char Write_Data[2];
	Write_Data[0] = type;
	Write_Data[1] = data;
	HAL_I2C_Master_Transmit(&hi2c3, 0x78, Write_Data, 2, 0xff);
}

void Function_OledEnable(unsigned char ms){
	HAL_GPIO_WritePin(OLED_POWER_GPIO_Port, OLED_POWER_Pin, GPIO_PIN_RESET);
	HAL_Delay(ms);
	OLED_Init();
}

float Function_GetTemp(void){
	unsigned char data[3];
	
	data[0] = 0x24;
	data[1] = 0x0B;  // 地址
	
	HAL_I2C_Master_Transmit(&hi2c1, 0x94, data, 2, 10); // 通过I2C传输向特定地址获取温度
	HAL_Delay(5);
	HAL_I2C_Master_Receive(&hi2c1, 0x95, data, 3, 10); // 获取到数值
	
  return (float)(data[0] << 8 | data[1]) * 175.0/65535 - 45; // 获取的数值是16位 << 8 等效于 * 2 ^ 8
}
#ifndef __Function__
#define __FUNCTION__

void OLED_Write(unsigned char type, unsigned char data);
void Function_OledEnable(unsigned char ms);
float Function_GetTemp(void);
#endif

main:

 
#include "main.h"
#include "i2c.h"
#include "gpio.h"
#include "oled.h"
#include "Function.h"
 
void SystemClock_Config(void);
 
int main(void)
{
   
  HAL_Init();

  
  SystemClock_Config();

 
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_I2C3_Init();
 
	Function_OledEnable(50);
 
  while (1)
  {
    
    OLED_ShowNumber(0, 0, 12345, 5, 16);
		OLED_ShowNumber(0, 2, (uint32_t) Function_GetTemp(), 3, 16);
    
  }
 
}

 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

 
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_I2C3;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  PeriphClkInit.I2c3ClockSelection = RCC_I2C3CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

 
void Error_Handler(void)
{
 
  __disable_irq();
  while (1)
  {
  }
   
}

效果:

蓝桥杯物联网竞赛_STM32L071_10_温度传感器扩展模块_第5张图片

你可能感兴趣的:(蓝桥杯,物联网,stm32)