This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I2C Problem

Hey Guys,

I am really in a desperate mode. I am new to this I2C and done some research about it, but unable to make it to work. I am using PCF8575C device and connect it according to the datasheet provided.

#include <REG51F.H>
#include <stdio.h>
#include <Input_Output.h>
#include <Header_Display.h>


//Function Declarations
void initRS();
void I2C_Init();
//void Init_Low();
void I2C_Start();
//void I2C_Restart();
void I2C_Stop();
//void I2C_SendAck();
//void I2C_SendNack();
void Delay_1sec();

unsigned char I2C_Write(unsigned char Data);
unsigned char I2C_Read(unsigned char Data);
unsigned char i;
unsigned char command;

#define SDA p0_0
#define SCL p0_1

//Main function
void main()
{
initRS();
initLED_BLINK();
header_display();
I2C_Init();

        while((command = _getkey ()) != 0x1B){   // Waiting for key input from user
                switch(command){


                        case 'b':
                                I2C_Start();
                          I2C_Write(0x4C);
                          I2C_Write(0xAA);
        I2C_Write(0xF0);
                        I2C_Stop();
                        break;

                        case 'c':
                                I2C_Start();
                                I2C_Read(0x4C);
                          I2C_Stop();
                        break;

                        case 'd':
                                I2C_Start();
                                I2C_Write(0xF0);
                        break;

                        case 'e':
                                I2C_Stop();
                        break;

                        case 'f':
                                I2C_Start();
                        break;

                        case 'g':
                                I2C_Init();
                        break;

                        case 'i':
                                SDA = 0;
                          Delay_1sec();
                          SDA = 1;
                        break;

                        case 'j':
                                SCL = 0;
                          Delay_1sec();
                          SCL = 1;
                        break;
                }
        }
}

// Function : Master sending START condition, HIGH to LOW transition on SDA while SCL keeping high
void I2C_Start()
{
SDA = 1;
Delay_1sec();
SCL = 1;
Delay_1sec();
SDA = 0;
Delay_1sec();
SCL = 0;
Delay_1sec();
}

//Function : I2C Write Byte transfer 3 bytes
unsigned char I2C_Write(unsigned char Data)
   {
    unsigned char i, ack_bit; // Variable to be used in for loop
    for(i=0;i<8;i++) // Repeat for every bit
    {
       SCL = 0; // Make SCL pin low // the start condition should have exited low !
       Delay_1sec(); // Data pin should change it's value,
       if ((Data & 0x80) == 0) // <<<<

         SDA = 0;

       else
         SDA = 1;
                           Delay_1sec();
         SCL = 1;
         Data<<=1;
         Delay_1sec();
                 }
         SCL=0; // all routines must exit with clock low, or bus clear !
                     Delay_1sec();

                     if(Data)
                                 ack_bit = SDA;
                           SCL = 1;
                           Delay_1sec();
                           SCL = 0;
                           Delay_1sec();

             return !ack_bit;
}

// Function : I2C Read Byte
unsigned char I2C_Read(unsigned char Data)
{
unsigned char i;
for(i=0;i<8;i++)
        {
SCL = 0;
Delay_1sec();
SCL = 1;
Delay_1sec();
Data |=1;
        if(i<7)
                Data <<=1;
  }
SCL = 0;
Delay_1sec();
SDA = 1;
Delay_1sec();
return Data;
}

// Function : To set initial values of SDA and SCL
void I2C_Init()
{
SDA = 0xFF;
SCL = 0xFF;
}

/*void Init_Low()
{
        SDA = 0;
        Delay_1sec();
        SCL = 0;
}


void I2C_Restart()
{
SDA = 1;
Delay_1sec();
SCL = 1;
Delay_1sec();
SDA = 0;
Delay_1sec();
}*/

void I2C_Stop()
{
SDA = 0;
Delay_1sec();
SCL = 1;
Delay_1sec();
SDA = 1;
}

/*void I2C_Ack()
{
SDA = 0;
Delay_1sec();
SCL = 1;
Delay_1sec();
SCL = 0;
}

void I2C_Nack()
{
SDA = 1;
SCL = 1;
SCL = 0;
}*/

void Delay_1sec()                          // Delay for 0.1 second
{
        unsigned int x,y;
        for(x=0; x<1000; x++)
        {
        for(y=0; y<21; y++)
                {}
        }
}


}

Please do help me. This is my code. Someone gave me this code and I tried it. It works even with a different address. Please help me. I really need it.

Thanks!

Parents
  • This is _your_ code?
    Don't you think you should talk with whoever gave it to you?
    Especially since there are reasons to not trust code that has

    void Delay_1sec()                          // Delay for 0.1 second
    


    So if a function named "Delay_1sec" gives a 0.1 second delay - what would then be a good name for a delay function that gives a 1-second delay???

    I2C communication would normally mean that there are at least two different components involved - so have you specified the name of two devices in your post?

Reply
  • This is _your_ code?
    Don't you think you should talk with whoever gave it to you?
    Especially since there are reasons to not trust code that has

    void Delay_1sec()                          // Delay for 0.1 second
    


    So if a function named "Delay_1sec" gives a 0.1 second delay - what would then be a good name for a delay function that gives a 1-second delay???

    I2C communication would normally mean that there are at least two different components involved - so have you specified the name of two devices in your post?

Children
No data