View previous topic :: View next topic |
Author |
Message |
komisarios
Joined: 17 Jun 2006 Posts: 38
|
Posted: Mon Nov 17, 2014 11:16 am Post subject: 24c01 dword write&read method suggestions? |
|
|
Hi all,
Can anyone give me suggestions of how to write and read a DWORD (4byte variable) into a 24c01 eeprom??
thank you!
(BASCOM-AVR version : 2.0.7.7 ) _________________ Macedonia is Greek |
|
Back to top |
|
|
komisarios
Joined: 17 Jun 2006 Posts: 38
|
Posted: Mon Nov 17, 2014 5:55 pm Post subject: |
|
|
please anyone heeeeeelp!!! _________________ Macedonia is Greek |
|
Back to top |
|
|
bzijlstra
Joined: 30 Dec 2004 Posts: 1179 Location: Tilburg - Netherlands
|
Posted: Mon Nov 17, 2014 6:32 pm Post subject: 24c01 and Single... |
|
|
Google is your friend...
Did a search on "Bascom 24c01" and stumbled on this program.. It is for a single but should be not to difficult to use it with an DWORD
Code: | 'Speichern in EEprom / c15022010 / n+p
'Atmega8 / LCD 2 x 16
'speichern einer Zahl ins EEprom (24C01)
'Achtung, max. Zahl = 2559999 !!!
$regfile = "m8def.dat"
$crystal = 1000000
Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3 , E = Portb.4 , Rs = Portb.5
Config Lcd = 16 * 2
Cls
'SDA und SCL def.
Config Sda = Portc.4
Config Scl = Portc.5
Dim A As Byte 'Speicheraddr schreiben
Dim B As Byte 'Speicheraddr lesen
Dim C As Single
Dim D As Single
Dim E As Single
Dim F As Single
Dim G As Single
Dim H As Single
Dim I As Single
Dim J As Byte
Dim K As Byte
Dim L As Byte
Dim J1 As Single
Dim K1 As Single
Dim L1 As Single
Dim Suwe As Single
'hier kann zum testen ein Startwert eingegeben werden (max. 2559999 !)
'C = 0
'- Anfangswert aus EEprom holen :
Goto Holen :
Do
'- Zahl zerlegen :
C = Suwe + 1
D = C / 10000
E = C / 100
F = Int(d) 'Wert 1
G = Frac(d)
G = G * 100
H = Int(g) 'Wert 2
I = Frac(g)
I = I * 100
I = Round(i)
J = F
K = H
L = I 'Wert 3
'- schreibe ins EEprom :
A = 30
I2cstart 'Start I2C
I2cwbyte &HA0 'Sende Slave Adresse
I2cwbyte A 'adr 1
I2cwbyte J
I2cstop
Waitms 10
A = A + 1 'adr + 1
I2cstart
I2cwbyte &HA0
I2cwbyte A 'adr 2
I2cwbyte K
I2cstop
Waitms 10
A = A + 1
I2cstart
I2cwbyte &HA0
I2cwbyte A 'adr 3
I2cwbyte L
I2cstop
Waitms 10
Locate 1 , 1
Lcd J ; " " ; K ; " " ; L ; " " 'zeige die Schreibwerte
Waitms 50
'- lesen vom EEprom :
Holen:
B = 30
I2cstart 'Start I2C
I2cwbyte &HA0 'sende Slave Adresse
I2cwbyte B 'sende Speicheradresse
I2cstart 'Start I2C
I2cwbyte &HA1 'sende Slave Adresse +1 für Lesen
I2crbyte J , Nack 'lese Adresse vom EEprom
I2cstop 'Stop I2C
B = B + 1 'adr + 1
I2cstart
I2cwbyte &HA0
I2cwbyte B
I2cstart
I2cwbyte &HA1
I2crbyte K , Nack
I2cstop
B = B + 1
I2cstart
I2cwbyte &HA0
I2cwbyte B
I2cstart
I2cwbyte &HA1
I2crbyte L , Nack
I2cstop
'- Zahl zusammensetzen :
J1 = J * 10000
K1 = K * 100
L1 = K1 + L
Suwe = J1 + L1
Locate 2 , 1
Lcd Fusing(suwe , "######.") 'zeige ausgelesene Werte
Waitms 200
Loop
End
|
And here some info about DWORD. It is from Application Note 193 by Mak3.
Dim My_dword As Dword At $140 'This places the my_long_2 variable at a fixed SRAM address starting at HEX 140
Dim Byte__1 As Byte At $140 Overlay 'NOTICE: because this will be stored at the specified memory location
Dim Byte__2 As Byte At $141 Overlay ' which could be already be occupied by another OVERLAY variable, or by a normal variable
Dim Byte__3 As Byte At $142 Overlay
Dim Byte__4 As Byte At $143 Overlay
Byte__1 = 1
Byte__2 = 2
Byte__3 = 3
Byte__4 = 4
'This is how it will be stored in SRAM
' <----------my_dword---------->
' +-------+------+------+------+
' | Byte_1|Byte_2|Byte_3|Byte_4|
' +-------+------+------+------+
'But when you print it with print bin(Variable) you will see it as
' <----------my_dword---------->
' +-------+------+------+------+
' | Byte_4|Byte_3|Byte_2|Byte_1|
' +-------+------+------+------+
Print "my_dword = " ; Bin(my_dword)
Print "-------------------------"
==
I did also a search on Macedonia by the way... Interesting history... but it has nothing to do with Bascom.
Have fun
Ben Zijlstra |
|
Back to top |
|
|
komisarios
Joined: 17 Jun 2006 Posts: 38
|
Posted: Mon Nov 17, 2014 7:10 pm Post subject: |
|
|
thanks mr Ben,
i've already googled these articles.
i think i can also work with ERAM variables but the question is that i need to use both the internal eeprom (m128=4k) and i2c (24c01) eeprom...
Also, i don't know how the two byte addressing (=65535 eeprom positions) can read/write the 131072 byte positions of a 24c01 (datasheet refers to 2 byte-addressing!) _________________ Macedonia is Greek |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon Nov 17, 2014 9:18 pm Post subject: |
|
|
komisarios wrote: | Also, i don't know how the two byte addressing (=65535 eeprom positions) can read/write the 131072 byte positions of a 24c01 (datasheet refers to 2 byte-addressing!) |
What device are you talking about?
The 24C01 is a 1kBit, 128 x 8 bit EEPROM, one byte is enough for addressing.
For the bigger types > 2kBit, part of the device address is used to select 2K pages. |
|
Back to top |
|
|
komisarios
Joined: 17 Jun 2006 Posts: 38
|
Posted: Mon Nov 17, 2014 9:38 pm Post subject: |
|
|
Sorry for the mistake, eeprom is an CAT24M01 (1Mb)
MWS, do you have any idea how to read addresses above >65535 ?? _________________ Macedonia is Greek |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon Nov 17, 2014 10:06 pm Post subject: |
|
|
It's all in the data sheet.
This EEPROM has 512 pages, each of 256 bytes.
The first byte sent after the device address is part of the page addressing, exactly page address bits 0 to 7, while bit 8 of the page address comes from bit 1 of the device address.
The second byte sent after the device address points to the byte within a page.
Basically very similar to the 24C01. |
|
Back to top |
|
|
komisarios
Joined: 17 Jun 2006 Posts: 38
|
Posted: Mon Nov 17, 2014 10:18 pm Post subject: |
|
|
could you just give me a sample code, if ,for example, i want to write or read address 130000
is the following correct?
dim fram_address as word
dim Fram_address_hi as byte
Fram_address_lo as byte
I2cstart
I2cwbyte &B10100000
Fram_address_hi = High(fram_address) ' Send the address high byte
I2cwbyte Fram_address_hi
Fram_address_lo = Low(fram_address) ' Send the address low byte
I2cwbyte Fram_address_lo
I2cstart
I2cwbyte &B10100001
I2crbyte Fram_data , Nack
I2cstop _________________ Macedonia is Greek |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon Nov 17, 2014 10:42 pm Post subject: |
|
|
Your sample looks ok so far, only you need to take care about the 17th bit, contained in the device address, which gives access to the upper 256 pages. |
|
Back to top |
|
|
komisarios
Joined: 17 Jun 2006 Posts: 38
|
Posted: Tue Nov 18, 2014 9:10 am Post subject: |
|
|
ok, i'll check the datasheet again regarding the 17th bit and do some testing
MWS, thanks for your time! _________________ Macedonia is Greek |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Nov 18, 2014 11:40 am Post subject: |
|
|
komisarios wrote: | ok, i'll check the datasheet again regarding the 17th bit and do some testing. |
Here it is, see below.
Quote: | MWS, thanks for your time! |
You can give back, if you share here the working code for this device. |
|
Back to top |
|
|
komisarios
Joined: 17 Jun 2006 Posts: 38
|
Posted: Tue Nov 18, 2014 5:59 pm Post subject: |
|
|
Here's the working code for both READS & WRITES for anyone who's working on I2C EEPROM's above 512kb
MWS, thanks for your help, i really had overlooked the paragraph on page 6 , regarding the 17th bit.
$regfile = "m128def.dat"
$crystal = 11059000
$hwstack = 100
$swstack = 100
$framesize = 100
Enable Interrupts
Config Scl = Portf.7
Config Sda = Portf.6
Config I2cdelay = 5
Const 24c01_lowrite = &B10100000
Const 24c01_hiwrite = &B10100010
Const 24c01_loread = &B10100001
Const 24c01_hiread = &B10100011
Dim Fram_address As Dword
Dim Fram_address_hi As Byte
Dim Fram_address_lo As Byte
Dim Fram_data As Byte
'**** WRITE
Fram_data = 46 ' a random test value
For Fram_address = 0 To 131072
I2cstart
If Fram_address > 65535 Then ' toggles the 17th bit on or off
I2cwbyte 24c01_hiwrite
Else
I2cwbyte 24c01_lowrite
End If
Fram_address_hi = High(fram_address)
I2cwbyte Fram_address_hi
Fram_address_lo = Low(fram_address)
I2cwbyte Fram_address_lo
I2cwbyte Fram_data
I2cstop
Waitms 3
Next Fram_address
'**** READ
For Fram_address = 0 To 131072
I2cstart
If Fram_address > 65535 Then ' toggles the 17th bit on or off
I2cwbyte 24c01_hiwrite
Else
I2cwbyte 24c01_lowrite
End If
Fram_address_hi = High(fram_address)
I2cwbyte Fram_address_hi
Fram_address_lo = Low(fram_address)
I2cwbyte Fram_address_lo
I2cstart
If Fram_address > 65535 Then ' toggles the 17th bit on or off
I2cwbyte 24c01_hiread
Else
I2cwbyte 24c01_loread
End If
I2crbyte Fram_data , Nack
I2cstop
Next Fram_address
End _________________ Macedonia is Greek |
|
Back to top |
|
|
|