Problem with pic18f4550 I2C interface - interfacing with EEPROM

Author
nprasad.atluri
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2012/11/06 23:05:46
  • Location: 0
  • Status: offline
2013/02/12 07:49:01 (permalink)
0

Problem with pic18f4550 I2C interface - interfacing with EEPROM

Hello
 
Help will be much appreciated.  I've been having a lot of trouble with the I2C interface with pic18f4550 with an EEPROM.  Any working sample codes will be much appreciated.  My code is pasted below.  Please look at it  - look at the EEPROM READ and EEPROM WRITE functions if the sequencing is wrong.  I have used the in-built functions from ' i2c.h ' and the code written for them is there in the 'src' folder in the MPLABX IDE.  Finally I need to write it for pic18f26k22.  I am trying it first on this.  Here is the code for pic18f4550.
 

#include "p18f4550.h"
#include "delays.h"
#include "i2c.h"


#pragma config FOSC = HS
#pragma config PLLDIV = 1               // Divide by 5 to provide the 96 MHz PLL with 4 MHz input
#pragma config CPUDIV = OSC1_PLL2 // Divide 96 MHz PLL output by 2 to get 48 MHz system clock
#pragma config USBDIV = 2               // USB clock comes from 96 MHz PLL output / 2
#pragma config FCMEN = OFF // Disable Fail-Safe Clock Monitor
#pragma config IESO = OFF // Disable Oscillator Switchover mode
#pragma config PWRT = OFF // Disable Power-up timer
#pragma config BOR = OFF // Disable Brown-out reset
#pragma config VREGEN = ON // Use internal USB 3.3V voltage regulator
#pragma config WDT = OFF // Disable Watchdog timer
#pragma config MCLRE = ON // Enable MCLR Enable
#pragma config LVP = OFF // Disable low voltage ICSP
#pragma config ICPRT = OFF // Disable dedicated programming port (44-pin devices)
#pragma config CP0 = OFF // Disable code protection
#pragma config PBADEN = OFF             //Disable analog operation on reset for portb




#define write_address 0xA0
#define read_address  0xA1






void writeEEPROM(unsigned char address,unsigned char data);
unsigned char readEEPROM(unsigned char address);
void LedBlink(void);
void I2Cinit(void);


void main(void){
    unsigned char eepromdata;


    I2Cinit();//Initialize I2C


    TRISDbits.TRISD0 = 0;//make portd first pin output
    TRISDbits.TRISD1 = 0;//make portd second pin output




    while(1){


                writeEEPROM(0x00,1);
                Delay10KTCYx(138); //give 500ms delay
                eepromdata = readEEPROM(0x00);
                Delay10KTCYx(138);//give 500ms delay


                if(eepromdata == 1)
                    LATDbits.LATD0 = 1;
                else
                    LATDbits.LATD0 = 0;


                LedBlink(); //blink Led on LATD1


           }
}


void I2Cinit(void){
//Configure I2C pins as inputs
    TRISBbits.TRISB0 = 1;//SDA
    TRISBbits.TRISB1 = 1;//SCL
    //Configure I2C
    OpenI2C(MASTER,SLEW_OFF); //I2C config in Master mode and with 100KhZ frequency
    SSPADD = 0x27; //100khz frequency for 100MhZ oscillator
}




void writeEEPROM(unsigned char address,unsigned char data){
    unsigned char temp;
    temp = address>>8;


    StartI2C();//send the start condition
    IdleI2C();


    WriteI2C(write_address);// send the device address with the write condition
    IdleI2C();


    WriteI2C(address);// send the high register address
    IdleI2C();


    WriteI2C(address);//send the low register address
    IdleI2C();


    WriteI2C(data);//send the data
    IdleI2C();


    StopI2C();
}




unsigned char readEEPROM(unsigned char address){


    unsigned char data;


    StartI2C();
    IdleI2C();


    WriteI2C(write_address);//send the device address
    IdleI2C();


    WriteI2C(address);//send higher register address
    IdleI2C();


    WriteI2C(address);//send lower register address
    IdleI2C();


    RestartI2C();
    IdleI2C();


    WriteI2C(read_address);//send the device address
    IdleI2C();


    data = ReadI2C();
    IdleI2C();


    StopI2C();// stop the I2C


    return data;
}




void LedBlink(void){
    LATDbits.LATD1 = 1;
    Delay10KTCYx(138);//give 500ms delay
    Delay10KTCYx(138);//give 500ms delay


    LATDbits.LATD1 = 0;
    Delay10KTCYx(138);//give 500ms delay
    Delay10KTCYx(138);//give 500ms delay


}

Thank you for your help people.

Naren

#1

4 Replies Related Threads

    kalpak
    Super Member
    • Total Posts : 3265
    • Reward points : 0
    • Joined: 2004/03/12 23:01:40
    • Location: India
    • Status: offline
    Re:Problem with pic18f4550 I2C interface - interfacing with EEPROM 2013/02/12 09:11:24 (permalink)
    0
    What is the specific problem?
    #2
    nprasad.atluri
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2012/11/06 23:05:46
    • Location: 0
    • Status: offline
    Re:Problem with pic18f4550 I2C interface - interfacing with EEPROM 2013/02/12 22:29:01 (permalink)
    0
    I see nothing happen.  I put an LED blink at a point where I blink it continuously if what I read from the EEPROM is exactly what I wrote to the EEPROM.  There is definitely some debugging to be done here - how can I go about it ? And also do you think the library/functions that MPLABX provides would work correctly ?
     
    Thank you very much.
     
    Naren
    #3
    nprasad.atluri
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2012/11/06 23:05:46
    • Location: 0
    • Status: offline
    Re:Problem with pic18f4550 I2C interface - interfacing with EEPROM 2013/02/12 23:46:55 (permalink)
    0
    I see it is hanging at I2C Read - where we write the Slave Address with the Read bit into the Slave (EEPROM) - it is hanging there - it probably doesn't get out of some loop there or the program just crashes - not sure.
    #4
    pityukecske
    Super Member
    • Total Posts : 396
    • Reward points : 0
    • Joined: 2011/01/13 03:21:31
    • Location: Marosvásárhely
    • Status: offline
    Re:Problem with pic18f4550 I2C interface - interfacing with EEPROM 2013/02/13 02:25:47 (permalink)
    +2 (1)
    hi,
    Are you sure, your I2C bus is up and running? If you have a scope or analyzer around check that. 
    Is your slave device sending the acknowledge bit?
    [i doubt, that 100MHz is ok, i remember a maximum of 48MHz for that chip.
     
    pityu
    #5
    Jump to:
    © 2017 APG vNext Commercial Version 4.5