OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2115|回复: 7

关于429开发板I2C操作AT24C02的疑问

[复制链接]

76

主题

88

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
264
金钱
264
注册时间
2017-10-26
在线时间
12 小时
发表于 2017-10-26 16:41:56 | 显示全部楼层 |阅读模式
1金钱
本帖最后由 mrmzyking 于 2017-10-27 11:56 编辑

我最近产品线切线,需要从原来使用的M4切换到意法的429上面,在测试的时候发现I2C有点问题。代码中如果将Debug设置为1,允许串口打印调试输出,则I2C操作AT24C02就完全正常,如果将Debug设置为0,则会出现错误。
请大家帮我看看,429的I2C需要怎么修改。不甚感激。

详细代码如下所示。

一、头文件部分
#ifndef __i2c_H
#define __i2c_H
#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "main.h"
#define Debug 0
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */
extern I2C_HandleTypeDef hi2c2;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

extern void _Error_Handler(char *, int);

void MX_I2C2_Init(void);

/* USER CODE BEGIN Prototypes */
extern void I2C_Start( void ) ;
extern void I2C_Stop( void ) ;
extern unsigned char I2C_ReadByte( unsigned char DevAddr , unsigned char MemAddr ) ;
extern unsigned char I2C_WriteByte( unsigned char DevAddr , unsigned char MemAddr , unsigned char Data ) ;
extern unsigned char I2C_WriteByte_Interrupt( unsigned char DevAddr , unsigned char MemAddr , unsigned char Data ) ;
extern unsigned char I2C_ReadByte_Interrupt( unsigned char DevAddr , unsigned char MemAddr ) ;

extern unsigned char I2C_ReadBuff( unsigned char DevAddr , unsigned char MemAddr , unsigned char *Buf , unsigned short Len ) ;
extern unsigned char I2C_WriteBuff(  unsigned char DevAddr , unsigned char MemAddr , unsigned char *Buf , unsigned short Len ) ;

/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif
#endif /*__ i2c_H */



二、I2C操作代码部分
/* Includes ------------------------------------------------------------------*/
#include "i2c.h"

#include "gpio.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

I2C_HandleTypeDef hi2c2;
//struct I2C_Struct AT24C02 ;
/* I2C2 init function */
void MX_I2C2_Init(void)
{

  hi2c2.Instance = I2C2;
  hi2c2.Init.ClockSpeed = 200000;
  hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c2.Init.OwnAddress1 = 0;
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c2.Init.OwnAddress2 = 0;
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
        __HAL_I2C_ENABLE_IT( &hi2c2 , I2C_IT_EVT | I2C_IT_ERR ) ;
        
        //HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
}

void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(i2cHandle->Instance==I2C2)
  {
  /* USER CODE BEGIN I2C2_MspInit 0 */

  /* USER CODE END I2C2_MspInit 0 */

    /**I2C2 GPIO Configuration   
    PH4     ------> I2C2_SCL
    PH5     ------> I2C2_SDA
    */
    GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
    HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);

    /* I2C2 clock enable */
    __HAL_RCC_I2C2_CLK_ENABLE();

    /* I2C2 interrupt Init */
    HAL_NVIC_SetPriority(I2C2_EV_IRQn, 0, 0);
    //HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
  /* USER CODE BEGIN I2C2_MspInit 1 */
        
  /* USER CODE END I2C2_MspInit 1 */
  }
}

