CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Setup/enable I2C internal pull-ups

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Torello



Joined: 29 Sep 2006
Posts: 114

View user's profile Send private message

Setup/enable I2C internal pull-ups
PostPosted: Tue Apr 30, 2024 11:25 pm     Reply with quote

Hi,
How do I enable/setup the internal pull-ups for I2C? Preferably with a CCS build-in function.
I'm using the PIC18F26Q83 that does have them.
I would only like pull-ups on those two pins (C3/C4)
_________________
Regards, Edwin. PCWHD v5.114
Ttelmah



Joined: 11 Mar 2010
Posts: 19260

View user's profile Send private message

PostPosted: Wed May 01, 2024 2:00 am     Reply with quote

OK.
Several things. The internal pullups are _only_ acceptable for slow speed
I2C, with a very short bus (low capacitance). If you have a bus that is
more than perhaps 150mm long, or uses more than perhaps 2 devices,
or clocks over perhaps 200KHz, you will have to provide external pullups.
Technically you can enable fast mode on the pins with this, and turn the
current up to 20* the default level, but reviews from users are suggesting
this can be unreliable. So keep things slow if you want this to work.
100KHz max.
Now the good thing is you are on the standard I2C pins (you must still
set PPS to these pins). Only the pull ups on these pins support the
current needed for I2C.
Code:

#include <maini2c18.h>

#pin_select SCL1=PIN_C3
#pin_select SDA1=PIN_C4
#use i2C(I2C1, slow=100000)
#BYTE RC3I2C=getenv("SFR:RC3I2C")
#BYTE RC4I2C=getenv("SFR:RC4I2C")
#define TENTIMES 0x20
#define TWENTYTIMES 0x30
#define MASK 0xC3

void main()
{
   port_c_pullups(0b00001100); //enable weak pullups
   RC3I2C = (RC3I2C & MASK) | TENTIMES; //set current to ten times on C3
   RC4I2C = (RC4I2C & MASK) | TENTIMES; //set current to ten times on C4

//etc..
Torello



Joined: 29 Sep 2006
Posts: 114

View user's profile Send private message

PostPosted: Wed May 01, 2024 3:54 am     Reply with quote

Thanx Ttelmah,

I've found more a less the same and was also struggling to get to 400Khz. The use_i2C is quite fuzzy and not always working with given parameters as described in the manual

Code:

---- In Header file:
#bit FME=getenv("BIT:FME")               //'0'= (postscale5, only 10x or 2c PU-currents) '1'= (postscale4, 20, 10, 5x PU-currents)
#byte RC4I2C=getenv("SFR:RC4I2C")
#byte RC3I2C=getenv("SFR:RC3I2C")
#byte I2C1BAUD = getenv("SFR:I2C1BAUD")
#define pSCL            PIN_C3
#define pSDA            PIN_C4
#pin_select SCL1OUT = pSCL
#pin_select SCL1IN  = pSCL
#pin_select SDA1OUT = pSDA
#pin_select SDA1IN  = pSDA
#use i2c(stream=MCP9808_Stream, Master, SDA=pSDA, SCL=pSCL, I2C1, CLOCK_SOURCE=HFINTOSC)

---- In C-module:

I2C1BAUD=2;                                                                                      // Scale HFINTOSC by 2+1 and that by 4 (FME=1) ->F_SCL= 4Mhz/12= 333Khz
FME=1;                                                                                           // Div=4, selectable PU-currents: 20, 10, 5x; -> R_pull approx.= 1k1, 2k2, 4k4)
RC3I2C=RC4I2C=0b01010001;                                                                        // fast slew, Rpull=2k2, I2C thresholds

_________________
Regards, Edwin. PCWHD v5.114
Ttelmah



Joined: 11 Mar 2010
Posts: 19260

View user's profile Send private message

PostPosted: Wed May 01, 2024 5:12 am     Reply with quote

Remember you have to do both pins. What you show only sets C3.....
The pins are individual.
You are selecting fast slew for 400K, but not setting a baud rate for the
#use I2C. Use a baud rate here, rather than writing to I2C baud.
However don't try to think of the internal pullups as being equivalent
to a resistor value. They are not, they are programmed current sources.
So draw more when the signal is low and decline as the signal goes up.
this is why people are finding them not reliable at higher baud rates....
Don't put the pins in the #use I2C. Using I2C1 is all you need. The compiler
(at least on current versions), automatically sets bot the 'in' and 'out', if
you just specify the signal as I show.
You do need to enable the weak pull up though. Very Happy
Torello



Joined: 29 Sep 2006
Posts: 114

View user's profile Send private message

PostPosted: Wed May 01, 2024 8:48 am     Reply with quote

Thanx for your extensive help!
I think am doing both pins: RC3I2C=RC4I2C=0b01100001;
~~
Maybe a complier bug (i'm stuck at 5.114)
Fast=xxxxxx in the #use_i2C(..) does not work. It compiles ok but the device is not working on the correct freq.
If I comment out the I2CBaud=2 and FME=1
#use i2c(stream=MCP9808_Stream, Master, I2C1, CLOCK_SOURCE=HFINTOSC, FAST) compiles ok but F-SCL=~800Khz...
#use i2c(stream=MCP9808_Stream, Master, I2C1, CLOCK_SOURCE=HFINTOSC, FAST=100000) same as above
In both cases the read back of the I2Cbaud=0 and FME=0. Thus the clock source (4Mhz) only gets div'd by FME=0 (=div 5). Which makes ~800khz
And the fast=xxxxxx hz parameter doesn't do anything. I know the exact frequency can't be made. But then I would expect the closest.
The parameter CLOCK_DIVISOR does work. But that only sets the bit FME.
I2C_Speed() function raises a compiler error.
So looks like using the I2C1Baud register is the only thing that works for now.
~~~
What you describe is a resistor pull-up behaviour, I would say. As the voltage across the resistor declines the current does also.
A good current source would keep the current the same independand of Vpin. The slope of the signals would then be more a straight line.
But they are not. Maybe caused by the slew-rate control?
~~~
Indeed I can leave the SDA SCL pins out. I2C1 is enough.
~~~
The PU bits in the RC3I2C/RC4I2C doesn't need the weak to be enabled, for what I see on the device.
I tested it by setting the bits explicitly low, readback and verify they are/stay low
I also watched the slope of the SCL impove at higher currents and still keeping the weak pull-ups disabled
_________________
Regards, Edwin. PCWHD v5.114
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
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


Powered by phpBB © 2001, 2005 phpBB Group