I am writing code for a PIC32MX250F128B to interface with the Microchip 24LC02B EEPROM with I2C interface. I have followed the datasheet of the EEPROM, the PIC32 and section 24 of the PIC32 family reference manual very closely and attempted to write code to interface with the EEPROM. I do not get any "hang" on any of the tests for the bits that need to be cleared/set after eg. an ACK is received - the code executes completely. However, when I check the value of the data variable right at the end of the readEeprom function, I just get a Null-character (after writing an 'H' in the EEPROM memory address specified, in byte write mode (page write code is commented out with #if)). This indicates that either the write did not occur properly, or the read did not occur properly.
I do not have access to an oscilloscope or logic analyzer to check what exactly happens on the SDA and SCL lines.
The PIC32 oscillator is set to 8 MHz and I intended to use a 100 kHz baud rate for communication with the EEPROM. So therefore, using the equation I2CBRG = (PBCLK/(2*FSCK))-2, as found in section 24 of the PIC32 family reference documents, I use 0x26 for theI2CBRG value.
Perhaps someone could spot if something is wrong in my code? I have tried to find the problem over a day now, but have not succeeded.
Here is my code: /* Interface test for I2C EEPROM with PIC32 */
#include <plib.h>
#include <p32xxxx.h>
/* Pin outline:
* SCL1 - pin 17 - (EEPROM pin 6)
* SDA1 - pin 18 - (EEPROM pin 5)
* WP - pin 16 (RB7) (EEPROM pin 7) - Currently connected to GND
*/
//configuration bits
// DEVCFG3
//#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
//#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
//#pragma config FUSBIDIO = OFF // USB USID Selection (Controlled by the USB Module)
//#pragma config FVBUSONIO = OFF // USB VBUS ON Selection (Controlled by USB Module)
// DEVCFG2
#pragma config FPLLIDIV = DIV_1 // PLL Input Divider (12x Divider)
#pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier)
//#pragma config UPLLIDIV = DIV_1 // USB PLL Input Divider (12x Divider)
//#pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 256)
// DEVCFG1
#pragma config FNOSC = FRC // Oscillator Selection Bits (Fast RC Osc (FRC))
#pragma config FSOSCEN = OFF // Secondary Oscillator Disnable (Disabled)
#pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
#pragma config FCKSM = CSECMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
//#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
//#pragma config FWDTWINSZ = WISZ_25 // Watchdog Timer Window Size (Window Size is 25%)
// DEVCFG0
//#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)
//////////////////////////////////////////
#define EEPROM_WP PORTBbits.RB7
#define CLOCK 8000000 /*8 MHz clock*/
#define HIGH 1 //logic values declaration
#define LOW 0 //logic values declaration
char data[10];
void init(void);
void readEeprom(void);
void writeEeprom(void);
int main (void)
{
init();
writeEeprom();
readEeprom();
} /*main*/
void init(void)
{
SYSTEMConfigPerformance(CLOCK);
TRISBbits.TRISB7 = 0x01;
/*I2C peripheral overrides states of respective interface pins - no need
to set state of SCLx and SDAx pins here*/
/*Initialize I2C Peripheral*/
I2C1CONbits.DISSLW = HIGH; /*disable slew rate for 100 kHz*/
I2C1BRG = 0x26; /*Set Baud Rate Generator*/
I2C1CONbits.ON = HIGH;
int i = 0;
for (i = 0; i < 10; i++)
{
data[i] = '0';
} /*for*/
} /*init*/
void writeEeprom(void)
{
/*Assert start condition*/
I2C1CONbits.SEN = HIGH; /*This bit is automatically cleared by MCU*/
/*Test if START condition is completed - test interrupt I2C1MIF in IFS1*/
while (I2C1CONbits.SEN == HIGH); /*wait until start condition finishes*/
/*Send device address and write indication - address = "1010xxx0"*/
I2C1TRN = 0b10100000;
/*Wait for transmit buffer empty - indicates that write is completed*/
while (I2C1STATbits.TBF == HIGH);
/*Wait for ACK signal from device - bit will clear when ACK is received*/
while (I2C1STATbits.ACKSTAT == HIGH);
/*Send word address for read*/
I2C1TRN = 0x00;
while (I2C1STATbits.ACKSTAT == HIGH); /*wait for ACK from device*/
#if 0 /*Page write*/
int i;
/*24LC02 can only write up to 8 bytes at a time*/
for (i = 0; i < 7; i++)
{
I2C1TRN = i;
/*Wait for transmit buffer empty - indicates that write is completed*/
while (I2C1STATbits.TBF == HIGH);
/*Generate ACK event*/
I2C1CONbits.ACKEN;
} /*for*/
/*Send stop event*/
I2C1CONbits.PEN;
#endif
#if 1 /*Byte write*/
I2C1TRN = 'H';
/*Wait for transmit buffer empty - indicates that write is completed*/
while (I2C1STATbits.TBF == HIGH);
/*Generate ACK event*/
I2C1CONbits.ACKEN;
/*Send stop event*/
I2C1CONbits.PEN;
#endif
} /*writeEeprom*/
void readEeprom(void)
{
/*Assert start condition*/
I2C1CONbits.SEN = HIGH; /*This bit is automatically cleared by MCU*/
/*Test if START condition is completed - test interrupt I2C1MIF in IFS1*/
while (I2C1CONbits.SEN == HIGH); /*wait until start condition finishes*/
/*Send device address and write indication - address = "1010xxx0"*/
I2C1TRN = 0b10100000;
/*Wait for transmit buffer empty - indicates that write is completed*/
while (I2C1STATbits.TBF == HIGH);
/*Wait for ACK signal from device - bit will clear when ACK is received*/
while (I2C1STATbits.ACKSTAT == HIGH);
/*Send address for read*/
I2C1TRN = 0x00; /*Upper 8 bits of address*/
while (I2C1STATbits.ACKSTAT == HIGH); /*wait for ACK from device*/
/*Send Repeated start event again*/
I2C1CONbits.RSEN = HIGH;
/*Test if START condition is completed - test interrupt I2C1MIF in IFS1*/
while (I2C1CONbits.SEN == HIGH); /*wait until start condition finishes*/
/*Send device address and read indication*/
I2C1TRN = 0b10100001;
while (I2C1STATbits.ACKSTAT == HIGH);
int i = 0;
#if 0 /*Sequential read*/
for (i = 0; i < 10; i++)
{
/*Enable receive mode - RCEN cleared after 8th bit is received*/
I2C1CONbits.RCEN = HIGH;
while (I2C1STATbits.RBF == LOW); /*Wait for receive buffer full*/
data[i] = I2C1RCV; /*Store receive buffer value in dataByte*/
while (I2C1STATbits.RBF == HIGH); /*Wait for receive buffer to clear*/
/*Generate ACK event - do not send ACK for last sequential read*/
if (i < 9)
{
I2C1CONbits.ACKEN;
} /*if*/
} /*for*/
/*Send stop event*/
I2C1CONbits.PEN;
#endif
#if 1 /*Single read*/
/*Enable receive mode - RCEN cleared after 8th bit is received*/
I2C1CONbits.RCEN = HIGH;
//while (I2C1STATbits.RBF == LOW); /*Wait for receive buffer full*/
data[i] = I2C1RCV; /*Store receive buffer value in dataByte*/
while (I2C1STATbits.RBF == HIGH); /*Wait for receive buffer to clear*/
/*Send stop event - no ACK event must be sent*/
I2C1CONbits.PEN;
#endif
} /*readEeprom()*/
I would greatly appreciate any help that anyone can offer. Thanks in advance.
EDIT: I realize that I misread in the datasheet that only one word address byte (instead of 2 - MSByte and LSByte) is required for the 24LC02. So I fixed that in my code, but I still have the same problem. I have also used an Arduino to (successfully) write to the EEPROM and read the data, and therefore I can confirm that even the writeEeprom function in my code is not working.
After some Google searches I have also read that the I2C peripheral could give problems while the PIC is in debug mode, which is what I am currently using.
The latest:
I sorted out the read problem, although my sequential read method is not performing it the intended way of doing it, but it works. However, I am having trouble writing to the EEPROM. My code follows the datasheet's guidlines exactly (even with some additional delays for safety), but it still doesn't work. My write function is the function called "writeEeprom" in the following code:
/* Interface test for I2C EEPROM 24LC02B with PIC32
*/
#include <plib.h>
#include <p32xxxx.h>
/* Pin outline:
* SCL1 - pin 17 - (EEPROM pin 6)
* SDA1 - pin 18 - (EEPROM pin 5)
* WP - pin 16 (RB7) (EEPROM pin 7) - Currently connected to GND
*/
//configuration bits
// DEVCFG3
//#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
//#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
//#pragma config FUSBIDIO = OFF // USB USID Selection (Controlled by the USB Module)
//#pragma config FVBUSONIO = OFF // USB VBUS ON Selection (Controlled by USB Module)
// DEVCFG2
#pragma config FPLLIDIV = DIV_1 // PLL Input Divider (12x Divider)
#pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier)
//#pragma config UPLLIDIV = DIV_1 // USB PLL Input Divider (12x Divider)
//#pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 256)
// DEVCFG1
#pragma config FNOSC = FRC // Oscillator Selection Bits (Fast RC Osc (FRC))
#pragma config FSOSCEN = OFF // Secondary Oscillator Disnable (Disabled)
#pragma config IESO = ON // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled)
#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
#pragma config FCKSM = CSECMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
//#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
//#pragma config FWDTWINSZ = WISZ_25 // Watchdog Timer Window Size (Window Size is 25%)
// DEVCFG0
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)
//////////////////////////////////////////
#define EEPROM_WP PORTBbits.RB7
#define CLOCK 8000000
#define HIGH 1 //logic values declaration
#define LOW 0 //logic values declaration
char dataArr[8];
int controlWrite = 0b10100000; /*Control byte for writing to EEPROM*/
int controlRead = 0b10100001; /*Control byte for reading from EEPROM*/
int wordAddress = 0x00; /*Address of word in EEPROM*/
void init(void);
void readEeprom(void);
void readEepromSeq(void);
void writeEeprom(void);
void delayus(unsigned t); //1us delay
void delayms(unsigned k); //1ms delay
int main (void)
{
init();
delayms(10);
writeEeprom();
delayms(10);
readEepromSeq();
} /*main*/
void init(void)
{
SYSTEMConfigPerformance(CLOCK);
/*I2C peripheral overrides states of respective interface pins - no need
* to set state of SCLx and SDAx pins here.
* I2C pins must be set to digital pins by clearing the respective ANSEL-SFR
*/
ANSELB = 0x00; /*Configure port B as digital port, not analog*/
/*Initialize I2C Peripheral*/
I2C1CONbits.DISSLW = HIGH; /*disable slew rate for 100 kHz*/
I2C1BRG = 0x26; /*Set Baud Rate Generator - for PBCLK = 8 MHz, Fck = 100 kHz*/
I2C1CONbits.ON = HIGH;
int i = 0;
for (i = 0; i < 10; i++)
{
dataArr[i] = '0';
} /*for*/
} /*init*/
void writeEeprom(void)
{
wordAddress = 0x00;
int data = 0x48; /*Character "H"*/
int i = 0;
/*Assert start condition*/
I2C1CONbits.SEN = HIGH; /*This bit is automatically cleared by MCU*/
/*Test if START condition is completed - test interrupt I2C1MIF in IFS1*/
while (I2C1CONbits.SEN == HIGH); /*wait until start condition finishes*/
/*Send device address and write indication - address = "1010xxx0"*/
I2C1TRN = controlWrite;
/*Wait for transmit buffer empty - indicates that write is completed*/
//while (I2C1STATbits.TBF == HIGH);
/*Wait for transmit process to end - bit clears once transmit not in progress*/
while (I2C1STATbits.TRSTAT == HIGH);
/*Wait for ACK signal from device - bit will clear when ACK is received*/
while (I2C1STATbits.ACKSTAT == HIGH);
/*Send address for read*/
I2C1TRN = wordAddress;
/*Wait for transmit process to end*/
while (I2C1STATbits.TRSTAT == HIGH);
/*Wait for ACK signal from device*/
while (I2C1STATbits.ACKSTAT == HIGH);
#if 0 /*Page write*/
int i;
/*24LC02 can only write up to 8 bytes at a time*/
for (i = 0; i < 7; i++)
{
I2C1TRN = i;
/*Wait for transmit buffer empty - indicates that write is completed*/
while (I2C1STATbits.TBF == HIGH);
/*Generate ACK event*/
delayms(10);
I2C1CONbits.ACKEN;
} /*for*/
/*Send stop event*/
I2C1CONbits.PEN;
#endif
#if 1 /*Byte write*/
I2C1TRN = data;
/*Wait for transmit buffer empty - indicates that write is completed*/
while(I2C1STATbits.TRSTAT == HIGH); /*Wait until transmit is completed*/
delayms(10);
/*wait for ACK from device*/
while (I2C1STATbits.ACKSTAT == HIGH);
delayms(10);
/*Send stop event*/
I2C1CONbits.PEN;
delayms(10);
#endif
} /*writeEeprom*/
void readEepromSeq()
{
int i = 0;
wordAddress = 0x00;
/*loop 8-times - the amount of bits in a byte*/
for (i = 0; i < 8; i++)
{
/*Assert start condition*/
I2C1CONbits.SEN = HIGH; /*This bit is automatically cleared by MCU*/
/*Test if START condition is completed - test interrupt I2C1MIF in IFS1*/
while (I2C1CONbits.SEN == HIGH); /*wait until start condition finishes*/
/*Send device address and write indication - address = "1010xxx0"*/
I2C1TRN = controlWrite;
/*Wait for transmit buffer empty - indicates that write is completed*/
while (I2C1STATbits.TBF == HIGH);
/*Wait for ACK signal from device - bit will clear when ACK is received*/
while (I2C1STATbits.ACKSTAT == HIGH);
delayms(10);
/*Send address for read*/
I2C1TRN = wordAddress; /*Lower 8 bits of address*/
while (I2C1STATbits.ACKSTAT == HIGH);
delayms(10);
/*Send Repeated start event again*/
I2C1CONbits.RSEN = HIGH;
/*Test if Repeated START condition is completed - test interrupt I2C1MIF in IFS1*/
while (I2C1CONbits.RSEN == HIGH); /*wait until repeated start condition finishes*/
/*Send device address and read indication*/
I2C1TRN = controlRead;
delayms(10);
while (I2C1STATbits.ACKSTAT == HIGH);
/*Enable receive mode - RCEN cleared after 8th bit is received*/
I2C1CONbits.RCEN = HIGH;
while (I2C1STATbits.RBF == LOW); /*Wait for receive buffer full*/
dataArr[i] = I2C1RCV; /*Store receive buffer value in dataByte*/
while (I2C1STATbits.RBF == HIGH); /*Wait for receive buffer to clear*/
/*Send stop event - no ACK event must be sent*/
I2C1CONbits.PEN;
wordAddress++;
} /*for*/
} /*readEepromSeq*/
void readEeprom(void)
{
int i = 0;
wordAddress = 0x00;
/*Assert start condition*/
I2C1CONbits.SEN = HIGH; /*This bit is automatically cleared by MCU*/
/*Test if START condition is completed - test interrupt I2C1MIF in IFS1*/
while (I2C1CONbits.SEN == HIGH); /*wait until start condition finishes*/
/*Send device address and write indication - address = "1010xxx0"*/
I2C1TRN = controlWrite;
/*Wait for transmit buffer empty - indicates that write is completed*/
while (I2C1STATbits.TBF == HIGH);
/*Wait for ACK signal from device - bit will clear when ACK is received*/
while (I2C1STATbits.ACKSTAT == HIGH);
delayms(10);
/*Send address for read*/
I2C1TRN = wordAddress; /*Lower 8 bits of address*/
while (I2C1STATbits.ACKSTAT == HIGH);
delayms(10);
/*Send Repeated start event again*/
I2C1CONbits.RSEN = HIGH;
/*Test if Repeated START condition is completed - test interrupt I2C1MIF in IFS1*/
while (I2C1CONbits.RSEN == HIGH); /*wait until repeated start condition finishes*/
/*Send device address and read indication*/
I2C1TRN = controlRead;
delayms(10);
while (I2C1STATbits.ACKSTAT == HIGH);
#if 1 /*Sequential read*/
for (i = 0; i < 8; i++)
{
/*Enable receive mode - RCEN cleared after 8th bit is received*/
I2C1CONbits.RCEN = HIGH;
while (I2C1STATbits.RBF == LOW); /*Wait for receive buffer full*/
dataArr[i] = I2C1RCV; /*Store receive buffer value in dataByte*/
while (I2C1STATbits.RBF == HIGH); /*Wait for receive buffer to clear*/
delayms(10);
/*Generate ACK event - do not send ACK for last sequential read*/
if (i < 8)
{
I2C1CONbits.ACKEN;
} /*if*/
} /*for*/
/*Send stop event*/
I2C1CONbits.PEN;
#endif
#if 0 /*Single read*/
/*Enable receive mode - RCEN cleared after 8th bit is received*/
I2C1CONbits.RCEN = HIGH;
while (I2C1STATbits.RBF == LOW); /*Wait for receive buffer full*/
dataArr[i] = I2C1RCV; /*Store receive buffer value in dataByte*/
while (I2C1STATbits.RBF == HIGH); /*Wait for receive buffer to clear*/
/*Send stop event - no ACK event must be sent*/
I2C1CONbits.PEN;
#endif
} /*readEeprom()*/
void delayus(unsigned t)
{
T1CONbits.ON = 1; //enable timer1
while(t--)
{
TMR1 = 0; //reset timer1
while(TMR1 < 8); //delay of 1us: (1us)/(125ns) = 8
} //while(t--)
T1CONbits.ON = 0; //disable timer1 (power saving)
} //delayus
void delayms(unsigned k)
{
T1CONbits.ON = 1; //enable timer1
while(k--)
{
TMR1 = 0; //reset timer1
while(TMR1 < 8000); //delay of 1us: (1ms)/(125ns) = 8000
} //while(t--)
T1CONbits.ON = 0; //disable timer1 (power saving)
} //delayms
Can someone please take the trouble and have a look at it and at least provide me with some comments? Once again, the problem lies within the "writeEeprom" function that is called in main().
Thank you.