PDA

View Full Version : problems working with 24C512



shaiqbashir
- 28th May 2008, 03:37
Dear fellows

Im facing problems working with 24C512 eeprom. Im posting here my coding. IN simulation, it seems to work fine on proteus but in the real it doesnt. Herez my coding.

Include "Modedefs.Bas"

' ** Setup the Crystal Frequency, in Mhz **

Define OSC 4 ' Set Xtal Frequency

' ** Setup the I2C configuration **

Define I2C_SCLOUT 1 ' No need for the pullup resistor on the SCL pin


DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
DEFINE HSER_SPBRG 25

' Set LCD Data port
DEFINE LCD_DREG PORTB
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTC
' Set LCD Register Select bit
DEFINE LCD_RSBIT 0
' Set LCD Enable port
DEFINE LCD_EREG PORTC
' Set LCD Enable bit
DEFINE LCD_EBIT 1
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 8
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 2000
' Set data delay time in us
DEFINE LCD_DATAUS 50

' ** Declare the Variables **

SCL Var PortD.0 ' I2C data pin
SDA Var PortD.1 ' I2C clock pin
Addr Var Word ' 16-bit memory address within the Eeprom
E_ByteOut Var Byte ' Data byte to be written to the Eeprom
E_ByteIn Var Byte ' Data byte read from the Eeprom
x var byte
' ** The main program starts here **

' Write the string, "HELLO WORLD" into the first 11 address's of the Eeprom

For Addr=0 To 10 ' Create a loop of 11
Lookup Addr,[1,255,222,134,10,1,13,19,10,99,219],E_ByteOut' Build up the string
Gosub EWrite ' Write the byte to the Eeprom
Next ' Close the Loop

' Read the first 11 address's within the Eeprom,
' and display them on the LCD

Again:
lcdout $FE,1, "starting"
Pause 1000 ' Clear the LCD
Pause 500 ' Pause for drama
For Addr=0 To 10 ' Create a loop of 11
Gosub ERead ' Read the byte from the Eeprom
hserout [dec e_bytein,13,10]
pause 100
lcdout $FE,1,#E_bytein
' Print the characters read in from the eeprom
Pause 2000 ' Pause inbetween displays, for drama
Next ' Close the loop
Goto Again ' Do it forever

' ** SUBROUTINES **


EWrite:
I2CWRITE SDA,SCL,%10100000,Addr,[E_ByteOut] ' Write out the byte
Pause 10 ' Delay 10ms after each write
Return

ERead:
I2CREAD SDA,SCL,%10100001,Addr,[E_ByteIn] ' Read in the byte
Return

The problem im facing is this that the LCD displays "starting" and then instead of displaying the values it read from the eeprom, it displays just 2.

PLease help me what to do here. I have tried addr.highbyte addr.lowbyte, took control as a byte variable. But nothing seems to work here.

Jerson
- 28th May 2008, 04:26
Maybe you're missing pull-ups on SDA and SCL?

mister_e
- 28th May 2008, 14:49
' ** Setup the I2C configuration **

Define I2C_SCLOUT 1 ' No need for the pullup resistor on the SCL pin
Not sure if it works without pull-up with this define...

BUT if you receive always 2, maybe you reverted your SDA, SCL line as well.. who knows?

shaiqbashir
- 28th May 2008, 17:46
Dear jerson and mister e

i have rechecked my connections, they are correct. I dont know where is the problem. I have even grounded WP which i havent done before. One more technical aspect that i want to tell you that i have connect WP and A0 and A1 directly to ground i.e. no pull down resistors. IS this making the problem. If any one of you have a working circuit of 24C512 with PIC, please send it so that i can rechecked my connections. Please send me coding as well.

skimask
- 28th May 2008, 18:01
no pull down resistors
Pull DOWN resistors? Don't remember anything about Pull DOWN resistors.


If any one of you have a working circuit of 24C512 with PIC, please send it so that i can rechecked my connections
The '512 datasheet I've got has a fairly good schematic. What's your datasheet say?


Please send me coding as well.
???????????????????????????????????????????????
Not asking for much eh?

Jerson
- 28th May 2008, 18:19
Are you sure your PIC is working? Any test pins that you can blink / toggle to make sure ? I think you have all the coding right including the pin config. I doubt your watchdog / brownout is killing the code execution. This can be ruled out if you can blink a test pin every time you read

mat janssen
- 28th May 2008, 20:57
Dear all,
If you read the datasheet of a 24C512 then you wil see this:

