cancel
Showing results for 
Search instead for 
Did you mean: 

software i2c not working..

hitsumen
Associate II
Posted on March 09, 2012 at 16:41

Want to read my LIS302 sensor with software I2C (bit banging)

Hardware i2c works.

Here is some code.

Please help me with software i2c...

http://pastebin.com/d0M9jRaY

21 REPLIES 21
emalund
Associate III
Posted on March 12, 2012 at 20:37

with software I2C (bit banging)

 

WHY?

Here is some code.

 

http://pastebin.com/d0M9jRaY

I have no intention of accessing ''some strange site'' what is wrong with posting code here?

Erik

hitsumen
Associate II
Posted on March 13, 2012 at 15:37

static void I2C1_GPIO_Config(void)

{

  GPIO_InitTypeDef  GPIO_InitStructure; 

  //GPIO_DeInit(GPIOB);  

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  

  // Configure I2C1 pins: SCL and SDA 

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;  

  GPIO_Init(GPIOB, &GPIO_InitStructure);  

}

void I2C_delay(void)

   unsigned int i=150; 

   while(i) 

   { 

     i--; 

   } 

}

bool I2C1_Start(void)

{

 SDAH;

 SCLH;

 I2C_delay();

 if(!SDAread)return FALSE; 

 SDAL;

 I2C_delay();

 if(SDAread) return FALSE; 

 SDAL;

 I2C_delay();

 return TRUE;

}

void I2C1_Stop(void)

{

 SCLL;

 I2C_delay();

 SDAL;

 I2C_delay();

 SCLH;

 I2C_delay();

 SDAH;

 I2C_delay();

}

void I2C1_Ack(void)

 SCLL;

 I2C_delay();

 SDAL;

 I2C_delay();

 SCLH;

 I2C_delay();

 SCLL;

 I2C_delay();

}

void I2C1_NoAck(void)

 SCLL;

 I2C_delay();

 SDAH;

 I2C_delay();

 SCLH;

 I2C_delay();

 SCLL;

 I2C_delay();

}

bool I2C1_WaitAck(void)   

{

 SCLL;

 I2C_delay();

 SDAH;   

 I2C_delay();

 SCLH;

 I2C_delay();

 if(SDAread)

 {

          SCLL;

          return FALSE;

 }

 SCLL;

 return TRUE;

}

void I2C1_SendByte(unsigned char SendByte) 

{

    unsigned char i=8;

    while(i--)

    {

        SCLL;

        I2C_delay();

        if(SendByte&0x80)

          SDAH;  

        else 

        SDAL;   

        SendByte<<=1;

        I2C_delay();

 SCLH;

        I2C_delay();

    }

    SCLL;

}

unsigned char I2C1_ReceiveByte(void)  

    unsigned char i=8;

    unsigned char ReceiveByte=0;

    SDAH;    

    while(i--)

    {

      ReceiveByte<<=1;      

      SCLL;

      I2C_delay();

      SCLH;

      I2C_delay(); 

      if(SDAread)

      {

        ReceiveByte|=0x01;

      }

    }

    SCLL;

    return ReceiveByte;

}

 

unsigned char I2C1_ReadByte( unsigned char DeviceAddress, int ReadAddress)

{  

    unsigned char temp;

    if(!I2C1_Start())return FALSE;    

    I2C1_SendByte((DeviceAddress & 0xFE)); 

    if(!I2C1_WaitAck()){I2C1_Stop(); return FALSE;}

    

I2C1_SendByte((unsigned char)((ReadAddress>>8) & 0xFF));     

    I2C1_WaitAck();

    I2C1_SendByte((unsigned char)((ReadAddress) & 0xFF));         

    I2C1_WaitAck();

    I2C1_Start();

    I2C1_SendByte((DeviceAddress & 0xFE)|0x01);    

    I2C1_WaitAck();

   

    temp = I2C1_ReceiveByte();

    

    I2C1_NoAck();

     

    I2C1_Stop();

    return temp;

}

/************************************MAIN****************************/

int main( void )

{

  prvSetupHardware();

  I2C1_GPIO_Config();

  initDisplay();

  itoc(I2C1_ReadByte(I2C_ADDRESS, 0x0F));

  GLCD_displayStringLn(Line3, string);

}

hitsumen
Associate II
Posted on March 13, 2012 at 16:58

ah.. Forget to mention this, you can ask if you need some more info..:

#define SCLH GPIOB->BSRR = GPIO_Pin_6

#define SCLL GPIOB->BRR = GPIO_Pin_6

#define SDAH GPIOB->BSRR = GPIO_Pin_7

#define SDAL GPIOB->BRR = GPIO_Pin_7

#define SCLread GPIOB->IDR & GPIO_Pin_6

#define SDAread GPIOB->IDR & GPIO_Pin_7

emalund
Associate III
Posted on March 13, 2012 at 17:56

still no answer

WHY?

Erik

hitsumen
Associate II
Posted on March 13, 2012 at 19:43

For learning purposes.. I am student..

Later I need to launch the camera which uses SCCB bus (something like i2c but a bit different)..

emalund
Associate III
Posted on March 13, 2012 at 20:11

1) is the device you access I²C or SMB (SMB has timeouits,I²C does not, otherwise they are, for all practical purposes identical.

2) you are using an arbitrary delay (all delays in C are, by definition, arbitrary) put a scope on SCL and verify it is OK

Erik

hitsumen
Associate II
Posted on March 15, 2012 at 09:32

1. Its I2C.

2. So the code is right? What do you mean arbitary?

emalund
Associate III
Posted on March 15, 2012 at 16:47

arbitrary Timing loops in C:

there is no rule whatsoever for how a compiler implement a C construct.

I recall an incident

version x of a '51 compiler implemented for (time = 0; time !=47; time++)

 

this way:

mov r7,time

loop: cjne r7, #47, continue

ret

continue:

mov acc, r7

inc a

mov r7,a

sjmp loop

then the compiler maker go smarter and in rev y it looked like this

mov r7,#47

loop: djnz r7,loop

ret

and everybodys code with delays in C fell apart.

The above is as best I recall and should be accurate.

Erik

emalund
Associate III
Posted on March 15, 2012 at 16:49

I guess your code, faulty or not, will generate a few SCL pulses, put a sope on SCL and make sure the timing is at or below what your external devices datasheet  specify

Erik