PDA

View Full Version : Whats wrong with this I2C code???? HELP?



yourmomOS
- 10th July 2006, 02:06
Im using a 16f628 and a 24LC512 i2c eeprom. Im pretty sure I have the eeprom wired up correctly as per the datasheet. I put in some labels to jump to incase the i2cwrite or read didnt work, I never get to them so Im assuming I am writing correctly, When I read, I get weird junk. I cant figure it out, someone please look at the code below and tell me what is wrong!

'set OSC freq
Define OSC 20
' Set receive register to receiver enabled
DEFINE HSER_RCSTA 90h
' Set transmit register to transmitter enabled
DEFINE HSER_TXSTA 20h
' Set baud rate
DEFINE HSER_BAUD 9600
'clear flag automatically when serial hardware buffer overflows
DEFINE HSER_CLROERR 1
CMCON = 7 'Turn off comparator

dpin var PORTA.0 'eeprom data pin
cpin var PORTA.1 'eeprom clock pin
eaddress var word 'eeprom address
temp var byte

MAIN:
HSEROUT ["Working", 10, 13]

temp = 40
eaddress = 0
i2cwrite dpin, cpin, $A0, eaddress, [temp], DIDNTWRITE
pause 200
temp = 41
eaddress = 1
i2cwrite dpin, cpin, $A0, eaddress, [temp], DIDNTWRITE
pause 200
temp = 42
eaddress = 2
i2cwrite dpin, cpin, $A0, eaddress, [temp], DIDNTWRITE
pause 200
temp = 43
eaddress = 3
i2cwrite dpin, cpin, $A0, eaddress, [temp], DIDNTWRITE
pause 200

hserout ["done writing to eeprom", 10, 13]

eaddress = 0
i2cread dpin, cpin, $A0, eaddress, [temp], DIDNTREAD
pause 200
hserout [temp]
eaddress = 1
i2cread dpin, cpin, $A0, eaddress, [temp], DIDNTREAD
pause 200
hserout [temp]
eaddress = 2
i2cread dpin, cpin, $A0, eaddress, [temp], DIDNTREAD
pause 200
hserout [temp]
eaddress = 3
i2cread dpin, cpin, $A0, eaddress, [temp], DIDNTREAD
pause 200
hserout [temp]

hserout ["done reading", 10, 13]
pause 2000
goto main

DIDNTWRITE:
HSEROUT ["WRITE DIDNT HAPPEN"]
PAUSE 1000
GOTO MAIN

DIDNTREAD:
HSEROUT ["DIDNT READ"]
PAUSE 1000
GOTO MAIN

mister_e
- 10th July 2006, 02:13
......................... forget me, i didn't read correctly

i need to sleep

yourmomOS
- 11th July 2006, 22:21
Dont everyone help at once now!

mister_e
- 12th July 2006, 05:44
something is wrong in your hardware and/or configuration fuses... it's working here. Be sure you have place 4.7K or lower on the scl and SDA line and it should work... unless you have reverted the SDA and SCL

If at least you have serial communication, the PIC is working at the great and at the right speed wich reject the config fuses problem

but i don't have the LC512, as now i didn't have any requirement for those... just the LC128 so it can exist some difference between... wich i doubt after a really fast datasheet reading.

yourmomOS
- 12th July 2006, 17:48
The DIDNTWRITE and DIDNTREAD are never jumped to when it does not read or write. I have even removed the cables going to the eeprom but it never goes to the label. I have a 24LC512 and a pic 16f628. Has anyone seen this before?

mister_e
- 12th July 2006, 20:33
You have a hardware problem... OR you don't have pull-resistor as shown in the PBP manual and in the datasheet, OR your PIC is defect, OR you have some short circuit somewhere.

EEprom with Pull up
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=968&stc=1&d=1152732703">

EEPROM no pull-up
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=967&stc=1&d=1152732703">

NoEEPROM With pul-ups
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=969&stc=1&d=1152732703">

mister_e
- 12th July 2006, 20:33
NoEEPROM No Pull ups
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=970&stc=1&d=1152732703">

yourmomOS
- 13th July 2006, 15:18
I started playing around and found out that if I disconnect the ground wire, the things start working. I tried different chips, different slots in my breadboard and different connector wires. Its the damndest thing. pin 4 should be connected to ground, but spits out garbage when I connect it, works when I disconnect it.