"device address,first word address,second word address,data"

so your line


EWrite:
I2CWRITE SDA,SCL,%10100000,Addr,[E_ByteOut]

is not correct
it must be
EWrite:
I2CWRITE SDA,SCL,%10100000,Addr1,[Addr2,E_ByteOut]
the same for I2CREAD
Don't forget to mark the read bit.

mister_e
- 28th May 2008, 21:01
mmm... not sure as the address is a WORD here and PBP handle the WORD variable for address. It always worked for me with the EEPROM i used, i've never use the C512, but C1024.

If by FIRST they mean Most Significant, it has to work, unless, yes you're in a kind of trouble... but this said, if you read the same way you write, you shouldn't have any problem huh?

For no reason, i stick to a swap of SDA & SCL pin idea and 1.8-4.7K pull-up on both pins.

shaiqbashir
- 29th May 2008, 03:17
thanks for your help

mat jensson you have raised a nice point here. I have tried almost every possibility in my coding as well as in my connections, but its not working at all. Please explain me your idea,
i mean what do u mean by marking the read bit? and how can i specify two addresses, can u explain me a bit.

One more thing that i want to share with u all. I get different numbers as output when i connect SDA SCL on different ports.


when i have

SDA = portB.1
SCL= portB.0
output= 2

SDA = portD.1
SCL= portD.0
output= 2

SDA = portC.1
SCL= portC.0
output= 8

I just couldnt get it.



Regards,

Shaiq Bashir

Bruce
- 29th May 2008, 03:56
Make sure you have A0,A1,A2 on your EEPROM at ground.

Make these few changes to your existing code.

CNTRL VAR BYTE ' added for control byte
CNTRL = %10100000 ' value of control byte

EWrite:
I2CWRITE SDA,SCL,CNTRL,Addr,[E_ByteOut] ' Write out the byte
Pause 10 ' Delay 10ms after each write
Return

ERead:
I2CREAD SDA,SCL,CNTRL,Addr,[E_ByteIn] ' Read in the byte
Return

Does this work?

mister_e
- 29th May 2008, 04:30
For the really last time, i'm still sure your SDA/SCL are reverted and/or you don't have the right Pull-Up resistors on SDA & SCL, and i also endorse the A<2:0> setting.

OK why i stick to it ?

1. Because i already used something like that in one of my Radio Decode Dongle to confirm the end-user haven't swap the SDA/SCL line when reading in circuit the target I2C EEprom inside the radio.

2. Let's say i use a at24c1024 here with the following...

CONT CON %10100000
ADDR VAR WORD
ByteA VAR BYTE

SDA var PORTC.4
SCL var PORTC.3
PAUSE 500

Start:
for addr = 0 to 10
ByteA=ADDR.LOWBYTE
I2CWRITE SDA,SCL,CONT,ADDR,[ByteA]
PAUSE 20
NEXT

FOR ADDR=0 TO 10
I2CREAD SDA,SCL,CONT,ADDR,[ByteA]
HSEROUT ["ADDR=", DEC ADDR,_
" ByteA=", DEC ByteA,13,10]
NEXT

I got the expected..


ADDR=0 ByteA=0
ADDR=1 ByteA=1
ADDR=2 ByteA=2
ADDR=3 ByteA=3
ADDR=4 ByteA=4
ADDR=5 ByteA=5
ADDR=6 ByteA=6
ADDR=7 ByteA=7
ADDR=8 ByteA=8
ADDR=9 ByteA=9
ADDR=10 ByteA=10

But now if in my code i swap the SDA, SCL definition to...

SDA var PORTC.3
SCL var PORTC.4


i got...

ADDR=0 ByteA=8
ADDR=1 ByteA=8
ADDR=2 ByteA=8
ADDR=3 ByteA=8
ADDR=4 ByteA=8
ADDR=5 ByteA=8
ADDR=6 ByteA=8
ADDR=7 ByteA=8
ADDR=8 ByteA=8
ADDR=9 ByteA=8
ADDR=10 ByteA=8

Amazing how close to your problem it is huh?

Let's do it much funky now... let's totally remove the EEPROM from the circuit. With the pull-up installed i still have all those 8, without pull-up i got all 0.

Now, with the right code, but the EEPROM have the wrong A<2:0> setting...
i got all 16, reverted... all 8.

Enjoy!

sayzer
- 29th May 2008, 09:43
Let this be the schematic for reference.


<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2625&d=1212050571" >