View previous topic :: View next topic |
Author |
Message |
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Mon Dec 03, 2007 6:38 pm Post subject: Newbie with I2C |
|
|
Dear Friends
for first time I'm working on M24C64 I2C memory.
Sample code say that I must add another byte if use 24c64 but I do not have understand where add it.
In my case pin A0 and A1 are connected to the ground.
This is a I2C sample code with my modify in bold:
'Direttive di compilazione
$regfile = "m8515.dat"
$crystal = 16000000
$baud = 9600
$hwstack = 32
$swstack = 8
$framesize = 50
'Configurazione Pin
Config Sda = PortD.7
Config Scl = PortD.5
Config Portd.3 = Output
WP Alias Portd.3
'Prototipi di funzione
Declare Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte)
Declare Sub Read_eeprom(byval Adres As Byte , Value As Byte)
'Dichiarazioni di variabili
Const Abilita = 0
Const Disabilita = 1
Const Addressw = 160 'slave write address
Const Addressr = 161 'slave read address
Dim B1 As Byte
Dim Adres As Byte
Dim Value As Byte 'dim byte
'_____________________________________________________________________________________________
'Sub Main
Call Write_eeprom(10 , 3) 'write value of three to address 10 of EEPROM
Call Read_eeprom(10 , Value) : Print Value 'read it back
End
'_____________________________________________________________________________________________
Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte)
WP = Abilita
I2cstart 'start condition
I2cwbyte Addressw 'slave address
I2cwbyte Adres 'address in EEPROM
I2cwbyte Value 'value to write
I2crbyte Value , Ack
I2cstop 'stop condition
Waitms 10 'wait for 10 milliseconds
WP = Disabilita
End Sub
'_____________________________________________________________________________________________
Sub Read_eeprom(byval Adres As Byte , Value As Byte)
I2cstart 'generate start
I2cwbyte Addressw 'slave address
I2cwbyte Adres 'address in EEPROM
I2cstart 'repeated start
I2cwbyte Addressr 'slave address (read)
I2crbyte Value , Nack 'read byte
I2cstop 'generate stop
End Sub |
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
Posted: Mon Dec 03, 2007 8:41 pm Post subject: |
|
|
Hi Gpezzella,
In the help file there's an "sample" how to read BIG eeproms. You need an extra byte for the eprom adress.
Here's the modified sample. I didn't tested it on real hardware.
Code: |
$regfile = "m48def.dat" ' specify the used micro
$crystal = 8000000 ' used crystal frequency
$baud = 19200 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 ' default use 10 for the SW stack
$framesize = 40 ' default use 40 for the frame space
'or use the TWI !
Config Scl = Portb.4
Config Sda = Portb.5
Declare Sub Write_eeprom(byval Adresh As Byte , Byval Adresl As Byte , Byval Value As Byte)
Declare Sub Read_eeprom(byval Adresh As Byte , Byval Adresl As Byte , Value As Byte)
Const Addressw = 174 'slave write address
Const Addressr = 175 'slave read address
Dim Value As Byte 'dim byte
Do
Call Write_eeprom(1 , 1 , 3) 'write value of three to EEPROM
Call Read_eeprom(1 , 1 , Value) : Print Value 'read it back
Call Read_eeprom(1 , 5 , Value) : Print Value
Wait 1
' Main prog here
Loop
'sample of writing a byte to EEPROM AT24C64
Sub Write_eeprom(byval Adresh As Byte , Byval Adresl As Byte , Byval Value As Byte)
I2cstart 'start condition
I2cwbyte Addressw 'slave address
I2cwbyte Adresh 'highaddress of EEPROM
I2cwbyte Adresl 'lowaddress 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 AT24C64
Sub Read_eeprom(byval Adresh As Byte , Byval Adresl As Byte , Value As Byte)
I2cstart 'generate start
I2cwbyte Addressw 'slave adsress
I2cwbyte AdresH 'highaddress of EEPROM
I2cwbyte Adresl 'lowaddress of EEPROM
I2cstart 'repeated start
I2cwbyte Addressr 'slave address (read)
I2crbyte Value , Nack 'read byte
I2cstop 'generate stop
End Sub
|
_________________ www.evertdekker.com Bascom code vault |
|
Back to top |
|
|
pinkfloyd11
Joined: 02 Jul 2007 Posts: 247
|
Posted: Tue Dec 04, 2007 11:26 am Post subject: Which are address High and Address Low in I2C |
|
|
Dear friends,
Between samples there is one for write on 24C64 memory:
Sub Write_eeprom(byval Address_H As Byte , Byval Address_L As Byte , Byval Value As Byte)
I2cstart 'start condition
I2cwbyte &B1010_0000 'slave address
I2cwbyte Address_H 'high address
I2cwbyte Address_L 'low address
I2cwbyte Value 'value to write
I2cstop 'stop condition
Waitms 10
End Sub
In the case that A0 and A1 are connect to the ground, which is Address_H and Address_L??
If for example I would write at location 3 the value 12, what I must do?
Thanks |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Tue Dec 04, 2007 5:05 pm Post subject: |
|
|
bonjour again,
imagine you have one street with somes big houses
the house where you are living is seat at n° 160
but, in this house you have 8128 flats
you want to write a byte (8bits) of 115 to the flat, let's say 23
you have to give first the address of the street, 160,
then the flat location 00 (hight byte we said the Main Signifiant Byte) then 23 (Less Signifiant Byte),
then the name of the resident here 115
Your eepprom is divided in 64kbits =8 X 8 bytes address inside the eeprom
going from 0 to 8127 if I'm ok so you have to give the full address
MSB then LSB
is it clear ?
bon courage
JP |
|
Back to top |
|
|
Visovian
Joined: 31 Oct 2007 Posts: 584 Location: Czech
|
Posted: Wed Dec 05, 2007 10:01 pm Post subject: |
|
|
Hello Gpezzella,
There are two slave addresses in Evert's example, Addressw and Addressr
These are built this way:
First 4 bits (7 to 4) is device type identifier, found in datasheet, for 24m64 it is 1010.
Further 3 bits (3 to 1) is chip address. This is set by pins E2, E1, E0. Say all of them are connected to ground.
Then chip address is 000. This has nothing to do with addresses inside of memory.
The last bit (bit0) is 0 for write and 1 for read.
So result is
Addressw = 1010_000_0 (slave address for write)
Addressr = 1010_000_1 (slave address for read)
(In datasheet is "device select" instead of "slave address").
And back to your question
Quote: | If for example I would write at location 3 the value 12, what I must do?
|
Code: |
Dim Addressw as byte
Dim Addressr as byte
Dim AdresH as byte
Dim AdresL as byte
Dim value as byte
Addressw = &b10100000
Addressr = &b10100001
AdresH = &h00 'whole adres is word &h0003
AdresL = &h03
Value = 12
I2cstart 'start condition
I2cwbyte Addressw 'slave address for write
I2cwbyte Adresh 'highaddress of EEPROM
I2cwbyte Adresl 'lowaddress of EEPROM
I2cwbyte Value 'value to write
I2cstop 'stop condition
Waitms 10 'wait for 10 milliseconds
|
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|