void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
{

  if(i2cHandle->Instance==I2C2)
  {
  /* USER CODE BEGIN I2C2_MspDeInit 0 */

  /* USER CODE END I2C2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_I2C2_CLK_DISABLE();

    /**I2C2 GPIO Configuration   
    PH4     ------> I2C2_SCL
    PH5     ------> I2C2_SDA
    */
    HAL_GPIO_DeInit(GPIOH, GPIO_PIN_4|GPIO_PIN_5);

    /* I2C2 interrupt Deinit */
    HAL_NVIC_DisableIRQ(I2C2_EV_IRQn);
  /* USER CODE BEGIN I2C2_MspDeInit 1 */

  /* USER CODE END I2C2_MspDeInit 1 */
  }
}
/* USER CODE BEGIN 1 */
void I2C_Start( void )
{        
        I2C2->CR1 |= 0x0400 ; //ack enable
        I2C2->CR1 |= 0x0100 ; //generate Start signal         
        while( ( I2C2->SR1 & 0x0001 ) != 0x0001 )  //EV5 detect the Start signal is send success ;
        {
                #if Debug
                printf("I2C Read ( Debug Start ) : I2C2->SR1 = 0x%X \r\n",I2C2->SR1) ;
                #endif        
        }
        
}
void I2C_Stop( void )
{        
        I2C2->CR1 &= ~0x0400 ; //ack enable
        I2C2->CR1 |= 0x0200 ;
        //printf("I2C Read : Send STOP signal \r\n") ;
}
unsigned char I2C_ReadByte( unsigned char DevAddr , unsigned char MemAddr )
{
        unsigned char tmpreg ;
        unsigned char Data ;
        #if Debug
        printf("-----------I2C Read Operation Begin----------------\r\n") ;
        #endif
        while( (I2C2->SR2 & 0x0002 ) == 1 )  //detect the I2C busy
        {
                #if Debug
                printf("I2C Read : 0.I2C bus is busy! \r\n");
                #endif
        }
        //1.Send Start signal
        I2C_Start(  ) ;
        #if Debug
        printf("I2C Read : 1.Send START signal \r\n");
        #endif
        //2.Send Device Addr ;
        tmpreg = DevAddr & 0xFE ;
        //I2C2->CR1 |= 0x0400 ; //ack enable
        I2C2->DR = tmpreg ;
        while( ( I2C2->SR1 & 0x0082 ) != 0x82 )  //EV6 EV8_1
        {
                #if Debug
                printf("I2C Read ( Debug ) : 2.I2C2->SR1 = 0x%X \r\n",I2C2->SR1) ;
                #endif
        }
        tmpreg = I2C2->SR2 ; //read SR2 reg to clean ADDR bit
        #if Debug
        printf("I2C Read : 2.Send Device Address \r\n");
        #endif
        //3.Send Memery Addr ;
        //I2C2->CR1 |= 0x0400 ; //ack enable
        I2C2->DR = MemAddr ;
        while( ( I2C2->SR1 & 0x0080 ) != 0x80 ) //EV8
        {
                #if Debug
                printf("I2C Read ( Debug ) : 3.I2C2->SR1 = 0x%X \r\n",I2C2->SR1) ;
                #endif
        }               
        #if Debug
        printf("I2C Read : 3.Send Memery Address \r\n");
        #endif
        //4.Send ReStart signal
        I2C_Start(  ) ;
        #if Debug
        printf("I2C Read : 4.Send RESTART signal \r\n") ;
        #endif
        //5.Send Device Addr ;
        tmpreg = DevAddr | 0x01 ;
        I2C2->CR1 &= ~0x0400 ; //ack disable
        I2C2->DR = tmpreg ;        
        while( ( I2C2->SR1 & 0x0002 ) != 0x02 )  //EV6
        {
                #if Debug
                printf("I2C Read ( Debug ) : 4.I2C2->SR1 = 0x%X \r\n",I2C2->SR1) ;
                #endif
        }
        tmpreg = I2C2->SR2 ; //read SR2 reg to clean ADDR bit
        #if Debug
        printf("I2C Read : 5.Send Device Address \r\n") ;
        #endif
        //6.Read Byte
        while( ( I2C2->SR1 & 0x0040 ) != 0x40 )  //EV7_1        
        {
                #if Debug
                printf("I2C Read ( Debug ) : 6.I2C2->SR1 = 0x%X \r\n",I2C2->SR1) ;
                #endif
        }        
        I2C2->CR1 &= ~0x0400 ; //ack enable debug
        Data = I2C2->DR ;        
        #if Debug
        printf("I2C Read : 6.Read Data \r\n") ;
        #endif        
        //7.Send Stop signal
//        while( ( I2C2->SR1 & 0x0044 ) != 0x44 ) //EV7
//        {
//                #if Debug
//                printf("I2C Read ( Debug ) : 7.I2C2->SR1 = 0x%X \r\n",I2C2->SR1) ;
//                #endif
//        }        
        I2C_Stop( ) ;
        //printf("I2C Read : 7.Send STOP signal Read Value = 0x%X\r\n",Data) ;        
        #if Debug
        printf("I2C Read : 7.Send STOP signal Read Value = 0x%X\r\n",Data) ;        
        printf("-----------I2C Read Operation End----------------\r\n") ;
        #endif
        return Data ;
}
unsigned char I2C_WriteByte( unsigned char DevAddr , unsigned char MemAddr , unsigned char Data )
{
        unsigned char tmpreg ;
        //unsigned char Result ;
        #if Debug
        printf("-----------I2C Write Operation Begin----------------\r\n") ;
        #endif
        while( (I2C2->SR2 & 0x0002 ) == 1 )  //detect the I2C busy
        {
                #if Debug
                printf("I2C Write : 0.I2C BUS is Busying now. \r\n") ;
                #endif
        }
        //1.send start
        I2C_Start(  ) ;
        #if Debug
        printf("I2C Write : 1.Send START signal \r\n");
        #endif
        //2.send device address
        tmpreg = DevAddr & 0xFE ;
        I2C2->DR = tmpreg ;
        while( ( I2C2->SR1 & 0x0082 ) != 0x82 )  //EV6 EV8_1
        {
                #if Debug
                printf("I2C Write : 2.I2C2->SR1 = 0x%X \r\n",I2C2->SR1) ;
                #endif
        }
        tmpreg = I2C2->SR2 ; //read SR2 REG to clean ADDR bit
        #if Debug
        printf("I2C Write : 2.Send Device Address \r\n");
        #endif
        //3.send memery address
        I2C2->DR = MemAddr ;
        while( ( I2C2->SR1 & 0x0080 ) != 0x80 )//EV8
        {
                #if Debug
                printf("I2C Write ( Debug ) : 3.I2C2->SR1 = 0x%X \r\n",I2C2->SR1) ;
                #endif
        }
        #if Debug
        printf("I2C Write : 3.Send Memery Address \r\n");
        #endif
        //4.send data
//        I2C2->CR1 |= 0x0400 ; //ack enable
        I2C2->DR = Data ;
        #if Debug
        printf("I2C Write : 4.Send Data \r\n");
        #endif
        //5.send stop
        while( ( I2C2->SR1 & 0x0084 ) != 0x84 ) //EV8_2
        {
                #if Debug
                printf("I2C Write ( Debug ) : 5.I2C2->SR1 = 0x%X \r\n",I2C2->SR1) ;
                #endif
        }
        I2C_Stop( ) ;
        //printf("I2C Write : Write Data = 0x%X \r\n",Data) ;        
        #if Debug
        printf("I2C Write : Write Data = 0x%X \r\n",Data) ;        
        printf("I2C Write : 5.Send STOP signal \r\n");
        printf("-----------I2C Write Operation End----------------\r\n") ;
        #endif
        return 1 ;
}
unsigned char I2C_ReadBuff( unsigned char DevAddr , unsigned char MemAddr , unsigned char *Buf , unsigned short Len )
{
        unsigned short i ;
        if( Len >= ( 256 - MemAddr ) )
        {
                return 0 ;
        }
        for( i = 0 ; i < Len ; i++ )
        {
                *( Buf + i ) = I2C_ReadByte( DevAddr , MemAddr + i ) ;
        }
        return 1 ;
}
unsigned char I2C_WriteBuff(  unsigned char DevAddr , unsigned char MemAddr , unsigned char *Buf , unsigned short Len )
{
        unsigned short i ;
        if( Len > ( 256 - MemAddr ) )
        {
                return 0 ;
        }
        for( i = 0 ; i < Len ; i++ )
        {
                I2C_WriteByte( DevAddr , MemAddr + i , *( Buf + i ) ) ;
        }
        return 1 ;
}
/* USER CODE END 1 */


