Jacob Pirna Startseite [www.pirnaer.de] >> [AVR/Atmel Mikrocontroller] >> BASCOM Beispiele für myAVR
[Grundgerüst LCD] [Echtzeit Uhr DS1307] [Temperaturmessung mit DS1820] [I2C (TWI) EEPROM]


Beispiel für myAVR Board mit LCD Display und seriellen I2C (TWI) EEPROM M24C64 (M24C128,M24C256)



Die EEPROM's für den I2C Bus (TWI) werden ähnlich wie der DS1307 angesprochen. Eine Besonderheit und auch kleine Falle ist die Übergabe der Adresse. Hier wird zuerst der höherwertige Teil (high) und dann der niederwertige Teil (low) übergeben. Wer schon öffter in Assembler oder hardware nah programmiert hat weis, dass es in der Regel genau umgekehrt ist.

'------------------------------------------------------------------------------
' LCD:  Db4-Db7 = Portd.4-Portd.7 E = Portd.3 Rs = Portd.2 Backlight = Portb.1
' I2C:  Sda = Portc.4 Scl = Portc.5
' UART: RxD = Portd.0 TxD = Portd.1
' Frei: Portb.0,Portb.2-Portb.5 Portc.0-Portc.3
'------------------------------------------------------------------------------
$regfile = "m8def.dat"
$crystal = 3686411
$baud = 9600

Ddrd = &HFE
Config Lcdpin = Pin , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7 , E = Portd.3 , Rs = Portd.2
Config Lcd = 16 * 2

Config Sda = Portc.4                                        ' I2C Bus konfigurieren
Config Scl = Portc.5
Const M24c64w = &HA0                                        ' Addresse des M24C64
Const M24c64r = &HA1


' Init
Ddrb.1 = 1
Cls
Cursor Noblink
Cursor Off
Portb.1 = 1                                                 ' 0=Licht aus / 1=Licht an (LCD)

Dim Adresse As Word
Dim Al As Byte
Dim Ah As Byte
Dim I As Byte
Dim Ar(4) As Byte

Ar(1) = 1 : Ar(2) = 2 : Ar(3) = 3 : Ar(4) = 4
Adresse = 25

' 4 Byte auf Adresse im M24C64 schreiben
Al = Low(adresse)
Ah = High(adresse)
I2cstart
I2cwbyte M24c64w
I2cwbyte Ah
I2cwbyte Al
I2cwbyte Ar(1)
I2cwbyte Ar(2)
I2cwbyte Ar(3)
I2cwbyte Ar(4)
I2cstop

' 4 Byte auf Adresse im M24C64 lesen
Al = Low(adresse)
Ah = High(adresse)
I2cstart
I2cwbyte M24c64w
I2cwbyte Ah
I2cwbyte Al
I2cstart
I2cwbyte M24c64r
I2crbyte Ar(1) , Ack
I2crbyte Ar(2) , Ack
I2crbyte Ar(3) , Ack
I2crbyte Ar(4) , Nack                                       ' Letztes Byte
I2cstop

Locate 1 , 1
For I = 1 To 4
 Lcd Hex(ar(i))
Next I

' Main Loop
Do
 Waitms 10
Loop


End
 

(c) Hans-Jürgen Jacob 2007 Pirna