• STM32F103C8T6 驱动MTS4温度传感器


    一、传感器介绍

            MTS4系列是数字模拟混合信号温度传感芯片,最高测温精度±0.1℃,用户无需进行校准。温度芯片感温原理基于CMOS半导体PN节温度与带隙电压的特性关系,经过小信号放大、模数转换、数字校准补偿后,数字总线输出,具有精度高、一致性好、测温快、功耗低、可编程配置灵活、寿命长等优点。
    芯片内置16-bit ADC,分辨率0.004°C,具有-103°C到+153°C的超宽工作范围。芯片在出厂前经过100%的测试校准,根据温度误差特性进行校准系数的拟合,芯片内部自动进行补偿计算。芯片支持数字I2C通信接口、测温数据内存访问、功能配置等均可通过数字协议指令实现。I2C接口适合高速率的板级应用场景,最高接口速度可达2MHz。
    芯片内置非易失性E2PROM存储单元,用于保存芯片ID号、高低温报警阈值、温度校准修正值以及用户自定义信息,如传感器节点编号、位置信息等。

    二、驱动说明

            官方提供了IIC和单总线的驱动例程,但使用的MCU为MM32SPIN05,由于手上没有MM32SPIN05的开发板,所以将例程改为STM32F103的。

    使用STM32CubeMX生成集成代码,基础代码包含USART1、GPIO、RCC,外设驱动库使用LL库。

    三、硬件接口

    SCL---PC6
    SDA---PC7

    UART_TX---PA9

    LED---PC13

    四、主代码

    1. /* USER CODE BEGIN Header */
    2. /**
    3. ******************************************************************************
    4. * @file : main.c
    5. * @brief : Main program body
    6. ******************************************************************************
    7. * @attention
    8. *
    9. * Copyright (c) 2024 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 "usart.h"
    22. #include "gpio.h"
    23. /* Private includes ----------------------------------------------------------*/
    24. /* USER CODE BEGIN Includes */
    25. #include "stdio.h"
    26. #include "delay.h"
    27. #include "T117_MTS4_I2C.h"
    28. /* USER CODE END Includes */
    29. /* Private typedef -----------------------------------------------------------*/
    30. /* USER CODE BEGIN PTD */
    31. /* USER CODE END PTD */
    32. /* Private define ------------------------------------------------------------*/
    33. /* USER CODE BEGIN PD */
    34. /* USER CODE END PD */
    35. /* Private macro -------------------------------------------------------------*/
    36. /* USER CODE BEGIN PM */
    37. /* USER CODE END PM */
    38. /* Private variables ---------------------------------------------------------*/
    39. /* USER CODE BEGIN PV */
    40. /* USER CODE END PV */
    41. /* Private function prototypes -----------------------------------------------*/
    42. void SystemClock_Config(void);
    43. /* USER CODE BEGIN PFP */
    44. /* USER CODE END PFP */
    45. /* Private user code ---------------------------------------------------------*/
    46. /* USER CODE BEGIN 0 */
    47. /* USER CODE END 0 */
    48. /**
    49. * @brief The application entry point.
    50. * @retval int
    51. */
    52. int main(void)
    53. {
    54. /* USER CODE BEGIN 1 */
    55. uint8_t status,Cfg;
    56. uint16_t Temp_u16;
    57. float Temp_f;
    58. /* USER CODE END 1 */
    59. /* MCU Configuration--------------------------------------------------------*/
    60. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    61. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_AFIO);
    62. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
    63. /* System interrupt init*/
    64. NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
    65. /* SysTick_IRQn interrupt configuration */
    66. NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),15, 0));
    67. /** DISABLE: JTAG-DP Disabled and SW-DP Disabled
    68. */
    69. LL_GPIO_AF_DisableRemap_SWJ();
    70. /* USER CODE BEGIN Init */
    71. /* USER CODE END Init */
    72. /* Configure the system clock */
    73. SystemClock_Config();
    74. /* USER CODE BEGIN SysInit */
    75. SysTick_Init();
    76. /* USER CODE END SysInit */
    77. /* Initialize all configured peripherals */
    78. MX_GPIO_Init();
    79. MX_USART1_UART_Init();
    80. /* USER CODE BEGIN 2 */
    81. printf("App start\n");
    82. printf("SystemCoreClock:%d\n", SystemCoreClock);
    83. uint8_t ID[8];
    84. if( MY_ReadID( ID ) == 0)
    85. {
    86. printf("MTS4:%02X%02X\n", ID[1], ID[0]);
    87. }
    88. if(T117I2C_Transmit(I2C_ADDR, EE_Cmd,0xB8)!= GPIOI2C_XFER_LASTACK)
    89. {
    90. PR("Recall FALSE\n");
    91. }
    92. delay_ms(2);
    93. MY_ReadStatusConfig( &status, &Cfg);
    94. PR("SC:%2X %2X\r\n",status,Cfg);
    95. /* USER CODE END 2 */
    96. /* Infinite loop */
    97. /* USER CODE BEGIN WHILE */
    98. while (1)
    99. {
    100. /* USER CODE END WHILE */
    101. /* USER CODE BEGIN 3 */
    102. LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
    103. if( MY_ConvertTemp()== 1)
    104. {
    105. delay_ms(tCon_A32);
    106. MY_ReadTempWaiting(&Temp_u16);
    107. Temp_f=MY_OutputtoTemp((int16_t)Temp_u16);
    108. PR("temp:%.2f\r\n",Temp_f);
    109. }
    110. delay_ms(980);
    111. }
    112. /* USER CODE END 3 */
    113. }
    114. /**
    115. * @brief System Clock Configuration
    116. * @retval None
    117. */
    118. void SystemClock_Config(void)
    119. {
    120. LL_FLASH_SetLatency(LL_FLASH_LATENCY_0);
    121. while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_0)
    122. {
    123. }
    124. LL_RCC_HSI_SetCalibTrimming(16);
    125. LL_RCC_HSI_Enable();
    126. /* Wait till HSI is ready */
    127. while(LL_RCC_HSI_IsReady() != 1)
    128. {
    129. }
    130. LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
    131. LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
    132. LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
    133. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSI);
    134. /* Wait till System clock is ready */
    135. while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSI)
    136. {
    137. }
    138. LL_Init1msTick(8000000);
    139. LL_SetSystemCoreClock(8000000);
    140. }
    141. /* USER CODE BEGIN 4 */
    142. /* USER CODE END 4 */
    143. /**
    144. * @brief This function is executed in case of error occurrence.
    145. * @retval None
    146. */
    147. void Error_Handler(void)
    148. {
    149. /* USER CODE BEGIN Error_Handler_Debug */
    150. /* User can add his own implementation to report the HAL error return state */
    151. __disable_irq();
    152. while (1)
    153. {
    154. }
    155. /* USER CODE END Error_Handler_Debug */
    156. }
    157. #ifdef USE_FULL_ASSERT
    158. /**
    159. * @brief Reports the name of the source file and the source line number
    160. * where the assert_param error has occurred.
    161. * @param file: pointer to the source file name
    162. * @param line: assert_param error line source number
    163. * @retval None
    164. */
    165. void assert_failed(uint8_t *file, uint32_t line)
    166. {
    167. /* USER CODE BEGIN 6 */
    168. /* User can add his own implementation to report the file name and line number,
    169. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    170. /* USER CODE END 6 */
    171. }
    172. #endif /* USE_FULL_ASSERT */

  • 相关阅读:
    Hive中的explode函数、posexplode函数与later view函数
    yum -y install samba samba-client 使用这个安装的,安装在哪个文件夹下
    Proxy-动态代理
    【微服务】SpringBoot启动流程注册FeignClient
    碳酸钙/GPC3单克隆抗体介导阿霉素二氧化硅纳米粒/DOX-GNRs@mSiO2-HA-RGD纳米制备方法
    redis6
    后端高频面试题分享-用Java判断一个列表是否是另一个列表的顺序子集
    07 创建型模式-单例模式
    2022中国工业视觉市场研究报告【免费下载】
    Nacos服务发现与注册中心之服务消费(发现)者(客户端源码)
  • 原文地址:https://blog.csdn.net/professionalmcu/article/details/139999970