搜索
bottom↓
回复: 5

有谁做过I2C

[复制链接]

出0入0汤圆

发表于 2009-2-19 15:24:20 | 显示全部楼层 |阅读模式
利用NEC单片机(78F0513)对AT24C02进行写数据,例如对AT24C02中的0x02地址写入数据0x20,再对该地址读取数据,是否为0x20
我是菜鸟,谁能分享例子菜鸟贴呢?

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 2009-2-19 20:17:08 | 显示全部楼层
可以用模拟i2c来做

io端口属性设置一下

出0入0汤圆

发表于 2009-2-21 22:25:57 | 显示全部楼层
....三言两语还真不好说。先看看AT24C02的和I2C的工作原理,再去网上搜一段I2C读写E2PROM的源代码,很容易理解。

出0入0汤圆

发表于 2009-2-22 11:23:41 | 显示全部楼层
给你参考一下,适用24c02 24c01A
#define SDA             P2.1
#define SDA_DIR         PM2.1
#define SCL             P4.2

//port direction
#define INPUT           1
#define OUTPUT          0

//for AT2402 write or read command
#define W_ADD_COM       0xA0    //1010 A2 A1 A0   0
#define R_ADD_COM       0xA1    //1010 A2 A1 A0   1
//the first address of data writed to 2402
#define RomAddress      0x02

//-----------------------------------------------------------------------------
// Function: I2C operation for at2402
//
//
//
//-----------------------------------------------------------------------------
//start I2C
void I2CStart(void)
{
    SCL = 0;
    SDA = 1;
    SCL = 1;
    NOP();NOP();
    SDA = 0;
    NOP();NOP();
    SCL = 0;
}
//stop I2C
void I2CStop(void)
{
    SDA = 0;
    SCL = 1;
    NOP();
    NOP();
    SDA = 1;
}
//ask
void I2CAck(void)
{
    SDA = 0;
    SCL = 1;
    NOP();
    NOP();
    SCL = 0;
    SDA = 1;
}
//noack
void I2CNoAck(void)
{
    SDA = 1;
    SCL = 1;
    NOP();
    NOP();
    SCL = 0;
}

char I2CTestAck(void)
{
    char ErrorBit;

    SDA = 1;
    SDA_DIR = INPUT;        //change SDA direction to input
    SCL = 1;
    NOP();NOP();
    NOP();NOP();
    NOP();NOP();
    ErrorBit = SDA;         // 0 is ask; 1 is noack
    SCL = 0;
    SDA_DIR = OUTPUT;       //change SDA direction to output
    return(ErrorBit);
}

/* write one byte to I2C */
void I2CWrite8Bit(unsigned char data)
{
    unsigned char i;
    SDA_DIR = OUTPUT;     //change SDA direction to output
    //SCL must is low level

    for(i = 8; i != 0; i--)
    {
        if(data & 0x80)
        {
            SDA = 1;
        }
        else
        {
            SDA = 0;
        }
        SCL = 1;
        data = data << 1;
        SCL = 0;
    }
}

/* read one byte from  I2C */
unsigned char I2CRead8Bit(void)
{
    unsigned char i, rbyte = 0;
    SDA_DIR = INPUT;        //change SDA direction to input

    for(i = 8; i != 0; i--)
    {
        SCL = 1;
        rbyte = rbyte << 1;
        if(SDA)
        {
            rbyte |= 0x01;
        }
        else
        {
            rbyte |= 0x00;
        }
        SCL = 0;
    }
    SDA_DIR = OUTPUT;     //change SDA direction to output
    return(rbyte);
}

void write2402(void)
{
    I2CStart();                 //start
    I2CWrite8Bit(W_ADD_COM);    //write the address of AT2401a
    if(I2CTestAck() == 1)  goto i2cerror;

    I2CWrite8Bit(RomAddress);
    if(I2CTestAck() == 1)  goto i2cerror;

    I2CWrite8Bit(67);
    if(I2CTestAck() == 1)  goto i2cerror;

    I2CWrite8Bit(12);
    if(I2CTestAck() == 1)  goto i2cerror;

    I2CWrite8Bit(134);
    if(I2CTestAck() == 1)  goto i2cerror;

    I2CWrite8Bit(12);
    if(I2CTestAck() == 1)  goto i2cerror;

    I2CStop();
    Delay10us(1000);
    return;

i2cerror:
    I2CStop();
}

void Read24c02(unsigned char *RamAddress, unsigned char bytes)
{
    I2CStart();
    I2CWrite8Bit(W_ADD_COM);
    if(I2CTestAck() == 1)  goto i2cend;

    I2CWrite8Bit(RomAddress);
    if(I2CTestAck() == 1)  goto i2cend;

    I2CStart();
    I2CWrite8Bit(R_ADD_COM);
    if(I2CTestAck() == 1)  goto i2cend;

    while(bytes != 0)
    {
        *RamAddress = I2CRead8Bit();
        I2CAck();       //
        RamAddress++;
        bytes--;
    }
    *RamAddress = I2CRead8Bit();
    I2CNoAck();         //

i2cend:
    I2CStop();          //
}

出0入0汤圆

 楼主| 发表于 2009-2-24 12:11:15 | 显示全部楼层
谢谢楼上的例子,让我受益不少

出0入0汤圆

发表于 2009-3-5 22:39:23 | 显示全部楼层
当你作了一次写操作以后地址会自动增加的
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-20 18:16

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表