Hi All,
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 moths.
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, after connecting probes to SDA & SCL 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 share my code and bread board connections if needed.
I can try whatever you guys think may go wrong.
Please help me in finding/troubleshoot the problem.
Here is my code which I am using.
#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;
}