CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

I2C problem with Differential Pressure sensor

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
NahunTPX



Joined: 11 Apr 2017
Posts: 2

View user's profile Send private message

I2C problem with Differential Pressure sensor
PostPosted: Fri Jun 02, 2017 10:47 pm     Reply with quote

Hello guys, I've been having this problem with I2C communication for a differential pressure sensor, the MS4525DO which is supposed to be delivering the value of the pressure in 14 bits and the temperature in other 11 bits, so it gives a response of 4 bytes, so you have to mask these bytes to take the correspondent bits for each measure. But when I try a simple I2C code using a PIC 18F45k20 to read these bytes I only get an int8 value of 255 for each read byte. When I try a similar code but using Arduino I get coherent values for both measures. Please, let me know what do you think could the problem be, because I've used an oscilloscope to see how the I2C protocol is working on for both cases (Arduino and PIC). The only thing I can notice for the PIC case is that the MS4525DO is giving an ACK bit of 1, and with Arduino is giving 0. I also tried SPI communication but I got the same result. I'm using the PCH 5.015 compiler version.
Code:

#include<18F45k20.h>

//--------------------------------------------------------------------
#FUSES NOWDT                    //Sin Watch Dog Timer
#FUSES INTRC_IO                  //Cristal Interno
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected

//--------------------------------------------------------------------
#use delay(internal=8Mhz)                             //Uso del oscilador interno
#use i2c(Master,slow,I2C1,force_hw)  //Uso del I2C para la comunicación
#use Rs232(baud=9600,xmit=Pin_C6, rcv=Pin_c7)         //Uso del protocolo RS232 para la comunicación serial

//--------------------------------------------------------------------
#define LCD_ENABLE_PIN  PIN_D1                                    ////
#define LCD_ENABLE_PIN  PIN_D1                                    ////
#define LCD_RS_PIN      PIN_D2                                    ////
#define LCD_RW_PIN      PIN_D3                                    ////
#define LCD_DATA4       PIN_D4                                    ////
#define LCD_DATA5       PIN_D5                                    ////
#define LCD_DATA6       PIN_D6                                    ////
#define LCD_DATA7       PIN_D7
#include"lcd.c"

//#use spi(master,DI=PIN_C4,DO=PIN_C5 ,CLK=PIN_C3 )///Uso del SPI para la comunicación con el sensor MS4525DO

void main()
{
int8 P_H,P_L,T_H,T_L;
int16 Pres,Temp;
lcd_init();
delay_ms(20);

while(1)
   {
         lcd_gotoxy(1,1);
         i2c_start();
         i2c_write(0x28);
         i2c_start();
         P_H = i2c_read();
         P_L = i2c_read();
         T_H=i2c_read();
         T_L=i2c_read();
         i2c_stop();
         //P_H=P_H&0x3f;
         //Pres=make16(P_H,P_L);
         //T_H=T_H&0x7
         //Temp=make16(T_H,T_L);
         printf(lcd_putc,"PH:%u PL: %u TH: %u TL: %u",P_H,P_L,T_H,T_L);
         delay_ms(1000);
   }
}

Thank you in advance.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 02, 2017 11:35 pm     Reply with quote

Do a Google search for this document. It explains the i2c interface
for this sensor chip:
Quote:
Interfacing to MEAS Digital Pressure Modules site:te.com

I didn't post the link because it's too long.

First it says the i2c address is in 7-bit format. But CCS uses 8-bit format.
You have to shift the 7-bit format left by 1 (or multiply it by 2) to get
8-bit format.
Quote:
1.2 I2C Address
The I2C address consists of a 7-digit binary value. The factory setting for
the I2C slave address is 0x28, 0x36 or 0x46 depending on the interface
type selected from the ordering information.

Also, there is a program near the top of the CCS code library forum to
help you discover the i2c address of your sensor chip.

2nd thing. If you look at page 4 of the document I listed, it shows the
bit diagrams for i2c transfers. Notice that it wants you to do a "Master Nak"
on the last i2c_read() operation. You are not currently doing this.
You need to add it. To do a master NAK on the last read, do this:
Code:

T_L = i2c_read(0);

This parameter of 0 is explained in the CCS manual. Look in the section
on i2c_read():
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
temtronic



Joined: 01 Jul 2010
Posts: 9125
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jun 03, 2017 4:24 am     Reply with quote

At first I thought ,hmm..255... probably a 3V device, 5 volt PIC problem or no pullups...so..
OK, I downloaded your linked sensor datasheet and have a 'silly' question.
Do you really have the I2C version of the device ? I ask because the datasheet shows an SPI version which used the same pins.

It's possible you ordered I2C and got SPI version ?

Jay
NahunTPX



Joined: 11 Apr 2017
Posts: 2

View user's profile Send private message

PostPosted: Sun Jun 04, 2017 12:12 am     Reply with quote

Thank you PCM, your I2C address Scanner code was very useful it result that real reading direction of the sensor was 0x50, and that was the reason for which all the read bytes were always 255. And if you read in that document called:
Quote:
Interfacing to MEAS Digital Pressure Modules
in the section of I2C address it says the following:
Quote:
The default hexadecimal I2C header for read access to the sensor is therefore 0x51, 0x6D, 0x8D respectively,
based on the ordering information.
but for some reason the right direction its 0x50 which was discovered using your code. So thank you very much I'm really newbie in this world of embedded programing and I don´t know too much about the compiler and this kind of programing itself.

Temtronic: Actually the package which I'm using is a custom package called PX4 used for RC airplanes so it have its owns pull up resistors and it's the I2C version. But by the way, thank you for answering.
Ttelmah



Joined: 11 Mar 2010
Posts: 19245

View user's profile Send private message

PostPosted: Sun Jun 04, 2017 12:28 am     Reply with quote

On I2C, the first byte you send after the 'start', comprises the following:

7bit I2C device address, followed by a one bit R/W flag.

The 'flag' is high for a read, and low for a write.

So a device with a 'physical' address of 0x28, will need 'address bytes' of 0x50 for a write (R/W low), and 0x51 for a read (R/W high).

Now lots of manufacturers quote their device addresses in terms of these bytes, while others give the 7bit 'physical' address (which then needs to be multiplied by 2 for the byte to send).

This is what PCM_programmer was pointing out.

It's slightly annoying, since manufacturers don't actually standardise (though the 8bit format is becoming more common). So you need to be careful when looking at the data sheet to check if there is any mention of '7bit'.....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group