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 */
相关推荐
OKay_J几秒前
蓝桥杯备赛笔记(嵌入式)
笔记·stm32·学习·蓝桥杯
正点原子12 分钟前
【正点原子STM32MP257连载】第四章 ATK-DLMP257B功能测试——USB测试 #USB HOST #USB 鼠标
linux·功能测试·stm32·嵌入式硬件·计算机外设·usb
promising-w13 分钟前
【TI MSPM0】ADC进阶学习
单片机·嵌入式硬件·学习·ti 单片机
爱喝西北风的东北风16 分钟前
DDS波形发生器仿真及技术原理
单片机·嵌入式硬件·fpga开发
FreakStudio8 小时前
一文速通 Python 并行计算:07 Python 多线程编程-线程池的使用和多线程的性能评估
python·单片机·嵌入式·多线程·面向对象·并行计算·电子diy
SlientICE11 小时前
TCP是什么?不需要!使用ESP32网络层直接通信!
网络·单片机·网络协议·tcp/ip
BW.SU14 小时前
单片机 + 图像处理芯片 + TFT彩屏 触摸开关控件 v1.2
单片机·人机交互·ra8889·开关控件·触摸屏设计
双叶83614 小时前
(51单片机)点阵屏LED显示图片(点阵屏LED教程)(74Hc595教程)
c语言·开发语言·单片机·嵌入式硬件·51单片机
深圳市青牛科技实业有限公司14 小时前
D3502C:一款高性能降压转换器的技术解析,可替代U3502C采用ESOP8封装
单片机·嵌入式硬件·快充·扫地机器人吸尘·筋膜枪电机·驱动轮电机
顾念`15 小时前
履带小车+六轴机械臂(2)
单片机·嵌入式硬件