完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
M4作为主机: int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_I2C1_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ int i; HAL_I2C_Master_Receive_IT(&hi2c1, 0x22, bbb, 4); HAL_Delay(1000); if(i2c_flag == 1) { i2c_flag = 0; printf("naddr is 11:"); for(i = 0; i < 4; i++) { printf("%02x ", aaa); } printf("naddr is 22:"); for(i = 0; i < 4; i++) { printf("%02x ", bbb); } } } /* USER CODE END 3 */ } M0作为从机: void IIC_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStruct; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//GPIO_Mode_IN GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3; GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;//open-drain GPIO_Init(GPIOB , &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOB , &GPIO_InitStruct); /* Connect PXx to I2C_SCL*/ GPIO_PinAFConfig( GPIOB , GPIO_PinSource6, GPIO_AF_1); /* Connect PXx to I2C_SDA*/ GPIO_PinAFConfig( GPIOB , GPIO_PinSource7, GPIO_AF_1); } void IIC_Configuration(void) { I2C_InitTypeDef I2C_InitStruct; /* I2C configuration */ I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable; I2C_InitStruct.I2C_DigitalFilter = 0x00; I2C_InitStruct.I2C_OwnAddress1 = 0x22; I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStruct.I2C_Timing = 0xb0420f13;//100Kbits /* I2C Peripheral Enable */ I2C_Cmd(I2C1, ENABLE); /* Apply I2C configuration after enabling it */ I2C_Init(I2C1, &I2C_InitStruct); } int main(void) { peripheral_init(); board_init(); I2C1_isr.CR1 |= 0X08; //地址匹配产生中断 while(1) { if(Recv_flag == 1) { Recv_flag = 0; if(I2C_GetFlagStatus(I2C1, I2C_FLAG_TCR) != SET) { printf("transfern"); for( i = 0; i < 5; i++) { while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) != SET); I2C_SendData(I2C1, Data_to_M4_1); } } } } } void I2C1_IRQHandler(void) { uint8_t i = 0; uint8_t j = 0; I2C_ClearITPendingBit(I2C1, I2C_ISR_ADDR); I2C1->ISR |= 0x00000001; I2C_ClearFlag(I2C1, I2C_FLAG_ADDR); printf("IIC irq in2n"); Recv_flag = 1; |
|
相关推荐
|
|
使用stm32l451片子,对ad7606进行3通道100k采样值跳动问题?
1060 浏览 1 评论
STM32进行DMA数据转运,为什么只有A组最后一位数据被转运到B组去?
1541 浏览 2 评论
1852 浏览 3 评论
1447 浏览 0 评论
嵌入式开发 Win11安装ST-Link 提示 设备描述符请求失败 怎么处理?
1584 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-26 01:20 , Processed in 0.521779 second(s), Total 69, Slave 49 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191