Electoday 2025

ไมโครคอนโทรลเลอร์ => AVR and Arduino => Topic started by: Tikki on January 09, 2015, 02:11:15 AM

Title: 24lc256 ข้อมูลหายหลังจากตัดไฟเลี้ยง
Post by: Tikki on January 09, 2015, 02:11:15 AM
ผมต่อ Arduino pro mini เข้ากับ 24lc256
การเขียนข้อมูลลงไปแล้วอ่านกลับขึ้นมาเป็นไปด้วยดี แต่พอตัดไฟเลี้ยงวงจรออก หรือกด reset ที่ arduino
ข้อมูลที่เก็บค้างไว้ใน 24lc256 จะหายเกลี้ยงเลย ใครพอจะทราบบ้างครับว่าเป็นที่อะไร เกิดจากต่อวงจรผิดรึเปล่า


PS.ผมต่อวงจรตามในรูปเลยครับ เห็นมีบางวงจรต่อ c 0.1 uf พ่วงที่ขาV+ ไปลง gnd ไม่รู้ว่าต่อไว้ทำไม หรือว่าต้องต่อแบบนั้นถึงจะถูกครับ
Title: Re: 24lc256 ข้อมูลหายหลังจากตัดไฟเลี้ยง
Post by: RoLRoR on January 09, 2015, 05:59:03 AM
ต่อ C ค่า 0.1uF เพื่อทำหน้าที่ decoupling
ผมเข้าใจว่า เป็น noise fillter ครับ

สายไฟจากบอร์ด arduino ถึง 24LC256 ยาว
ต่อ C ค่า 10uF คร่อมขา Vcc-Vss
ของ 24LC256 ด้วยอีกตัว ช่วยไฟนิ่งดี.

เพื่อคงสถานะลอจิกไว้.ที่ขา SCL และ SDA
ใส่ R อีก2ตัว ค่า 4.7k-10k ต่อ Pull-Up
ไปขา Vcc ของ 24LC256 ทั้งสองขาด้วย
เพราะตอนที่ Arduino เริ่มทำงาน
ขา Ax ที่ใช้ ยังไม่ได้เซ็ตเป็นขา output

และถ้ายังเป็นอยู่...
ดูที่รูปการต่อวงจรจริง กับ โปรแกรมอีกทีครับ
Title: Re: 24lc256 ข้อมูลหายหลังจากตัดไฟเลี้ยง
Post by: Tikki on January 09, 2015, 11:00:25 AM
ขอบคุณครับ
ผมลองต่อตามที่ว่ามาแล้ว พอตัดไฟเลี้ยงก็ยังหายครับ มันเป็นไปได้ไหมว่าจังหวะที่เราตัดไฟ หรือเริ่มทำงาน Arduino มันไปรีเซตตัว 24lc256 หน่วยความจำเลยหายไป

อันนี้เป็นโค้ดที่ผมใช้อ่านและเขียนครับ

Quote
void writeEEPROM(int deviceaddress, unsigned int eeaddress, char* data)
{
   // Uses Page Write for 24LC256
   // Allows for 64 byte page boundary
   // Splits string into max 16 byte writes
   unsigned char i = 0, counter = 0;
   unsigned int  address;
   unsigned int  page_space;
   unsigned int  page = 0;
   unsigned int  num_writes;
   unsigned int  data_len = 0;
   unsigned char first_write_size;
   unsigned char last_write_size;
   unsigned char write_size;

   // Calculate length of data
   do{ data_len++; } while (data[data_len]);

   // Calculate space available in first page
   page_space = int(((eeaddress / 64) + 1) * 64) - eeaddress;

   // Calculate first write size
   if (page_space>16){
      first_write_size = page_space - ((page_space / 16) * 16);
      if (first_write_size == 0) first_write_size = 16;
   }
   else
      first_write_size = page_space;

   // calculate size of last write 
   if (data_len>first_write_size)
      last_write_size = (data_len - first_write_size) % 16;

   // Calculate how many writes we need
   if (data_len>first_write_size)
      num_writes = ((data_len - first_write_size) / 16) + 2;
   else
      num_writes = 1;

   i = 0;
   address = eeaddress;
   for (page = 0; page<num_writes; page++)
   {
      if (page == 0) write_size = first_write_size;
      else if (page == (num_writes - 1)) write_size = last_write_size;
      else write_size = 16;

      Wire.beginTransmission(deviceaddress);
      Wire.write((int)((address) >> 8));   // MSB
      Wire.write((int)((address)& 0xFF)); // LSB
      counter = 0;
      do{
         Wire.write((byte)data);
         i++;
         counter++;
      } while ((data) && (counter<write_size));
      Wire.endTransmission();
      address += write_size;   // Increment address for next write

      delay(6);  // needs 5ms for page write
   }
}
void readEEPROM(int deviceaddress, unsigned int eeaddress, unsigned char* data, unsigned int num_chars)
{
   unsigned char i = 0;
   Wire.beginTransmission(deviceaddress);
   Wire.write((int)(eeaddress >> 8));   // MSB
   Wire.write((int)(eeaddress & 0xFF)); // LSB
   Wire.endTransmission();

   Wire.requestFrom(deviceaddress, num_chars);

   while (Wire.available()) data[i++] = Wire.read();

}

Title: Re: 24lc256 ข้อมูลหายหลังจากตัดไฟเลี้ยง
Post by: RoLRoR on January 09, 2015, 06:22:38 PM
ถ้า write แล้ว read กลับออกมาได้
คงเป็นโปรแกรมส่วนอื่นครับ
Title: Re: 24lc256 ข้อมูลหายหลังจากตัดไฟเลี้ยง
Post by: Tikki on January 09, 2015, 11:06:01 PM
รบกวนถามว่า ปกติการต่อใช้งาน 24lc256 เราต่อขา WP ลง GND เอาไว้เลยหรือเราควรจะต่อขาเข้า arduino เอาไว้เพื่อทำการสั่ง High ในตอนเริ่มต้นทำงานของบอร์ดรึเปล่าครับ
Title: Re: 24lc256 ข้อมูลหายหลังจากตัดไฟเลี้ยง
Post by: RoLRoR on January 09, 2015, 11:51:38 PM
มีบิต R/W บนคำสั่ง command
ขา WP ผมว่าใช้เป็นการป้องกันทาง HW
ต่อลงกราวด์ได้ เพื่ออ่านเขียนทาง command
จะใช้ขาควบคุมก็น่าทดลองดู ใส่ R Pull-Up เหมือนเดิม

ลองเขียนโค้ดทดสอบโดยรวมที่ไม่ซับซ้อน
และ เขียน อ่าน แค่ byte เดียว ก่อนครับ