stm32读取ltc2990两路温度值
时间: 2023-10-19 10:16:56 浏览: 150
以下是使用STM32读取LTC2990两路温度值的示例代码:
```c
#include "stm32f1xx_hal.h"
#include "stdio.h"
#define LTC2990_ADDR 0x90 // LTC2990的I2C地址
I2C_HandleTypeDef hi2c1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
uint16_t get_temperature(uint8_t channel);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
uint16_t temp1, temp2;
char str[50];
while (1)
{
temp1 = get_temperature(0);
temp2 = get_temperature(1);
sprintf(str, "Temperature 1: %d.%02u C\r\n", temp1 / 256, (temp1 % 256) * 100 / 256);
HAL_UART_Transmit(&huart1, (uint8_t*)str, strlen(str), HAL_MAX_DELAY);
sprintf(str, "Temperature 2: %d.%02u C\r\n", temp2 / 256, (temp2 % 256) * 100 / 256);
HAL_UART_Transmit(&huart1, (uint8_t*)str, strlen(str), HAL_MAX_DELAY);
HAL_Delay(1000);
}
}
uint16_t get_temperature(uint8_t channel)
{
uint8_t buffer[2];
uint16_t temperature;
// 设置读取的寄存器地址
uint8_t reg_address = 0x10 + channel;
// 发送读取命令
HAL_I2C_Master_Transmit(&hi2c1, LTC2990_ADDR, ®_address, 1, HAL_MAX_DELAY);
// 读取温度值
HAL_I2C_Master_Receive(&hi2c1, LTC2990_ADDR, buffer, 2, HAL_MAX_DELAY);
// 计算温度值
temperature = ((uint16_t)buffer[0] << 8) | buffer[1];
return temperature;
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**Configure the CPU clock source
*/
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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 400000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
}
void Error_Handler(void)
{
while(1);
}
```
在上述代码中,我们使用了STM32的I2C接口来读取LTC2990的温度值。该代码使用了两个函数,一个是`get_temperature()`函数,用来读取LTC2990的某个通道的温度值;另一个是`main()`函数,用来循环读取两个通道的温度值,并将其打印到串口终端上。
`get_temperature()`函数的实现比较简单,首先我们需要设置要读取的寄存器地址,然后向LTC2990发送读取命令,最后读取返回的数据,并计算出温度值。
`main()`函数的实现比较简单,我们循环读取两个通道的温度值,并将其打印到串口终端上。在打印温度值时,我们使用了`sprintf()`函数来将其格式化为字符串。在实际使用时,可以根据自己的需要对该函数进行修改。
阅读全文