stm32f030的i2c读写eeprom数据不能进入接收中断

2019-07-14 14:04发布

我按照如下配置,但是就是不能进入接收中断,求大神帮忙看看。谢谢了
void I2C_Configuration(void)
{
GPIO_InitTypeDef  GPIO_InitStruct;
I2C_InitTypeDef  I2C_InitStruct;
NVIC_InitTypeDef  NVIC_InitStructure;

/* Configure the I2C clock source. The clock is derived from the HSI */
RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
/*Enable GPIOB and I2c1 clock*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE);

/* 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);

/*GPIO Configuration*/
/*Configure I2C1 pins: SCL */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB,&GPIO_InitStruct);

/*Configure sEE_I2C pins: SDA */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB , &GPIO_InitStruct);

/* Enable the USART1 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/*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 =0x00;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_Timing = 0x00210507;
I2C_Init(I2C1, &I2C_InitStruct);

I2C_ITConfig(I2C1,I2C_IT_RXI | I2C_IT_TXI | I2C_IT_TCI | I2C_IT_STOPI,ENABLE);
// I2C1->CR1|=0x7F<<1;  //¿ªÆôÖжÏ
I2C_Cmd(I2C1, ENABLE);
}
void sI2CInterrupt(void)
{
if(I2C_GetITStatus(I2C1,I2C_ISR_TXIS) != RESET)
{
  if(num++ < 1)
  {
   I2C_SendData(I2C1, 0x00);
  }
  else
  {
   I2C_SendData(I2C1, Data2);
   num = 0;
  }
}
else if(I2C_GetITStatus(I2C1,I2C_ISR_TCR) != RESET)
{
  I2C_TransferHandling(I2C1,0xA8,1,I2C_AutoEnd_Mode,I2C_No_StartStop);  
}
else if(I2C_GetITStatus(I2C1,I2C_IT_RXNE) != RESET)  //Êý¾Ý½ÓÊÕÖжÏ
{
  Data1 = I2C_ReceiveData(I2C1);
}
else if(I2C_GetITStatus(I2C1,I2C_ISR_TC) != RESET)
{
  I2C_TransferHandling(I2C1,0xA8,1,I2C_AutoEnd_Mode,I2C_Generate_Start_Read);
}
else if(I2C_GetITStatus(I2C1,I2C_IT_STOPF) != RESET)
{
  I2C_ClearITPendingBit(I2C1,I2C_IT_STOPF);
}
   
}

void sReadI2C(INT8U* lubData,INT16U luwI2CAddr,INT8U lubDataLen)
{
INT8U i;
INT8U lubDeviceAddr;

lubDeviceAddr = luwI2CAddr / 256;
lubDeviceAddr = (lubDeviceAddr & 0x03) << 1;

// µÈ´ý×ÜÏß¿ÕÏÐ
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_TransferHandling(I2C1,EepromAddr | lubDeviceAddr,1,I2C_SoftEnd_Mode,I2C_Generate_Start_Write);
}

void I2C_uWriteData(INT8U *WriteBuf,INT8U lubWriteAddr,INT8U WriteLen,INT8U lubDeviceAddr)
{
INT8U i;

lubDeviceAddr = (lubDeviceAddr & 0x03) << 1;
// µÈ´ý×ÜÏß¿ÕÏÐ
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_TransferHandling(I2C1,EepromAddr | lubDeviceAddr,1,I2C_Reload_Mode,I2C_Generate_Start_Write);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
8条回答
peternie
1楼-- · 2019-07-14 16:35
用HAL库吧,中断方式适合DMA。也有非中断的阻塞调用方式。
leilijiao
2楼-- · 2019-07-14 19:21
 精彩回答 2  元偷偷看……
peternie
3楼-- · 2019-07-14 21:48
leilijiao 发表于 2019-2-21 08:11
HAL的库你有吗?谢谢

下载CubeMX,HAL库自来~~
leilijiao
4楼-- · 2019-07-15 03:22
peternie 发表于 2019-2-21 08:30
下载CubeMX,HAL库自来~~

找到了,就是下载下来不对,我再试试,谢谢你了
wang22222
5楼-- · 2019-07-15 08:33
楼主1楼这个程序是通的吧?
leilijiao
6楼-- · 2019-07-15 09:31
wang22222 发表于 2019-2-21 09:00
楼主1楼这个程序是通的吧?

本帖最后由 alisa123 于 2016-4-7 13:34 编辑


我忘记了,下面这个链接是通的。

一周热门 更多>