Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] I2C Read/Write Problem with MikroC.......

Status
Not open for further replies.

sreepss

Full Member level 4
Joined
Jun 10, 2009
Messages
204
Helped
29
Reputation
60
Reaction score
27
Trophy points
1,308
Location
Cochin, Kerala, India
Activity points
2,376
Hai all I encounter a problem during the I2c Operation. Iam using the MikroC compailer but thei2c operation does not works.

aim using the EEPROM 24C256 and a PIC16F877 as controller.

the I2c Bus reads only 255 after the writing process (In Proteus Simulator);

Pls help me in this problem..

iam using the example code in Mikro C

---------- Post added at 07:52 ---------- Previous post was at 07:48 ----------

the code was void main(){
PORTB = 0;
TRISB = 0;

I2C_Init(100000);
I2C_Start(); // Issue I2C start signal
I2C_Wr(0xA2); // Send byte via I2C (command to 24cO2)
I2C_Wr(2); // Send byte (address of EEPROM location)
I2C_Wr(0xF0); // Send data (data to be written)
I2C_Stop();

Delay_ms(100);

I2C_Start(); // Issue I2C start signal
I2C_Wr(0xA2); // Send byte via I2C (device address + W)
I2C_Wr(2); // Send byte (data address)
I2C_Repeated_Start(); // Issue I2C signal repeated start
I2C_Wr(0xA3); // Send byte (device address + R)
PORTB = I2C_Rd(0u); // Read the data (NO acknowledge)
I2C_Stop();
}


 

As I can see, you copied the code from the MikroC example as is. The code given by MikroC is for the 24C02 EEPROM that is 2Kbit of memory which can addressed by only 8-bits, where as the EEPROM you are using is 24C256 that is 256Kbit of memory which need 15-bits to be completely addressed.
So, in your code you have to send two bytes for the address not just one as in the above code.
Code:
I2C_Init(100000);
I2C_Start(); // Issue I2C start signal
I2C_Wr(0xA2); // Send byte via I2C (command to 24cO2)
I2C_Wr(0); // [B]Send 1st byte of EEPROM memory address[/B]
I2C_Wr(2); // [B]Send 2nd byte of EEPROM memory address[/B]
I2C_Wr(0xF0); // Send data (data to be written)
I2C_Stop();

Refer to the EEPROM datasheet https://www.google.com/url?sa=t&rct=j&q=24c256&source=web&cd=1&ved=0CBwQFjAA&url=http%3A%2F%2Fwww.techdesign.be%2Fprojects%2Fdatasheet%2F24LC256.pdf&ei=7ju6TtvoIoKk4gT1gLWFCA&usg=AFQjCNGq58uFFYQkvYdJ5cG--rUPpSVWrA&sig2=DrAMrn8ZIwh11BzhtU7o9A
 

Thanx seadolphine200, but the problem stands still,

these are the problem that i encounterd,

first call the i2c write seq,

and read it drom the i2c eeprom(24C256) and display it over the LCD(But here i use portB direct out)

but insted of eeprom content, it outs the location address it self

int data;
char disp[10];
void main(){

LCD_init(&PORTD);
lcd_cmd(LCD_CURSOR_OFF);
lcd_cmd(LCD_CLEAR);
lcd_out(1,1,"TEST I2C");

PORTB = 0x00;
TRISB = 0;

I2C_Init(100000);
Delay_ms(20);
I2C_Start(); // Issue I2C start signal
Delay_ms(20);
I2C_Wr(0xA2);
I2C_Wr(0); // Send byte via I2C (command to 24cO2)
Delay_ms(20);
I2C_Wr(2); // Send byte (address of EEPROM location)
Delay_ms(20);
I2C_Wr(0xF0); // Send data (data to be written)
I2C_Stop();

Delay_ms(100);

I2C_Start(); // Issue I2C start signal
Delay_ms(20);
I2C_Wr(0xA2); // Send byte via I2C (device address + W)
Delay_ms(20);
I2C_Wr(0);
I2C_Wr(2); // Send byte (data address)
I2C_Repeated_Start(); // Issue I2C signal repeated start
I2C_Wr(0xA3); // Send byte (device address + R)
data = I2C_Rd(0u); // Read the data (NO acknowledge)
I2C_Stop();
Delay_ms(200);

WordToStr(data,disp) ;
Lcd_out(2,1,disp);
delay_ms(2000);
}
this is the screenshot frm proteus isis



---------- Post added at 13:01 ---------- Previous post was at 11:18 ----------

Thanx **** the problem was solved myself.. its due to the pullaup resistor
 

I have a problem when reading data from gyro L3G4200D connected I2C with Pic18f45k22, so I can't display LCD. plz help me...

#define Gyro_Wr 0xD2
#define Gyro_Rd 0xD3
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
#define CTRL_REG6 0x25
#define OUT_X_L 0x28
#define OUT_X_H 0x29
#define OUT_Y_L 0x2A
#define OUT_Y_H 0x2B
#define OUT_Z_L 0x2C
#define OUT_Z_H 0x2D

// LCD module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_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

//I2C module connection
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
//end I2C module connection

unsigned short ox,oy,oz;
char text1[15], text2[15], text3[15] ;

//SETUP--------------------------
void writeI2C( unsigned short reAddr, unsigned short I2Cdata) {

I2C1_Start();
I2C1_Wr(Gyro_Wr);
I2C1_Wr(reAddr);
I2C1_Wr(I2Cdata);
I2C1_Stop();
}
void setup(){
writeI2C(CTRL_REG1, 0x1F); // Turn on all axes, disable power down
writeI2C(CTRL_REG3, 0x08); // Enable control ready signal
writeI2C(CTRL_REG4, 0x80); // Set scale (500 deg/sec)
delay_ms(100); // Wait to synchronize
}


//GET VALUE--------------------------------
unsigned short readI2C(unsigned short regAddr) {
unsigned short I2CRead;
I2C1_Start();
I2C1_Wr(Gyro_Wr);
I2C1_Wr(regAddr);
I2C1_Repeated_start();
I2C1_Wr(Gyro_Rd);
I2CRead = I2C1_Rd(0u);
I2C1_Stop();
return I2CRead;
}
unsigned short getGyroValue(unsigned short Device_Wr_L,unsigned short Device_Wr_H) {
unsigned short temp ;
unsigned short MSB,LSB;

MSB = ReadI2C(Device_Wr_H);
LSB = ReadI2C(Device_Wr_L);

temp = ((MSB << 8) | LSB);
return temp;
}

void main() {

ANSELB=0;
ANSELC=0;
LATC=0xff;
TRISD=0;
LATD=0xff;
I2C1_Init(100000); // initialize I2C communication
setup();
delay_ms(100);
ox= getGyroValue(OUT_X_L,OUT_X_H);
portD=ox;
while(1) {
LCD_Init(); //goi LCD
Lcd_Cmd(_LCD_CLEAR); //xoa man hinh
Lcd_Cmd(_LCD_CURSOR_OFF); //cursor off

LCD_out(1,1,"raw Ox:");
Lcd_out(2,12,"rad/s" );


floatToStr(ox/144,text1);
lcd_out(2,1,text1);


delay_ms(9000);
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top