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 */
相关推荐
Zw-awa12 小时前
SPI 是什么:MISO、MOSI、SCK、CS 四条线怎么配合,CPOL/CPHA 四种模式、位序、时序预算与驱动分层
stm32
网易独家音乐人Mike Zhou13 小时前
【STM32】STM32WB55 BLE Notify 配置文档 —— 从 CubeMX 开服务到手机收到数据
stm32·单片机·mcu·嵌入式·蓝牙·iot·ble
女神下凡15 小时前
芯易选(133):建荣(AppoTech)_开发指南
linux·arm开发·stm32·单片机·嵌入式硬件
网易独家音乐人Mike Zhou15 小时前
【STM32】STM32WB55 BLE 广播不出来 —— 排查与烧录避坑记录
stm32·单片机·mcu·物联网·嵌入式·蓝牙·ble
Brilliantwxx16 小时前
【项目】 智慧监测系统 开发笔记(三)界面切换、阀值保存、TB6612、联网、ESP8266机智云、语音控制、智能公元
笔记·stm32·单片机·嵌入式硬件
wuyk55519 小时前
嵌入式故障诊断-Bug016-指针强转对齐错误非对齐访问HardFault
stm32·单片机·嵌入式硬件·mcu
Zw-awa2 天前
FOC 与 DTC:磁场定向控制与直接转矩控制的宏观印象
stm32
凯基迪科技2 天前
芯片解密:MCU 单片机解密原理、方案流程与避坑实战指南
单片机·芯片解密·单片机解密·stm32程序提取·muc解密
辰哥单片机设计2 天前
STM32智能家居(机智云)丨2027单片机新项目
stm32·单片机·嵌入式硬件
女神下凡3 天前
芯参谋(131):山景(MVsilicon)_开发指南
linux·arm开发·stm32·单片机·嵌入式硬件