小哈_abc作者
16年5个月前IP:未同步
249283
34楼
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//
//###############AT24C02读写模块###################//
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//
/*********************************数据定义**********************************/
#define    OP_READ    0xa1        // 器件地址以及读取操作
#define    OP_WRITE 0xa0        // 器件地址以及写入操作

/*******************************共阳LED段码表*******************************/
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

/********************************定义全局变量*******************************/
int eepromdata;                //从EEPROM里读出来的数据

/*********************************端口定义**********************************/
sbit SDA = P3^4;
sbit SCL = P3^3;

/****************************************************************************/
void delayms(unsigned char ms)    
{
    unsigned char i;
    while(ms--)
    {
        for(i = 0; i < 120; i++);
    }
}

/****************************************************************************/
void start()
{
    SDA = 1;
    SCL = 1;
    _nop_();
    _nop_();
    SDA = 0;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    SCL = 0;
}


/****************************************************************************/
void stop()
{
    SDA = 0;
    _nop_();
    _nop_();
    SCL = 1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    SDA = 1;
}


/****************************************************************************/
unsigned char shin()

{
    unsigned char i,read_data;
    for(i = 0; i < 8; i++)
    {
        SCL = 1;
        read_data <<= 1;
        read_data |= (unsigned char)SDA;
        SCL = 0;
    }
    return(read_data);
}


/****************************************************************************/
bit shout(unsigned char write_data)
{
    unsigned char i;
    bit ack_bit;
    for(i = 0; i < 8; i++)        
    {
        SDA = (bit)(write_data & 0x80);
        _nop_();
        SCL = 1;
        _nop_();
        _nop_();
        SCL = 0;
        write_data <<= 1;
    }
    SDA = 1;            
    _nop_();
    _nop_();
    SCL = 1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    ack_bit = SDA;            // 读取应答
    SCL = 0;
    return ack_bit;            // 返回AT24Cxx应答位
}

/****************************************************************************/
void write_byte(unsigned char addr, unsigned char write_data)
{
    start();
    shout(OP_WRITE);
    shout(addr);
    shout(write_data);
    stop();
    delayms(10);        
}
/****************************************************************************/
unsigned char read_current()
{
    unsigned char read_data;
    start();
    shout(OP_READ);
    read_data = shin();
    stop();
    return read_data;
}

/****************************************************************************/
unsigned char read_random(unsigned char random_addr)
{
    start();
    shout(OP_WRITE);
    shout(random_addr);
    return(read_current());
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//
//###############AT24C02读写模块###################//
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//
引用时先拉高
    SDA = 1;
    SCL = 1;

如想向AT24C02第一位写入2
                         write_byte(1, 2);    //    write_byte(地址, 数据);    一次写一字节,注意不要超值
同理向AT24C02第二位写入温度值的十位
              write_byte(2, shi);    
    
如果想读出AT24C02第一位数据,可以将事先定义的变量eepromdata赋值为独处的数据
    eepromdata= read_random(1);
+700
科创币
warmonkey
2010/08/16
评论
文号 / 249283

千古风流
名片发私信
学术分 3
总主题 42 帖总回复 759 楼拥有证书:学者 笔友
注册于 2009/07/25 13:54:06最后登录 2023/11/27 07:24:59
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:0

个人简介

暂未填写
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的