三、主函数部分
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_hal.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)
{
        unsigned short i ;
        unsigned char ReadI2C[255] ;
        unsigned char WriteI2C[256] ;
        unsigned char DeviceAddress , MemeryAddress , Len ;
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C2_Init();
  MX_USART2_UART_Init();

  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

                //I2C WRITE
               
                for( i = 0 ; i < 256 ; i++ )
                {
                        WriteI2C =  i % 255;
                        ReadI2C = 0 ;
                }               
                DeviceAddress = 0xA0 ;
                MemeryAddress = 0x00 ;
                Len = 10 ;
                I2C_WriteBuff( DeviceAddress , MemeryAddress , &WriteI2C[0] , Len ) ;
                printf("Write AT24C02 Device Address = %X , Memery Address = %X , Length = %d \r\n",DeviceAddress,MemeryAddress,Len) ;
                printf("Writing Data = \t");
                for( i = 0 ; i < Len ; i++ )
                {
                        printf(" 0x%X \t",WriteI2C ) ;
                }
                printf("\r\n") ;
                //I2C READ
                WriteI2C[0] = 0 ;
                ReadI2C[0] = 0 ;
                DeviceAddress = 0xA1 ;
                MemeryAddress = 0x00 ;
                Len = 10 ;
                I2C_ReadBuff( DeviceAddress , MemeryAddress , &ReadI2C[0] , Len ) ;
                printf("Read AT24C02 Device Address = %X , Memery Address = %X , Length = %d \r\n",DeviceAddress,MemeryAddress,Len) ;
                printf("Read Data = \t");
                for( i = 0 ; i < Len ; i++ )
                {
                        printf(" 0x%X \t",ReadI2C ) ;
                }
                printf("\r\n") ;
        while (1)
  {
  }
  /* USER CODE END 3 */

}
int fputc(int c, FILE *f)
{
        unsigned char data ;
        SendChar( ( ( unsigned char )( c & 0xFF ) ) ) ;
  return 1 ;
}
int fgetc(FILE *f)
{
    return 1;
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

    /**Configure the main internal regulator output voltage
    */
  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 15;
  RCC_OscInitStruct.PLL.PLLN = 216;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Activate the Over-Drive mode
    */
  if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure the Systick interrupt time
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
void _Error_Handler(char * file, int line)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT

/**
   * @brief Reports the name of the source file and the source line number
   * where the assert_param error has occurred.
   * @param file: pointer to the source file name
   * @param line: assert_param error line source number
   * @retval None
   */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */

}

#endif


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

76

主题

88

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
264
金钱
264
注册时间
2017-10-26
在线时间
12 小时
 楼主| 发表于 2017-10-26 17:02:49 | 显示全部楼层
