PDA

View Full Version : i2cRead i2cwrite very frustrating



bradb
- 6th September 2011, 22:19
I had a program I wrote w/ PBP2.5 using a PIC 18F4550, everything worked great. I am ditching USB and going zigbee, so I'm changing from the 18F4450 to an PIC18F46K22. In the process I have also upgraded to PBP3.

This code from PBP2.5 on an 18F4450 works flawlessly:


Define OSC 48
C0B0W CON %10100000 'Chip 0, Bank 0, Write
C0B1W CON %10101000 'Chip 0, Bank 1, Write
C0B0R CON %10100001 'Chip 0, Bank 0, Read
C0B1R CON %10101001 'Chip 0, Bank 1, Read
DEFINE I2C_SLOW 1 'have to use slow for the i2c LCD
I2CLCD CON 80
SDA VAR PORTB.0
SCL VAR PORTB.1
I2CADDRESS VAR WORD
SECONDS VAR WORD
i2CTEST VAR BYTE[8]
TESTLCD VAR BYTE[8]
Main:
I2CADDRESS = 0
I2CWRITE SDA,SCL,C0B0W,I2CADDRESS,[65,66,67,68,69,70,71,72,73]
pause 50
i2cread SDA,SCL,C0B0R,I2CADDRESS,[STR I2CTEST\8]
pause 50
HSEROUT ["<TEST>", STR I2CTEST\8, "</TEST>"]

Seconds = Seconds + 1
arraywrite TestLCD ,[DEC4 Seconds]
I2CWRITE SDA,SCL,I2CLCD,[$FE,$45,$40,STR TestLCD\4]
Goto Main



This code, on the 18f46k22 does NOT work. I can write the to I2C LCD, but I can NOT read/write to the i2c eeproms.


Define OSC 64
C0B0W CON %10100000 'Chip 0, Bank 0, Write
C0B1W CON %10101000 'Chip 0, Bank 1, Write
C0B0R CON %10100001 'Chip 0, Bank 0, Read
C0B1R CON %10101001 'Chip 0, Bank 1, Read
DEFINE I2C_SLOW 1 'have to use slow for the i2c LCD
I2CLCD CON 80
SDA VAR PORTC.4
SCL VAR PORTC.3
I2CADDRESS VAR WORD
SECONDS VAR WORD
i2CTEST VAR BYTE[8]
TESTLCD VAR BYTE[8]
Main:
I2CADDRESS = 0
I2CWRITE SDA,SCL,C0B0W,I2CADDRESS,[65,66,67,68,69,70,71,72,73]
pause 50
i2cread SDA,SCL,C0B0R,I2CADDRESS,[STR I2CTEST\8]
pause 50
HSEROUT ["<TEST>", STR I2CTEST\8, "</TEST>"]

Seconds = Seconds + 1
arraywrite TestLCD ,[DEC4 Seconds]
I2CWRITE SDA,SCL,I2CLCD,[$FE,$45,$40,STR TestLCD\4]
Goto Main


I have 3 bread boards.
Breadboard 1 has an 18f4550 and all the related hardware specific to make the chip run.
Breadboard 2 has an 18F46k22 and all the related hardware specific to make the chip run.
Breadboard 3 has all my I2c eeproms and LCDs connected to it, so I can move it between breadboard 1 and 2.

The only difference between breadboard 1 and 2 as far as the I2C goes, the ports for SDA/SLC are different on the chips.

Both Setups work to the LCD via i2c. Both chips count up and display correctly to the LCD correctly, so I know I2C is working.

Only the 18F4550 Reads and writes to the eeprom. I get <TEST>ABCDEFGH</TEST> back via the serial line on the 18F4550.

On the 18F46K22 I get <TEST>........</TEST> (the .'s are hex 00)

The Eeprom is 24FC1025.

I have tried to isolate this as much as possible, but I'm lost. Any clue? Is something different on PBP3 vs PBP2.5 as far as I2C?

Dave
- 6th September 2011, 22:57
Bradb, I beleive you have to pass the constant "C0B0W" to a variable to have it work as the last bit will be changed whether it is a Write or a Read instruction. At least that is the way I have used it in the past 3 years and on all of my products that use the 24LC1025's. I have never had a problem with them and, infact are using 2 of them w/18F26K22 for my newest project.

bradb
- 6th September 2011, 23:10
Dave,
Agreed about the last bit, but I'm using 2 different constants, C0B0W and C0B0R (chip0 bank0, read or write) to address the last bit issue.

So that's not it....

bradb
- 7th September 2011, 00:59
I found it...
I was missing...

ANSELC.3 = 0
ANSELC.4 = 0

I had moved to port D during my testing, so actually mine was
ANSELD.0 = 0
ANSELD.1 = 0

Why doesn't I2CWRITE and I2CREAD auto set these like it does for SERIN and SEROUT??? Or At least mention that it needs done in the manual. 7 hours of my life I'll never get back. (I'll also never forget ANSEL needs set on a I2C!!)

Dave
- 7th September 2011, 20:48
Bradb, Good to hear you got it working. I didn't see the port config bit's in your code snipit so I assumed you had everything set correctly.