刚接触
STM32处理器。在调试STM32F103ZE的I2C时,遇到了问题,请教一下各位高手。
我使用的是的I2C2,初始化程序如下:
void I2C2_Configura
tion(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure I2C2 pins: SCL-PB10 and SDA-PB11 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = BSP_INT_ID_I2C2_EV; //中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //强占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;//次优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure);//初始化中断
NVIC_InitStructure.NVIC_IRQChannel = BSP_INT_ID_I2C2_ER; //中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //强占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;//次优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure);//初始化中断
I2C_DeInit(I2C2);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x65;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Cmd(I2C2,ENABLE);//打开模块功能
I2C_Init(I2C2,&I2C_InitStructure);
I2C_ITConfig(I2C2,I2C_IT_BUF | I2C_IT_EVT | I2C_IT_ERR,ENABLE);
}
通过示波器观察SCL和SDA的波形,CPU复位后,SCL和SDA都保持高电平,一旦我运行I2C_GenerateSTART(I2C2,ENABLE)后,SDA变成了低电平,并且一直保持低电平,SCL一直为高,但就是不进入I2C事件中断(应当起始条件发送后,SB置,产生中断)。等再次复位CPU后,SCL和SDA又都为高电平。
运行I2C_GenerateSTART(I2C2,ENABLE)后,各寄存器值如下:
I2C2->CR1=0x0501
I2C2->CR2=0x0724
I2C2->SR1=0x0000
I2C2->SR2=0x0002
不知道大家有没有遇到跟我类似的问题,请帮忙看一下问题出在哪里。
一周热门 更多>