Datasheet
PIC17C7XX
DS30289C-page 176 1998-2013 Microchip Technology Inc.
15.4 Example Program
Example 15-2 shows MPLAB
®
C17 ’C’ code for using
the I
2
C module in Master mode to communicate with a
24LC01B serial EEPROM. This example uses the PIC
®
MCU ‘C’ libraries included with MPLAB C17.
EXAMPLE 15-2: INTERFACING TO A 24LC01B SERIAL EEPROM (USING MPLAB C17)
// Include necessary header files
#include <p17c756.h> // Processor header file
#include <delays.h> // Delay routines header file
#include <stdlib.h> // Standard Library header file
#include <i2c16.h> // I2C routines header file
#define CONTROL 0xa0 // Control byte definition for 24LC01B
// Function declarations
void main(void);
void WritePORTD(static unsigned char data);
void ByteWrite(static unsigned char address,static unsigned char data);
unsigned char ByteRead(static unsigned char address);
void ACKPoll(void);
// Main program
void main(void)
{
static unsigned char address; // I2C address of 24LC01B
static unsigned char datao; // Data written to 24LC01B
static unsigned char datai; // Data read from 24LC01B
address = 0; // Preset address to 0
OpenI2C(MASTER,SLEW_ON); // Configure I2C Module Master mode, Slew rate control on
SSPADD = 39; // Configure clock for 100KHz
while(address<128) // Loop 128 times, 24LC01B is 128x8
{
datao = PORTB;
do
{
ByteWrite(address,datao); // Write data to EEPROM
ACKPoll(); // Poll the 24LC01B for state
datai = ByteRead(address); // Read data from EEPROM into SSPBUF
} while(datai != datao); // Loop as long as data not correctly
// written to 24LC01B
address++; // Increment address
}
while(1) // Done writing 128 bytes to 24LC01B, Loop forever
{
Nop();
}
}