Sign In

Problem With 24LC256 & Atmega16 Read/write & Strange Wave Form On Rigol DS1054Z

Praveen Sharma topksharma@gmail.com Norway

Hardware: ATmega16 Running At 4 MHz Connected With Breadboard Software: C Project Using Atmel IDE Ver 7

Hi,

I am very beginner to electronics.
I have experience with software development but no experience with electronics.
I have learned everything on my own from last 3-4 months.

I am trying to write to and read from 24LC256 chip with Atmega16.
In my program I don't get any error , program runs good but I don't read what I write I always read 0xFF which is default value.

So to find out more about this I actually bought an Rigol DS1054Z and, after connecting probes to SDA & SCL (with pin and GND) I get a very strange wave form, I have attached a picture of the wave form.

I expected to see a wave form which has a straight line and then goes low or high, I mean same which we see on images where we learn about I2C protocol.

I can try whatever you guys think may go wrong.

Please help me in finding/troubleshoot the problem.
Thanks in advance.

Images

Problem with 24LC256 & Atmega16 read/write & strange wave form on Rigol DS1054Z

Program


#ifndef F_CPU					// if F_CPU was not defined in Project -> Properties
#define F_CPU 2000000UL			// define it now as 2 MHz unsigned long
#endif

#include <avr/io.h>
#include<avr/interrupt.h>
#include "util/delay.h"

#define EEPROM_WRITE 0xA0
#define EEPROM_READ 0xA1

void i2c_init()
{
	//set SCL to 100kHz
	// CPU freq = 2MHz
	TWSR = 0x00;
	TWBR = (1<<TWPS1);
	//enable TWI
	TWCR = (1<<TWEN);
}

void i2c_start()
{
	TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
	while ((TWCR & (1<<TWINT)) == 0);
	
}

void i2c_stop()
{
	TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
	_delay_ms(100);
}

void i2c_write(uint8_t u8data)
{
	TWDR = u8data;
	TWCR = (1<<TWINT)|(1<<TWEN);
	while ((TWCR & (1<<TWINT)) == 0);
	_delay_ms(10);
}


uint8_t i2c_read()
{
	
	TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
	while ((TWCR & (1<<TWINT)) == 0);
	return TWDR;
}

int main()
{
	i2c_init();
	uint8_t readByte=0;
	uint16_t address=0x0;
	uint8_t writeVal=0;
	while (1)
	{		
		i2c_start();
		i2c_write(EEPROM_WRITE);
		i2c_write(address>>8);
		i2c_write(address & 0xFF);
		i2c_write(writeVal);
		i2c_stop();
		writeVal++;
		address++;		
		address=0;
		//for (int a =0;a<128;a++)
		//{
		i2c_start();
		i2c_write(EEPROM_READ);
		i2c_write(address>>8);
		i2c_write(address & 0xFF);
		readByte =i2c_read();
		_delay_ms(10);
		i2c_stop();
		address++;
		_delay_ms(500);
	}
	return 0;
}


Replies

R Avinash Gupta
2017-05-01 02:30:16
Please attach proteus schematic file ...

Post a reply to Praveen Sharma

Think you can help Praveen Sharma ? Then post your thoughts that might help Praveen Sharma. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.