用stm32 cubemx默认配置i2c1和urart1,本例子是用硬件i2c非模拟i2c
/* I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
* USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
在main.c修改如下
温度读取流程:
1.STM32向TMP1117发送一个写命令HAL_I2C_Master_Transmit,其中包含TMP117中温度寄存器的地址(地址0x00)的1个字节。
2.使用读取命令HAL_I2C_Master_Receive从 TMP117 读取 2 个字节。这 2 个字节将包含温度数据。
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart1;
uint16_t count = 0;
/* USER CODE BEGIN PV */
static const uint8_t TMP117_ADDR = 0x48 << 1; // Use 8-bit address
//I2C从机的地址最低为是读写标志位,1表示读,0表示写,剩下七位表示从机地址
static const uint8_t REG_TEMP = 0x00;
/* USER CODE END PV */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int fputc(int ch ,FILE *f)
{
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xffff);
return ch;
}
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
HAL_StatusTypeDef ret;
uint8_t buf[12];
int16_t val;
float temp_c;
/* USER CODE END 1 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
// Tell TMP117 that we want to read from the temperature register
buf[0] = REG_TEMP;
ret = HAL_I2C_Master_Transmit(&hi2c1, TMP117_ADDR, buf, 1, HAL_MAX_DELAY);//buf为数组,同函数&可省略。
if ( ret != HAL_OK ) {
strcpy((char*)buf, "Error Tx\r\n");
} else {
// Read 2 bytes from the temperature register
ret = HAL_I2C_Master_Receive(&hi2c1, TMP117_ADDR, &buf[0], 2, HAL_MAX_DELAY);
if ( ret != HAL_OK )
{
strcpy((char*)buf, "Error Rx\r\n");
}
else {
//Combine the bytes
val = ((int16_t)buf[0] << 8)| buf[1];
// Convert to float temperature value (Celsius)
temp_c = val * 0.0078125;
printf("%d---Temp: %0.3f\r\n", count++, temp_c);
}
}
// Send out buffer (temperature or error message)
HAL_UART_Transmit(&huart1, buf, strlen((char*)buf), HAL_MAX_DELAY);
// Wait
HAL_Delay(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/*I2C写数据的函数*/
HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
/*
I2C_HandleTypeDef *hi2c :就是设置的实例,比如I2C1 &hi2c1
uint16_t DevAddress : 从机设备地址,比如0x48
uint8_t *pData :存放你要写的数据
uint16_t Size :数据的大小
uint32_t Timeout :最大的传输时间
例如
HAL_I2C_Master_Transmit(&hi2c1,0xA1,(uint8_t*)TxData,2,1000)
*/
/*I2C读数据的函数*/
HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
/*
I2C_HandleTypeDef *hi2c :就是设置的实例,比如I2C1 &hi2c1
uint16_t DevAddress : 从机设备地址,比如0x48
uint8_t *pData :存放你要读的数据
uint16_t Size :数据的大小
uint32_t Timeout :最大的传输时间
*/
void HAL_IIC_WriteByte_ToSlave(uint8_t I2C_Command)
{
// uint8_t *pData;
// pData = &I2C_Command;
HAL_I2C_Mem_Write(&hi2c1, TMP117_ADDR, reg, I2C_MEMADD_SIZE_8BIT, &I2C_Command, 1, 100);
}
void HALIIC_ReadByte_FromSlave(uint8_t *RXbuf)
{
HAL_I2C_Mem_Read(&hi2c1, TMP117_ADDR, reg, I2C_MEMADD_SIZE_8BIT, RXbuf, 1, 100);
}