In Proteus simulation (it is first of all running very slow due to excessive CPU load). #include <LPC21XX.h> #include <stdio.h> #include <string.h> #define SDA (1 << 3) // P0.3 #define SCL (1 << 2) // P0.2 #define eprom_dir_sda_write() IODIR0 |= (1 << 3) // LCD Data Bus = Write #define eprom_dir_clk_write() IODIR0 |= (1 << 2) #define eprom_dir_sda_read() IODIR0 &= ~(1 << 3) // LCD Data Bus = Write #define eprom_dir_clk_read() IODIR0 &= ~(1 << 2) #define sda_set() IOSET0 |= (1 << 3) #define scl_set() IOSET0 |= (1 << 2) #define sda_clr() IOCLR0 |= (1 << 3) #define scl_clr() IOCLR0 |= (1 << 2) #define HIGH 0x01 #define LOW 0x00 #define readaddress 161 #define writeaddress 160 void eeprom_start(void); void eeprom_stop(void); void eeprom_write(unsigned char input_data); unsigned char eeprom_read(void); void delaytime(unsigned int time_end); void eprom_memwrite(unsigned int memorylocation, unsigned char value ) ; unsigned char eprom_memread(unsigned int memorylocation ) ; int main() { PINSEL0|=0x00000050;//Configuring P0.2 and P0.3 as SCL and SDA ser_ini(); lcd_init(); lcd_print("Hello"); eprom_dir_sda_write(); eprom_dir_clk_write(); eeprom_start(); eeprom_write(writeaddress); eeprom_write(0); eeprom_write(0); for (i=0;i<50;i++) eeprom_write(2); eeprom_stop(); delaytime(10) ; eeprom_start(); eeprom_stop(); i=eeprom_memread(1); lcd_write_ascii(i|0x30); ser_data(i|0x30); } }
void eeprom_start (void) { eprom_dir_sda_write(); eprom_dir_clk_write(); sda_set(); delaytime(3); scl_set(); delaytime(2); sda_clr(); scl_clr(); }
void eeprom_stop (void) { eprom_dir_sda_write(); eprom_dir_clk_write(); scl_clr(); sda_clr(); scl_set(); delaytime(3); sda_set(); }
void eeprom_write (unsigned char output_data) { unsigned char index; eprom_dir_sda_write(); eprom_dir_clk_write(); for (index = 0; index < 8; index++) { if (output_data & 0x80) sda_set(); else sda_clr(); output_data <<= 1; scl_set(); delaytime(2); scl_clr(); } scl_set(); delaytime(2); scl_clr(); }
unsigned char eeprom_read (void) { unsigned char index,input_data; eprom_dir_sda_read(); eprom_dir_clk_write(); input_data = 0; for (index = 0; index < 8; index++) { input_data <<= 1; scl_set(); input_data |= (IOPIN0 & SDA) ? 1 : 0; delaytime(2); scl_clr(); } return (input_data); }
unsigned char eeprom_memread ( unsigned int memorylocation ) { unsigned char data_in; eeprom_start(); eeprom_write(writeaddress); eeprom_write(memorylocation&0xff00) ; eeprom_write(memorylocation&0x00ff) ; eeprom_stop(); eeprom_start(); eeprom_write(readaddress); data_in = eeprom_read(); return data_in; }
void eeprom_memwrite ( unsigned int memorylocation, unsigned char value ) { eeprom_start(); eeprom_write(writeaddress); eeprom_write(memorylocation&0xff00); eeprom_write(memorylocation&0x00ff); eeprom_write(value); eeprom_stop(); delaytime(10) ; eeprom_start(); eeprom_stop(); }
void delaytime (unsigned int time_end) { unsigned int index; for (index=0;index<time_end;index++); }
Everytime it gives me 0 or 95 when i read it. Plz I ll be veryh appreciative of your help
LPC21xx have lots of hw goodies.
The user manual (available - as expected - from the manufacturer) does have a chapter with the name "I2C Interface". A very good chapter to study, unless the hardware is incorrectly designed - or the hardware needs multipe I2C busses.
When looking for the user manual for the processor, it would - of course - be an idea to also look for sample source code and/or application notes. LPC21xx are very much used even if now often replaced with LPC23xx or LPC17xx. There isn't exactly any lack of information available.
... it would be a surprise to me if it did not have HW IIC
Erik
Thanks Andrew and really sorry for the mess. Actually it was my first post and about the posting tips part I was really dumb to have skipped them. I was working on this code continuously on this code and it gave way to mental fatigue. But really thankful to you I will get back to the code and recheck it. Thanks for the help
The forum gives you very clear and simple instructions on how to post source code - as shown in this picture: www.danlhenry.com/.../keil_code.png
It even gives you a link titled Tips for Posting Messages which gives further explanation and examples: http://www.keil.com/forum/tips.asp
But you managed to miss all this, resulting in an illegible mess - with all your layout lost.
Programming is all about attention to detail: if you can miss clearly-stated, simple instructions like the above, then you're going to have a hard time with programming!
Just one example from your code:
eeprom_write( memorylocation & 0xff00 ); eeprom_write( memorylocation & 0x00ff );
What is the type of the parameter required by the eeprom_write function...?
"good gode? u talk to satnav!"
Not to many GPS devices uses a EEPROM as interface media for interfacing with a microcontroller. So no, the code posted here probably have very (extremely) little with satellite naviation to do. And it is posted here because it doesn't work - should give you a hint.
Are you hearing voices arash tareeq? What are they telling you?
good gode? u talk to satnav!
View all questions in Keil forum