Setting up I2C on 18F4550?

Status
Not open for further replies.

ClydeCrashKop

Well-Known Member
Most Helpful Member
Hi Guys
I am trying to use an ADXL345 Accelerometer and 18F4550 in Hi-Tech C for PIC18 MCU Family with the hardware MSSP I2C port.
RB0 is SDA, RB1 is SCL. I want the 100 KHz clock but so far no clock signal. Both have pull up resistors to 3V.
I am using a 20MHz crystal.
#include <pic18.h>
#include <htc.h>
#include <stdio.h>
/* Configuration words for a PIC18F4450
* with a 20 MHz crystal oscillator
*/
__CONFIG(1,USBPLL&PLLDIV5&CPUDIV4&HSPLL&FCMEN&IESOEN);
__CONFIG(2,VREGDIS&PWRTEN&BORDIS&BORV42&WDTDIS&WDTPS32K);
__CONFIG(3,PBDIGITAL&LPT1DIS&MCLREN);
__CONFIG(4,XINSTDIS&STVRDIS&LVPDIS&ICPORTDIS&DEBUGDIS);
__CONFIG(5,UNPROTECT);
__CONFIG(6,UNPROTECT);
__CONFIG(7,UNPROTECT);
#define _XTAL_FREQ 20000000 // Xtal speed
TRISBbits.TRISB0 = 1; /* SDA and SCL as input pin */
TRISBbits.TRISB1 = 1; /* these pins can be configured either i/p or o/p */
SSPSTAT |= 0x80; /* Slew rate disabled */
SSPCON1 = 0x28;//Enable SDA and SCL, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1))
/* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */
SSPCON2 = 0x00; // Reset MSSP Control Register
SSPADD = 49; //20000000 / 4= 5000000 5000000/ 100000= 50 50-1=49
PIR1bits.SSPIF=0; // Clear MSSP Interrupt Flag

Does anyone see what I am overlooking or doing wrong to set up the hardware I2C?
 
Virtually the same as mine..
C:
void InitI2C()
    {
    TRISBbits.TRISB0=1;
    TRISBbits.TRISB1=1;
    SSPCON1=0b00101000;
    SSPCON2=0;
    SSPSTAT=0b10000000;
    SSPADD=50; 
    PIR1bits.SSPIF=0;
    PIR2bits.BCLIF=0;
    }
I see PORTB as digital... in your config... So I can't see why it wouldn't work!!!
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…