Microcontroller › 8051 › Serial EEPROM 24C02 with AT89c51 microcontroller › //Program to interface
July 27, 2014 at 3:46 am
#11946
Participant
<code>
//Program to interface Serial EEPROM AT24C02 with 8051 microcontroller (AT89C51)
#include <mcs51/at89x51.h>
#define _nop_()
__asm
nop
__endasm;
#define SDA P2_4 //Pin 25
#define SCL P2_3 //Pin 24
// device_address 0xAE //device address/1010 xxx 0
// device_address_read 0xAF //device address/1010 xxx 1
// word_address 0x00 //word_address 0x00/00000 000
//data_1 P3
//data_2 P1
__bit ack;
void delaySeconds(unsigned char asec)
{
unsigned char il,ms;
int s=0;
for(il=0;il<asec;il++)
{
for(s=0;s<1000;s++)
{
for(ms=0;ms<250;ms++)
{
}
}
}
}
void aknowledge() //acknowledge condition
{
SCL=1;
_nop_();
_nop_();
SCL=0;
}
void start() //start condition
{
SDA=1;
SCL=1;
_nop_(); //No operation
_nop_();
SDA=0;
SCL=0;
}
void stop() //stop condition
{
SDA=0;
SCL=1;
_nop_();
_nop_();
SDA=1;
SCL=0;
}
void send_byte(unsigned char value) //send byte serially
{
unsigned int ii;
unsigned char send;
send=value;
for(ii=0;ii<8;ii++)
{
SDA=send/128; //extracting MSB
send=send<<1; //shiftng left
SCL=1;
_nop_();
_nop_();
SCL=0;
}
ack=SDA; //reading acknowledge
SDA=0;
}
unsigned char read_byte() //reading from EEPROM serially
{
unsigned int ii;
unsigned char reead;
SDA=1;
reead=0;
for(ii=0;ii<8;ii++)
{
reead=reead<<1;
SCL=1;
_nop_();
_nop_();
if(SDA==1)
reead++;
SCL=0;
}
SDA=0;
return reead; //Returns 8 bit data here
}
void save(unsigned char device_address_s, unsigned char word_address_s, unsigned char data_1_s) //save in EEPROM
{
start();
send_byte(device_address_s); //device address/1010 xxx 0
aknowledge();
send_byte(word_address_s); //word_address 0x00 00000 000
aknowledge();
send_byte(data_1_s); //send data
aknowledge();
stop();
_nop_();
_nop_();
aknowledge();
}
unsigned char Read(unsigned char device_address_r, unsigned char device_address_read_r, unsigned char word_address_r)
{
unsigned char data_1_r;
start();
send_byte(device_address_r);
aknowledge();
send_byte(word_address_r);
aknowledge();
start();
send_byte(device_address_read_r); //device address
aknowledge();
data_1_r=read_byte();
aknowledge();
stop();
_nop_();
_nop_();
aknowledge();
return data_1_r;
}
void main()
{
unsigned char dev_add_w, dev_add_r;
unsigned char word_add[] = {0x00, 0x01};
unsigned char data_store[] = {0x0CC, 0xAA};
unsigned char data_chk[] = {0x00, 0x00};
dev_add_w = 0xAE;
dev_add_r = 0xAF;
while(1)
{
save(dev_add_w, word_add[0], data_store[0]);
data_chk[0] = Read(dev_add_w, dev_add_r, word_add[0]);
if (data_chk[0] == data_store[0])
{
P3 = data_chk[0];
}
delaySeconds(2);
data_chk[0] = 0;
save(dev_add_w, word_add[1], data_store[1]);
data_chk[1] = Read(dev_add_w, dev_add_r, word_add[1]);
if (data_chk[1] == data_store[1])
{
P3 = data_chk[1];
}
delaySeconds(2);
data_chk[1] = 0;
}
}
</code>
compiled in MIDE 51
At first i’m trying to do that for 11 numbers, but for debugging i reduced it to just this. the problem I’m facing is: most of the time the 7segment LED not displaying any thing, (sometime it shows properly, though very few times) all connections connectivity are OK.
is the code OK? can you help me by running the code into your system?
Sorry for late reply, my exams are going on
Sorry for late reply, my exams are going on
Thank you very much