Második nekifutás:
Kód: Egész kijelölése
'*-----------------------------------------------------------------------------------------
'* name : i2c_Teszt.bas
'* MegaBoard Chr EEprom Teszt. + Futás idömérés.
'* micro : Mega16
'* EEprpm Tip : 24LC1024
'* EEprom Addres : Write: HA0 (160), Read HA1 (161)
'* suited for demo : yes Flash 13%
'* Fordito : BASCOM AVR, (V.1.11.9.0)
'* Datum : 2008-10-12
'* Irta : kapu48
'* Ha kiveszed a Print-ek elöl a megjegyzést ( ' ) láthatod az írás, olvasás folyamatát.
'*-----------------------------------------------------------------------------------------
$regfile = "m16def.dat" ' specify the used micro
$crystal = 14746500 ' used crystal frequency
$baud = 9600 ' use baud rate
$hwstack = 128 ' default use 32 for the hardware stack
$swstack = 128 ' default use 10 for the SW stack
$framesize = 40 ' default use 40 for the frame space
'or use the TWI !
Config Scl = Portc.0
Config Sda = Portc.1
I2cinit
Declare Sub Write_eeprom(byval Addresl As Byte ,byval Value As Byte)
Declare Sub Read_eeprom(byval Addresl As Byte ,byval Value As Byte)
Declare Sub Write_eeprom_dim(byval Addresl As Byte , Byval Kezd As Byte , Byval Count As Byte)
Declare Sub Read_eeprom_dim(byval Addresl As Byte , Byval Kezd As Byte , Byval Count As Byte)
Declare Sub Read_clock()
Const Addressw = 160 'HA0 EEPROM slave write address
Const Addressr = 161 'HA1 EEPROM slave read address
Const Addresh = 0 ' EEprom H address
Dim S As Byte , M As Byte , H As Byte , D As Byte , Month As Byte 'Óra változói
Dim Wm As Byte , Yd As Byte , Value_w(127) As Byte , Value_r(127) As Byte , Kezd As Byte , Count As Byte
Dim B1 As Byte , Addresl As Byte , Valuew As Byte , Valuer As Byte , I As Byte , Maxaddres As Byte 'dim byte
Maxaddres = 127 ' EEprom 24_1024 Maximum 0 - 127 byte
Valuew = 0 'Ki irt adat
Valuer = 0 'Be olvasott adat
Print " START "
Print " EEprom Törlés "
Call Read_clock()
Valuew = 255
For I = 0 To Maxaddres
Call Write_eeprom(i , Valuew)
' Print "Write cim: " ; I ; " Adat: " ; Valuew '; Valuew 'write value of three to address 1 of EEPROM
Next I
Print "EEprom iras "
Call Read_clock()
For I = 0 To Maxaddres
Valuew = I
Call Write_eeprom(i , Valuew)
' Print "Write cim: " ; I ; " Adat: " ; Valuew '; Valuew 'write value of three to address 1 of EEPROM
Next I
Call Read_clock()
Print "EEprom olvasas "
For I = 0 To Maxaddres
Call Read_eeprom(i , Valuer)
' Print "Read Cim: " ; I ; " Adat: " ; Valuer 'read it back
Next I
Call Read_clock()
Print "Tomb feltoltese "
For I = 0 To Maxaddres ' Tömb feltöltése
Value_w(i) = I
Next I
Addresl = 0 ' EEprom kezdö cim
Kezd = 0 ' Tömb Value_w(Kezd-et)
Count = Maxaddres ' Átmozgatott Bytek száma.
Call Read_clock()
Print "EEprom T iras "
Call Write_eeprom_dim(addresl , Kezd , Count ) ' EEprom kezdet, Tömb Value_w(Kezd-et), Ki irt Bytek
Call Read_clock()
Print "EEprom T olvasas "
Call Read_eeprom_dim(addresl , Kezd , Count) ' EEprom kezdet, Tömb Value_r(Kezd-et), Be olvasot Bytek
Call Read_clock()
Print " END "
End
'sample of writing a byte to EEPROM AT241024
Sub Write_eeprom(byval Addresl As Byte , Byval Valuew As Byte)
I2cstart 'start condition
I2cwbyte Addressw 'slave address
I2cwbyte Addresh
I2cwbyte Addresl 'asdress of EEPROM
I2cwbyte Valuew 'value to write
If Err <> 0 Then Print "Write Hiba! " ; Addresl ; " Cimen"
I2cstop 'stop condition
Waitms 10 'wait for 10 milliseconds
End Sub
'sample of reading a byte from EEPROM AT241024
Sub Read_eeprom(byval Addresl As Byte , Byval Valuer As Byte)
I2cstart 'generate start
I2cwbyte Addressw 'slave adsress
I2cwbyte Addresh
I2cwbyte Addresl 'address of EEPROM
I2cstart 'repeated start
I2cwbyte Addressr 'slave address (read)
I2crbyte Valuer , Nack 'read byte
If Err <> 0 Then Print "Read Hiba! " ; Addresl ; " Cimen"
I2cstop 'generate stop
End Sub
Sub Write_eeprom_dim(byval Addresl As Byte , Byval Kezd As Byte , Byval Count As Byte)
I2cstart 'start condition
I2cwbyte Addressw 'slave address
I2cwbyte Addresh
I2cwbyte Addresl 'asdress of EEPROM
For I = Kezd To Count
I2cwbyte Value_w(i) 'value to write
If Err <> 0 Then Print "Write Hiba! " ; I ; " Cimen"
' Print "Write Cim: " ; I ; " Adat: " ; Value_w(i)
Next I
I2cstop 'stop condition
' Waitms 10 'wait for 10 milliseconds
End Sub
Sub Read_eeprom_dim(byval Addresl As Byte , Byval Kezd As Byte , Byval Count As Byte)
I2cstart 'generate start
I2cwbyte Addressw 'slave adsress
I2cwbyte Addresh
I2cwbyte Addresl 'address of EEPROM
I2cstart 'repeated start
I2cwbyte Addressr 'slave address (read)
For I = Kezd To Count
I2crbyte Value_r(i) , Ack
If Err <> 0 Then Print "Read Hiba! " ; I ; " Cimen"
' Print "Read Cim: " ; I ; " Adat: " ; Value_r(i)
Next I 'read byte
I2cstop 'generate stop
End Sub
' when you want to control a chip with a larger memory like the 24c64 it requires an additional byte
' to be sent (consult the datasheet):
' Wires from the I2C address that are not connected will default to 0 in most cases!
' I2cstart 'start condition
' I2cwbyte &B1010_0000 'slave address
' I2cwbyte H 'high address
' I2cwbyte L 'low address
' I2cwbyte Value 'value to write
' I2cstop 'stop condition
' Waitms 10
Sub Read_clock() ' " Read Clock"
I2cstart 'generate start
I2cwbyte &HA2 'write addres
I2cwbyte 2 'select second register
I2cstart 'generate repeated start
I2cwbyte &HA3 'write address for reading
I2crbyte S , Ack 'read seconds
I2crbyte M , Ack 'read minuts
I2crbyte H , Ack 'read hours
I2crbyte Yd , Ack 'read year
I2crbyte D , Ack 'read day
I2crbyte Wm , Ack 'read weekday
I2crbyte Month , Nack 'read month
I2cstop 'generate stop
S = Makedec(s)
M = Makedec(m)
H = Makedec(h)
Yd = Makedec(yd)
D = Makedec(d)
Wm = Makedec(wm)
Month = Makedec(month)
If H < 10 Then Print " " ;
Print H ; ":";
If M < 10 Then Print "0";
Print M ; ":";
If S < 10 Then Print "0";
Print S
' Print D ; "." ; Month ; "." ; Yd ; " " ; Wm
' Waitms 500
End Sub
Futási időeredmények:
START
EEprom Törlés
16:37:19
EEprom iras
16:37:20
16:37:22
EEprom olvasas
16:37:22
Tomb feltoltese
16:37:22
EEprom T iras
16:37:22
EEprom T olvasas
16:37:22
END
Annyi látszik, hogy csak rövid idők telnek el!
De itt talán óra jelet kellene számolni?