This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I2C random read.

Hi,

I need to read and write to the location address of unsigned int.Means location beyond the unsigned char i.e 255.

I am pasting the code.It works fine for the writing and reading to the location in the range of unsigned char.

#include <AT89X51.H>
#include <stdio.h>
#include <intrins.h>

#define uchar unsigned char
#define uint unsigned int
#define Byte unsigned char
#define Word unsigned int
#define bool bit
#define true 1
#define false 0

void I2CStart(void);
void I2CStop(void);
bool WaitAck(void);
void SendAck(void);
void SendNotAck(void);
void I2CSendByte(Byte);
Byte I2CReceiveByte(void);


void inoutstat(uchar);
void timetag(uchar);

#define SomeNOP(); _nop_();_nop_();_nop_();_nop_();


void read_mem(uchar );
void write_mem(uchar );

void inoutstat(void);
void timestamp(void);

sbit SDA = P2^0;
sbit SCL = P2^1;


uchar stat,buff[10],wr_buff[10],byte_address[2];


void main(void)
{
uchar i=0,SOP;


SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xfd; /* TH1: reload value for 1200 baud @ 16MHz */
TR1 = 1; /* TR1: timer 1 run */
TI = 1;

while(1)
{
SOP=getchar();
if(SOP=='R')
{
byte_address[0]=getchar();
read_mem(1);
printf("%c",buff[0]);

inoutstat(); // Change the Status (IN -> OUT and OUT -> IN

timestamp(); // Time Stamp the Status Change for attendance

read_mem(7);

for(i=0;i<7;i++)
printf("%c",buff[i]);

SOP=0;
}

}
printf("\n");
}


void inoutstat(void)
{
read_mem(1);
stat=buff[0];
if(stat=='I')
stat='O';
else
stat='I';

wr_buff[0]=stat;
write_mem(1);
}

void timestamp(void)
{
uchar id,hr,sec,date,month,yr;
id=byte_address[0];
hr=9;
sec=7;
date=5;
month=11;
yr=4;

wr_buff[0]=id;
wr_buff[1]=hr;
wr_buff[2]=sec;
wr_buff[3]=date;
wr_buff[4]=month;
wr_buff[5]=yr;
wr_buff[6]=stat;

byte_address[0]=1;
read_mem(1);
byte_address[0]=11;
write_mem(7);
}

void write_mem(uchar count)
{
int i;
I2CStart();
I2CSendByte(0xA2);
WaitAck();
I2CSendByte(byte_address[0]);
WaitAck();
for(i=0;i<count;i++)
{
I2CSendByte(wr_buff[i]);
WaitAck();
}
I2CStop();
}

void read_mem(uchar count)
{
uchar i;
I2CStart();
I2CSendByte(0xA2);
WaitAck();
I2CSendByte(byte_address[0]);
WaitAck();
I2CStart();
I2CSendByte(0xA3);
WaitAck();
for (i=0;i<count;i++)
{
buff[i]=I2CReceiveByte();
if (i!=count-1) SendAck();
}
SendNotAck();
I2CStop();
}

  • 1) Right over the entry window it states "Place source code source code between pre and /pre."
    2) I do not have the time to look at uncommented code.

    Erik

  • #include <AT89X51.H>
    #include <stdio.h>
    #include <intrins.h>
    
    #define uchar unsigned char
    #define uint unsigned int
    #define Byte unsigned char
    #define Word unsigned int
    #define bool bit
    #define true 1
    #define false 0
    
    void I2CStart(void);
    void I2CStop(void);
    bool WaitAck(void);
    void SendAck(void);
    void SendNotAck(void);
    void I2CSendByte(Byte);
    Byte I2CReceiveByte(void);
    
    
    void inoutstat(uchar);
    void timetag(uchar);
    
    #define SomeNOP(); _nop_();_nop_();_nop_();_nop_();
    
    
    void read_mem(uchar );
    void write_mem(uchar );
    
    void inoutstat(void);
    void timestamp(void);
    
    sbit SDA = P2^0;
    sbit SCL = P2^1;
    
    
    uchar stat,buff[10],wr_buff[10],byte_address[2];
    
    
    void main(void)
    {
    uchar i=0,SOP;
    
    
    SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
    TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
    TH1 = 0xfd; /* TH1: reload value for 1200 baud @ 16MHz */
    TR1 = 1; /* TR1: timer 1 run */
    TI = 1;
    
    while(1)
    {
    SOP=getchar();
    if(SOP=='R')
    {
    byte_address[0]=getchar();
    read_mem(1);
    printf("%c",buff[0]);
    
    inoutstat(); // Change the Status (IN -> OUT and OUT -> IN
    
    timestamp(); // Time Stamp the Status Change for attendance
    
    read_mem(7);
    
    for(i=0;i<7;i++)
    printf("%c",buff[i]);
    
    SOP=0;
    }
    
    }
    printf("\n");
    }
    
    
    void inoutstat(void)
    {
    read_mem(1);
    stat=buff[0];
    if(stat=='I')
    stat='O';
    else
    stat='I';
    
    wr_buff[0]=stat;
    write_mem(1);
    }
    
    void timestamp(void)
    {
    uchar id,hr,sec,date,month,yr;
    id=byte_address[0];
    hr=9;
    sec=7;
    date=5;
    month=11;
    yr=4;
    
    wr_buff[0]=id;
    wr_buff[1]=hr;
    wr_buff[2]=sec;
    wr_buff[3]=date;
    wr_buff[4]=month;
    wr_buff[5]=yr;
    wr_buff[6]=stat;
    
    byte_address[0]=1;
    read_mem(1);
    byte_address[0]=11;
    write_mem(7);
    }
    
    void write_mem(uchar count)
    {
    int i;
    I2CStart();
    I2CSendByte(0xA2);
    WaitAck();
    I2CSendByte(byte_address[0]);
    WaitAck();
    for(i=0;i<count;i++)
    {
    I2CSendByte(wr_buff[i]);
    WaitAck();
    }
    I2CStop();
    }
    
    void read_mem(uchar count)
    {
    uchar i;
    I2CStart();
    I2CSendByte(0xA2);
    WaitAck();
    I2CSendByte(byte_address[0]);
    WaitAck();
    I2CStart();
    I2CSendByte(0xA3);
    WaitAck();
    for (i=0;i<count;i++)
    {
    buff[i]=I2CReceiveByte();
    if (i!=count-1) SendAck();
    }
    SendNotAck();
    I2CStop();
    } 

  • Have you not heard of indention. Such a sloppy write does not gain from pre and /pre.

    Anyhow nobody can nhelpm you till you specify which eeprom you use.

    Erik

  • Hi Erik,

    I am trying my best to explain my problem...
    I will be using AT24C512 and what i want to do is write "1" or "0" at memory location 1 to 5 then after 3 bytes of information starting from 7 location till the memory is full.

    for this i want to know how to write at location beyond 255 (unsigned char) address.

    thankx
    rutu

  • have you considered to read the datasheet
    http://www.atmel.com/dyn/resources/prod_documents/doc1116.pdf

    pages 11 and 12 state it all.

    Erik

  • Hi Erik,

    Thanks, i did it.Now can write to any location on AT24C512 and do Random Read also.

    Rutu.

  • Rutu,

    Do you have to be told to read a datasheet?

    Erik