I2C Master doesn't work.. need help..

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello together,

   

 

   

i have an SDP601 Sensor http://www.mouser.com/ds/2/682/Sensirion_Differential_Pressure_SDP6x0series_Datas-767275.pdf

   

The Sensor uses I2C to communicate. I have connected the Sensor to a PSOC 5LP Development Board but it doesn't work. 

   


#include <project.h>
#define I2C_SLAVE_ADDRESS    (0x40)
    uint8 temp;
    uint8 status;
    uint8 Sensor_Command = 0xF1;              // Command for Sensor
    uint8 Sensor[3];
    uint8 i= 0;
    uint8 j= 0;
int main()
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    LCD_Start();
    I2C_Start();
   
    status = I2C_MasterSendStart(0x40, I2C_WRITE_XFER_MODE);
    if(I2C_MSTR_NO_ERROR == status)   /* Check if transfer completed without errors */
    {         
        status = I2C_MasterWriteByte(Sensor_Command);
    }
    status = I2C_MasterSendStart(0x40, I2C_READ_XFER_MODE);
   
        Sensor[0] = I2C_MasterReadByte(I2C_ACK_DATA);
        status = I2C_MasterStatus();
        Sensor[1] = I2C_MasterReadByte(I2C_ACK_DATA);
        status = I2C_MasterStatus();
        Sensor[2] = I2C_MasterReadByte(I2C_ACK_DATA);
        status = I2C_MasterStatus();
    I2C_MasterSendStop();      /* Send Stop */
    LCD_PrintInt8(Sensor[0]);
    LCD_PrintInt8(Sensor[1]);
    LCD_PrintInt8(Sensor[2]);
 
   
    while(1)
    {
   
    }
}
 

   

 

   

I tried many other code but it will not work. What can I do? Can you write working example Code?

   

 

   

SCL is connected to P0[0] SDA is connected to P0[1]

   

The LCD is connected to P2[0]-P2[6]

0 Likes
12 Replies
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received

Did you put the pull up resistors on the I2C lines? Please post your complete code.

0 Likes
Anonymous
Not applicable

Yes i have 2 pull up resistors to VDD

   

 

   

This is the whole testing code..

0 Likes
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received

I want to check the other parts of the program like the topdesign.cysch and other things that may cause issues.  Please Press File and then select Create WorkSpace Bundle and attach to a Comment.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

okay here is the whole testing project

0 Likes
Anonymous
Not applicable

The Sensor Array puts F10000 on the LCD. I dont know why its showing the command header of the sensor als Measuring value xD

0 Likes
rola_264706
Level 8
Level 8
50 likes received 25 likes received 10 likes received
        I think it should be 0x81 not 0xF1 according to the data sheet for the part.   
0 Likes
Anonymous
Not applicable

Yes but the PSOC Function wants the adress und sets the 1 for read to the LSB that should be 81?

   

 

   

I tried everything but it didnt work. What can i do?

   

 

   

Can someone write the code?

0 Likes
Anonymous
Not applicable
        This Code dont work ... /* ======================================== * * Copyright YOUR COMPANY, THE YEAR * All Rights Reserved * UNPUBLISHED, LICENSED SOFTWARE. * * CONFIDENTIAL AND PROPRIETARY INFORMATION * WHICH IS THE PROPERTY OF your company. * * ======================================== */ #include        #define I2C_SLAVE_ADDRESS (0x20) int main() { uint8 Sensor[3]; uint8 status = 0; uint8 status1 = 0; uint8 sensor1 = 0; uint8 sensor2 = 0; uint8 sensor3 = 0; uint8 status_after_start = 0; uint8 status_after_write = 0; uint8 size = 0; uint8 bufSize; uint8 mybyte = 0xF1; LED_red_Write(1); LED_Blue_Write(1); LED_Green_Write(1); LED_red_Write(0); LED_Blue_Write(0); LED_Green_Write(0); I2C_Start(); /* Initialize buffer with packet */ /* ... */ I2C_MasterWriteBuf(0x40, &mybyte, 1, I2C_MODE_COMPLETE_XFER); /* while (0u == (I2C_MasterStatus() & I2C_MSTAT_WR_CMPLT)) { LED_Blue_Write(1); } */ /* Displays transfer status */ if (0u == (I2C_MSTAT_ERR_XFER & I2C_MasterStatus())) { LED_Green_Write(1); /* Check if all bytes was written */ if(I2C_MasterGetWriteBufSize() == 1u) { status = 0x12; } } else { LED_red_Write(1); } I2C_MasterReadBuf(0x40, Sensor, 1, I2C_MODE_COMPLETE_XFER); I2C_MasterClearStatus(); while(1) { } }   
0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The high-level APIs usually are used in this way:

   

Writing to slave Count bytes
I2C_MasterWriteBuf(SlaveAddress,DataPtr,Count,I2C_MODE_COMPLETE_XFER);

   

Reading from Slave sending register/command byte first:
I2C_MasterWriteBuf(SlaveAddress,&RegAddress,1,I2C_MODE_NO_STOP);
I2C_MasterReadBuf(SlaveAddress,DataPtr,Count,I2C_MODE_REPEAT_START);

   

It is easier for us to help when you post your complete project so that we all can have a look at all of your settings. To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Here is the project. Bob didn't I do exactly the same?

   

 

   

The Programm is hanging in the first while with the blueLED

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

No, you didn't. You use I2C_MODE_COMPLETE_XFER instead of I2C_MODE_NO_STOP

   

Keep in mind that most of the APIs (except those for reading a byte) return a status byte which, when non-zero indicate an error condition.

   

You should check for that and find the reason (listed in I2C.h)

   

 

   

Bob

0 Likes
Anonymous
Not applicable

I2C_MasterWriteBuf(0x40, &mybyte, 1, I2C_MODE_NO_STOP);  returns 0x00 and looks fine

   

I2C_MasterReadBuf(0x40, Sensor, 1, I2C_MODE_REPEAT_START); returns 0x02 and looks like an error. But i dont know why

0 Likes