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.

problem with I2C need help urgently

Status
Not open for further replies.

vinay shabad

Junior Member level 3
Joined
Dec 13, 2010
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,769
hiii everyone, i am working on SHT21 sensor using I2C
i am unable to read the values from sensor please need help where i am going wrong ,correct my programme .It will be very help full if u have any code or suggestion to work on

here is my code
#include <P24F16KA102.h>
#include "I2CM1.h"
#include "displayM1.h"
void initI2CportSHT2X() {

TRISBbits.TRISB5 = 1; // Digital Output (make it input only when reading data. see spi)
// PORTBbits.RB5 = 1;
TRISBbits.TRISB6 = 1; // Digital Output
// PORTBbits.RB6 = 1;

// I2C1CON = 0x28; // enable I2C Master mode
//I2C1CON2 = 0x00; // clear control bits
I2C1STAT = 0x4014; // disable slew rate control; disable SMBus

//I2C1ADD = 19; // set baud rate to 100 kHz (Fosc = 48 MHz)

IFS1bits.MI2C1IF = 0;
IEC1bits.MI2C1IE = 0;

}
void config_i2c1_write(){
unsigned char value;
I2C1CON=0b1001000000111111;
//I2C1BRG=39;
//__________________________________________________________________________________
IdleI2C1();
StartI2C1();

/* Wait till Start sequence is completed */
while(I2C1CONbits.SEN);
/* Clear interrupt flag */
IFS1bits.MI2C1IF = 0;

/*send Slaveaddress for write*/
MasterWriteI2C1(0x80);//9f
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag
// while(I2C1STATbits.ACKSTAT); // Wait for the ack bit


/*send REGaddress for write*/
MasterWriteI2C1(0xE6);//9f
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag
// while(I2C1STATbits.ACKSTAT); // Wait for the ack bit

/*send Dataaddress for write*/
MasterWriteI2C1(0xE5);//9f
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag
//while(I2C1STATbits.ACKSTAT); // Wait for the ack bit

StopI2C1();
//_____________________________________________________________________________
IdleI2C1();
RestartI2C1();
/*send adress for read*/
MasterWriteI2C1(0x81);//9f
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag
// while(I2C1STATbits.ACKSTAT); // Wait for the ack bit

value = MasterReadI2C1();
while(I2C1STATbits.RBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag
//___________________________________________
AckI2C1();
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag

NotAckI2C1();
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag


StopI2C1();
return;

}
please help me i struck at this point
 

Where exactly is the problem?

Do you get an ACK at the send Slave address for write after start condition?
Are you using pull-up resistors on SDA and SCL lines?
 

yes i am using pull up resistors i am unable to read data from sensor
 

Well I do not use PIC, but maybe we can isolate the problem.
Saying that you are "unable to read data from sensor" is very general, you must be more specific.

Are you using a programmer or an emulator? If you use a programmer and you don't monitor the program flow, then I'm telling you that I2C could prove tricky!
That's why I am asking again (in case you are using emulator):
Do you get an ACK at the send Slave address for write after start condition?

If you do, then the problem is simple, you debug sequence by sequence and you find the programming error you made.
If not, then you must check your I2C registers' initializations.


I²C - Wikipedia, the free encyclopedia

Alex
 
Last edited:

yes i am using PICKIT3 programmer with C30 compiler
 

vinay shabad said:
yes i am using PICKIT3 programmer with C30 compiler

With a PICKIT3 you can debug.

So tell us, what do you see during debugging?
Do you see an acknowledge returned?
 

during debugging programme is running successfully but i am anable to see any values on display

---------- Post added at 14:32 ---------- Previous post was at 14:31 ----------

yes i see ack nd programme is running with out any error but i cant see any values on display
 

Code:
value = MasterReadI2C1();

So at this command you are getting the right value?
 

vinay shabad said:
during debugging programme is running successfully but i am anable to see any values on display

alexxx said:
value = MasterReadI2C1();
So at this command you are getting the right value?
vinay shabad said:
no i am not getting

Please try to be more helpfull, so that the others can help you more easy.:smile:

vinay shabad said:
yes i see ack nd programme is running with out any error but i cant see any values on display

So you are getting an ACK at the start condition. At which I2C state does the device stop sending ACK?
Please try to be more descriptive this time.:wink:
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top