如果在I2C.H文件中将#define Debug 1的话,通过串口打印输出的内容如下所示,对AT24C02的操作完全正常,见下面内容:
-----------I2C Write Operation Begin----------------[2017-10-26 05:00:01.939]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:01.939]
I2C Write : 1.Send START signal [2017-10-26 05:00:01.939]
I2C Write : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:01.945]
I2C Write : 2.Send Device Address [2017-10-26 05:00:01.945]
I2C Write : 3.Send Memery Address [2017-10-26 05:00:01.945]
I2C Write : 4.Send Data [2017-10-26 05:00:01.945]
I2C Write : Write Data = 0x0 [2017-10-26 05:00:01.951]
I2C Write : 5.Send STOP signal [2017-10-26 05:00:01.951]
-----------I2C Write Operation End----------------[2017-10-26 05:00:01.951]
-----------I2C Write Operation Begin----------------[2017-10-26 05:00:01.955]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:01.955]
I2C Write : 1.Send START signal [2017-10-26 05:00:01.955]
I2C Write : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:01.964]
I2C Write : 2.Send Device Address [2017-10-26 05:00:01.964]
I2C Write : 3.Send Memery Address [2017-10-26 05:00:01.964]
I2C Write : 4.Send Data [2017-10-26 05:00:01.969]
I2C Write : Write Data = 0x1 [2017-10-26 05:00:01.969]
I2C Write : 5.Send STOP signal [2017-10-26 05:00:01.969]
-----------I2C Write Operation End----------------[2017-10-26 05:00:01.969]
-----------I2C Write Operation Begin----------------[2017-10-26 05:00:01.969]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:01.971]
I2C Write : 1.Send START signal [2017-10-26 05:00:01.971]
I2C Write : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:01.974]
I2C Write : 2.Send Device Address [2017-10-26 05:00:01.974]
I2C Write : 3.Send Memery Address [2017-10-26 05:00:01.977]
I2C Write : 4.Send Data [2017-10-26 05:00:01.977]
I2C Write : Write Data = 0x2 [2017-10-26 05:00:01.981]
I2C Write : 5.Send STOP signal [2017-10-26 05:00:01.981]
-----------I2C Write Operation End----------------[2017-10-26 05:00:01.988]
-----------I2C Write Operation Begin----------------[2017-10-26 05:00:01.988]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:01.990]
I2C Write : 1.Send START signal [2017-10-26 05:00:01.990]
I2C Write : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:01.990]
I2C Write : 2.Send Device Address [2017-10-26 05:00:01.990]
I2C Write : 3.Send Memery Address [2017-10-26 05:00:01.993]
I2C Write : 4.Send Data [2017-10-26 05:00:01.996]
I2C Write : Write Data = 0x3 [2017-10-26 05:00:01.996]
I2C Write : 5.Send STOP signal [2017-10-26 05:00:01.996]
-----------I2C Write Operation End----------------[2017-10-26 05:00:02.024]
-----------I2C Write Operation Begin----------------[2017-10-26 05:00:02.024]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.024]
I2C Write : 1.Send START signal [2017-10-26 05:00:02.024]
I2C Write : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.024]
I2C Write : 2.Send Device Address [2017-10-26 05:00:02.024]
I2C Write : 3.Send Memery Address [2017-10-26 05:00:02.024]
I2C Write : 4.Send Data [2017-10-26 05:00:02.024]
I2C Write : Write Data = 0x4 [2017-10-26 05:00:02.024]
I2C Write : 5.Send STOP signal [2017-10-26 05:00:02.024]
-----------I2C Write Operation End----------------[2017-10-26 05:00:02.024]
-----------I2C Write Operation Begin----------------[2017-10-26 05:00:02.024]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.024]
I2C Write : 1.Send START signal [2017-10-26 05:00:02.028]
I2C Write : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.028]
I2C Write : 2.Send Device Address [2017-10-26 05:00:02.028]
I2C Write : 3.Send Memery Address [2017-10-26 05:00:02.028]
I2C Write : 4.Send Data [2017-10-26 05:00:02.028]
I2C Write : Write Data = 0x5 [2017-10-26 05:00:02.028]
I2C Write : 5.Send STOP signal [2017-10-26 05:00:02.032]
-----------I2C Write Operation End----------------[2017-10-26 05:00:02.032]
-----------I2C Write Operation Begin----------------[2017-10-26 05:00:02.038]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.038]
I2C Write : 1.Send START signal [2017-10-26 05:00:02.038]
I2C Write : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.038]
I2C Write : 2.Send Device Address [2017-10-26 05:00:02.045]
I2C Write : 3.Send Memery Address [2017-10-26 05:00:02.045]
I2C Write : 4.Send Data [2017-10-26 05:00:02.049]
I2C Write : Write Data = 0x6 [2017-10-26 05:00:02.049]
I2C Write : 5.Send STOP signal [2017-10-26 05:00:02.049]
-----------I2C Write Operation End----------------[2017-10-26 05:00:02.049]
-----------I2C Write Operation Begin----------------[2017-10-26 05:00:02.049]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.056]
I2C Write : 1.Send START signal [2017-10-26 05:00:02.056]
I2C Write : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.056]
I2C Write : 2.Send Device Address [2017-10-26 05:00:02.056]
I2C Write : 3.Send Memery Address [2017-10-26 05:00:02.067]
I2C Write : 4.Send Data [2017-10-26 05:00:02.067]
I2C Write : Write Data = 0x7 [2017-10-26 05:00:02.067]
I2C Write : 5.Send STOP signal [2017-10-26 05:00:02.067]
-----------I2C Write Operation End----------------[2017-10-26 05:00:02.067]
-----------I2C Write Operation Begin----------------[2017-10-26 05:00:02.067]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.076]
I2C Write : 1.Send START signal [2017-10-26 05:00:02.076]
I2C Write : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.083]
I2C Write : 2.Send Device Address [2017-10-26 05:00:02.083]
I2C Write : 3.Send Memery Address [2017-10-26 05:00:02.083]
I2C Write : 4.Send Data [2017-10-26 05:00:02.083]
I2C Write : Write Data = 0x8 [2017-10-26 05:00:02.083]
I2C Write : 5.Send STOP signal [2017-10-26 05:00:02.083]
-----------I2C Write Operation End----------------[2017-10-26 05:00:02.086]
-----------I2C Write Operation Begin----------------[2017-10-26 05:00:02.086]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.086]
I2C Write : 1.Send START signal [2017-10-26 05:00:02.086]
I2C Write : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.088]
I2C Write : 2.Send Device Address [2017-10-26 05:00:02.088]
I2C Write : 3.Send Memery Address [2017-10-26 05:00:02.091]
I2C Write : 4.Send Data [2017-10-26 05:00:02.095]
I2C Write : Write Data = 0x9 [2017-10-26 05:00:02.095]
I2C Write : 5.Send STOP signal [2017-10-26 05:00:02.095]
-----------I2C Write Operation End----------------[2017-10-26 05:00:02.097]
Write AT24C02 Device Address = A0 , Memery Address = 0 , Length = 10 [2017-10-26 05:00:02.100]
Writing Data =          0x0          0x1          0x2          0x3          0x4          0x5          0x6          0x7          0x8          0x9         [2017-10-26 05:00:02.102]
-----------I2C Read Operation Begin----------------[2017-10-26 05:00:02.106]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.106]
I2C Read : 1.Send START signal [2017-10-26 05:00:02.111]
I2C Read ( Debug ) : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.111]
I2C Read : 2.Send Device Address [2017-10-26 05:00:02.111]
I2C Read : 3.Send Memery Address [2017-10-26 05:00:02.115]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x84 [2017-10-26 05:00:02.115]
I2C Read : 4.Send RESTART signal [2017-10-26 05:00:02.117]
I2C Read ( Debug ) : 4.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.119]
I2C Read : 5.Send Device Address [2017-10-26 05:00:02.121]
I2C Read : 6.Read Data [2017-10-26 05:00:02.121]
I2C Read : 7.Send STOP signal Read Value = 0x0[2017-10-26 05:00:02.124]
-----------I2C Read Operation End----------------[2017-10-26 05:00:02.126]
-----------I2C Read Operation Begin----------------[2017-10-26 05:00:02.128]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.131]
I2C Read : 1.Send START signal [2017-10-26 05:00:02.131]
I2C Read ( Debug ) : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.133]
I2C Read : 2.Send Device Address [2017-10-26 05:00:02.133]
I2C Read : 3.Send Memery Address [2017-10-26 05:00:02.136]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x84 [2017-10-26 05:00:02.139]
I2C Read : 4.Send RESTART signal [2017-10-26 05:00:02.139]
I2C Read ( Debug ) : 4.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.141]
I2C Read : 5.Send Device Address [2017-10-26 05:00:02.143]
I2C Read : 6.Read Data [2017-10-26 05:00:02.143]
I2C Read : 7.Send STOP signal Read Value = 0x1[2017-10-26 05:00:02.145]
-----------I2C Read Operation End----------------[2017-10-26 05:00:02.147]
-----------I2C Read Operation Begin----------------[2017-10-26 05:00:02.150]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.152]
I2C Read : 1.Send START signal [2017-10-26 05:00:02.153]
I2C Read ( Debug ) : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.155]
I2C Read : 2.Send Device Address [2017-10-26 05:00:02.157]
I2C Read : 3.Send Memery Address [2017-10-26 05:00:02.160]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x84 [2017-10-26 05:00:02.160]
I2C Read : 4.Send RESTART signal [2017-10-26 05:00:02.163]
I2C Read ( Debug ) : 4.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.163]
I2C Read : 5.Send Device Address [2017-10-26 05:00:02.165]
I2C Read : 6.Read Data [2017-10-26 05:00:02.165]
I2C Read : 7.Send STOP signal Read Value = 0x2[2017-10-26 05:00:02.168]
-----------I2C Read Operation End----------------[2017-10-26 05:00:02.170]
-----------I2C Read Operation Begin----------------[2017-10-26 05:00:02.173]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.175]
I2C Read : 1.Send START signal [2017-10-26 05:00:02.175]
I2C Read ( Debug ) : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.178]
I2C Read : 2.Send Device Address [2017-10-26 05:00:02.180]
I2C Read : 3.Send Memery Address [2017-10-26 05:00:02.182]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x84 [2017-10-26 05:00:02.184]
I2C Read : 4.Send RESTART signal [2017-10-26 05:00:02.184]
I2C Read ( Debug ) : 4.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.187]
I2C Read : 5.Send Device Address [2017-10-26 05:00:02.187]
I2C Read : 6.Read Data [2017-10-26 05:00:02.189]
I2C Read : 7.Send STOP signal Read Value = 0x3[2017-10-26 05:00:02.191]
-----------I2C Read Operation End----------------[2017-10-26 05:00:02.193]
-----------I2C Read Operation Begin----------------[2017-10-26 05:00:02.195]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.198]
I2C Read : 1.Send START signal [2017-10-26 05:00:02.198]
I2C Read ( Debug ) : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.201]
I2C Read : 2.Send Device Address [2017-10-26 05:00:02.201]
I2C Read : 3.Send Memery Address [2017-10-26 05:00:02.205]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x84 [2017-10-26 05:00:02.207]
I2C Read : 4.Send RESTART signal [2017-10-26 05:00:02.207]
I2C Read ( Debug ) : 4.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.210]
I2C Read : 5.Send Device Address [2017-10-26 05:00:02.210]
I2C Read : 6.Read Data [2017-10-26 05:00:02.212]
I2C Read : 7.Send STOP signal Read Value = 0x4[2017-10-26 05:00:02.212]
-----------I2C Read Operation End----------------[2017-10-26 05:00:02.214]
-----------I2C Read Operation Begin----------------[2017-10-26 05:00:02.218]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.220]
I2C Read : 1.Send START signal [2017-10-26 05:00:02.222]
I2C Read ( Debug ) : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.225]
I2C Read : 2.Send Device Address [2017-10-26 05:00:02.225]
I2C Read : 3.Send Memery Address [2017-10-26 05:00:02.225]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x84 [2017-10-26 05:00:02.227]
I2C Read : 4.Send RESTART signal [2017-10-26 05:00:02.229]
I2C Read ( Debug ) : 4.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.231]
I2C Read : 5.Send Device Address [2017-10-26 05:00:02.233]
I2C Read : 6.Read Data [2017-10-26 05:00:02.233]
I2C Read : 7.Send STOP signal Read Value = 0x5[2017-10-26 05:00:02.236]
-----------I2C Read Operation End----------------[2017-10-26 05:00:02.238]
-----------I2C Read Operation Begin----------------[2017-10-26 05:00:02.240]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.242]
I2C Read : 1.Send START signal [2017-10-26 05:00:02.244]
I2C Read ( Debug ) : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.244]
I2C Read : 2.Send Device Address [2017-10-26 05:00:02.247]
I2C Read : 3.Send Memery Address [2017-10-26 05:00:02.247]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x84 [2017-10-26 05:00:02.249]
I2C Read : 4.Send RESTART signal [2017-10-26 05:00:02.251]
I2C Read ( Debug ) : 4.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.253]
I2C Read : 5.Send Device Address [2017-10-26 05:00:02.255]
I2C Read : 6.Read Data [2017-10-26 05:00:02.257]
I2C Read : 7.Send STOP signal Read Value = 0x6[2017-10-26 05:00:02.260]
-----------I2C Read Operation End----------------[2017-10-26 05:00:02.260]
-----------I2C Read Operation Begin----------------[2017-10-26 05:00:02.262]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.271]
I2C Read : 1.Send START signal [2017-10-26 05:00:02.271]
I2C Read ( Debug ) : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.275]
I2C Read : 2.Send Device Address [2017-10-26 05:00:02.275]
I2C Read : 3.Send Memery Address [2017-10-26 05:00:02.275]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x84 [2017-10-26 05:00:02.275]
I2C Read : 4.Send RESTART signal [2017-10-26 05:00:02.277]
I2C Read ( Debug ) : 4.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.277]
I2C Read : 5.Send Device Address [2017-10-26 05:00:02.277]
I2C Read : 6.Read Data [2017-10-26 05:00:02.279]
I2C Read : 7.Send STOP signal Read Value = 0x7[2017-10-26 05:00:02.281]
-----------I2C Read Operation End----------------[2017-10-26 05:00:02.283]
-----------I2C Read Operation Begin----------------[2017-10-26 05:00:02.286]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.288]
I2C Read : 1.Send START signal [2017-10-26 05:00:02.290]
I2C Read ( Debug ) : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.290]
I2C Read : 2.Send Device Address [2017-10-26 05:00:02.292]
I2C Read : 3.Send Memery Address [2017-10-26 05:00:02.294]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x84 [2017-10-26 05:00:02.296]
I2C Read : 4.Send RESTART signal [2017-10-26 05:00:02.298]
I2C Read ( Debug ) : 4.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.298]
I2C Read : 5.Send Device Address [2017-10-26 05:00:02.300]
I2C Read : 6.Read Data [2017-10-26 05:00:02.302]
I2C Read : 7.Send STOP signal Read Value = 0x8[2017-10-26 05:00:02.304]
-----------I2C Read Operation End----------------[2017-10-26 05:00:02.306]
-----------I2C Read Operation Begin----------------[2017-10-26 05:00:02.308]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x0 [2017-10-26 05:00:02.312]
I2C Read : 1.Send START signal [2017-10-26 05:00:02.312]
I2C Read ( Debug ) : 2.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.312]
I2C Read : 2.Send Device Address [2017-10-26 05:00:02.315]
I2C Read : 3.Send Memery Address [2017-10-26 05:00:02.315]
I2C Read ( Debug Start ) : I2C2->SR1 = 0x84 [2017-10-26 05:00:02.317]
I2C Read : 4.Send RESTART signal [2017-10-26 05:00:02.319]
I2C Read ( Debug ) : 4.I2C2->SR1 = 0x0 [2017-10-26 05:00:02.321]
I2C Read : 5.Send Device Address [2017-10-26 05:00:02.324]
I2C Read : 6.Read Data [2017-10-26 05:00:02.324]
I2C Read : 7.Send STOP signal Read Value = 0x9[2017-10-26 05:00:02.326]
-----------I2C Read Operation End----------------[2017-10-26 05:00:02.328]
Read AT24C02 Device Address = A1 , Memery Address = 0 , Length = 10 [2017-10-26 05:00:02.331]
Read Data =          0x0          0x1          0x2          0x3          0x4          0x5          0x6          0x7          0x8          0x9         [2017-10-26 05:00:02.333]


