PDA

View Full Version : PIC18F14K50 and i2cread/i2cwrite on PORT C



RuudNL
- 27th February 2012, 11:24
I am trying to use i2cread and i2cwrite on a PIC18F14K50.
PORTC.6 = Clock, PORTC.7 = Data.
Of course I know that there is an issue with the comparator/analog in settings.
So I am using:

ANSEL = 0
ANSELH = 0

to configure PORTC for digital I/O.
But i get the impression that this is not good enough...
When I measure the voltage on the clock pin (C6), it is low but can easily be pulled high with a resistor connected between PORTC.6 and +5V, giving me the impression that this pin is not configured as a digital output.
Am I still missing something?
(I prefer PORTC.6 and PORTC.7 because this would make the PCB design easier...)

RuudNL
- 27th February 2012, 15:12
Mmm... To eliminate things, I tried to use i2cread/i2cwrite on PORTB.4 (Clock) and PORTB.5 (Data), but this also doesn't seem to work.
TRISB=0 sets the outputs to a solid low level, but the I2C commands are still not working.

Darrel Taylor
- 27th February 2012, 15:46
You need those pull-up resistors on both pins for I2C.
2.2 or 4.7K.

RuudNL
- 27th February 2012, 18:22
You need those pull-up resistors on both pins for I2C.
2.2 or 4.7K.

Yes, I know that this is an often made mistake.
But of course I have those pull-up resistors connected!
The strange thing is that everything worked fine with a PIC18F4550, but not with a PIC 18F14K50.
When I look with an oscilloscope on the data and clock lines, I don't see any signal at all while sending an I2C command.

Darrel Taylor
- 28th February 2012, 02:33
TRISB=0 sets the outputs to a solid low level, but the I2C commands are still not working.
Don't do that.
I2C requires the pins to be in INPUT mode when not transfering data.
The pins are never driven high, only low, which is why you need the pull-up resistors.

Since it's a 14K50, you're probably running at 48Mhz.
So you may want to add ...

DEFINE I2C_SLOW 1

Depending on if the device you're talking to is a 100Khz I2C part.
If it's a 400Khz part, you won't need it.

RuudNL
- 28th February 2012, 10:01
I know...
The only reason I tried TRISB=0 is because I wanted to check if I could use the pins as a digital output.
In the meantime I found the problem. In fact, it had nothing to do with the i2cread/i2cwrite commands.
The crystal I used apparently had a low activity. Sometimes the oscillator was working, but most of the time it was not!
I changed the crystal and now it works! (Maybe changing the values of the capacitors would have worked, but I did not try that.)