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

I2C problems with PIC18F47K42

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



Joined: 26 Feb 2018
Posts: 5

View user's profile Send private message

I2C problems with PIC18F47K42
PostPosted: Mon Feb 26, 2018 4:25 am     Reply with quote

Hello

I am having trouble with using the I2C HW of a pic18F47K42. If i use FORCE_SW, everything works. I am using following to configure the I2C:

Code:

//Select I2C with PPS

#PIN_SELECT SCL1IN = PIN_C3
#PIN_SELECT SCL1OUT = PIN_C3
#PIN_SELECT SDA1IN = PIN_C4
#PIN_SELECT SDA1OUT = PIN_C4

#use i2c(Master,Slow,I2C1,FORCE_HW)


I tried I2C with the XC8 compiler and the MCC (microchip code generator). This works, so i am sure that my chip is working OK.

Somebody that knows what i am doing wrong?

I tried to do following, but with no success:
- Set pins as open drain.
- Set them as input or as output
- Using I2C1 or define pins again in #use i2c

Thanks in advance.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Feb 26, 2018 6:25 am     Reply with quote

I would use this:
Code:

//Select I2C with PPS
#PIN_SELECT SCL1 = PIN_C3
#PIN_SELECT SDA1 = PIN_C4

//Using the peripheral name always forces hardware
#use i2c(Master,Slow=100000,I2C1)


You only need to map separate in/out functions, where these are separate paths on the same peripheral. I don't think these are separate on this peripheral, and the SCL in, is for the slave device only.
BartDelboo



Joined: 26 Feb 2018
Posts: 5

View user's profile Send private message

PostPosted: Mon Feb 26, 2018 7:40 am     Reply with quote

Ok, i tried the code you suggested, but still no activity at all on the I2C bus.
If i look with my scope, i see that SDA and SCL lines remain high.
Some other ideas? Someone already tested I2C with this type of PIC (18F47K42)?

I find it strange that FORCE_SW and XC8 compiler work. Can it be that there is a fault in the compiler or the PIC header file?

Thanks!
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Feb 26, 2018 7:51 am     Reply with quote

Simple to see, just dump both the listings of the compiled code, and compare.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Feb 26, 2018 9:16 am     Reply with quote

I suggest you ask CCS. This is very much a beta chip at present. The current compiler is the first listing it.....
It's only just been added to the devices supported list.
MicroChip always have the advantage of writing the compiler before the chips are released. CCS have to wait till they are given data to start.
Talk to them nicely, and they will probably give you an example.
They do show SDA in and out as two separate mappings, so I'd actually expect it to need:
Code:

//Select I2C with PPS
#PIN_SELECT SCL1 = PIN_C3
#PIN_SELECT SDA1IN = PIN_C4
#PIN_SELECT SDA1IOUT = PIN_C4

//Using the peripheral name always forces hardware
#use i2c(Master,Slow=100000,I2C1)

I'd also be explicitly ensuring the analog is off on those pins, and that the timer2 input is disabled.
jartur32



Joined: 24 Feb 2014
Posts: 10

View user's profile Send private message

Jartur
PostPosted: Mon Dec 10, 2018 6:06 pm     Reply with quote

Hello BartDelboo, could you check this way, maybe works with the PIC18F47K42 ?

Code:

// Active the fast io
#use fast_io(c)

// Define your pin names
#define SCL_PIN                     PIN_C3
#define SDA_PIN                    PIN_C4

// I2C declaration code
#pin_select SCL1OUT = SCL_PIN
#pin_select SCL1IN = SCL_PIN
#pin_select SDA1OUT = SDA_PIN
#pin_select SDA1IN = SDA_PIN
#use i2c( MASTER, FAST, I2C1, FORCE_HW )

// init funtion add this line to active the open drain
// and use this pin like inputs
void init(void)
{
   set_tris_c(0b00011000);              // SDA and SCL like input
   set_open_drain_c(0b00011000);  // SDA and SCL like open drain
   output_c(0x00);
}
void main(void)
{
   do{
   // code
   }while(TRUE);                            // Infinite loop
}


Let me know your result test, and have a good day.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Tue Dec 11, 2018 8:00 am     Reply with quote

I have also had this happen with v5.078 with PIC16F1939 and PIC16F19175. It only worked after I added the FORCE_SW option. It did not work if I specified FORCE_HW or left it blank. One difference is that I did not use I2C1/I2C2, and instead specified the SCL and SDA pin separately. I never bothered to try and fix it though.

The thing is I'm a bit fuzzy on the details and I don't have the hardware set up anymore to test it out.

On PIC24FJ128GA204, with v5.078 of CCS, I don't have any issues with using the hardware module with #use i2c.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue Dec 11, 2018 10:02 am     Reply with quote

It appears that for this particular chip, the routines to actually talk to the I2C
hardware are missing!...

The #PIN_SELECT, is working fine (walked through and checked the
registers all OK).
However when you call any I2C routine setup to use the hardware, no code
is being generated.....
Code:

....................     
....................    i2c_start(ROM);
....................    i2c_write(ROM,0xA0);
....................    i2c_write(ROM,0);
....................    i2c_write(ROM,0xA1);
....................    test=i2c_read(ROM);
00076:  MOVFF  00,test
....................    i2c_stop(ROM);


The only line that generates anything is the I2C_read, and this only zero.

It appears that the device database (at least here), does not know about the
peripheral. Yet the #USE does.

So no code bodges work.
soonc



Joined: 03 Dec 2013
Posts: 215

View user's profile Send private message

Re: I2C problems with PIC18F47K42
PostPosted: Wed Feb 13, 2019 4:32 pm     Reply with quote

BartDelboo wrote:
Hello

I am having trouble with using the I2C HW of a pic18F47K42. If i use FORCE_SW, everything works. I am using following to configure the I2C:

Code:

//Select I2C with PPS

#PIN_SELECT SCL1IN = PIN_C3
#PIN_SELECT SCL1OUT = PIN_C3
#PIN_SELECT SDA1IN = PIN_C4
#PIN_SELECT SDA1OUT = PIN_C4

#use i2c(Master,Slow,I2C1,FORCE_HW)


I tried I2C with the XC8 compiler and the MCC (microchip code generator). This works, so i am sure that my chip is working OK.

Somebody that knows what i am doing wrong?

I tried to do following, but with no success:
- Set pins as open drain.
- Set them as input or as output
- Using I2C1 or define pins again in #use i2c

Thanks in advance.


I'm using the PIC18F46K42 and 47K42... using I2C2 or FORCE_HW does not work. Using the pins does work.

Remember to use the hardware you should be using the
i2c_transfer() and not the old style code.

#define SCL_PIN PIN_B1 // using SCL2
#define SDA_PIN PIN_B2 // using SDA2

#pin_select SCL2OUT = SCL_PIN
#pin_select SDA2OUT = SDA_PIN

//#use i2c(MASTER, I2C2) // Does not work
//#use i2c(MASTER, scl=SCL_PIN, sda=SDA_PIN, FORCE_HW) // not working

#use i2c(MASTER, scl=SCL_PIN, sda=SDA_PIN, FAST) Works !
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