Sir, I am trying to write 34 into the eeprom at address location 0x0000, but whenever I read and dislpay it on lcd, 49 is displayed. please help me.
my code
sbit SCL=P3^7; // Port pin for SCL line sbit SDA=P3^6; // Port pin for SDA line
void i2c_delay(void); // I2C Clock delay void i2c_start(void); // Generates a I2C Start condition void i2c_stop(void); // Generates a I2C Stop condition void i2c_wbyte(char i2c_wb); // Writes a single byte from I2C Slave unsigned char i2c_rbyte(void); // Reads and returns a single byte from I2C Slave char i2c_ack(void); void i2c_nack(void); void i2c_write(char id, char add,char dat); unsigned char i2c_read(char id,char add);
void i2c_delay(void) { char a; for(a=0;a<80;a++); }
void i2c_start(void) { SCL = 1; i2c_delay(); SDA = 1; i2c_delay(); SDA = 0; i2c_delay(); SCL = 0; i2c_delay(); }
void i2c_stop(void) { //SDA_Dir = 1; //SDA pin as output SDA = 0; i2c_delay(); SCL = 1; i2c_delay(); SDA = 1; i2c_delay(); SCL = 0; }
void i2c_wbyte(unsigned char i2c_wb) { char a; //DA_Dir = 1; // Set SDA pin as output for(a=0;a<8;a++) // Send 8 bits { SCL = 0; i2c_delay(); if(i2c_wb & (0x80 >> a)) { SDA = 1; } else { SDA = 0; } i2c_delay(); SCL = 1; i2c_delay(); }
}
unsigned char i2c_rbyte(void) { char a; unsigned char b; b=0; //SDA_Dir = 0; // Set SDA pin as input for(a=0;a<8;a++) { SCL = 1; i2c_delay(); if(SDA) { b |= (0x80 >> a); // Set data bit on SDA line } i2c_delay(); SCL = 0; i2c_delay(); } return(b); } char i2c_ack(void) { char a; //SDA_Dir = 1; // Set SDA pin as output i2c_delay(); SCL = 0; i2c_delay(); SDA = 1; i2c_delay(); SCL = 1; i2c_delay();
//SDA_Dir = 0; // Set SDA pin as input i2c_delay(); a = SDA; i2c_delay(); SCL = 0; i2c_delay(); return(a); }
void i2c_nack(void) { //SDA_Dir = 1; // Set SDA pin as output SDA = 1; i2c_delay(); SCL = 1; i2c_delay(); SCL = 0; i2c_delay(); } void i2c_write(unsigned char id, unsigned char add, unsigned char dat) { i2c_start(); i2c_wbyte(id); i2c_ack(); i2c_wbyte(add); i2c_ack(); i2c_wbyte(dat); i2c_ack(); i2c_stop(); }
unsigned char i2c_read(unsigned char id,unsigned char add) { unsigned char rd; i2c_start(); i2c_wbyte(id); i2c_ack(); i2c_wbyte(add); i2c_ack(); i2c_start(); i2c_wbyte((id|0x01)); i2c_ack(); rd = i2c_rbyte(); i2c_nack(); i2c_stop(); return(rd); } main() { unsigned char v=34; unsigned char dat=0; i2c_write(0xA0,0x0000,v); dat=i2c_read(0xA0,0x0000); send2lcd(dat); } the delay between read and write correct? the delay between scl and sda line, is ok? please help me
thanks in advance
void i2c_delay(void) { volatile register double b; volatile register double c; for (b=0 ; b < 80 ; b++) // loop1 { for (c=0 ; c < 80 ; c += b); // on the first iteration of loop 1 b will // be zero thus this will never complete } }</pr> why the construed construct? Erik
why the construed construct?
its not construed. its called style. its like what i call my code signature.
the loop will end because it is volatile and something else will change it.
the loop will end because it is volatile and something else will change it. that's nice, who but you would know.
READ THE INSTRUCTIONS FOR POSTING and post your code again.
Did you even wonder if anycone could make head or tails of your original post when you previewed it?
Erik
I have use i2c_delay function in my program. but there is a error i.e syntax error near 'register' me not able to find that error...
me not able to find that error...
neither are we when you do no post your code correctly, and show the full error message
is it REALLY that difficult to read and follow the instructions in the "Enter Message Details Below" window?