Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

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
 

kender

Advanced Member level 4
Joined
Jun 19, 2005
Messages
1,422
Helped
138
Reputation
276
Reaction score
38
Trophy points
1,328
Location
Stanford, SF Bay Peninsula, California, Earth, Sol
Activity points
9,966
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.
 

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
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:

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();
}
 

microcon555

Advanced Member level 4
Joined
Feb 13, 2009
Messages
109
Helped
9
Reputation
18
Reaction score
5
Trophy points
1,298
Activity points
1,877
i2c protocol 2 byte addressing

where is endless loop while(1) in your code

MicroCode
 

Kurenai_ryu

Advanced Member level 2
Joined
Jun 10, 2006
Messages
671
Helped
159
Reputation
316
Reaction score
99
Trophy points
1,308
Location
Bolivia
Activity points
5,913
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top