I2C EEPROM

Started by mpcomputer, February 01, 2013, 10:09:17 PM

Previous topic - Next topic

mpcomputer

เก็บค่า long int แบบตัวแปรหลายตัว ลง EEPROM ภายนอกครับ 24lc256 มีวิธีไหนบ้างครับ
struct real         
{
  int rcoin;
  int rqp;
  long int rpl1;
  long int rpl2;
  long int rpl3;
  long int rpl4;
  long int rpl5;
  long int rpl6;
  long int rpl7;
  long int rpl8;
  long int rpl9;
  long int rpl10;
}
rlist;

ไม่เข้าใจการเขียนโค๊ดว่าต้องเขียนลงยังไงอ่านยังไงครับ ขอบคุณครับ

firmware.c

ก่อนอื่นเลยต้งถามก่อนว่าติดต่อ i2c EEPROM 24c256 ได้หรือยังครับ ถ้าติดต่อได้แล้วต่อไปวิธีการเก็บต้องแยกเก็บทีละ byte อาจจะใช้การ shift bit หรือเทคนิคการใช้ union ก็ได้แล้วแต่ความเหมาะสม
IAR Embedded Workbench for ARM
AVR-Studio + AVR-GCC
CodeBlocks + MinGw
CodeBlocks + Gtk+

mpcomputer

ยังไม่เข้าใจเลยครับ ขอแนวทางอีกนิดครับ ขอบคุณครับ


firmware.c

EEPROM_write_long(unsigned long in_eep) {
    union {
       unsigned long  ulong;
       struct {
            unsigned char lo_lo;
            unsigned char lo_hi;
            unsigned char hi_lo;
            unsigned char hi_hi;
       } byte;
    } eep;
     
    eep.ulong = in_eep;
    Eeprom_write_byte(0, eep.byte.lo_lo);
    Eeprom_write_byte(1, eep.byte.lo_hi);
    .........
    .........
}
IAR Embedded Workbench for ARM
AVR-Studio + AVR-GCC
CodeBlocks + MinGw
CodeBlocks + Gtk+

firmware.c

ถ้าเป็นแบบอย่างด้านนที่คุณเพิ่ง edit คำถามไป ผมแนะนำให้ pack ลง structure หรือไม่ก็ array
IAR Embedded Workbench for ARM
AVR-Studio + AVR-GCC
CodeBlocks + MinGw
CodeBlocks + Gtk+

firmware.c

struct real {
  int rcoin;
  int rqp;
  long int rpl1;
  long int rpl2;
  long int rpl3;
  long int rpl4;
  long int rpl5;
  long int rpl6;
  long int rpl7;
  long int rpl8;
  long int rpl9;
  long int rpl10;
} rlist;

unsigned short eep_size;
unsigned char  eep_adr;
unsigned char *p_eep_dat;

eep_size = sizeof(rlist);
p_eep_dat = (unsigned char *)&rlist;
do {
eeprom_write_byte(eep_adr, *p_eep_dat);
eep_adr++; p_eep_dat++;
} while (--eep_size);
IAR Embedded Workbench for ARM
AVR-Studio + AVR-GCC
CodeBlocks + MinGw
CodeBlocks + Gtk+

mpcomputer

ขอบคุณครับท่าน firmware.c  ประโยชน์ทั้งนั้นขอเก็บก่อนนะครับ

mpcomputer

#include <WProgram.h>
#define DEVICE 0x50 //this is the device ID from the datasheet of the 24LC256

//in the normal write anything the eeaddress is incrimented after the writing of each byte. The Wire library does this behind the scenes.

template <class T> int eeWrite(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
int i;
Wire.beginTransmission(DEVICE);
Wire.send((int)(ee >> 8)); // MSB
Wire.send((int)(ee & 0xFF)); // LSB
for (i = 0; i < sizeof(value); i++)
Wire.send(*p++);
Wire.endTransmission();
return i;
}

template <class T> int eeRead(int ee, T& value)
{
byte* p = (byte*)(void*)&value;
int i;
Wire.beginTransmission(DEVICE);
Wire.send((int)(ee >> 8)); // MSB
Wire.send((int)(ee & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(DEVICE,sizeof(value));
for (i = 0; i < sizeof(value); i++)
if(Wire.available())
*p++ = Wire.receive();
return i;
}


ผมใช้ไลบรารี่อันนี้ครับ มันเก็บข้อมูล
long int rpl7;
  long int rpl8;
  long int rpl9;
  long int rpl10;
ช่วงนี้ไม่ได้สงสัยมันจะเกินหน้าหรืออะไรนี่แหละครับ
พออ่านตัวอื่นอ่านได้ที่เหลือมันเป็น0ครับ