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 coding with PIC18F45K20

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



Joined: 18 May 2012
Posts: 3

View user's profile Send private message

i2c coding with PIC18F45K20
PostPosted: Sun May 20, 2012 10:02 am     Reply with quote

Hi

I am trying to use PIC18F45K20 44pins MCU on PICKIT3(http://ww1.microchip.com/downloads/en/DeviceDoc/41303G.pdf) to program the LT6904 oscillator (http://cds.linear.com/docs/Datasheet/69034fe.pdf) but I have no idea how to start.

I would like to know should SCL and SDA be input or output for the PIC? From the data sheet of PIC (page 202) it says that these pins are input which make me wondering how it send the data if these pins are input.

these are some settings that I have set

TRISCbits.TRISC2 = 0; //ADR
TRISCbits.TRISC3 = 0; //SCL
TRISCbits.TRISC4 = 1; //SDA

SSPCON1bits.SSPEN = 1; //Enable
SSPCON1bits.SSPM3 = 1; // 1000 = I2C Master mode, clock =
//FOSC/(4 * (SSPADD + 1))
SSPCON1bits.SSPM2 = 0; //
SSPCON1bits.SSPM1 = 0; //
SSPCON1bits.SSPM0 = 0; //
SSPSTATbits.SMP = 1; //100kHz

Hope that anyone with experiences in I2C can guide me to properly setup the PIC

Thanks
_________________
Thank you
zaqwsx
mtsoule



Joined: 19 Oct 2011
Posts: 31

View user's profile Send private message

SCL and SDA
PostPosted: Sun May 20, 2012 10:26 am     Reply with quote

SCL is your clock and it is an output from the pic, which I believe is your master device, SDA will be an input and an output, it is bidirectional.

You have to make them inputs or outputs, but the compiler does most of the work for you.

I do believe.
temtronic



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

View user's profile Send private message

PostPosted: Sun May 20, 2012 7:32 pm     Reply with quote

Best place to start is looking at any of the several examples in the examples folder that CCS supplies. Find one, like an RTC driver and see what's going on. Compare that code to your device.
All I2C devices perform in a similar fashion and be SURE to add the correct I2C bus pullup resistors. For 5V PICs 3k3r or 4k7r will probably work.
Read the device datasheet, Google 'your device and PIC', search this forum and look into the 'code library' forum as well. It's unlikely you're the first one to ever use that device with a PIC and C code..so you'll probably find 'driver' code on the Web.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue May 22, 2012 10:30 am     Reply with quote

You didn't state how much experience you have with PIC's so I'm not sure how 'basic' to go.

Don't try anything with the TRIS registers, let the compiler do all of that for you.

Use the build-in functions for I2C; i2c_start(), i2c_stop(), i2c_write() & i2c_read().

Makre sure you have pull-up resistors on each of the SDA & SCL lines, usually around 4.7K depending on the capacitance of the lines.

Read the communications spec sheet, of the LT6904, until your eyes are ready to bug out. Understanding it's requirements is important.

If you have an oscilloscope you can monitor the bus lines to see if they are talking to each other. Write a simple program that will make the device ACK back. ACK means the slave device saw it's address placed on the bus line and recognized that address as it's own and 'answered' back that it's listening for commands now.

There are several posts for I2C devices on this forum. Start simple and once you get the slave device ACKing back then progress from there.

Ronald
zaqwsx



Joined: 18 May 2012
Posts: 3

View user's profile Send private message

PostPosted: Tue May 22, 2012 11:21 am     Reply with quote

Hi Ronald

I have used PIC16F887 before but this is the first time I use PIC18F45K20. I thought I would have to set the data direction or else the PIC would not be functioning well. There seems to have no built-in i2c header files in my compiler. I am using MPLAB IDE 8.8

Thanks
_________________
Thank you
zaqwsx
jeremiah



Joined: 20 Jul 2010
Posts: 1322

View user's profile Send private message

PostPosted: Tue May 22, 2012 2:26 pm     Reply with quote

zaqwsx wrote:
Hi Ronald

I have used PIC16F887 before but this is the first time I use PIC18F45K20. I thought I would have to set the data direction or else the PIC would not be functioning well. There seems to have no built-in i2c header files in my compiler. I am using MPLAB IDE 8.8

Thanks


CCS doesn't rely on header files for the built ins. You need to look up #use i2c() in the compiler manual. Using that will turn on the i2c functions.
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