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.

serial (232) writing in 24C04 for 89c51 -- can some one help

Status
Not open for further replies.

ronydc

Full Member level 3
Joined
Nov 17, 2005
Messages
166
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,298
Activity points
2,612
24c04 asm 8051

hi friends,
can some one guide me how to write the serial data comming from pc. to the eeprom like 24c04..? i m using 89c51 MCU from ATMEL and using KAIL UV-2 to program it in "c"
i can take the serial data out on the ports.. but i am stuck at how to write it to the EEPROM like 24C04 and read it back.. on serial port..( on pc).
plz help me and plz suggest me the solution in "c" bcs i m not much familier with 8051 Assembly.
thanks in advance.

-rony.
 

serial port reader atmel 24c04

I think you need the driver for 24C04 first. There are a lot of threads in this forum with fully working driver.

John
 

24c04

24XX512.h


#include "i2c.h" /* Need i2c bus */

#define EEPROMS_ID 0xA0 /* Microchip 24xx512 */

unsigned char EEPROM_get(unsigned int addr)
{
unsigned char dat;

I2C_start(); /* Start i2c bus */

I2C_write(EEPROMS_ID); /* Connect to EEPROM */
I2C_write(addr&0xF0); /* Request RAM address (Hight byte) */
I2C_write(addr&0x0F); /* Request RAM address (Low byte) */

I2C_start(); /* Start i2c bus */

I2C_write(EEPROMS_ID+1);/* Connect to EEPROM for Read */
dat = I2C_read(); /* Receive data */

I2C_noack();

I2C_stop(); /* Stop i2c bus */

return dat;
}

void EEPROM_set(unsigned int addr, unsigned char val)
{
I2C_start();

I2C_write(EEPROMS_ID); /* Connect to EEPROM */
I2C_write(addr&0xF0); /* Request RAM address (Hight byte) */
I2C_write(addr&0x0F); /* Request RAM address (Low byte) */

I2C_write(val); /* Write sec on RAM specified address */

I2C_stop(); /* Stop i2c bus */
}



i2c.h


#define SDA P2_2 /* Set P2.7 = SDA */
#define SCL P2_3 /* Set P2.6 = SCL */
#define I2C_DELAY 0x0F /* For delay i2c bus */

void I2C_delay(void)
{
unsigned char i;

for(i=0; i<I2C_DELAY; i++);
}

void I2C_clock(void)
{
I2C_delay();

SCL = 1; /* Start clock */

I2C_delay();

SCL = 0; /* Clear SCL */
}

void I2C_start(void)
{
if(SCL)
SCL = 0; /* Clear SCL */

SDA = 1; /* Set SDA */
SCL = 1; /* Set SCL */

I2C_delay();

SDA = 0; /* Clear SDA */

I2C_delay();

SCL = 0; /* Clear SCL */
}

void I2C_stop(void)
{
if(SCL)
SCL = 0; /* Clear SCL */

SDA = 0; /* Clear SDA */
I2C_delay();

SCL = 1; /* Set SCL */

I2C_delay();

SDA = 1; /* Set SDA */
}

bit I2C_write(unsigned char dat)
{
bit data_bit;
unsigned char i;

for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */
{
data_bit = dat & 0x80; /* Filter MSB bit keep to data_bit */
SDA = data_bit; /* Send data_bit to SDA */

I2C_clock(); /* Call for send data to i2c bus */

dat = dat<<1;
}

SDA = 1; /* Set SDA */

I2C_delay();

SCL = 1; /* Set SCL */

I2C_delay();

data_bit = SDA; /* Check acknowledge */
SCL = 0; /* Clear SCL */

I2C_delay();

return data_bit; /* If send_bit = 0 i2c is valid */
}

unsigned char I2C_read(void)
{
bit rd_bit;
unsigned char i, dat;

dat = 0x00;

for(i=0;i<8;i++) /* For loop read data 1 byte */
{
I2C_delay();

SCL = 1; /* Set SCL */

I2C_delay();

rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */

SCL = 0; /* Clear SCL */
}

return dat;
}

void I2C_ack()
{
SDA = 0; /* Clear SDA */

I2C_delay();

I2C_clock(); /* Call for send data to i2c bus */

SDA = 1; /* Set SDA */
}

void I2C_noack()
{
SDA = 1; /* Set SDA */

I2C_delay();

I2C_clock(); /* Call for send data to i2c bus */

SCL = 1; /* Set SCL */
}


main

#include <AT89X51.H>
#include <24xx512.h>

unsigned int del,i,x,temp;

char getCharacter (void)
{
char chr; // variable to hold the new character
while (RI != 1) {;}
chr = SBUF;
RI = 0;
return(chr);
}
void send (char a)
{
SBUF = a;
while (TI != 1);
TI=0;
}

void main (void){
char chr;
int i=0,j=0,count=0,inc=47;//inc=47
int index=0;
P1=0X00;
P2=0X00;

SCON = 0x50; // mode 1, 8-bit uart, enable receiver
TMOD = 0x20; // timer 1, mode 2, 8-bit reload
TH1 = 0XE6; // 1200
TL1 = 0XE6;
TR1 = 1;
TI = 0;
SBUF =0;

while(1){


your code

}//end of while

}//end of main
 

89c51 i2c

ronydc said:
hi friends,
can some one guide me how to write the serial data comming from pc. to the eeprom like 24c04..? i m using 89c51 MCU from ATMEL and using KAIL UV-2 to program it in "c"
i can take the serial data out on the ports.. but i am stuck at how to write it to the EEPROM like 24C04 and read it back.. on serial port..( on pc).
plz help me and plz suggest me the solution in "c" bcs i m not much familier with 8051 Assembly.
thanks in advance.

-rony.

**broken link removed**
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top