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 */
相关推荐
redcocal8 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
辰哥单片机设计11 小时前
门磁模块详解(防盗感应开关 STM32)
stm32·单片机·嵌入式硬件·传感器
夜间去看海11 小时前
基于51单片机的自动清洗系统(自动洗衣机)
嵌入式硬件·51单片机·proteus·洗衣机
yrx02030712 小时前
stm32 IIC总线busy解决方法
stm32·单片机·嵌入式硬件
YHPsophie13 小时前
ATGM331C-5T杭州中科微BDS/GNSS全星座定位授时模块应用领域
经验分享·笔记·单片机·信息与通信·交通物流
Archie_IT14 小时前
【STM32系统】基于STM32设计的SD卡数据读取与上位机显示系统(SDIO接口驱动、雷龙SD卡)——文末资料下载
arm开发·stm32·单片机·嵌入式硬件
辰哥单片机设计14 小时前
1×4矩阵键盘详解(STM32)
stm32·单片机·嵌入式硬件·矩阵·传感器
wmkswd14 小时前
CAN总线-STM32上CAN外设
stm32·单片机·嵌入式硬件
Ruohongxu14 小时前
LAN8720A-CP-TR-ABC QFN-24 以太网收发器芯片
单片机