Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I2C memory example codes with pic 16f84a

Status
Not open for further replies.

pavan_85

Member level 1
Joined
Jul 3, 2008
Messages
33
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Location
hyderabad
Activity points
1,497
i2c protocol

Hi all,
I have written a code to store data into a serial eeprom. I want to check whether the code is right or wrong...before dumping the code into the micro processor...

I am trying the simulations using proteus but i am unable to find get the results.

please help me out.

pavan
 

how to write program in i²c potocol

Code review is a worthwhile thing. But why are you hesitant to write the code to the processor? I would write a small test program (aka "test bed") which would 1st call your EEPROM functions and write known set of bytes into a predetermined location in the EEPROM. 2nd it would read from the EEPROM and dump the bytes to the debug output.
 

eeprom i2c protocol

Hi all,

These are schematics and code...
My intention was to first to pass a control byte to the eeprom and get the acknowledgment.when i get this ack i will make the led on port1 high.
I donot know whether my code works or not...


schematics:
4_1248829755.gif

code:
Code:
#include <regx51.h>

// external declarations
sbit sda = P1^1;
sbit scl = P1^2;

sbit led = P1^0;

void start();
void stop();
void delay(unsigned int);//in micro seconds
void write(unsigned char);

void wait(unsigned int itime)
{
	unsigned int i,j;
	for(i=0;i<itime;i++)
		for(j=0;j<10;j++); //10us delay ----clock freq = 12Mhz-----------
}

void start()
{
	scl = 1;
	sda = 1;
	wait(5);	//x10us delay
	sda = 0;
	wait(5);	//x10us delay
	scl = 0;
	sda = 0;
		
}

void stop()
{
	scl = 0;
	sda = 0;
	scl = 1;
	wait(5);	//x10us delay
	sda = 1;
		
}

void write(unsigned char data1)
{
	unsigned int i;
	for(i=0;i<8;i++)
	{
		scl = 0;
			if(data1&0x80)
			{
				sda = 1;
			}
			else
				sda = 0;
		scl = 1;
		data1 = data1 << 1;
	}
}

void eeprom_write()
{
	led = 1;   //this is just to check whether we are getting ack or not from EEPROM
	start();
	write(0x50);
	sda = 1;
	while(sda==1);
	led = 0;
	stop();
}

void main(void)
{
eeprom_write();
}
 

i2c protocol 2 byte addressing

where is endless loop while(1) in your code

MicroCode
 

declaration sbit

yeah! also you aren't writting data in the eeprom, you need to give it's I2C Address (which is A0h for the first 2kb of the 24C16) so... also!!!!! you are missing the Acknowledge bit!!!! (you must clock it!!!)


Code:
#include <regx51.h>

// external declarations
sbit sda = P1^1;
sbit scl = P1^2;

sbit led = P1^0;

void start();
void stop();
void delay(unsigned int);//in micro seconds
bit write(unsigned char data1)

void wait(unsigned int itime)
{
   unsigned int i,j;
   for(i=0;i<itime;i++)
      for(j=0;j<10;j++); //10us delay ----clock freq = 12Mhz-----------
}

void start()
{
   scl = 1;
   sda = 1;
   wait(5);   //x10us delay
   sda = 0;
   wait(5);   //x10us delay
   scl = 0;
   sda = 0;
      
}

void stop()
{
   scl = 0;
   sda = 0;
   scl = 1;
   wait(5);   //x10us delay
   sda = 1;
      
}

bit write(unsigned char data1)
{
   bit ack;
   unsigned char i;
   for(i=0;i<8;i++)
   {
      scl = 0;
         if(data1&0x80)
            sda = 1;
         else
            sda = 0;
      scl = 1;
      data1 = data1 << 1;
      wait(5);   //x10us delay 
      scl = 0;
   }
   scl=0;
   sda=1; //input...
   wait(5);   //x10us delay 
   ack=sda;
   scl=0;
   return ack;
} 
void eeprom_write(char address,char data)
{
   led = 1;  
   start();
   led=write(0xA0); //i2c address
   led=write(address); //byte address
   led=write(data); //data itself
   stop();
 //you need at least 10ms to write the next data, so add a delay here...
}

void main(void)
{
  sda=scl=1; // just in case... it's not needed in a 8051

  eeprom_write(0x00, 0x50); // at address 0, value 50h

  while(1); //eternal darkn... ejem.. loop...

}

also! why didn't you added te I2C analyser in proteus? it helps to check all the i2c frame... I did'nt have time to check the simulation but i think it will work... hope it helps...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top