View previous topic :: View next topic |
Author |
Message |
zaqwsx
Joined: 18 May 2012 Posts: 3
|
i2c coding with PIC18F45K20 |
Posted: Sun May 20, 2012 10:02 am |
|
|
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
|
SCL and SDA |
Posted: Sun May 20, 2012 10:26 am |
|
|
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: 9229 Location: Greensville,Ontario
|
|
Posted: Sun May 20, 2012 7:32 pm |
|
|
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
|
|
Posted: Tue May 22, 2012 10:30 am |
|
|
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
|
|
Posted: Tue May 22, 2012 11:21 am |
|
|
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: 1349
|
|
Posted: Tue May 22, 2012 2:26 pm |
|
|
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. |
|
|
|