[md]改2个地址就可以,参考STM32_AN4502_FW_V2.0.3,这份档案,是PMUbs从机 smbus初始化 void MX_I2C1_SMBUS_Init(void) //i2c.c中 { /* USER CODE BEGIN I2C1_Init 0 */ /* USER CODE END I2C1_Init 0 */ /* USER CODE BEGIN I2C1_Init 1 */ /* USER CODE END I2C1_Init 1 */ hsmbus1.Instance = I2C1; hsmbus1.Init.Timing = 0x10805D88; //100K hsmbus1.Init.AnalogFilter = SMBUS_ANALOGFILTER_ENABLE; hsmbus1.Init.OwnAddress1 = 2; hsmbus1.Init.AddressingMode = SMBUS_ADDRESSINGMODE_7BIT; hsmbus1.Init.DualAddressMode = SMBUS_DUALADDRESS_ENABLE; //2个地址 hsmbus1.Init.OwnAddress2 = 0x40U; // hsmbus1.Init.OwnAddress2Masks = SMBUS_OA2_MASK05; // hsmbus1.Init.GeneralCallMode = SMBUS_GENERALCALL_DISABLE; hsmbus1.Init.NoStretchMode = SMBUS_NOSTRETCH_DISABLE; hsmbus1.Init.PacketErrorCheckMode = SMBUS_PEC_ENABLE; hsmbus1.Init.PeripheralMode = SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE; hsmbus1.Init.SMBusTimeout = 0x00008249; if (HAL_SMBUS_Init(&hsmbus1) != HAL_OK) { Error_Handler(); } /** configuration Alert Mode */ if (HAL_SMBUS_EnableAlert_IT(&hsmbus1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN I2C1_Init 2 */ __HAL_SMBUS_ENABLE_IT(&hsmbus1,SMBUS_IT_ADDRI); __HAL_SMBUS_ENABLE_IT(&hsmbus1,SMBUS_IT_RXI); __HAL_SMBUS_ENABLE_IT(&hsmbus1,SMBUS_IT_TXI); __HAL_SMBUS_ENABLE_IT(&hsmbus1,SMBUS_IT_STOPI); /* USER CODE END I2C1_Init 2 */ } uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx) { /* Check the parameters */ assert_param(IS_I2C_ALL_PERIPH(I2Cx)); /* Return the data in the DR register */ return (uint8_t)I2Cx->RXDR; } 中断接收 /******************************************************************************/ /* STM32F0xx Peripheral Interrupt Handlers */ /* Add here the Interrupt Handlers for the used peripherals. */ /* For the available peripheral interrupt handler names, */ /* please refer to the startup file (startup_stm32f0xx.s). */ /******************************************************************************/ /** * @brief This function handles I2C1 global interrupt. */ void I2C1_IRQHandler(void) { /* USER CODE BEGIN I2C1_IRQn 0 */ if(__HAL_SMBUS_GET_FLAG(&hsmbus1,SMBUS_IT_ADDRI)!= RESET) { TX1_Buffer[Tx1_Cnt++] = I2C_ReceiveData(I2C1); __HAL_SMBUS_CLEAR_FLAG(&hsmbus1,SMBUS_IT_ADDRI); } else if(__HAL_SMBUS_GET_FLAG(&hsmbus1,SMBUS_IT_RXI)!= RESET) { TX1_Buffer[Tx1_Cnt++] = I2C_ReceiveData(I2C1); __HAL_SMBUS_CLEAR_FLAG(&hsmbus1,SMBUS_IT_RXI); } else if(__HAL_SMBUS_GET_FLAG(&hsmbus1,SMBUS_IT_NACKI)!= RESET) { TX1_Buffer[Tx1_Cnt++] = I2C_ReceiveData(I2C1); __HAL_SMBUS_CLEAR_FLAG(&hsmbus1,SMBUS_IT_NACKI); } else if(__HAL_SMBUS_GET_FLAG(&hsmbus1,SMBUS_IT_TCI)!= RESET) { TX1_Buffer[Tx1_Cnt++] = I2C_ReceiveData(I2C1); __HAL_SMBUS_CLEAR_FLAG(&hsmbus1,SMBUS_IT_TCI); } else if(__HAL_SMBUS_GET_FLAG(&hsmbus1,SMBUS_IT_ERRI)!= RESET) { TX1_Buffer[Tx1_Cnt++] = I2C_ReceiveData(I2C1); __HAL_SMBUS_CLEAR_FLAG(&hsmbus1,SMBUS_IT_ERRI); } else if(__HAL_SMBUS_GET_FLAG(&hsmbus1,SMBUS_IT_STOPI)!= RESET) { Tx1_Flag = Tx1_Cnt; Tx1_Cnt = 0; __HAL_SMBUS_CLEAR_FLAG(&hsmbus1,SMBUS_IT_STOPI); } /* USER CODE END I2C1_IRQn 0 */ if (hsmbus1.Instance->ISR & (SMBUS_FLAG_BERR | SMBUS_FLAG_ARLO | SMBUS_FLAG_OVR | SMBUS_FLAG_TIMEOUT | SMBUS_FLAG_ALERT | SMBUS_FLAG_PECERR)) { HAL_SMBUS_ER_IRQHandler(&hsmbus1); } else { HAL_SMBUS_EV_IRQHandler(&hsmbus1); } /* USER CODE BEGIN I2C1_IRQn 1 */ /* USER CODE END I2C1_IRQn 1 */ }uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx) { /* Check the parameters */ assert_param(IS_I2C_ALL_PERIPH(I2Cx)); /* Return the data in the DR register */ return (uint8_t)I2Cx->RXDR; }
所在话题
参与活动
-
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
线下 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
网络 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
网络 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
网络 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
线下 2020-10-16 -
滴滴押注社区团购,明确“投入不设上限,要做市场第一”
线下 2020-10-16