Microcontroller › 8051 › Serial EEPROM 24C02 with AT89c51 microcontroller
- This topic has 4 replies, 2 voices, and was last updated 10 years, 8 months ago by
Ashutosh Bhatt.
-
AuthorPosts
-
July 14, 2014 at 12:22 pm #3147
PICATA
ParticipantI want to read and write 11, 8 bit data, with seperate word address, but in single 24C02 EEPROM
used code from
“How to Interface Serial EEPROM 24C02 with 8051 microcontroller (AT89C51)”
I used word address as:
unsigned char word_add[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
but it is not working properly.
is word_add chosen wrong??
-thnaks in advance
July 23, 2014 at 6:55 am #11926Ashutosh Bhatt
Participantr u having any compilation error in your program?
wht is exact problem that you r getting?
July 27, 2014 at 3:46 am #11946PICATA
Participant<code>//Program to interface Serial EEPROM AT24C02 with 8051 microcontroller (AT89C51)#include <mcs51/at89x51.h>#define _nop_()__asmnop__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 MSBsend=send<<1; //shiftng leftSCL=1;_nop_();_nop_();SCL=0;}ack=SDA; //reading acknowledgeSDA=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 0aknowledge();send_byte(word_address_s); //word_address 0x00 00000 000aknowledge();send_byte(data_1_s); //send dataaknowledge();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 addressaknowledge();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 51At 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 onThank you very much
July 29, 2014 at 5:03 pm #11956PICATA
ParticipantThe P3 value only showing the value which is in 0x00 word address (irrespective of word data)
I changed
unsigned char word_add[] = {0x00, 0x01};to
unsigned char word_add[] = {0x01, 0x00};
but the result same, data stored in 0x00 is showing only
I also checked it for 0x02, 0x08 address.
what was the problem? Please help me out.
August 1, 2014 at 7:18 am #11961Ashutosh Bhatt
Participanti have not completely checked the code but seems OK
but the thing is there may be or can be some unknown errors
the proper handshaking may not be happning between 89C51 and 24C02
this can happen because 8051 does not have built in IIC interface. we have to develop using program
that is why for IIC based devices like EEPROM, RTC, temperature sensors always choose/use micro controller with built in IIC interface like AVR or PIC or ARM
-
AuthorPosts
- You must be logged in to reply to this topic.