Looking for information on two wire serial eeprom

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
I'm working with an ATMEGA16 and an AT24C512 two wire serial eeprom. I have never worked with this memory chip or the two wire interface, and I was wondering if anyone had any example code for this or a similar application.

thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The datasheet is rather clear about it, and there's a two-wire hardware interface on the atmega 16, IIRC.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Let me re-phrase this question more specifically.... In setting the registers for the TWI, I haven't found anything in the uC manual which describes how to set the ports for SDA and SCL. I have found a snippet of asm code in the include file for I2C.

My code for a write to the memory is written below.
I put some flags into my write_to_memory function just to see where the function is failing. I make it to the second line of code before "flag1" pops up continuously. So, the START command is not being sent.

Any help would be appreciated. An example would be fantastic.

// I2C Bus functions
#asm
.equ __i2c_port=0x12 ;PORTD
.equ __sda_bit=4
.equ __scl_bit=5
#endasm
#include

void write_to_memory(unsigned int byte_addr, unsigned char data)
{
unsigned char addrH,addrL;
addrH=byte_addr>>8;
addrL=byte_addr;

// Send start
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)))
printf("flag1");

if((TWSR & 0xF8)!=START)
MEM_FULL=ON;

// Send device address
TWDR=SLA_W;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)))
printf("flag2");
if((TWSR & 0xF8)!=MT_SLA_ACK)
MEM_FULL=ON;

// Send address high byte
TWDR=addrH;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)))
printf("flag3");
if((TWSR & 0xF8)!=MT_DATA_ACK)
MEM_FULL=ON;

// Send address low byte
TWDR=addrL;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)))
;
if((TWSR & 0xF8)!=MT_DATA_ACK)
MEM_FULL=ON;

// Send data
TWDR=data;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)))
;
if((TWSR & 0xF8)!=MT_DATA_ACK)
MEM_FULL=ON;

// Send stop
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PLEASE use the CODE button when posting code to preserve formating.

Download AVRLIB, among other useful bits...

Quote:
/*! \file i2ceeprom.h \brief Interface for standard I2C EEPROM memories. */
//*****************************************************************************
//
// File Name : 'i2ceeprom.h'
// Title : Interface for standard I2C EEPROM memories
// Author : Pascal Stang - Copyright (C) 2003
// Created : 2003.04.23
// Revised : 2003.04.23
// Version : 0.1
// Target MCU : Atmel AVR series
// Editor Tabs : 4
//
/// \ingroup driver_hw
/// \defgroup i2ceeprom Interface for standard I2C EEPROM memories (i2ceeprom.c)
/// \code #include "i2ceeprom.h" \endcode
/// \par Overview
/// This library provides functions for reading and writing standard
/// 24Cxxx/24LCxxx I2C EEPROM memories. Memory sizes up to 64Kbytes are
/// supported. Future revisions may include page-write support.
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The TWI uses specific pins and can't be moved around. Codevision has a bit-banged I2C library which allows you to move SDA/SCL to different pins but it doesn't use the TWI on the AVR. It looks like you're trying to mix the two implementations.