HDC2010+STM32读取数据发送到onenet平台

第一次用HDC2010用stm32l051单片机读取数据看了2天的datasheet都没看明白,好在在老板的帮助下里面的数据读取出来。之后的工作一个人好在顺利完成。以下记录一下写的代码

cpp 复制代码
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 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 "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
#include "stm32l0xx.h"                  // Device header
#include "stdio.h"
#include <string.h> 

uint8_t Receivedata[4];
uint8_t measure[1]={0x01},inter[1]={0x50},eable[1]={0x00};
void SystemClock_Config(void);

int main(void)
{

	

  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_I2C2_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET);
  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_SET);
  /* USER CODE END 2 */
  	HAL_I2C_Mem_Write(&hi2c2, 0x80, 0x07, I2C_MEMADD_SIZE_8BIT,eable,1,0xFF);
  	HAL_I2C_Mem_Write(&hi2c2, 0x80, 0x0E, I2C_MEMADD_SIZE_8BIT,inter,1,0xFF);
  	HAL_I2C_Mem_Write(&hi2c2, 0x80, 0x0F, I2C_MEMADD_SIZE_8BIT,measure,1,0xFF);
			HAL_Delay(10);
	HAL_I2C_Mem_Read(&hi2c2, 0x80, 0x00, I2C_MEMADD_SIZE_8BIT,Receivedata,4,0xFF);
	
  while (1)
  {
	HAL_I2C_Mem_Read(&hi2c2, 0x80, 0x00, I2C_MEMADD_SIZE_8BIT,Receivedata,4,0xFF);
	uint8_t rawData[2] = {0, 0};
	rawData[0]=Receivedata[0];
	rawData[1]=Receivedata[1];
	
	 uint16_t temp_raw = (uint16_t)(rawData[1] << 8) | rawData[0];
	float temp = (float)temp_raw * (165.0f / 65536.0f) - 40.0f;
//	printf("%08f\n",temp);
	
	
	uint8_t raw1Data[2] = {0, 0};
	raw1Data[0]=Receivedata[2];
	raw1Data[1]=Receivedata[3];
	uint16_t hum_raw = (uint16_t)(raw1Data[1] << 8) | raw1Data[0];
	float hum = (float)hum_raw * (100.0f / 65536.0f);
//	printf("%08f\n",hum);
	
//	char jsonStr[256];
//    snprintf(jsonStr, sizeof(jsonStr),
//             "{ \"id\": \"1\", \"params\": { \"Temperature\": { \"value\": {\"Temperature\":\"%.1f\"} } } }",
//             temp);
//    printf("%s", jsonStr);
	
	char jsonStr1[512];
    snprintf(jsonStr1, sizeof(jsonStr1),"{ \"id\": \"1\", \"params\": { \"humiture\": { \"value\": {\"temperature\":\"%.1f\", \"humidity\":\"%.1f\"} } } }",temp, hum);
    printf("%s", jsonStr1);
	
   	HAL_Delay(20000);
		
  }
 
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  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_6;
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  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_USART1|RCC_PERIPHCLK_USART2
                              |RCC_PERIPHCLK_I2C1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}


#endif /* USE_FULL_ASSERT */
相关推荐
Wallace Zhang2 小时前
STM32F103_LL库+寄存器学习笔记22 - 基础定时器TIM实现1ms周期回调
笔记·stm32·学习
小石(努力版)2 小时前
嵌入式STM32学习——外部中断EXTI与NVIC的基础练习⭐
stm32·单片机·学习
GodKK老神灭3 小时前
STM32 变量加载到flash的过程中
stm32
檀越剑指大厂3 小时前
T-BOX硬件方案深度解析:STM32与SD NAND Flash存储的完美搭配
stm32·单片机·嵌入式硬件
7yewh4 小时前
MCU程序加密保护(一)闪存读写保护法 加密与解密
单片机·嵌入式硬件
不脱发的程序猿4 小时前
如何优化MCU中断响应时间
单片机·嵌入式硬件·rtos
平凡灵感码头4 小时前
基于智能家居项目 实现DHT11驱动源代码
stm32·单片机·智能家居
搬砖的小码农_Sky4 小时前
FPGA:XILINX FPGA产品线以及器件选型建议
嵌入式硬件·fpga开发·硬件架构·硬件工程
道亦无名5 小时前
STM32H743输出50%的占空比波形
stm32·单片机·嵌入式硬件
小智学长 | 嵌入式5 小时前
单片机-STM32部分:10、串口UART
stm32·单片机·嵌入式硬件