源码如下
void I2C1_Init(void)
{
I2C_InitTypeDef I2C_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
RCC_APB1PeriphClockCmd(RCC_APB1ENR_I2C1EN, ENABLE);
GPIO_InitSet(PI_DSI_HW_I2C_SCL_PIN, GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP);
GPIO_InitSet(PI_DSI_HW_I2C_SDA_PIN, GPIO_Mode_AF, GPIO_OType_OD, GPIO_PuPd_UP);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Disable;
I2C_InitStructure.I2C_DigitalFilter = 0; // disable
I2C_InitStructure.I2C_Timing = 0x002105070; // 0x30E32E44
I2C_InitStructure.I2C_OwnAddress1 = 0x70;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress= I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
I2C_ITConfig(I2C1, I2C_IT_TXIS
| I2C_IT_RXNE
| I2C_IT_ADDR
| I2C_IT_NACKF
| I2C_IT_STOPF
| I2C_IT_TC
| I2C_IT_BERR
| I2C_IT_ARLO
| I2C_IT_OVR
| I2C_IT_PECERR
| I2C_IT_TIMEOUT, ENABLE);
I2C_Cmd(I2C1, ENABLE);
}
void I2C1_IRQHandler(void)
{
volatile uint32_t ISR_Status = I2C1->ISR;
volatile uint8_t addr = (ISR_Status >> 16) & 0xFE;
I2C1->ICR = 0x3F38; // clear all interrupt flag
if((ISR_Status & I2C_ISR_ADDR) != 0) // Slave address matched
{
}
else
{
if (addr == 0x70)
{
if((ISR_Status & I2C_ISR_RXNE) != 0) // Data register not empt
{
}
else if(( ISR_Status & I2C_ISR_STOPF) != 0) // Stop detected
{
I2C1->CR1 |= 0x0001;
}
else if( (( ISR_Status & I2C_ISR_NACKF) != 0)
|| (( ISR_Status & I2C_ISR_TC) != 0)
|| (( ISR_Status & I2C_ISR_BERR) != 0)
|| (( ISR_Status & I2C_ISR_ARLO) != 0)
|| (( ISR_Status & I2C_ISR_OVR) != 0)
|| (( ISR_Status & I2C_ISR_PECERR) != 0)
|| (( ISR_Status & I2C_ISR_TIMEOUT) != 0)
|| (( ISR_Status & I2C_ISR_TXIS) != 0)
)
{
}
if(((ISR_Status & I2C_ISR_DIR) != 0))
{
I2C1->TXDR = I2C1->RXDR;
}
}
}
}
问题现象:
程序里面设置的是在中断中直接把 RXDR中的数据发送出去,但是实际上总是有一字节的的延迟,如图中所示,
0x70是从机地址,0x03是HK32从机I2C接收的数据,直接发出去的却是0x02(之前一个字节的数据),下一个字节才变成正常的0x03, 然后又接收到 0x09的数据,第一次发送的却还是之前的0x03,下一个字节才变成 0x09
file:///C:/Users/Administrator/Desktop/2.bmp
是有什么地方配置的不正确吗? |