hier ist mein code:
Code:
$regfile = "m32def.dat"
$crystal = 9000000
$baud = 9600
$hwstack = 64
$swstack = 64
$framesize = 64
Portd.5 = 1
Config Portd.2 = Output 'leds
Config Portd.3 = Output
Config Portd.4 = Output
Config Portd.5 = Input 'taster
Portd.5 = 1
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.3 , Db6 = Portc.2 , Db7 = Portc.1 , E = Portc.5 , Rs = Portc.7
Config Lcdbus = 4
Config Lcdmode = Port
Portc.6 = 0 'display rw bei Bascom immer 0
Config Portc = Output 'display
Reset Portc.1 'write mode for display
Initlcd
Cursor Off
Cls
$lib "i2c_twi.lbx" 'Hardware I2C
Config Twi = 100000
'SDA und SCL definieren
Config Sda = Portc.0
Config Scl = Portd.7
Portc.0 = 1
Portd.7 = 1
I2cinit
'-------------------------------------------------------------------------------
'$lib "Lcd_i2c.lib" 'i2c Treiber für das LCD
'Dim _lcd_e As Byte 'Nötig für die Enable Auswahl
'lcd_e = 128 'Für LCDs mit 1 Controller
'Waitms 200
'-------------------------------------------------------------------------------
'Config Adc = Single , Prescaler = Auto 'Für Analogtastatur
'Config Portd.3 = Output 'Für LED1
'Config Portd.4 = Output 'Für LED2
'Beep Alias Portc.2 'für Mega32
'Beep Alias Portb.0 'für Mega8
'-------------------------------------------------------------------------------
Const Pcf8591 = 144 'DA-AD-Wandler auf Basis
Const Eeprom2 = 166 'Eeprom2 auf Basis
Const Eeprom1 = 174 'Eeprom1 oder Ram auf Basis
Const Eeprom0 = 160 'externes Cip Card Eeprom0
Const Pcf8574_1 = 64
Const Tda8444 = 78 'DA-Wandler auf Extension-Platine
Const Pcf8574_lcd = 112 'Portextension für LCD
Const Pcf8583_rtc = 162 'RTC auf Extension-Platine
Const Max520 = 94 'für MAX520 (extern)
Const Keyboard = 0 'ADC-Nr. für Analogtastatur
'-------------------------------------------------------------------------------
Declare Function Read_eeprom(byval Dev As Byte , Byref Address As Word)as Byte
Declare Function Read_eeprom_word(byref Address As Word)as Word
Declare Sub Write_eeprom(byval Dev As Byte , Byref Address As Word , Byval Wert_
As Byte)
Declare Sub Write_eeprom_word(byref Address As Word , Byref Value As Word)
Dim Address_low As Byte 'Address ist hier überlappend
Dim Address_high As Byte 'dimensioniert, um High- und Low-
Dim Address As Word At Address_low Overlay 'Byte einfach ansprechen zu können.
Dim Value_low As Byte 'Dito mit Value
Dim Value_high As Byte
Dim Value As Word At Value_low Overlay
Dim Wert As Byte
Dim Temp_w As Word
Dim I As Word
'------------------------------ Demodurchlauf ----------------------------------
Cls 'Intro
Lcd "EEPROM TEST"
Locate 2 , 1
Lcd "SCHREIBE"
Wait 2
Value = 0
For Address = 250 To 500 'von Adresse 250 bis 500
Call Write_eeprom_word(address , Value) 'value ins EEprom schreiben.
Print "adresse: " ; Address ; " Value: " ; Value 'Value zur Abwechslung
Value = Value + 20
Next 'um 20 erhöhen
'-------------------------------------------------------------------------------
Wait 2
Cls
Lcd " LESE"
For Address = 250 To 500 'die Werte wieder auslesen
Temp_w = Read_eeprom_word(address)
Locate 2 , 1
Lcd "A: " ; Address ; " " ; "W: " ; Temp_w ; " "
Waitms 200 'Pause, damit man was sieht
Print "adresse: " ; Address ; " Value: " ; Temp_w
Next
Locate 1 , 1
Lcd "FERTIG"
'------------------------ Ende Demodurchlauf -----------------------------------
End
'------------------------ Funktionen und Subs ----------------------------------
'-------------------------------------------------------------------------------
'- Word (value) auf 2 aufeinander folgende EEprom Adressen schreiben
Sub Write_eeprom_word(byref Address As Word , Byref Value As Word)
Call Write_eeprom(eeprom1 , Address , Value_low)
Incr Address
Call Write_eeprom(eeprom1 , Address , Value_high)
End Sub
'-------------------------------------------------------------------------------
'- Word von 2 aufeinanderfolgenden EEprom Adressen lesen
'- Der gelesene Wert ist der Rückgabewert der Funktion
Function Read_eeprom_word(byref Address As Word)as Word
Value_low = Read_eeprom(eeprom1 , Address)
Incr Address
Value_high = Read_eeprom(eeprom1 , Address)
Read_eeprom_word = Value
End Function
'-------------------------------------------------------------------------------
'- Byte (wert) auf EEprom Adresse (address) speichern
Sub Write_eeprom(byval Dev As Byte , Byref Address As Word , Byval Wert As Byte)
I2cstart
I2cwbyte Dev
I2cwbyte Address_high
I2cwbyte Address_low
I2cwbyte Wert
I2cstop
Waitms 10
End Sub
'--------------------------------------------------------
'- Byte von EEprom Adresse (address) lesen
'- Der gelesene Wert ist der Rückgabewert der Funktion
Function Read_eeprom(byval Dev As Byte , Byref Address As Word)
Dim Devread As Byte
Devread = Dev + 1
I2cstart
I2cwbyte Dev
I2cwbyte Address_high
I2cwbyte Address_low
I2cstop
I2cstart
I2cwbyte Devread
I2crbyte Read_eeprom , Nack
I2cstop
End Function
Lesezeichen