dear all
i am stuck in a mid part of the project... i want to interface at24c08 eeprom with p89v51rd2... please can any one write a workable code for me...
i only want ot send bytes of character and store it there so that i can retrieve the data some time later... please help me... i m in a bad situation... i have tried many other code available on the net but its not working... please help...
# include<reg52.h> # include<stdio.h> # include<intrins.h>
#define ACK 1 #define NO_ACK 0
sbit a=P2^0; sbit b= P2^1; char getCharacter (void); void sendCharacter (char); void Start(void); void Stop(void); unsigned char ReadI2C(bit); unsigned char ReadBYTE(unsigned int); void WriteBYTE(unsigned int ,unsigned char);
sbit SDA = P0^1; sbit SCL = P0^3;
void main (void) {
char chr; unsigned char EData;
#ifndef MONITOR51 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; /* TI: set TI to send first char of UART */ #endif a=0;b=0; while(1) { if(a==1) goto send; else if (b==1) goto receive; else continue; }
send: chr=0x55; WriteBYTE(0x0000,chr); printf("byte stored"); goto end;
receive: printf("\nhere it is:"); EData = ReadBYTE(0x0000); printf("%c",EData); sendCharacter(EData); printf("\ndone!!!");
end: while(1) goto end; } /* char getCharacter (void) { char chr; while(RI!=0){;} chr=SBUF; RI=0; return(chr); } */ void sendCharacter (char chr) { while (TI!=1) {;} TI=0; SBUF=chr; return; } void Start(void) { SDA = 1; SCL = 1; _nop_();_nop_(); SDA = 0; _nop_();_nop_(); SCL = 0; _nop_();_nop_(); } void Stop(void) { SDA = 0; _nop_();_nop_(); SCL = 1; _nop_();_nop_(); SDA = 1; } void WriteI2C(unsigned char Data) { int i; for (i=0;i<8;i++) {
SDA = (Data & 0x80) ? 1:0; SCL=1;SCL=0; Data<<=1; }
SCL = 1; _nop_();_nop_(); SCL = 0; }
unsigned char ReadI2C(bit ACK_Bit) {
unsigned char Data=0; int i; SDA = 1; for (i=0;i<8;i++) { SCL = 1; Data<<= 1; Data = (Data | SDA); SCL = 0; _nop_(); }
if (ACK_Bit == 1) SDA = 0; // Send ACK else SDA = 1; // Send NO ACK
_nop_();_nop_(); SCL = 1; _nop_();_nop_(); SCL = 0;
return Data; } unsigned char ReadBYTE(unsigned int Addr) { unsigned char Data; Start(); WriteI2C(0xA0); WriteI2C((unsigned char)(Addr>>8)&0xFF); WriteI2C((unsigned char)Addr&0xFF); Start(); WriteI2C(0xA1); Data = ReadI2C(NO_ACK); Stop(); return(Data); } void WriteBYTE(unsigned int Addr,unsigned char Data) { Start(); WriteI2C(0xA0); WriteI2C((unsigned char)(Addr>>8)&0xFF); // send address high WriteI2C((unsigned char)Addr&0xFF); // send address low WriteI2C(Data); Stop(); }
now this program is not working... can anyone help me with this.... is the addressing i am doing for 24c08 valid or it should be different...???
First Format the code see "Tips for Posting Messages". Second do you have a oscilloscope?
What does the Datasheet tell you?