Comunication With I2C Serial EEPROM
Comunication With I2C Serial EEPROM
Comunication With I2C Serial EEPROM
2. Introduction
The application example described in this document is applied to the following:
• MCU: M16C/28, M16C/29 Group
This program can be used with other microcomputers within the M16C family, which have the same SFR (special
function register) as the M16C/28, and M16C/29 microcomputers. Please check the manual for any additions and
modifications to functions. Careful evaluation is recommended before using this application note.
Item Function
2
Based on Philips I C bus standard:
• 7-bit addressing format
Format
• High-speed clock mode
• Standard clock mode
Based on Philips I2C bus standard:
• Master transmit
Communication mode • Master receive
• Slave transmit
• Slave receive
SCL clock frequency 16.1kHz ~ 400kHz (VIIC* = 4MHz)
Serial data line SDAMM(SDA)
I/O pin
Serial clock line SDLMM (SCL)
2
*VIIC = I C bus system clock
In current application, the topological structure of I2C bus is single-master and single-slave. SCL mode is standard
clock mode; SCL clock frequency is 100 kHz; Communication mode is master transmit and master receive.
The device address of HN58X2402SI is 7 bits, the first 4 bits of the address is fixed to (1010)2, and the last 3 bits are
decided by A2, A1 and A0 pins. That is, the device address of a single EEPROM is (1010A2A1A0)2. Take
HN58X2402SI for example, an I2C bus supports 8 chips with different device address.
In current application, the device address pins A2, A1 and A0 are set to “0”, “1”, and “1” separately. As write protect
function is not used in application, WP pin is connected to ground (disable write protect function).
1 0 1 0 W
A2
A1
A0
a7
a6
a5
a4
a3
a2
a1
a0
4. Design Description
4.1 Hardware structure
In current application, the structure of hardware system is shown in figure 4.1.
Vcc
A0 Vcc
“H” MCU
A1 WP
“H”
“L” A2 SCL
SCLMM
Vss SDA
SDAMM
Initialize I2C
module
MODE_IDLE
Read Write
finished finished
Set by Set by
program program
MODE_READ MODE_WRITE
/
K tion
Stop
AC di
N on
C
op
St
/****************************************************************/
/* Header Including */
/****************************************************************/
#include "sfr29.h"
/****************************************************************/
/* Macro Definition */
/****************************************************************/
#define EEPROM_ADDR 0b01010011 /* EEPROM Device Address */
/* (lower 7 bits) */
#define WRITE 0x0
#define WRITE_ADDR 0b1111000 /* the memory to be write to */
#define READ 0x1
#define READ_ADDR 0b1111000 /* the memory to be read from */
/****************************************************************/
/* Function Declaration */
/****************************************************************/
void main(void);
/****************************************************************/
/* Variable Definition */
/****************************************************************/
unsigned char iic_tr[8] = {1,2,3,4,5,6,7,8}; /* buffer for transfer */
unsigned char iic_re[8] = {0,0,0,0,0,0,0,0}; /* buffer for receive */
/*****************************************************************
Function: main
Description: main function
Calls: init_main(void)
ini_ini(unsigned char)
unsigned char eeprom_operation(unsigned char slave,
unsigned char memory,
unsigned char *buf,
unsigned char len,
unsigned char rw);
Input: None
Output: None
Return: None
*****************************************************************/
void main()
{
init_main(); /* Initialize MCU */
iic_ini(1); /* Initialize I2C bus */
asm("fset I"); /* Enable interrupt */
/* write data */
if(eeprom_operation(EEPROM_ADDR,WRITE_ADDR,iic_tr,8,WRITE)!=0) {;
}
else {;
}
/* read data */
if(eeprom_operation(EEPROM_ADDR,READ_ADDR,iic_re,8,READ)!=0) {;
}
else {;
}
while(1){
}
}
/*****************************************************************
Function: init_mcu
Description: initialize MCU
Calls: None
Input: None
Output: None
Return: None
*****************************************************************/
void init_main(void)
{
/* Setting system clock related registers */
/* Setting system clock related registers */
prcr = 0x01; /* cm0,cm1,cm2 writing enable */
cm2 = 0x00; /* system register2 Initialize */
cm1 = 0x20; /* system register1 Xcin-Xcout:High */
cm0 = 0x08; /* system register0 Xcin-Xcout:High */
prcr = 0x00; /* cm0,cm1,cm2 writing disable */
/****************************************************************/
/* Project : M16C/28, M16C/29 Application Note */
2
/* : Communication with I C serial EEPROM */
/* : by Multi-master I2C bus Interface */
/* MCU : M30290FCTHP */
/* C Compiler : NC30WA, version 5.40 */
/* File name : i2C_functions.c */
/* Function : Read / Write I2C Serial EEPROM */
/* Code Version : 1.0 */
/* */
/* Copyright (C) 2007 Renesas Technology Corp. */
/* All right reserved. */
/****************************************************************/
/****************************************************************/
/* Header Including */
/****************************************************************/
#include "sfr29.h"
/****************************************************************/
/* Macro definition */
/****************************************************************/
#define BYTE_LIMIT 8 /* limit of continuous write */
/****************************************************************/
/* Function declaration */
/****************************************************************/
void iic_ini(unsigned char);
unsigned char eeprom_operation(unsigned char slave,unsigned char memory,
unsigned char *buf,unsigned char len,
unsigned char rw);
void eeprom_delay(void);
void iic_int(void);
/****************************************************************/
/* Variable definition */
/****************************************************************/
typedef union{
struct{
unsigned char b0:1;
}bit;
unsigned char all;
}byte_dt;
/*****************************************************************
Function: iic_ini
Description: initialize I2C bus
Calls: None
Input: 0: Disable I2C module
1: Enable I2C module
/*****************************************************************
Function: eeprom_operation
Description: trigger I2C bus operation (Read / Write)
Calls: None
Input: unsigned char slave : device address
/*****************************************************************
Function: iic_int
Description: I2C interrupt handler
Calls: void master_transfer(void)
void master_receive(void)
void iic_ini(void)
Input: None
Output: None
Return: None
*****************************************************************/
/*****************************************************************
Function: master_transfer
Description: I2C bus write operation with acknowledge polling
Calls: None
Input: None
Output: None
case WRITE_MEMADDR:
case WRITE_DATA:
case WRITE_STOP:
case WRITE_POLLING:
case WRITE_FINISH:
default:
break;
}
}
/*****************************************************************
Function: master_receive
Description: I2C bus read operation
Calls: None
Input: None
Output: None
Return: None
*****************************************************************/
static void master_receive(void){
case READ_MEMADDR:
case READ_DEVADDR:
case READ_READSET:
case READ_DATA:
case READ_STOP:
default:
break;
}
}
/*****************************************************************
Function: eeprom_delay
Description: wait while I2C bus busy or not in IDLE mode
Calls: None
Input: None
Output: None
Return: None
*****************************************************************/
void eeprom_delay(void){
while((bb == 1) || (iic_mode != MODE_IDLE)){;
}
}
Enquiry
http://www.renesas.com/inquiry
[email protected]
Hardware Manual
M16C/28 Group Hardware Manual
M16C/29 Group Hardware Manual
HN58X2402SI Hardware Manual
(Use the latest version on the home page: http://www.renesas.com)