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.

I2C questions for PIC

Status
Not open for further replies.

boncker

Member level 2
Member level 2
Joined
Dec 9, 2006
Messages
51
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,288
Location
Brazil
Activity points
1,610
i2c pic i2c_read()

Hello guys,

I have a question about I2C communication in PICs. I need to write a C program that makes communication using I2C protocol with another device (i.e. a temperature sensor). If, in the PIC software, I write "i2c_read( )" (after all the declarations before), will the sensor start to send the data to PIC immediately? If yes, how can I get the temperature data and put it in a variable?
I would be glad if you could put some example codes.

Thanks!
 

Which C compiler are you using?

Because some C compiler come with an I2C library. Also, C compilers tend to have different ways of using register names.

Cheers,
 

Hi

The below program is an example to read the DS1307 RTC device on the CCS complier it may help you to understand how to keep the reading data to the variable but it may not be used on another different device type, Regard to the device specification by refer to the details on its datasheet.
if you have any question, post at here many peoples looking to help you.

Have a good times.
Joe

Void DS1307_Read() {

i2c_start(); // Send I2C Start Transfer
i2c_write(Ds1307_Wt); // Send identifier I2C address - write
i2c_write(0x00); // Point to address 0x00
i2c_stop(); // Send I2C Stop Transfer

i2c_start(); // Send I2C Start Transfer
i2c_write(Ds1307_Rd); // Send identifier I2C address - Read
Seconds = i2c_read(1); // Read data , ACK
Minutes = i2c_read(1); // Read data , ACK
Hours = i2c_read(1); // Read data , ACK
Day = i2c_read(1); // Read data , ACK
Date = i2c_read(1); // Read data , ACK
Month = i2c_read(1); // Read data , ACK
Year = i2c_read(0); // Read data , Not ACK}
i2c_stop(); // Send I2C Stop Transfer
}
 

    boncker

    Points: 2
    Helpful Answer Positive Rating
boncker said:
Hello guys,

I have a question about I2C communication in PICs. I need to write a C program that makes communication using I2C protocol with another device (i.e. a temperature sensor). If, in the PIC software, I write "i2c_read( )" (after all the declarations before), will the sensor start to send the data to PIC immediately? If yes, how can I get the temperature data and put it in a variable?
I would be glad if you could put some example codes.

Thanks!
hi
i have problem as u too
becuase of I2C protocol in CCS errer....
so that i write code like Assembler that mean i write step by step follow the signal pull of datasheet of temperature sensor.....
thank
buntheun
 

Ok, joeboy1995 has given an example of the I2C bus sequence.

Depending on the resolution of your temperature sensor, you may need to transfer more than one byte. For example, MAX6633 is a 12-bit sensor with I2C compatible interface, and you would need to transfer 2 bytes of data for one temperature reading.

Cheers,
 

hi all,
i think that the problem is in the slave adress (the sensor) , you must ground A0,A1,A2 pins of the sensor
the i2c_read in ccs will work with is adress by default ,
or you must change

hope that will help you
 

hiii guys my program is with c30 compiler but i hope it will help u
#include <P24F16KA102.h>
#include "I2CM1.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 = 0x80; // 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=0b1000001000110000;
I2C1BRG=39;
IdleI2C1();

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

/*send address 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

/*send adress for read*/
MasterWriteI2C1(0xE3);//9f
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag

/*send pointer*/
MasterWriteI2C1(0x81);//9f
while(I2C1STATbits.TBF); // 8 clock cycles
while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
IFS1bits.MI2C1IF = 0; // Clear interrupt flag


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



value = MasterReadI2C1();
while(I2C1STATbits.RBF); // 8 clock cycles
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



return;

}


hope that will help you
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top