极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11089|回复: 4

[已解决]用共用体读写EEPROM乱码

[复制链接]
发表于 2014-6-20 09:59:46 | 显示全部楼层 |阅读模式
本帖最后由 陈野猪 于 2014-6-20 22:19 编辑

买了个DUE,但是没有内置的EEPROM,只好用AT24C32,参考坛子里用共用体写了一个读写程序,但是写进去后读出来结果不一样。后来用mega2560测试也不行,各位大大帮我指点一下吧~~
  1. #include <Wire.h> //I2C library

  2. void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
  3.     int rdata = data;
  4.     Wire.beginTransmission(deviceaddress);
  5.     Wire.write((int)(eeaddress >> 8)); // MSB
  6.     Wire.write((int)(eeaddress & 0xFF)); // LSB
  7.     Wire.write(rdata);
  8.     Wire.endTransmission();
  9.   }

  10.   // WARNING: address is a page address, 6-bit end will wrap around
  11.   // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  12.   void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
  13.     Wire.beginTransmission(deviceaddress);
  14.     Wire.write((int)(eeaddresspage >> 8)); // MSB
  15.     Wire.write((int)(eeaddresspage & 0xFF)); // LSB
  16.     byte c;
  17.     for ( c = 0; c < length; c++)
  18.       Wire.write(data[c]);
  19.     Wire.endTransmission();
  20.   }

  21.   byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
  22.     byte rdata = 0xFF;
  23.     Wire.beginTransmission(deviceaddress);
  24.     Wire.write((int)(eeaddress >> 8)); // MSB
  25.     Wire.write((int)(eeaddress & 0xFF)); // LSB
  26.     Wire.endTransmission();
  27.     Wire.requestFrom(deviceaddress,1);
  28.     if (Wire.available()) rdata = Wire.read();
  29.     return rdata;
  30.   }

  31.   // maybe let's not read more than 30 or 32 bytes at a time!
  32.   void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
  33.     Wire.beginTransmission(deviceaddress);
  34.     Wire.write((int)(eeaddress >> 8)); // MSB
  35.     Wire.write((int)(eeaddress & 0xFF)); // LSB
  36.     Wire.endTransmission();
  37.     Wire.requestFrom(deviceaddress,length);
  38.     int c = 0;
  39.     for ( c = 0; c < length; c++ )
  40.       if (Wire.available()) buffer[c] = Wire.read();
  41.   }
  42.   union floatType
  43.   {
  44.     float a;
  45.     byte b[4];
  46.   };
  47.   floatType num1;
  48.   union intType
  49.   {
  50.     int a;
  51.     byte b[2];
  52.   };
  53.   intType int1;
  54.   void setup()
  55.   {
  56.     int1.a=0;
  57.     num1.a=987.65;
  58. //    num1.a=781.03;
  59.     int addr=0,i;   
  60.     Wire.begin(); // initialise the connection
  61.     Serial.begin(9600);   
  62.     Serial.println("Write:");
  63. //    for(int i=0;i<2;i++)
  64. //    {
  65. //      byte temp=num1.b[i];
  66. //      i2c_eeprom_write_byte(0x57, addr++,int1.b[i]);
  67. //    }
  68.      for(int i=0;i<4;i++)
  69.     {
  70.       i2c_eeprom_write_byte(0x57, addr++,num1.b[i]);
  71.     }
  72.     Serial.print('\n');   
  73.   }

  74.   void loop()
  75.   {
  76.     int i,b;
  77.     float num2;
  78.     int int2;
  79.     intType int1;
  80.     floatType temp;
  81.     int addr=0; //first address   
  82.     Serial.println("Read:");
  83.     for(i=0;i<4;i++)
  84.     {     
  85.       temp.b[i] = i2c_eeprom_read_byte(0x57, i); //access an address from the memory
  86.         
  87.     }
  88.     num2=temp.a;
  89.     Serial.println(num2);
  90.      Serial.print('\n');
  91.     delay(2000);
  92.   }
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-6-20 22:19:25 | 显示全部楼层
问题已经解决 在写入时加入延时就可以了
回复 支持 反对

使用道具 举报

发表于 2014-8-29 16:31:05 | 显示全部楼层
陈野猪 发表于 2014-6-20 22:19
问题已经解决 在写入时加入延时就可以了

你好,我copy你的代码,读写都添加延时调试时,串口只循环返回
Read:
nan
何解?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-5 16:09:31 | 显示全部楼层
加饭 发表于 2014-8-29 16:31
你好,我copy你的代码,读写都添加延时调试时,串口只循环返回
Read:
nan

用的什么板子呢?
回复 支持 反对

使用道具 举报

发表于 2014-9-10 13:46:00 | 显示全部楼层
UNO和mega都试过,结果都一样
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 00:46 , Processed in 0.036540 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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