查看: 1865|回复: 1

【赚周年币】技术贴Week 2-Day6-----mbed之I2C读写AT24c02

[复制链接]
  • TA的每日心情
    奋斗
    2020-5-27 15:41
  • 签到天数: 868 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2017-1-10 09:41:38 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 wambob 于 2017-1-10 09:52 编辑

         首次用mbed驱动 EEPROM失败后,自然是查找原因了,硬件问题,软件问题。
    先把硬件问题除外,因为这个是最近的开发板。参考了各种资源,反复验证代码,还是不成功,再次详细查看AT24c02的手册。
    Features
    • Low-voltage and Standard-voltage Operation
    – 2.7 (VCC = 2.7V to 5.5V)
    – 1.8 (VCC = 1.8V to 5.5V)
    • Internally Organized 128 x 8 (1K), 256 x 8 (2K), 512 x 8 (4K),
    1024 x 8 (8K) or 2048 x 8 (16K)
    • Two-wire Serial Interface
    • Schmitt Trigger, Filtered Inputs for Noise Suppression
    • Bidirectional Data Transfer Protocol
    • 100 kHz (1.8V) and 400 kHz (2.7V, 5V) Compatibility
    • Write Protect Pin for Hardware Data Protection
    • 8-byte Page (1K, 2K), 16-byte Page (4K, 8K, 16K) Write Modes
    • Partial Page Writes Allowed
    • Self-timed Write Cycle (5 ms max)
    • High-reliability
    – Endurance: 1 Million Write Cycles
    – Data Retention: 100 Years
    • Automotive Grade and Lead-free/Halogen-free Devices Available
    • 8-lead PDIP, 8-lead JEDEC SOIC, 8-lead MAP, 5-lead SOT23,
    8-lead TSSOP and 8-ball dBGA2 Packages
    • Die Sales: Wafer Form, Waffle Pack and Bumped Wafers
    其中一句 100KHz 和400KHz兼容
    我记得我是使用的400KHz,有问题吗?
    为了验证,我修改时钟为200kHz:
    1. i2c_master.frequency(200000);
    复制代码
    先向24c02读写写一个字节验证,令人欣喜,读写正确,再次读写整个24c02,也正确
    想看个串口结果
    1.JPG
    代码
    1. #include "mbed.h"
    2. Serial pc(P0_4,P0_0);
    3. I2C i2c_master(P0_11,P0_10);
    4. char buff[256];
    5. char buff1[256];
    6. const char senddata=0xaa;
    7. char readdata;
    8. int main() {
    9.      int i;
    10.      i2c_master.frequency(200000);
    11.      pc.printf(" write data:%x \r\n",senddata);
    12.      i2c_master.start();
    13.      i2c_master.write(0xA0);
    14.      i2c_master.write(0x1);
    15.      i2c_master.write(senddata);
    16.      i2c_master.stop();
    17.      wait(0.02);
    18.      i2c_master.start();
    19.      i2c_master.write(0xA0);
    20.      i2c_master.write(0x1);   
    21.      i2c_master.start();
    22.      i2c_master.write(0xA1);
    23.      readdata=i2c_master.read(1);
    24.      i2c_master.stop();
    25.      pc.printf(" read data:%x \r\n",readdata);
    26.      pc.printf(" write \r\n");
    27.      for(i=0;i<256;i++){        
    28.         buff[i] =i;   
    29.         pc.printf("0x%02x  ",buff[i]);   
    30.         i2c_master.start();
    31.         i2c_master.write(0xA0);
    32.         i2c_master.write(i);
    33.          i2c_master.write(buff[i]);
    34.          i2c_master.stop();
    35.         if(i%16==15)
    36.         pc.printf("\r\n");         
    37.         }  
    38.         wait(0.2);   
    39.     pc.printf(" read \r\n");
    40.     for(i=0;i<256;i++){  
    41.           i2c_master.start();
    42.      i2c_master.write(0xA0);
    43.      i2c_master.write(i);
    44.       i2c_master.start();
    45.       i2c_master.write(0xA1);
    46.        buff1[i] =i2c_master.read(1);
    47.      i2c_master.stop();      
    48.         pc.printf("0x%02x  ",buff1[i]);
    49.         if(i%16==15)
    50.         pc.printf("\r\n");         
    51.       }     
    52.     while(1) {
    53.         
    54.     }
    55. }
    复制代码
    400KHz真的不可以吗。我又改成400kHz。编译下载,打开串口助手,复位开发板
    串口显示:
    2.JPG
    读数大部分正确。,但只是头2个字节有问题。使用200kHz则没这个问题。

    评分

    参与人数 1 +63 收起 理由
    EEboard爱板网 + 63 3周发帖养成记 奖励

    查看全部评分

    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-1-27 17:10
  • 签到天数: 790 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2017-1-10 15:55:06 | 显示全部楼层
    谢谢分享   
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-26 07:25 , Processed in 0.130345 second(s), 19 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.