[CW32F030系列] 硬件iic的使用

[复制链接]
724|1
 楼主| 51xlf 发表于 2022-11-23 22:07 | 显示全部楼层 |阅读模式
利用I2C接口,采用轮询方式读写EEPROM芯片
  1. #define TESTI2C  2    //I2C1 = 1   I2C2 = 2

  2. #define  I2C1_SCL_GPIO_PORT       CW_GPIOB
  3. #define  I2C1_SCL_GPIO_PIN        GPIO_PIN_10    //如果改动口线则GPIO初始化代码需要做同步修改
  4. #define  I2C1_SDA_GPIO_PORT       CW_GPIOB   
  5. #define  I2C1_SDA_GPIO_PIN        GPIO_PIN_11    //如果改动口线则GPIO初始化代码需要做同步修改

  6. #define  I2C2_SCL_GPIO_PORT       CW_GPIOB
  7. #define  I2C2_SCL_GPIO_PIN        GPIO_PIN_0     //如果改动口线则GPIO初始化代码需要做同步修改
  8. #define  I2C2_SDA_GPIO_PORT       CW_GPIOB   
  9. #define  I2C2_SDA_GPIO_PIN        GPIO_PIN_1     //如果改动口线则GPIO初始化代码需要做同步修改


  10. //EEPROM内部地址
  11. #define WRITEADDRESS   0      //写入或者读取的EEPROM的地址
  12. #define WriteReadCycle  22    //写读次数,每次写入数据为n+i(n为次数,i=0~7)

  13. uint8_t u8Senddata[8] = {0x66,0x02,0x03,0x04,0x05,0x60,0x70,0x20};
  14. uint8_t u8Senddata2[8] = {0x55,0xAA,0xAA,0x55,0x55,0xAA,0x55,0xAA};
  15. uint8_t u8Recdata[16]={0x00};
  16. uint8_t u8SendLen=0;
  17. uint8_t u8RecvLen=0;
  18. uint8_t Send** = 0,Comm_** = 0;
  19. uint8_t u8recv**=0;
  20. uint8_t u8State = 0;


  21. void RCC_Configuration(void);
  22. void GPIO_Configuration(void);
  23. void NVIC_Configuration(void);


  24. int32_t main(void)
  25. {
  26.                 uint16_t tempcnt = 0 ;
  27.        
  28.           I2C_InitTypeDef I2C_InitStruct;
  29.        
  30.     //时钟初始化
  31.     RCC_Configuration();

  32.     //IO口初始化
  33.     GPIO_Configuration();
  34.    
  35.     //I2C初始化
  36.     I2C_InitStruct.I2C_BaudEn = ENABLE;
  37.                 I2C_InitStruct.I2C_Baud = 0x01;//500K=(8000000/(8*(1+1))
  38.     I2C_InitStruct.I2C_FLT = DISABLE;
  39.     I2C_InitStruct.I2C_AA = DISABLE;
  40.     #if(0X01 == TESTI2C)
  41.         I2C1_DeInit();
  42.         I2C_Master_Init(CW_I2C1,&I2C_InitStruct);//初始化模块
  43.                                 I2C_Cmd(CW_I2C1,ENABLE);  //模块使能
  44.     #elif(0X02 == TESTI2C)
  45.         I2C2_DeInit();
  46.         I2C_Master_Init(CW_I2C2,&I2C_InitStruct);//初始化模块
  47.                           I2C_Cmd(CW_I2C2,ENABLE);  //模块使能
  48.     #endif  
  49.                
  50.                 while(1)
  51.                 {
  52.                         #if(0X01 == TESTI2C)
  53.                                 I2C_MasterWriteEepromData(CW_I2C1,WRITEADDRESS,u8Senddata,8);
  54.                                 FirmwareDelay(3000);
  55.                                 I2C_MasterWriteEepromData(CW_I2C1,WRITEADDRESS+8,u8Senddata2,8);
  56.                                 FirmwareDelay(3000);
  57.                                 I2C_MasterReadEepomData(CW_I2C1,WRITEADDRESS,u8Recdata,16);
  58.                         #elif(0X02 == TESTI2C)
  59.                                 I2C_MasterWriteEepromData(CW_I2C2,WRITEADDRESS,u8Senddata,8);
  60.                                 FirmwareDelay(3000);
  61.                                 I2C_MasterWriteEepromData(CW_I2C2,WRITEADDRESS+8,u8Senddata2,8);
  62.                                 FirmwareDelay(3000);
  63.                                 I2C_MasterReadEepomData(CW_I2C2,WRITEADDRESS,u8Recdata,16);
  64.                         #endif
  65.                         tempcnt++;
  66.                         for(uint8_t i=0;i<8;i++)
  67.                         {
  68.                                 u8Senddata[i] =tempcnt+i;
  69.                         }
  70.                         if(tempcnt >=WriteReadCycle)
  71.                         {
  72.                                 break;
  73.                         }       
  74.                        
  75.                 }
  76.        
  77.     while(1)
  78.     {
  79.     }  
  80. }


iic的IO配置
  1. void GPIO_Configuration(void)
  2. {  
  3.         GPIO_InitTypeDef GPIO_InitStructure;
  4.     #if(0X01 == TESTI2C)
  5.         PB10_AFx_I2C1SCL();
  6.         PB11_AFx_I2C1SDA();
  7.                 GPIO_InitStructure.Pins = I2C1_SCL_GPIO_PIN | I2C1_SDA_GPIO_PIN;
  8.                 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
  9.                 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
  10.                 GPIO_Init(I2C1_SCL_GPIO_PORT, &GPIO_InitStructure);
  11.     #elif(0X02 == TESTI2C)
  12.         PB00_AFx_I2C2SCL();
  13.         PB01_AFx_I2C2SDA();
  14.                 GPIO_InitStructure.Pins = I2C2_SCL_GPIO_PIN | I2C2_SDA_GPIO_PIN;
  15.                 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
  16.                 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
  17.                 GPIO_Init(I2C2_SCL_GPIO_PORT, &GPIO_InitStructure);
  18.     #endif
  19. }



chenjun89 发表于 2022-12-3 18:43 来自手机 | 显示全部楼层
很多时候都是用的软件模拟IIC
您需要登录后才可以回帖 登录 | 注册

本版积分规则

551

主题

9949

帖子

24

粉丝
快速回复 在线客服 返回列表 返回顶部