如果将Debug设置为0以后,对AT24C02的写操作都会出现问题。调试了好几天了,实在没办法了,请教大家了!
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165516
金钱
165516
注册时间
2010-12-1
在线时间
2116 小时
发表于 2017-10-27 01:02:13 | 显示全部楼层
帮顶
回复

使用道具 举报

2

主题

756

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
4163
金钱
4163
注册时间
2017-10-24
在线时间
251 小时
发表于 2017-10-27 11:20:27 | 显示全部楼层
试试将原来printf的地方添加一个5~10ms延时吧,例如:

#if Debug
                printf("I2C Read ( Debug ) : 4.I2C2->SR1 = 0x%X \r\n",I2C2->SR1) ;
#else
               delay_ms(10);
#endif

printf函数执行需要不少时间,条件编译除去之后可能造成时序不对。
十六进制带我飞。
回复

使用道具 举报

76

主题

88

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
264
金钱
264
注册时间
2017-10-26
在线时间
12 小时
 楼主| 发表于 2017-10-27 11:35:45 | 显示全部楼层
已经解决了,固件库里面有bug,我重新写了HAL_I2C_Init函数,重构函数如下所示:总算解决了!
HAL_StatusTypeDef HAL_I2C_Init_MY(I2C_HandleTypeDef *hi2c)
{
  uint32_t freqrange = 0U;
  uint32_t pclk1 = 0U;

  /* Check the I2C handle allocation */
  if(hi2c == NULL)
  {
    return HAL_ERROR;
  }

  /* Check the parameters */
  assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  assert_param(IS_I2C_CLOCK_SPEED(hi2c->Init.ClockSpeed));
  assert_param(IS_I2C_DUTY_CYCLE(hi2c->Init.DutyCycle));
  assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
  assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
  assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
  assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
  assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
  assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));

  if(hi2c->State == HAL_I2C_STATE_RESET)
  {
    /* Allocate lock resource and initialize it */
    hi2c->Lock = HAL_UNLOCKED;
    /* Init the low level hardware : GPIO, CLOCK, NVIC */
    HAL_I2C_MspInit(hi2c);
  }

  hi2c->State = HAL_I2C_STATE_BUSY;

  /* Disable the selected I2C peripheral */
  __HAL_I2C_DISABLE(hi2c);

  /* Get PCLK1 frequency */
  pclk1 = HAL_RCC_GetPCLK1Freq();

  /* Calculate frequency range */
  freqrange = I2C_FREQRANGE(pclk1);

  /*---------------------------- I2Cx CR2 Configuration ----------------------*/
  /* Configure I2Cx: Frequency range */
  hi2c->Instance->CR2 = 10;

  /*---------------------------- I2Cx TRISE Configuration --------------------*/
  /* Configure I2Cx: Rise Time */
  hi2c->Instance->TRISE = 3;

  /*---------------------------- I2Cx CCR Configuration ----------------------*/
  /* Configure I2Cx: Speed */
  hi2c->Instance->CCR = 100;

  /*---------------------------- I2Cx CR1 Configuration ----------------------*/
  /* Configure I2Cx: Generalcall and NoStretch mode */
  hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode);

  /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
  /* Configure I2Cx: Own Address1 and addressing mode */
  hi2c->Instance->OAR1 = (hi2c->Init.AddressingMode | hi2c->Init.OwnAddress1);

  /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
  /* Configure I2Cx: Dual mode and Own Address2 */
  hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2);

  /* Enable the selected I2C peripheral */
  __HAL_I2C_ENABLE(hi2c);

  hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  hi2c->State = HAL_I2C_STATE_READY;
  hi2c->PreviousState = HAL_I2C_MODE_NONE;
  hi2c->Mode = HAL_I2C_MODE_NONE;

  return HAL_OK;
}
回复

