Hallo!

Ich traue mich meine Versuche gar nicht hier rein zu stellen. Da wird jetzt sicher einer sagen, was verzapft dieser Anfänger denn da für einen Blödsinn. :-| Es geht um das Auslesen und Beschreiben eines 24LC256 EEPROM.

Man wird unweigerlich merken, dass ich aus der GUI-Anwendungs-Programmierung komme und noch nicht lange mit den AVRs arbeite. Aber besser bringe ich es im Moment nicht zusammen. Irgendwie scheinen die Prozeduren und Funktionen im Bascom erschreckende Einschränkungen bez. Variablen zu haben. Keine Arrays, keine Overlays. Mit ``byval`` deklarierte Parameter lassen sich innerhalb der Funktion nicht bitweise verändern. Man muss sie vorher in eine andere Variable kopieren. Habe ich noch etwas übersehen? ;-)

Code:
$regfile = "M8def.dat"
$crystal = 8000000
$hwstack = 32
$swstack = 10
$framesize = 40
$baud = 38400


Declare Sub Eeprom_write_byte(_
   Byval Slave_address As Byte , Byval Data_address As Word , _
   Byval Value As Byte _
)
Declare Function Eeprom_read_byte(_
   Byval Slave_address As Byte , Byval Data_address As Word _
) As Byte


'TWI (I²C)
$lib "i2c_twi.lbx"       'Hardware TWI einschalten
Config Scl = Portc.5
Config Sda = Portc.4
Const Slave_address = &B10100000
Config Twi = 400000
I2cinit


'TEST --------------------

Dim W As Word
Dim Bt As Byte


For W = 0 To 200
   Bt = W
   Call Eeprom_Write_byte(slave_address , W , Bt)
   Print "written:" ; Bt
Next W

For W = 200 To 0 Step -1
   Bt = Eeprom_Read_byte(slave_address , W)
   Print "read:" ; Bt
Next W

'TEST --------------------


End



Sub Eeprom_write_byte(_
   Byval Slave_address As Byte , Byval Data_address As Word , _
   Byval Value As Byte _
)

   Local Slave_w As Byte
   Local Slave_r As Byte
   Local Addr_high As Byte
   Local Addr_low As Byte

   Slave_w = Slave_address And &B11111110
   Slave_r = Slave_address Or &B00000001

   Addr_high = High(data_address)
   Addr_low = Low(data_address)

   I2cstart
   I2cwbyte Slave_w
   I2cwbyte Addr_high
   I2cwbyte Addr_low
   I2cwbyte Value
   I2cstop

   Waitms 5

End Sub


Function Eeprom_Read_byte(_
   Byval Slave_address As Byte , Byval Data_address As Word _
) As Byte

   Local Slave_w As Byte
   Local Slave_r As Byte
   Local Addr_high As Byte
   Local Addr_low As Byte
   Local Retval As Byte

   Slave_w = Slave_address And &B11111110
   Slave_r = Slave_address Or &B00000001

   Addr_high = High(data_address)
   Addr_low = Low(data_address)

   I2cstart
   I2cwbyte Slave_w
   I2cwbyte Addr_high
   I2cwbyte Addr_low
   I2cstart
   I2cwbyte Slave_r
   I2crbyte Retval , Nack
   I2cstop

   Eeprom_Read_byte = Retval

End Function
@Chris:
Das Beschreiben und Auslesen des EEPROM funktioniert schon mal.

lg
Gerold
:-)