I2C EEPROM kezelése

Hogyan programozzak AVR chipet? Programozók beállításai...
Bascom nyelvű programok...
Avatar
kapu48
Elektronbűvölő
Hozzászólások: 3375
Csatlakozott: 2008. augusztus 29. péntek, 6:00

Hozzászólás Szerző: kapu48 »

Memoria kezelés hogyan?
Ezt a rutint megváltoztattam:

Kód: Egész kijelölése

Prog13:
Print "EEPROM"
   C = 0
   I2cstart                                                 ' Send I2C Start.
   I2cwbyte &B10100000                                      ' Select MicroChip EEPROM device type, address and write bit.
   I2cwbyte 0                                               ' Send high address to write to.
   I2cwbyte 0                                               ' Send low address to write to.
   A = &B10101010
   Print "Written:" ; A
   I2cwbyte &B10101010
                                 ' Send the data to be written.
   If Err = 0 Then                                          ' check status of operation.
      Print "OK."                                           ' Report success.
   Else                                                     ' or
      Print "Err."                                          ' Report failure.
   End If
   I2cstop                                                  ' All we want to do is done.
   I2cstart                                                 ' Send I2C Start.
   I2cwbyte &B10100000                                      ' Select MicroChip EEPROM device type, address and write bit.
   I2cwbyte 0                                               ' Send high address to write to.
   I2cwbyte 0                                               ' Send low address to write to.
   I2crbyte C                                               ' Send the data to be written.
   Print "Read:" ; C
   I2cstop                                                  ' All we want to do is done.
Return
Igy:

Kód: Egész kijelölése

Dim C As Byte 		 ’ felvettem C-t
Prog13:
Print "EEPROM"
   C = 0			’ C = 0
   I2cstart                                                 
   I2cwbyte &B10100000                                      
   I2cwbyte 0                                               
   I2cwbyte 0                                               
   A = &B10101010
   Print "Written:" ; A
   I2cwbyte &B10101010                                 
   If Err = 0 Then                                          
      Print "OK."                                           
   Else                                                     
      Print "Err."                                          
   End If
   I2cstop                                                 
   I2cstart                                               
   I2cwbyte &B10100000                                    
   I2cwbyte 0                                             
   I2cwbyte 0                                             
   I2crbyte C             		 ’ C-be olvasom be, mert A-ban már eredetileg is benne van!
   Print "Read:" ; C   		’ IT 0-át IRKÍ!!! 
   I2cstop                                                 
Return
Akkor most az írás nem jó, vagy az olvasás???
Ennyi változtatás

Kód: Egész kijelölése

   I2crbyte C ,  Nack    ' Read 255
   I2crbyte C ,  ack      ' Read 255
Mostnmár mintha az írás nem lenne jó![/code]
Avatar
kapu48
Elektronbűvölő
Hozzászólások: 3375
Csatlakozott: 2008. augusztus 29. péntek, 6:00

Hozzászólás Szerző: kapu48 »

Ez van a helpben:

Kód: Egész kijelölése

'-----------------------------------------------------------------------------------------
'name                     : i2c.bas
'copyright                : (c) 1995-2005, MCS Electronics
'purpose                  : demo: I2CSEND and I2CRECEIVE
'micro                    : Mega48
'suited for demo          : yes
'commercial addon needed  : no
'-----------------------------------------------------------------------------------------

$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

Declare Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte)
Declare Sub Read_eeprom(byval Adres As Byte , Value As Byte)

Const Addressw = 174                                        'slave write address
Const Addressr = 175                                        'slave read address

Dim B1 As Byte , Adres As Byte , Valuew As Byte , Valuer As Byte       'dim byte
Valuew = 21
Valuer = 0
Call Write_eeprom(1 , Valuew) : Print "Write: 1: " ; Valuew 'write value of three to address 1 of EEPROM


Call Read_eeprom(1 , Valuer) : Print "Read: 1: " ; Valuer   'read it back
Call Read_eeprom(5 , Valuer) : Print "Read: 5: " ; Valuer   'again for address 5


'-------- now write to a PCF8474 I/O expander -------
I2csend &H40 , 255                                          'all outputs high
I2creceive &H40 , B1                                        'retrieve input
Print "Received data " ; B1                                 'print it
End

Rem Note That The Slaveaddress Is Adjusted Automaticly With I2csend & I2creceive
Rem This Means You Can Specify The Baseaddress Of The Chip.




