Forum: Compiler & IDEs i2c -EEprom 24c256 mit Wordadresse schreiben/lesen


von funkker (Gast)


Lesenswert?

Hallo, wie kann ich in der Demo von fleury eine Wordadresse benutzen für 
den 24c256 ?

Danke.
1
int main(void)
2
{
3
    unsigned char ret;
4
    
5
6
    DDRB  = 0xff;                              // use all pins on port B for output 
7
    PORTB = 0xff;                              // (active low LED's )
8
9
    i2c_init();                                // init I2C interface
10
11
    /* write 0x75 to eeprom address 0x05 (Byte Write) */
12
    ret = i2c_start(Dev24C02+I2C_WRITE);       // set device address and write mode
13
    if ( ret ) {
14
        /* failed to issue start condition, possibly no device found */
15
        i2c_stop();
16
        PORTB=0x00;                            // activate all 8 LED to show error */
17
    }else {
18
        /* issuing start condition ok, device accessible */
19
        i2c_write(0x05);                       // write address = 5
20
        i2c_write(0x75);                       // ret=0 -> Ok, ret=1 -> no ACK 
21
        i2c_stop();                            // set stop conditon = release bus
22
23
        /* write ok, read value back from eeprom address 0x05, wait until 
24
           the device is no longer busy from the previous write operation */
25
        i2c_start_wait(Dev24C02+I2C_WRITE);     // set device address and write mode
26
        i2c_write(0x05);                        // write address = 5
27
        i2c_rep_start(Dev24C02+I2C_READ);       // set device address and read mode
28
        ret = i2c_readNak();                    // read one byte
29
        i2c_stop();
30
        
31
        PORTB = ~ret;                           // output byte on the LED's
32
33
        /* write 0x70,0x71,072,073 to eeprom address 0x00..0x03 (Page Write),
34
           wait until the device is no longer busy from the previous write operation */
35
        i2c_start_wait(Dev24C02+I2C_WRITE);     // set device address and write mode
36
        i2c_write(0x00);                        // write start address = 0
37
        i2c_write(0x70);                        // write data to address 0
38
        i2c_write(0x71);                        //    "    "   "    "    1
39
        i2c_write(0x72);                        //    "    "   "    "    2
40
        i2c_write(0x74);                        //    "    "   "    "    3
41
        i2c_stop();                             // set stop conditon = release bus
42
    
43
        /* write ok, read value back from eeprom address 0..3 (Sequencial Read),
44
           wait until the device is no longer busy from the previous write operation */
45
        i2c_start_wait(Dev24C02+I2C_WRITE);      // set device address and write mode
46
        i2c_write(0x00);                         // write address = 0
47
        i2c_rep_start(Dev24C02+I2C_READ);        // set device address and read mode
48
        ret = i2c_readAck();                       // read one byte form address 0
49
        ret = i2c_readAck();                       //  "    "    "    "     "    1
50
        ret = i2c_readAck();                       //  "    "    "    "     "    2
51
        ret = i2c_readNak();                       //  "    "    "    "     "    3
52
        i2c_stop();                              // set stop condition = release bus
53
    
54
        PORTB = ~ret;                            // output byte on the LED's        
55
    }
56
    
57
    for(;;);  
58
}

von funkker (Gast)


Lesenswert?

Weiss keiner wie man die da reinbringt : 2byte-adresse, lo-hi-byte von 
der 16 Bitadresse.

ES wird ja gleich nach der Byteadresse der Wert reingeschrieben :
i2c_write(0x05);                       // write address = 5
i2c_write(0x75);                       // ret=0 -> Ok, ret=1 -> no ACK

Hmm.., finde keine Lösung.

von Oliver (Gast)


Lesenswert?

funkker schrieb:
> Hmm.., finde keine Lösung.

Hm, hast du schon einmal ins Datenblatt geschaut? Da sollte eigentlich 
alles drinstehen, was wissen willst.

Wenn nicht, gibts hier noch einen Geheimtip:

http://lmgtfy.com/?q=24c256+ansteuern

Oliver

von Jörg W. (dl8dtl) (Moderator) Benutzerseite


Lesenswert?

Das TWI-Demo der avr-libc wurde kürzlich von Ruwan Jayanetti auf
optionale Unterstützung von 16-bit-EEPROMs "aufgebohrt".  Leider
bin ich noch nicht dazu gekommen, die offizielle Doku auf der
Webseite mal auf die Version 1.7.0 zu aktualisieren, aber du kannst
dir zumindest die Quelldatei aus dem Subversion-Repository holen:

http://svn.savannah.nongnu.org/viewvc/trunk/avr-libc/doc/examples/twitest/twitest.c?revision=2138&root=avr-libc

von holger (Gast)


Lesenswert?

>Weiss keiner wie man die da reinbringt : 2byte-adresse, lo-hi-byte von
>der 16 Bitadresse.
>
>ES wird ja gleich nach der Byteadresse der Wert reingeschrieben :
>i2c_write(0x05);                       // write address = 5
>i2c_write(0x75);                       // ret=0 -> Ok, ret=1 -> no ACK
>
>Hmm.., finde keine Lösung.


Vieleicht will der ja ZWEI Bytes für die Adresse haben?
Wie soll das denn sonst passen?

i2c_write(0x00);                       // write address = 5 high byte
i2c_write(0x05);                       // write address = 5 low byte
i2c_write(0x75);                       // ret=0 -> Ok, ret=1 -> no ACK

von Manfred (Gast)


Lesenswert?

Hallo!
Vielleicht hilft das..
1
// Write one Byte
2
char EEProm_WriteByte (char dev,unsigned int adr, char _data)
3
{
4
  // 1. Select device
5
  if (!i2c_start (dev+I2C_WRITE))
6
  {
7
    // 2. Write address
8
    i2c_write ((char) (adr >> 8));
9
    i2c_write ((char) (adr & 0x00ff));
10
    // 3. Write data
11
    i2c_write (_data);
12
    i2c_stop();
13
  return 0;
14
  }
15
  else
16
  {
17
    i2c_stop();
18
  return 1;
19
  }
20
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.