24LC16 (16K Bit I2C Serial EEPROM)

      기억 용량은 16 kBit로 조금 적긴하지만 매우 유용한 전자소자입니다.
      I2C 통신방법으로 I2C를 익히기 위하여 매우 좋은 전자소자입니다.


     
ref) http://ww1.microchip.com/downloads/en/DeviceDoc/20002213B.pdf




24LC02 또는 24LC16을 사용하여 Memory에 정보를 쓰고 읽기위한 회로


24LC02 또는 24LC16을 다루기 위한 프로그램은 다음과 같습니다.
//***********************************************************************
// 24LC16 Momory 다루기
//***********************************************************************  											 
                    
#include  <16f687.h>
										
#fuses    HS                                        //* 4MHz 이상은 HS로
#use      delay (clock = 18432000)      //* 사용한 크리스탈

//* *** UART (RS232)를 사용하기 위한 설정, LCD 화면 표시하기 위함
#use      rs232 (baud=19200, xmit=PIN_B7, rcv=PIN_B5)
                                                      
#define     LED_ON			output_low  (PIN_C2)
#define     LED_OFF			output_high (PIN_C2)
#define     IN_SW			input (PIN_A3)
										    
#define     DEV_ADDR_24LC16		0b10100000
#define     SCL_24LC16  		PIN_C3																
#define     SDA_24LC16  		PIN_C4
#define     SCL_HIGH		output_high (SCL_24LC16)
#define     SCL_LOW			output_low  (SCL_24LC16)
#define     SDA_HIGH		output_high (SDA_24LC16)
#define     SDA_LOW			output_low  (SDA_24LC16)
#define     INP_SDA			input (SDA_24LC16)		//* to read data from EEPROM
	
//* I2C Start Datasheet page4, -FIGURE 1-2: BUS TIMING START/STOP 참고									
void start_24LC16 (void)	
{	SCL_HIGH ; SDA_HIGH ; delay_us (100) ;
	SDA_LOW  ; delay_us (20) ; SCL_LOW ; delay_us (20) ; 
}

//* I2C Stop, Datasheet page4, -FIGURE 1-2: BUS TIMING START/STOP 참고
void stop_24LC16 (void)		
{	SCL_LOW  ; SDA_LOW ; delay_us (100) ;
	SCL_HIGH ; delay_us (20) ; SDA_HIGH ; delay_us (20) ; 
}
					
//* write 1 byte, Datasheet page6, -FIGURE 4-1: DATA TRANSFER SEQUENCE ON THE SERIAL BUS 참고						
void put_byte_24LC16 (byte by_data)		
{	byte	by_idx ;

	for (by_idx = 0;by_idx <= 7;by_idx++) {
		rotate_left (&by_data, 1) ;
		if (by_data & 0x01) SDA_HIGH ; else SDA_LOW ;
		delay_us (5) ;
		SCL_HIGH ; delay_us (20) ; SCL_LOW  ; delay_us (10) ;
	}
}

//* read 1 byte, Datasheet page6, -FIGURE 4-1: DATA TRANSFER SEQUENCE ON THE SERIAL BUS 참고
byte get_byte_24LC16 (void)		
{	byte	by_idx ;
	byte	by_data ;

	INP_SDA ;
	by_data = 0 ; 
	for (by_idx = 0;by_idx <= 7;by_idx++) {
		SCL_HIGH  ; delay_us (20) ;
		by_data = by_data + (INP_SDA << (7 - by_idx)) ;
		SCL_LOW  ; delay_us (20) ;
	}
	return (by_data) ;
}
			
boolean ack_24LC16 (void)
{	boolean     flgErr ;
	int16 ovr_time = 0 ;

	output_low (SDA_24LC16) ;
	INP_SDA ;
	delay_us(50) ;
	flgErr = 0 ;

	while (INP_SDA) {
		if (ovr_time > 500) { 
			flgErr = 1 ; 
			break ;     
		}
		ovr_time ++ ; delay_us (1) ;
	}
	SCL_HIGH ; delay_us (20) ; SCL_LOW  ; delay_us (10) ;
	output_low (SDA_24LC16) ;
	return (flgErr) ;
}

//* write 1 byte data at setted address, page 9, FIGURE 6-1: BYTE WRITE 
void write_24LC16 (int16 eeprom_addr, byte eeprom_value)	
{	int16	rtn_value ;
	byte 	by_data1, by_data2 ;

	start_24LC16 () ;				//* start i2c
	put_byte_24LC16 (DEV_ADDR_24LC16 | 0x00) ;	//* device addressing to write, page 7
	ack_24LC16 () ;				//* ack
	put_byte_24LC16 (eeprom_addr) ;	//* write address for data
	ack_24LC16 () ;				//* ack
	put_byte_24LC16 (eeprom_value) ;  //* write data
	ack_24LC16 () ;				//* ack
	stop_24LC16 () ;				//* stop i2c
}

//* write 1 byte data from setted address, page 12, FIGURE 8-2: RANDOM READ
byte read_24LC16 (int16 eeprom_addr)	
{	byte	rtn_value ;
	byte 	by_data ;

	start_24LC16 () ;				//* start i2c
	put_byte_24LC16 (DEV_ADDR_24LC16 | 0x00) ;	//* device addressing to write, page 7
	ack_24LC16 () ;				//* ack
	put_byte_24LC16 (eeprom_addr) ; 
	ack_24LC16 () ;				//* ack

	start_24LC16 () ;				//* start i2c
	put_byte_24LC16 (DEV_ADDR_24LC16 | 0x01) ;	//* device addressing to read, page 7
	ack_24LC16 () ;				//* ack
	by_data = get_byte_24LC16 () ;
	stop_24LC16 () ;				//* stop i2c
	rtn_value = by_data ;
	return (rtn_value) ;
}

void main (void)
{	int16 idx ;
        int16 value ;
				
	printf ("$#CLR_LCD;") ; delay_ms (100) ;
	printf ("$#BL_ON;") ;

	Printf ("$5,1,24LC16 TEST;")
	while (1) {                  
		for (idx = 0;idx <= 200;idx++) {
			write_24LC16 (idx, idx)  ; 
			printf ("$3,3,Write: %3ld;", idx) ;
			delay_ms (4) ;
			value = read_24LC16 (idx) ;
			printf ("$3,4,Read : %3ld;", value) ;
			
			LED_ON ; delay_ms (100) ; LED_OFF ; delay_ms (200) ;   
		}
	} 
}     
                                                     
//***********************************************************************  										 



I2C Momory 다루기
(그림을 클릭하면 동영상을 볼 수 있습니다.)