stm8s读写24c64不成功,有源码,请原子哥指点一下

2019-07-19 21:10发布


//---初始化-------------------------------------------------//
void I2C_EEInit(void)
{
 u8 Input_Clock = 0x0;
 Input_Clock = CLK_GetClockFreq()/1000000; 
 I2C_Cmd(ENABLE);
 I2C_Init(I2C_Speed, I2C1_SLAVE_ADDRESS7, I2C_DUTYCYCLE_2,I2C_ACK_CURR, I2C_ADDMODE_7BIT, Input_Clock);
} //-----向器件指定地址写一个字节---------------------------------------//
void I2C_EE_ByteWrite(u16 WriteAddr,u8 Data)
{
 I2C_GenerateSTART(ENABLE);while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
 I2C_Send7bitAddress(0xa0,I2C_DIRECTION_TX);  while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ));
 I2C_SendData(WriteAddr>>8);while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
 I2C_SendData(WriteAddr&0xff);while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
 I2C_GenerateSTART(ENABLE); while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)); 
 I2C_Send7bitAddress(0xa0, I2C_DIRECTION_RX);while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
 I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
 I2C_SendData(Data); while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
 I2C_GenerateSTOP(ENABLE);
}
void I2C_EE_BufferWrite(u8 * pBuffer, u16 WriteAddr, u16 NumByteToWrite )
{
 u16 i;
 for(i=0;i<NumByteToWrite;i++)
 {
  I2C_EE_ByteWrite(WriteAddr+i,* (pBuffer+i));
 }
} //读回存放数组,器件ID,寄存器起始地址,数据数量
void I2C_EE_BufferRead(u8* pBuffer,u8 ReadAddr, u8 NumByteToRead)
{  
 while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
 I2C_GenerateSTART(ENABLE); while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT )); 
 I2C_Send7bitAddress(0xa0, I2C_DIRECTION_TX); while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));  I2C_Cmd(ENABLE);
 I2C_SendData(ReadAddr>>8);while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
 I2C_SendData(ReadAddr&0xff);while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
 I2C_GenerateSTART(ENABLE); while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
 I2C_Send7bitAddress(0xa0, I2C_DIRECTION_RX); while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));  while(NumByteToRead) 
 {
  if(NumByteToRead == 1)
  {
   I2C_AcknowledgeConfig(I2C_ACK_NONE);
   I2C_GenerateSTOP(ENABLE);
  }   if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED)) 
  {      
   *pBuffer = I2C_ReceiveData();
   pBuffer++; 
   NumByteToRead--;       
  }  
 }
 I2C_AcknowledgeConfig(I2C_ACK_CURR);
}
固件库 version V2.1.0
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
9条回答
yzk
2019-07-20 19:52

回复【5楼】正点原子:
是stm8s的代码,注释还没改过来
---------------------------------

SDA_OUT();     
IIC_SDA=1;       
IIC_SCL=1;    
IIC_SDA=0;       
IIC_SCL=0; 
SDA_IN(); 

这个定义怎样写的?

我试了试不行,不知写入成功没有,读出全是0。

我的代码把定义改了一下。

u8 READ_SDA(void)
{
    if(GPIOB->IDR&GPIO_PIN_5)
     return 1;
    else
     return 0;
     
}
void  IIC_SCL(u8 bit)
{
  if(bit==1)
    GPIOB->ODR |= (uint8_t)(GPIO_PIN_4);
  else
    GPIOB->ODR &= (~((uint8_t)(GPIO_PIN_4)));
}
void  IIC_SDA(u8 bit)
{
  if(bit==1)
    GPIOB->ODR |= (uint8_t)(GPIO_PIN_5);
  else
    GPIOB->ODR &= (~((uint8_t)(GPIO_PIN_5)));
}

void  SDA_OUT(void)
{

  GPIOB->ODR |= (uint8_t)(GPIO_PIN_5);
  GPIOB->CR1 &= (~((uint8_t)(GPIO_PIN_5)));
  GPIOB->CR2 &= (~((uint8_t)(GPIO_PIN_5))); 
  GPIOB->DDR |= ((uint8_t)GPIO_PIN_5);
}
void  SDA_IN(void)
{
  GPIOB->ODR |= (uint8_t)(GPIO_PIN_5);
  GPIOB->CR1 &= (~((uint8_t)(GPIO_PIN_5)));
  GPIOB->CR2 &= (~((uint8_t)(GPIO_PIN_5))); 
  GPIOB->DDR &= (~((uint8_t)(GPIO_PIN_5)));
}

void delay_us(u8 us)
{
 u8 ti; 
 while(us--)
 {
  ti=0x1f;
  while(ti--) asm("nop");
 }  
   
}
//初始化IIC
void IIC_Init(void)
{     
  GPIOB->CR1 &= (~((uint8_t)(GPIO_PIN_4)));
  GPIOB->CR2 &= (~((uint8_t)(GPIO_PIN_4)));  
  GPIOB->DDR |= (uint8_t)GPIO_PIN_4;
  GPIOB->ODR |= (uint8_t)(GPIO_PIN_4);
 
  GPIOB->CR1 &= (~((uint8_t)(GPIO_PIN_5)));
  GPIOB->CR2 &= (~((uint8_t)(GPIO_PIN_5)));  
  GPIOB->DDR |= (uint8_t)GPIO_PIN_5;
  GPIOB->ODR |= (uint8_t)(GPIO_PIN_5);
}


                    AT24CXX_Write(0,FLASH_Buffer,16); //写入

                    AT24CXX_Read(0,FLASH_Buffer,16);//读出

一周热门 更多>