English
Language : 

PIC17C7XX_13 Datasheet, PDF (176/306 Pages) Microchip Technology – High-Performance 8-bit CMOS EPROM Microcontrollers with 10-bit A/D
PIC17C7XX
15.4 Example Program
Example 15-2 shows MPLAB® C17 ’C’ code for using
the I2C 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;
static unsigned char datao;
static unsigned char datai;
// I2C address of 24LC01B
// Data written to 24LC01B
// Data read from 24LC01B
address = 0;
OpenI2C(MASTER,SLEW_ON);
SSPADD = 39;
// Preset address to 0
// Configure I2C Module Master mode, Slew rate control on
// 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++;
}
while(1)
{
Nop();
}
}
// Increment address
// Done writing 128 bytes to 24LC01B, Loop forever
DS30289C-page 176
 1998-2013 Microchip Technology Inc.