STM32通讯设计
- 通讯流程
- STM32程序
通讯流程
1.使用HT2202芯片配置为主机接收(轮询模式)。
2.将STM32芯片配置为从机发送,中断模式下发送固定数据。
3.如果HT2202芯片能够收到STM32发送的数据则通讯成功,否则通讯失败。
STM32程序
void MX_I2C1_Init(void)
{/* USER CODE BEGIN I2C1_Init 0 *//* USER CODE END I2C1_Init 0 *//* USER CODE BEGIN I2C1_Init 1 *//* USER CODE END I2C1_Init 1 */hi2c1.Instance = I2C1;hi2c1.Init.ClockSpeed = 400000;hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_16_9;hi2c1.Init.OwnAddress1 = 154;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();}/* USER CODE BEGIN I2C1_Init 2 */driver_I2C_MspInit(hi2c1);uint8_t dataBuffer[10]; // 假设缓冲区大小为10字节for (int i = 0; i < 10; i++){dataBuffer[i] = i; // 填充数据到缓冲区}if (HAL_I2C_Slave_Transmit_IT(&hi2c1, dataBuffer, 10) != HAL_OK){// 发送数据失败处理}/* USER CODE END I2C1_Init 2 */}