Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Newbie with I2C

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
pinkfloyd11

Bascom Member



Joined: 02 Jul 2007
Posts: 248

italy.gif
PostPosted: Mon Dec 03, 2007 6:38 pm    Post subject: Newbie with I2C Reply with quote

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
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2111

netherlands.gif
PostPosted: Mon Dec 03, 2007 8:41 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
pinkfloyd11

Bascom Member



Joined: 02 Jul 2007
Posts: 248

italy.gif
PostPosted: Tue Dec 04, 2007 11:26 am    Post subject: Which are address High and Address Low in I2C Reply with quote

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
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 932
Location: France

france.gif
PostPosted: Tue Dec 04, 2007 5:05 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Wed Dec 05, 2007 10:01 pm    Post subject: Reply with quote

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
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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