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

📄 eeprom.c

📁 AT24C512的读写操作
💻 C
字号:
/***********************************************************************************************
名    称:写入一个字节到24c512并读出来验证
功能描述: 我们将24c512的两条总线接在了P36和P37上,因此,必须先定义:
          sbit SCL=P3^6; sbit SDA=P3^7; 在这个试验中,我们写入了一组字节数值到24c512的0x0081为首的位置。
          然后在将其读出并在P1口显示
创建日期:2009-04-25
***********************************************************************************************/
#include <reg51.h> //包括一个51标准内核的头文件
#define uchar unsigned char //定义一下方便使用 
#define uint unsigned int 
#define ulong unsigned long 


#define WriteDeviceAddress 0xa0 //定义器件在IIC总线中的地址 
#define ReadDviceAddress 0xa1
sbit SCL=P3^6; 
sbit SDA=P3^7;

uchar data[6]={0x01;0x02;0x03;0x04;0x05;0x06}
uchar buf[6];
/************开始信号***************/
void I2cStart(void)
{
    SCL=1;
    SDA=1;
    NOP;
    SDA=0;
    NOP;
    SCL=0;
}
/**********停止信号*************/
void I2cStop(void)
{
    SDA=0;
    SCL=1;
    NOP;
    SDA=1;
    NOP;
    SCL=0;    
}

/************应答信号**************/
void I2cAck(bit ACK)
{
    SDA=ACK;
    NOP;    
    SCL=1;
    NOP;    
    SCL=0;
    SDA=1;
}
/**************向EEPROM写入一个字节*******************/
void I2cWriteByte(unsigned char wbyte)
{
    unsigned char i;
    for(i=0;i<8;i++)
    {
        wbyte<<=1;
        if(CY)SDA=1;
        else SDA=0;
        NOP;
        SCL=1;
        NOP;
        SCL=0;
    }
    SDA=1;
    NOP;
    SCL=1;
    while(SDA);
    SCL=0;
}
/**************从EEPROM读出一个字节******************/
unsigned char I2cReadByte(void)
{
    unsigned char i,rbyte;
    for(i=8;i--;)
    {
        SCL=1;
        rbyte<<=1;
        if(SDA)rbyte++;
        SCL=0;
    }
    return rbyte;
}

/************************向EEPROM指定地址写入一个字节********************************/
void EepromByteWrite(unsigned int addr,unsigned char wdata)
{
    I2cStart();
    I2cWriteByte(0xa0);
    I2cWriteByte(*(char*)&addr);
    I2cWriteByte(*(1+(char*)&addr));
    I2cWriteByte(wdata);
    I2cStop();
    delay_ms(10);
}
/************************从EEPROM指定地址读出一个字节********************************/
unsigned char EepromByteRead(unsigned int addr)
{
    unsigned char i;
    I2cStart();
    I2cWriteByte(0xa0);    
    I2cWriteByte(*(char*)&addr);
    I2cWriteByte(*(1+(char*)&addr));

    I2cStart();
    I2cWriteByte(0xa1);
    i=I2cReadByte();
    I2cStop();
    return i;    
}
/************************延时 n 毫秒********************************/
void delay_ms(unsigned char delaytimes)
{
    unsigned char dely;
    while(delaytimes--)
    {
        dely=154;
        for (;--dely;);
    }
}
/************************主函数****************************/
main()
{  
  uchar i;
  uint p;
  p=0x0081;   
  for(i=0;i<6;i++)
  {
    EepromSequentialWrite(p++,data[i]);	//从0x0081开始存入6个数据
   }
  delay(100);   //延时一段时间后开始读取刚才写入的数据
  p=0x0081;
  for(i=0;i<6;i++)
   {
   buf[i]=EepromSequentialRead(p++); //从0x0081开始读取数据并存入数据buf数组单元
   P1=buf[i];		 //取出一个数据后就送P1口显示  P1口接8个LED指示灯
   delay(5000);		 //延时	5 秒以便观察,接着接收显示下一个数
   }

 }

⌨️ 快捷键说明

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