使用道具 举报

76

主题

88

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
264
金钱
264
注册时间
2017-10-26
在线时间
12 小时
 楼主| 发表于 2017-10-27 11:42:34 | 显示全部楼层
现在关闭Debug以后,串口打印输出的内容也正常了
Write AT24C02 Device Address = A0 , Memery Address = 0 , Length = 100
Writing Data =          0x0          0x1          0x2          0x3          0x4          0x5          0x6          0x7          0x8          0x9          0xA          0xB          0xC          0xD          0xE          0xF          0x10          0x11          0x12          0x13          0x14          0x15          0x16          0x17          0x18          0x19          0x1A          0x1B          0x1C          0x1D          0x1E          0x1F          0x20          0x21          0x22          0x23          0x24          0x25          0x26          0x27          0x28          0x29          0x2A          0x2B          0x2C          0x2D          0x2E          0x2F          0x30          0x31          0x32          0x33          0x34          0x35          0x36          0x37          0x38          0x39          0x3A          0x3B          0x3C          0x3D          0x3E          0x3F          0x40          0x41          0x42          0x43          0x44          0x45          0x46          0x47          0x48          0x49          0x4A          0x4B          0x4C          0x4D          0x4E          0x4F          0x50          0x51          0x52          0x53          0x54          0x55          0x56          0x57          0x58          0x59          0x5A          0x5B          0x5C          0x5D          0x5E          0x5F          0x60          0x61          0x62          0x63        
Read AT24C02 Device Address = A1 , Memery Address = 0 , Length = 200
Read Data =          0x0          0x1          0x2          0x3          0x4          0x5          0x6          0x7          0x8          0x9          0xA          0xB          0xC          0xD          0xE          0xF          0x10          0x11          0x12          0x13          0x14          0x15          0x16          0x17          0x18          0x19          0x1A          0x1B          0x1C          0x1D          0x1E          0x1F          0x20          0x21          0x22          0x23          0x24          0x25          0x26          0x27          0x28          0x29          0x2A          0x2B          0x2C          0x2D          0x2E          0x2F          0x30          0x31          0x32          0x33          0x34          0x35          0x36          0x37          0x38          0x39          0x3A          0x3B          0x3C          0x3D          0x3E          0x3F          0x40          0x41          0x42          0x43          0x44          0x45          0x46          0x47          0x48          0x49          0x4A          0x4B          0x4C          0x4D          0x4E          0x4F          0x50          0x51          0x52          0x53          0x54          0x55          0x56          0x57          0x58          0x59          0x5A          0x5B          0x5C          0x5D          0x5E          0x5F          0x60          0x61          0x62          0x63          0x64          0x65          0x66          0x67          0x68          0x69          0x6A          0x6B          0x6C          0x6D          0x6E          0x6F          0x70          0x71          0x72          0x73          0x74          0x75          0x76          0x77          0x78          0x79          0x7A          0x7B          0x7C          0x7D          0x7E          0x7F          0x80          0x81          0x82          0x83          0x84          0x85          0x86          0x87          0x88          0x89          0x8A          0x8B          0x8C          0x8D          0x8E          0x8F          0x90          0x91          0x92          0x93          0x94          0x95          0x96          0x97          0x98          0x99          0x9A          0x9B          0x9C          0x9D          0x9E          0x9F          0xA0          0xA1          0xA2          0xA3          0xA4          0xA5          0xA6          0xA7          0xA8          0xA9          0xAA          0xAB          0xAC          0xAD          0xAE          0xAF          0xB0          0xB1          0xB2          0xB3          0xB4          0xB5          0xB6          0xB7          0xB8          0xB9          0xBA          0xBB          0xBC          0xBD          0xBE          0xBF          0xC0          0xC1          0xC2          0xC3          0xC4          0xC5          0xC6          0xC7        
回复

使用道具 举报

76

主题

88

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
264
金钱
264
注册时间
2017-10-26
在线时间
12 小时
 楼主| 发表于 2017-10-27 11:55:01 | 显示全部楼层
主要问题是HAL_I2C_Init函数中对于
hi2c->Instance->CR2 = 10;
hi2c->Instance->TRISE = 3;
hi2c->Instance->CCR = 100;
这三个寄存器的配置是有错误的,特别是对于CR2的值配置超出了范围造成的,而且必须结合AT24C02的操作时序进行配置才行。我现在将I2C的操作时序配置为50KHz,SCL上升沿最大为300ns,在while查询语句中加入了超时防护,这样就可以了。
但是总的来说STM32F429的I2C操作过程中SR1寄存器的状态一直不太稳定,SR1寄存器中的值可能会与数据手册中说的不一样,因为我项目赶进度,这个问题留到以后再来查证了!
回复

使用道具 举报

3

主题

31

帖子

0

精华

初级会员

Rank: 2

积分
73
金钱
73
注册时间
2017-10-27
在线时间
2 小时
发表于 2017-10-27 16:50:33 | 显示全部楼层
学习,帮顶
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-18 23:15

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表