一、传感器介绍
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
四、主代码
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 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 "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "delay.h"
#include "T117_MTS4_I2C.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
uint8_t status,Cfg;
uint16_t Temp_u16;
float Temp_f;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_AFIO);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
/* System interrupt init*/
NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
/* SysTick_IRQn interrupt configuration */
NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),15, 0));
/** DISABLE: JTAG-DP Disabled and SW-DP Disabled
*/
LL_GPIO_AF_DisableRemap_SWJ();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
SysTick_Init();
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
printf("App start\n");
printf("SystemCoreClock:%d\n", SystemCoreClock);
uint8_t ID[8];
if( MY_ReadID( ID ) == 0)
{
printf("MTS4:%02X%02X\n", ID[1], ID[0]);
}
if(T117I2C_Transmit(I2C_ADDR, EE_Cmd,0xB8)!= GPIOI2C_XFER_LASTACK)
{
PR("Recall FALSE\n");
}
delay_ms(2);
MY_ReadStatusConfig( &status, &Cfg);
PR("SC:%2X %2X\r\n",status,Cfg);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
LL_GPIO_TogglePin(GPIOC, LL_GPIO_PIN_13);
if( MY_ConvertTemp()== 1)
{
delay_ms(tCon_A32);
MY_ReadTempWaiting(&Temp_u16);
Temp_f=MY_OutputtoTemp((int16_t)Temp_u16);
PR("temp:%.2f\r\n",Temp_f);
}
delay_ms(980);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
LL_FLASH_SetLatency(LL_FLASH_LATENCY_0);
while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_0)
{
}
LL_RCC_HSI_SetCalibTrimming(16);
LL_RCC_HSI_Enable();
/* Wait till HSI is ready */
while(LL_RCC_HSI_IsReady() != 1)
{
}
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSI);
/* Wait till System clock is ready */
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_HSI)
{
}
LL_Init1msTick(8000000);
LL_SetSystemCoreClock(8000000);
}
/* 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 */
五、运行
STM32读取MTS4数字温度传感器