Hi everybody,
I'm having a little of problem with the I2C library, I read the datasheet of the eeprom 24LC256 in details to find out how I should communicate via I2C with it. I did everything like in the datasheet to "byte write" and then "random read". I just wanted to try to write a byte somewhere in the eeprom and then read it to make sure it was indeed written.
I'm using the simulation software proteus. I tested each function in my code one by one, but using a lcd screen and a variable tested with a "if" to make sure everytime an I2C funtion was executed, I got a "0" as indicated in the library documentation.
In the end I managed to pinpoint the problem, or I think I did. The I2C_rd() command doesn't want to work if there's no argument in it, so I tried 0, for the no ack, but I still can't get the byte I wrote. Same thing with 1. So I really don't know what to do now.
The address of the eeprom on the i2c bus is 000 so with the control byte either A0 for writing or A1 for reading mode. The random address I tried to use for the eeprom is 0x708F and I tried to write the byte 0x20. By the way, if someone know a way to convert the byte read into a char readeable by the lcd screen that would be nice, because I have to combare the byte read with the actual byte I put and display a message on the lcd screen.
Here's the code: (also I put a lot of delay_ms, I had problems with that, so since I wasn't sure where was the problem I put them everywhere, I'll remove them later)
I'm having a little of problem with the I2C library, I read the datasheet of the eeprom 24LC256 in details to find out how I should communicate via I2C with it. I did everything like in the datasheet to "byte write" and then "random read". I just wanted to try to write a byte somewhere in the eeprom and then read it to make sure it was indeed written.
I'm using the simulation software proteus. I tested each function in my code one by one, but using a lcd screen and a variable tested with a "if" to make sure everytime an I2C funtion was executed, I got a "0" as indicated in the library documentation.
In the end I managed to pinpoint the problem, or I think I did. The I2C_rd() command doesn't want to work if there's no argument in it, so I tried 0, for the no ack, but I still can't get the byte I wrote. Same thing with 1. So I really don't know what to do now.
The address of the eeprom on the i2c bus is 000 so with the control byte either A0 for writing or A1 for reading mode. The random address I tried to use for the eeprom is 0x708F and I tried to write the byte 0x20. By the way, if someone know a way to convert the byte read into a char readeable by the lcd screen that would be nice, because I have to combare the byte read with the actual byte I put and display a message on the lcd screen.
Here's the code: (also I put a lot of delay_ms, I had problems with that, so since I wasn't sure where was the problem I put them everywhere, I'll remove them later)
Code:
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
// End LCD module connection definition
// Define Messages
char message2[] = "000000000000000";
char message4[] = "It Works!";
char message5[] = "LCD Operational";
int bob;
void main() {
TRISC = 0b00000000; // PORTC All Outputs
TRISA = 0b00000000; // PORTA All Outputs, Except RA3
TRISB = 0b00000000;
Lcd_Init(); // Initialize LCD
Lcd_Out(1,2,message5); // Write message1 in 1st row
Lcd_Out(2,1,message2);
I2C1_Init(100000);
Delay_ms(20);
I2C1_Start(); // issue I2C1 start signal
Delay_ms(20);
I2C1_Wr(0xA0); // send byte via I2C1 (device address + W)
Delay_ms(20);
I2C1_Wr(0x70); //send high byte
Delay_ms(20);
I2C1_Wr(0x8F); // send low byte
Delay_ms(20);
I2C1_Wr(0x20); // send data (data to be written)
Delay_ms(20);
I2C1_Stop(); // issue I2C1 stop signal
Delay_ms(100);
I2C1_start(); // Claim I2C1 BUS
Delay_ms(20);
I2C1_wr(0xA0); // Tell all I2C1 devices you are talking to EEPROM in WRITE MODE
Delay_ms(20);
I2C1_wr(0x70); // Address High Byte
Delay_ms(20);
I2C1_wr(0x8F); // Address Low Byte
Delay_ms(20);
I2C1_start(); // RESTART I2C1 BUS (necissary for microchip protocol)
Delay_ms(20);
I2C1_wr(0xA1); // Tell all I2C1 you are talking to EEPROM in READ MODE.
Delay_ms(20);
bob = I2C1_Rd(1); // Read in the data ---------------------problem here-----------------
Delay_ms(20);
if (bob == 0x44)
{
Lcd_Out(2,2,message4);
}
Delay_ms(20);
I2C1_stop(); // release the bus
}
Last edited: