hey i hav written code 4 i2c programming...bt cnt read d data i hav written...pls help me as soon as possible... here is my code...
#include<reg51.h> #define EEPROMS_ID 0xA0 /* Microchip 24xx512 */ //64k rom 24c16 sbit dbit=ACC^7; sbit SDA=P2^2; /* Set P3.2 = SDA */ sbit SCL=P2^3; /* Set P3.3 = SCL */ void I2C_stop(void) { unsigned char j; SDA = 0; /* Clear SDA */ for(j=0;j<=17;j++); SCL = 1; /* Set SCL */ for(j=0;j<=17;j++); SDA = 1; /* Set SDA */ } I2C_write(unsigned char dat) { bit data_bit; unsigned char i,j; SDA = 1; /* Set SDA */ for(j=0;j<=4;j++); SCL = 1; /* Set SCL */ for(j=0;j<=17;j++); SDA = 0; /* Clear SDA */ for(j=0;j<=17;j++); SCL = 0; /* Clear SCL */ for(j=0;j<=17;j++); ACC=dat; //repeat: for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */ { data_bit = dbit; /* Filter MSB bit keep to data_bit */ SDA = data_bit; /* Send data_bit to SDA */ SCL = 1; /* Set SCL */ for(j=0;j<=17;j++); SCL = 0; /* Set SCL */ for(j=0;j<=17;j++); ACC = ACC<<1; } SDA = 1; /* Set SDA */ for(j=0;j<=4;j++); SCL = 1; /* Set SCL */ for(j=0;j<=17;j++); data_bit = SDA; /* Check acknowledge */ SCL = 0; /* Clear SCL */ for(j=0;j<=17;j++); //if(data_bit!=0) //goto repeat; } I2C_write1(unsigned char dat) { bit data_bit; unsigned char i,j; ACC=dat; //repeat: for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */ { data_bit = dbit; /* Filter MSB bit keep to data_bit */ SDA = data_bit; /* Send data_bit to SDA */ SCL = 1; /* Set SCL */ for(j=0;j<=17;j++); SCL = 0; /* Set SCL */ for(j=0;j<=17;j++); ACC = ACC<<1; } SDA = 1; /* Set SDA */ for(j=0;j<=4;j++); SCL = 1; /* Set SCL */ for(j=0;j<=17;j++); data_bit = SDA; /* Check acknowledge */ SCL = 0; /* Clear SCL */ for(j=0;j<=17;j++); //if(data_bit!=0) //goto repeat; } I2C_read() { unsigned char i,j,data_in; SDA = 1; /* Set SDA */ for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */ { //SCL = 0; /* Clear SCL */ //for(j=0;j<=17;j++); SCL = 1; /* Clear SCL */ for(j=0;j<=17;j++); dbit = SDA; /* Filter MSB bit keep to data_bit */ ACC = ACC>>1; SCL = 0; /* Clear SCL */ for(j=0;j<=17;j++); } //SCL = 0; /* Clear SCL */ data_in=ACC; SDA = 1; /* Set SDA */ for(j=0;j<=4;j++); SCL = 1; /* Set SCL */ for(j=0;j<=17;j++); SCL = 0; /* Clear SCL */ for(j=0;j<=17;j++); return data_in; } void EEPROM_set(unsigned char addr, unsigned char val) //write the data { I2C_write(EEPROMS_ID); /* Connect to EEPROM */ I2C_write1(addr); I2C_write1(val); /* Write sec on RAM specified address */ I2C_stop(); } EEPROM_get(unsigned char addr) //random read function { char rdcmd,dat_read; I2C_write(EEPROMS_ID); /* Connect to EEPROM */ I2C_write1(addr); rdcmd=EEPROMS_ID+1; I2C_write(rdcmd); dat_read=I2C_read(); I2C_stop(); return dat_read; } void main() { unsigned char data_wrt,dt_read,j; unsigned char address_read,address_wrt; data_wrt=0xf0; address_read=0x01; address_wrt=0x01; EEPROM_set(address_wrt,data_wrt); for(j=0;j<=200;j++); dt_read=EEPROM_get(address_read); while(1) P1=dt_read; }
pls rply as soon as possible.....
hey i hav tried other variable also but still nt working...
cn u pls suggest me wat r d other mistakes...
... not posting in English. I, for one have no desire to decipher your gobellygook.
If you are posting from a cellphone ignore this message.
Erik
"cn u pls suggest me wat r d other mistakes..."
The problem is that you are using the verbose C mode which is much too slow. You need to use the SCS mode (Short C Syntax) to speed up your program. I use this sed script to translate from verbose C to SCS:
s/define/dfyn/ s/include/nkld/ s/double/dbl/ s/int/nt/ s/struct/strk/ s/break/brk/ s/else/ls/ s/long/lng/ s/switch/swch/ s/case/ks/ s/enum/nm/ s/typedef/typdf/ s/char/kr/ s/extern/xtrn/ s/return/rtrn/ s/union/unn/ s/const/knst/ s/float/flt/ s/short/srt/ s/unsigned/nsynd/ s/continue/kntnu/ s/for/4/ s/signed/synd/ s/void/vd/ s/default/dflt/ s/goto/go2/ s/sizeof/syzf/ s/volatile/vltyl/ s/do/do/ s/if/f/ s/static/sttk/ s/while/wyl/
k...thnx 4 suggestion...i hav changed my program as ur wish...
bt i m still nt getting output.... pls rrply wat r d actual prob apart from dat u told.....
The problem is that you are using the verbose C mode which is much too slow a perfect solution to the problem
And the cool thing is that if you are ever asked for an example of your coding style (say for a job interview as a project team contributor or some lame thing like that) and need to translate your SCS back to the "old-farts" (my term) verbose C (a.k.a. LuZrC to kewl Do0dZ), it's a simple matter of swapping the substitution terms and BAM! -- you're done. You don't give away the fact that you can't communicate with other project contributors. Hired! Oh, and you only got the interview because you had someone else write your resume and got your degree from ... (the SPAMMERS that post here).
Don't get me started!