'sample of writing a byte to EEPROM AT2404
Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte)
    I2cstart                                                'start condition
    I2cwbyte Addressw                                       'slave address
    I2cwbyte Adres                                          'asdress of EEPROM
    I2cwbyte Value                                          'value to write
    I2cstop                                                 'stop condition
    Waitms 10                                               'wait for 10 milliseconds
End Sub


'sample of reading a byte from EEPROM AT2404
Sub Read_eeprom(byval Adres As Byte , Value As Byte)
   I2cstart                                                 'generate start
   I2cwbyte Addressw                                        'slave adsress
   I2cwbyte Adres                                           'address of EEPROM
   I2cstart                                                 'repeated start
   I2cwbyte Addressr                                        'slave address (read)
   I2crbyte Value , Nack                                    '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
Az eredmény:
Write: 1: 21
Read: 1: 255
Read: 5: 255
Received data 255

Eeprom csere nem segítet!
Valami ötlet?

Ha kiveszem a Nack-ot akkor!
Az eredmény:
Write: 1: 21
Read: 1: 0
Read: 5: 0
Received data 255
:?: :roll:
A hozzászólást 1 alkalommal szerkesztették, utoljára kapu48 2008. október 11. szombat, 19:13-kor.
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Hozzászólás Szerző: Robert »

Memória típusa?
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Hozzászólás Szerző: Robert »

24LC nem mindegy!
1 vagy 2 vagy 3 byte a chipen belüli címzése!
Avatar
kapu48
Elektronbűvölő
Hozzászólások: 3375
Csatlakozott: 2008. augusztus 29. péntek, 6:00

Hozzászólás Szerző: kapu48 »

Memoria tipusa: 24LC1025

Akkor kel:
I2cwbyte Adresl
I2cwbyte Adresh

:?:
Na ez nem segítet! :evil:
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Hozzászólás Szerző: Robert »

http://nearspacevermont.org/downloads/d ... 21941E.pdf

7. oldalon van a címkiosztás.
12. oldal a kezelése a Írás/Ovasásnak.


A HW-ADRRESSbit jó?
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Hozzászólás Szerző: Robert »

A kód a 24LC kisebbjével hogy megy?
Avatar
kapu48
Elektronbűvölő
Hozzászólások: 3375
Csatlakozott: 2008. augusztus 29. péntek, 6:00

Hozzászólás Szerző: kapu48 »

Adatlap: 24LC1025
Nem értem de majd próbálkozok!
:oops:
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Hozzászólás Szerző: Robert »

24lc256 vagy 24lc64 működik?
A HW-es címe jól van beírva a chipnek?
I2C inicializálva van?
Avatar
kapu48
Elektronbűvölő
Hozzászólások: 3375
Csatlakozott: 2008. augusztus 29. péntek, 6:00

Hozzászólás Szerző: kapu48 »

Kisebb mem ugyan ez az eredmény!
Const Addressw = 174 'slave write address
Const Addressr = 175
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Hozzászólás Szerző: Robert »

I2CDetect program megtalálja?
Ez a minták közt van, azt hiszem a 4 vagy az 5. program....
Avatar
kapu48
Elektronbűvölő
Hozzászólások: 3375
Csatlakozott: 2008. augusztus 29. péntek, 6:00

Hozzászólás Szerző: kapu48 »

3-at talál.
90 0
A0 0, A0 2 címeken
Ezt még nem tudom melyik mi?

De akkor nem stimmel mert én AE – AF címekre küldöm!
:idea:


Hőmérő cím = ?
Óra cím = ?
EEprom cím = ?
Avatar
kapu48
Elektronbűvölő
Hozzászólások: 3375
Csatlakozott: 2008. augusztus 29. péntek, 6:00

Hozzászólás Szerző: kapu48 »

Hőmérő cím = H90 - H91 (144 - 145)
Óra cím = HA2 - A3 (162 - 163)
EEprom cím = HA0 - HA1 (160 - 161)

Meg csináltam! :D :D :D
Write: 1: 21
Read: 1: 21
Read: 5: 255
Received data 255

Na jó meg csináltuk! :)
A hozzászólást 2 alkalommal szerkesztették, utoljára kapu48 2008. október 11. szombat, 21:15-kor.
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Hozzászólás Szerző: Robert »

Az I2C írások/olvasások után érdemes meglesni az Err változót. ha ez 1, akkor hibás volt a parancs. Ha 0, akkor jól végre lett hajtva.
Avatar
kapu48
Elektronbűvölő
Hozzászólások: 3375
Csatlakozott: 2008. augusztus 29. péntek, 6:00

Hozzászólás Szerző: kapu48 »

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?
Válasz küldése