⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eep24c32.c

📁 keilc eeprom 8051单晶片应用Keil c语言_记忆IC 24C32的应用
💻 C
字号:

/*****************************/
//24c32 x 1
void Eeprom24c32WriteByte_1(Word addr,Byte value)
{
    I2cStart( );
    I2cSentByte(0xA0);
    
    //Send Address
    I2cSentByte (addr >> 8);			// Send HIBYTE address
    I2cSentByte (addr & 0xff);			// Send LOBYTE address 
    I2cSentByte (value);
    I2cStop( );
}
 
//24c32 x 2
void Eeprom24c32WriteByte_2(Word addr,Byte value)
{
    I2cStart( );
    
    if (addr < 0x8000)
       I2cSentByte(0xA0);
    else
       I2cSentByte(0xA2);
       
    //Send Address
    I2cSentByte ((Byte) ((addr >> 8) & 0x7f));	// Send HIBYTE address
    I2cSentByte ((Byte) addr);			// Send LOBYTE address 
    I2cSentByte (value);
    I2cStop( );
} 

/*****************************/
//24c32 x 1
void Eeprom24c32WriteMulti_1(Word addr,Byte count)
{
    Byte i;
    	
    I2cStart( );
    I2cSentByte(0xA0);
    
    //Send Address
    I2cSentByte (addr >> 8);			// Send HIBYTE address
    I2cSentByte (addr & 0xff);			// Send LOBYTE address 
    
    for(i=0;i<count;i++)
       I2cSentByte (TrmBuf[i]);
    I2cStop( );
} 

//24c32 x 2
void Eeprom24c32WriteMulti_2(Word addr,Byte count)
{
    Byte i;
    	
    I2cStart( );
    
    if (addr < 0x8000)
       I2cSentByte(0xA0);
    else
       I2cSentByte(0xA2);
       
    //Send Address
    I2cSentByte ((Byte) ((addr >> 8) & 0x7f));
    I2cSentByte ((Byte) addr);
    for(i=0;i<count;i++)
       I2cSentByte (TrmBuf[i]);
       
    I2cStop( );
} 

/*****************************/
//24c32 x 1
Byte Eeprom24c32ReadByte_1(Word addr)
{
    Byte temp;

    I2cStart( );
    I2cSentByte(0xA0);
    I2cSentByte (addr >> 8);			// Send HIBYTE address
    I2cSentByte (addr & 0xff);			// Send LOBYTE address 

    I2cStart( );
    I2cSentByte(0xA1);
    temp = I2cReceiveByte( );
    SendAcknowledge(1);    
    I2cStop( );
    return temp;
}

//24c32 x 2
Byte Eeprom24c32ReadByte_2(Word addr)
{
    Byte temp;

    //Reading from the second Eeprom if MSB is set. (Write Address)
    //Otherwise reading from the first. (Write Address)
    if (addr & 0x8000) temp = 0xA2;
    else temp = 0xA0;
    
    I2cStart( );
    I2cSentByte(temp);
    I2cSentByte( (Byte) ((addr >> 8) & 0x7f) );
    I2cSentByte( (Byte) addr );
    
    I2cStart( );
    I2cSentByte(temp|0x01);
    temp = I2cReceiveByte( );
    SendAcknowledge(1);
    I2cStop( );
    return temp;
}

/*****************************/
//24c32 x 1
Word Eeprom24c32ReadWord_1(Word addr)
{
    Byte temp;
    Word value;

    I2cStart( );
    I2cSentByte(0xA0);
    I2cSentByte (addr >> 8);			// Send HIBYTE address
    I2cSentByte (addr & 0xff);			// Send LOBYTE address 
    
    I2cStart( );
    I2cSentByte(0xA1);
    value = I2cReceiveByte( );			// hi-byte
    SendAcknowledge(0);
    value <<= 8;
    
    temp = I2cReceiveByte( );			//low-byte 
    value |= temp;    
    SendAcknowledge(1);
    I2cStop( );	
    return value;
}

//24c32 x 2
Word Eeprom24c32ReadWord_2(Word addr)
{
    Byte temp;
    Word value;

    I2cStart( );
    
    if (addr < 0x8000)
       I2cSentByte(0xA0);
    else
       I2cSentByte(0xA2);
       
    I2cSentByte( (Byte) ((addr >> 8) & 0x7f) );
    I2cSentByte( (Byte) addr );

    I2cStart( );	
    if (addr < 0x8000)
       I2cSentByte(0xA0|0x01);
    else
       I2cSentByte(0xA2|0x01);    

    value = I2cReceiveByte( );
    SendAcknowledge(0);
    value <<= 8;
    
    temp = I2cReceiveByte( );
    value |= temp;    
    SendAcknowledge(1);
    I2cStop( );
    return value;
}

/*****************************/
//24c32 x 1
void Eeprom24c32ReadMulti_1(Word addr,Byte count)
{
    Byte i;

    I2cStart( );
    I2cSentByte(0xA0);
    I2cSentByte (addr >> 8);			// Send HIBYTE address
    I2cSentByte (addr & 0xff);			// Send LOBYTE address 

    I2cStart( );
    I2cSentByte(0xA1);

    for(i=0;i<count;i++)
    {
       TrmBuf[i] = I2cReceiveByte( );
       if (i != (count - 1))  SendAcknowledge(0);
    }
    SendAcknowledge(1);
    I2cStop( );
}

//24c32 x 2
void Eeprom24c32ReadMulti_2(Word addr,Byte count)
{
    Byte i;

    I2cStart( );
    
    if (addr < 0x8000)
       I2cSentByte(0xA0);
    else
       I2cSentByte(0xA2);
    I2cSentByte( (Byte) ((addr >> 8) & 0x7f) );
    I2cSentByte( (Byte) addr );

    I2cStart( );	
    if (addr < 0x8000)
       I2cSentByte(0xA0|0x01);
    else
       I2cSentByte(0xA2|0x01);    

    for(i=0;i<count;i++)
    {
       TrmBuf[i] = I2cReceiveByte( );
       if (i != (count - 1))  SendAcknowledge(0);
    }
    SendAcknowledge(1);
    I2cStop( );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -