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


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

    1. /* USER CODE BEGIN Header */
    2. /**
    3. ******************************************************************************
    4. * @file : main.c
    5. * @brief : Main program body
    6. ******************************************************************************
    7. * @attention
    8. *
    9. * Copyright (c) 2022 STMicroelectronics.
    10. * All rights reserved.
    11. *
    12. * This software is licensed under terms that can be found in the LICENSE file
    13. * in the root directory of this software component.
    14. * If no LICENSE file comes with this software, it is provided AS-IS.
    15. *
    16. ******************************************************************************
    17. */
    18. /* USER CODE END Header */
    19. /* Includes ------------------------------------------------------------------*/
    20. #include "main.h"
    21. #include "i2c.h"
    22. #include "usart.h"
    23. #include "gpio.h"
    24. #include "stm32l0xx.h" // Device header
    25. #include "stdio.h"
    26. #include
    27. uint8_t Receivedata[4];
    28. uint8_t measure[1]={0x01},inter[1]={0x50},eable[1]={0x00};
    29. void SystemClock_Config(void);
    30. int main(void)
    31. {
    32. HAL_Init();
    33. /* USER CODE BEGIN Init */
    34. /* USER CODE END Init */
    35. /* Configure the system clock */
    36. SystemClock_Config();
    37. /* USER CODE BEGIN SysInit */
    38. /* USER CODE END SysInit */
    39. /* Initialize all configured peripherals */
    40. MX_GPIO_Init();
    41. MX_I2C1_Init();
    42. MX_I2C2_Init();
    43. MX_USART1_UART_Init();
    44. MX_USART2_UART_Init();
    45. /* USER CODE BEGIN 2 */
    46. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET);
    47. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_SET);
    48. /* USER CODE END 2 */
    49. HAL_I2C_Mem_Write(&hi2c2, 0x80, 0x07, I2C_MEMADD_SIZE_8BIT,eable,1,0xFF);
    50. HAL_I2C_Mem_Write(&hi2c2, 0x80, 0x0E, I2C_MEMADD_SIZE_8BIT,inter,1,0xFF);
    51. HAL_I2C_Mem_Write(&hi2c2, 0x80, 0x0F, I2C_MEMADD_SIZE_8BIT,measure,1,0xFF);
    52. HAL_Delay(10);
    53. HAL_I2C_Mem_Read(&hi2c2, 0x80, 0x00, I2C_MEMADD_SIZE_8BIT,Receivedata,4,0xFF);
    54. while (1)
    55. {
    56. HAL_I2C_Mem_Read(&hi2c2, 0x80, 0x00, I2C_MEMADD_SIZE_8BIT,Receivedata,4,0xFF);
    57. uint8_t rawData[2] = {0, 0};
    58. rawData[0]=Receivedata[0];
    59. rawData[1]=Receivedata[1];
    60. uint16_t temp_raw = (uint16_t)(rawData[1] << 8) | rawData[0];
    61. float temp = (float)temp_raw * (165.0f / 65536.0f) - 40.0f;
    62. // printf("%08f\n",temp);
    63. uint8_t raw1Data[2] = {0, 0};
    64. raw1Data[0]=Receivedata[2];
    65. raw1Data[1]=Receivedata[3];
    66. uint16_t hum_raw = (uint16_t)(raw1Data[1] << 8) | raw1Data[0];
    67. float hum = (float)hum_raw * (100.0f / 65536.0f);
    68. // printf("%08f\n",hum);
    69. // char jsonStr[256];
    70. // snprintf(jsonStr, sizeof(jsonStr),
    71. // "{ \"id\": \"1\", \"params\": { \"Temperature\": { \"value\": {\"Temperature\":\"%.1f\"} } } }",
    72. // temp);
    73. // printf("%s", jsonStr);
    74. char jsonStr1[512];
    75. snprintf(jsonStr1, sizeof(jsonStr1),"{ \"id\": \"1\", \"params\": { \"humiture\": { \"value\": {\"temperature\":\"%.1f\", \"humidity\":\"%.1f\"} } } }",temp, hum);
    76. printf("%s", jsonStr1);
    77. HAL_Delay(20000);
    78. }
    79. }
    80. /**
    81. * @brief System Clock Configuration
    82. * @retval None
    83. */
    84. void SystemClock_Config(void)
    85. {
    86. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    87. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    88. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
    89. /** Configure the main internal regulator output voltage
    90. */
    91. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
    92. /** Initializes the RCC Oscillators according to the specified parameters
    93. * in the RCC_OscInitTypeDef structure.
    94. */
    95. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    96. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    97. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    98. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    99. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
    100. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_6;
    101. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3;
    102. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    103. {
    104. Error_Handler();
    105. }
    106. /** Initializes the CPU, AHB and APB buses clocks
    107. */
    108. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
    109. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    110. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    111. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    112. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    113. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    114. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
    115. {
    116. Error_Handler();
    117. }
    118. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
    119. |RCC_PERIPHCLK_I2C1;
    120. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
    121. PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
    122. PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
    123. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    124. {
    125. Error_Handler();
    126. }
    127. }
    128. /* USER CODE BEGIN 4 */
    129. /* USER CODE END 4 */
    130. /**
    131. * @brief This function is executed in case of error occurrence.
    132. * @retval None
    133. */
    134. void Error_Handler(void)
    135. {
    136. /* USER CODE BEGIN Error_Handler_Debug */
    137. /* User can add his own implementation to report the HAL error return state */
    138. __disable_irq();
    139. while (1)
    140. {
    141. }
    142. /* USER CODE END Error_Handler_Debug */
    143. }
    144. #ifdef USE_FULL_ASSERT
    145. /**
    146. * @brief Reports the name of the source file and the source line number
    147. * where the assert_param error has occurred.
    148. * @param file: pointer to the source file name
    149. * @param line: assert_param error line source number
    150. * @retval None
    151. */
    152. void assert_failed(uint8_t *file, uint32_t line)
    153. {
    154. /* USER CODE BEGIN 6 */
    155. /* User can add his own implementation to report the file name and line number,
    156. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    157. /* USER CODE END 6 */
    158. }
    159. #endif /* USE_FULL_ASSERT */

  • 相关阅读:
    stm32G0 启动
    uniapp开发h5,解决项目启动时,Network: unavailable问题
    Java版工程行业管理系统源码-专业的工程管理软件-提供一站式服务
    C/C++内存管理
    什么是“SQL注入攻击”?如何预防和应对?
    项目架构:eslint 代码检测、提交代码审查
    Android Update Engine 分析(二十四)制作降级包时,到底发生了什么?
    git的理解与使用
    .rmallox勒索病毒来袭:如何守护您的数据安全?
    设计模式与应用:原型模式
  • 原文地址:https://blog.csdn.net/qq_45789558/article/details/136736973