Hab grade ein Brett vor dem Kopf.
Ich will obiges EEPROM an einem AVR betreiben. Funktioniert auch fast,
mein Problem ist nur wie folgt:
Schreibe ich an Adresse 0x0000 ein Byte wird auch nur da ein Byte
gespeichert.
Schreibe ich an eine andere Adresse als 0x0000 wird das Byte in einem
Bereich von 255 gespeichert, in dem die Adresse liegt, d.h. will ich
z.B. an Adresse 0x0005 ein Byte speichern wird das Byte an jede Adresse
zwischen 0x0001 und 0x00ff gespeichert, soll es z.B. an Adresse 0x0100
gespeichert werden wird es an jede Adresse zwischen 0x0100 und 0x01ff
gespeichert usw.
Ich seh grad den Fehler nicht, vielleicht hilft ja die
Schwarm-Intelligenz
Lesen eines Bytes von einer Adresse:
1 | uint8_t readByte24LC1025(uint16_t adress){
|
2 | uint8_t data = 0;
|
3 | i2c_start(i2c_Adress_24LC1025);
|
4 | i2c_byte((uint8_t)(adress >> 8));
|
5 | i2c_byte((uint8_t)(adress && 0xff));
|
6 | i2c_start(i2c_Adress_24LC1025 | 1);
|
7 | data = i2c_readNAck();
|
8 | i2c_stop();
|
9 | return data;
|
10 | }
|
Schreiben eines Bytes an eine Adresse:
1 | void writeByte24LC1025(uint16_t adress, uint8_t data){
|
2 | i2c_start(i2c_Adress_24LC1025);
|
3 | i2c_byte((uint8_t)(adress >> 8));
|
4 | i2c_byte((uint8_t)(adress && 0xff));
|
5 | i2c_byte(data);
|
6 | i2c_stop();
|
7 | }
|
Die I2C-Funktionen (die "steuern" u.a. noch ein Display an und ein
BME280)
1 | void i2c_start(uint8_t i2c_addr){
|
2 | // i2c start
|
3 | TWCR = (1 << TWINT)|(1 << TWSTA)|(1 << TWEN);
|
4 | while((TWCR & (1 << TWINT)) == 0);
|
5 | // send adress
|
6 | TWDR = i2c_addr;
|
7 | TWCR = (1 << TWINT)|( 1 << TWEN);
|
8 | while((TWCR & (1 << TWINT)) == 0);
|
9 | }
|
10 | void i2c_stop(void){
|
11 | // i2c stop
|
12 | TWCR = (1 << TWINT)|(1 << TWSTO)|(1 << TWEN);
|
13 | }
|
14 | void i2c_byte(uint8_t byte){
|
15 | TWDR = byte;
|
16 | TWCR = (1 << TWINT)|( 1 << TWEN);
|
17 | while((TWCR & (1 << TWINT)) == 0);
|
18 | }
|
19 | uint8_t i2c_readAck(void){
|
20 | TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
|
21 | while ((TWCR & (1<<TWINT)) == 0);
|
22 | return TWDR;
|
23 | }
|
24 | uint8_t i2c_readNAck(void){
|
25 | TWCR = (1<<TWINT)|(1<<TWEN);
|
26 | while ((TWCR & (1<<TWINT)) == 0);
|
27 | return TWDR;
|
28 | }
|