stm32 iic 从机模式 时钟低电平问题

 

iic从机模式,时钟延展导致_I2C_通信不可靠导致SCL一直为低电平,造成I2C逻辑异常。

该问题由于CR1的NOSTRETCH 寄存器默认为0导致。

 

 

寄存操作如下:

I2C1->CR1 |= I2C_CR1_NOSTRETCH;

 

程序实现:

void i2c_init(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

I2C_InitTypeDef I2C_InitStructure;



RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);



GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;

GPIO_Init(GPIOB, &GPIO_InitStructure);



NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);



NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;

NVIC_Init(&NVIC_InitStructure);



I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

I2C_InitStructure.I2C_OwnAddress1 = I2CSLAVE_ADDR<<1;

I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

I2C_InitStructure.I2C_ClockSpeed = I2C_CLOCK_FRQ;

I2C_Cmd(I2C1, ENABLE); // I2C Peripheral Enable

I2C_Init(I2C1, &I2C_InitStructure);

I2C1->CR1 |= I2C_CR1_NOSTRETCH;

I2C_ITConfig(I2C1, I2C_IT_EVT, ENABLE); //Part of the STM32 I2C driver

I2C_ITConfig(I2C1, I2C_IT_BUF, ENABLE);

I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE); //Part of the STM32 I2C driver



}


void I2C1_ClearFlag(void) {
  // ADDR-Flag clear
  while((I2C1->SR1 & I2C_SR1_ADDR) == I2C_SR1_ADDR) {
    I2C1->SR1;
    I2C1->SR2;
  }

  // STOPF Flag clear
  while((I2C1->SR1&I2C_SR1_STOPF) == I2C_SR1_STOPF) {
    I2C1->SR1;
    I2C1->CR1 |= 0x1;
  }
}

void I2C1_EV_IRQHandler(void) 
{
  uint32_t event;

    uint8_t i;
  // Reading last event
  I2cEvent=I2C_GetLastEvent(I2C1);
    /*EV1*/
    if((I2cEvent &I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED) == I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED)
    {
        
    }
    else if((I2cEvent &I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED) == I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED)
    {
    }
    /*EV2*/
    if( (I2cEvent &I2C_EVENT_SLAVE_BYTE_RECEIVED) == I2C_EVENT_SLAVE_BYTE_RECEIVED)
    {
    }
    /*EV3*/
  if( (I2cEvent &I2C_EVENT_SLAVE_BYTE_TRANSMITTED) == I2C_EVENT_SLAVE_BYTE_TRANSMITTED) 
    {
  }
  else if((I2cEvent &I2C_EVENT_SLAVE_BYTE_TRANSMITTING) == I2C_EVENT_SLAVE_BYTE_TRANSMITTING) 
    {
    }
    /*EV4*/
  if((I2cEvent &I2C_EVENT_SLAVE_STOP_DETECTED) == I2C_EVENT_SLAVE_STOP_DETECTED) 
    {
    }
}

void I2C1_ER_IRQHandler(void) 
{
     if (I2C_GetITStatus(I2C1, I2C_IT_BERR)) 
    {
    I2C_ClearITPendingBit(I2C1, I2C_IT_BERR);
  }
  if (I2C_GetITStatus(I2C1, I2C_IT_AF)) 
    {
    I2C_ClearITPendingBit(I2C1, I2C_IT_AF);
  }
    if (I2C_GetITStatus(I2C1, I2C_IT_ARLO)) 
    {
    I2C_ClearITPendingBit(I2C1, I2C_IT_ARLO);
  }
    if (I2C_GetITStatus(I2C1, I2C_IT_OVR)) 
    {
    I2C_ClearITPendingBit(I2C1, I2C_IT_OVR);
  }
    if (I2C_GetITStatus(I2C1, I2C_IT_PECERR)) 
    {
    I2C_ClearITPendingBit(I2C1, I2C_IT_PECERR);
  }
    if (I2C_GetITStatus(I2C1, I2C_IT_TIMEOUT)) 
    {
    I2C_ClearITPendingBit(I2C1, I2C_IT_TIMEOUT);
  }
    if (I2C_GetITStatus(I2C1, I2C_IT_TIMEOUT)) 
    {
    I2C_ClearITPendingBit(I2C1, I2C_IT_TIMEOUT);
  }
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值