LIS3LV02DL Accelerometer I2C Issue

Discussion in 'Programmer's Corner' started by john_doe01, Mar 12, 2012.

  1. john_doe01

    Thread Starter New Member

    Nov 27, 2011
    24
    0
    Hi,
    im working on a two wheeled balancing robot project.

    Im using an LIS3LV02DL 3-Axis accelerometer to gather acceleration readings of the bot
    Im using a PIC18F452 and programming in C
    ive used a logic analysers protocol analyser feature and the I2C protocol sent is correct as far as i can tell but am receiving no data from the Acc.
    im sending the salve address of the accelerometer followed by the who am i slave address which should return 00111010.

    i assume ive made a mistake in the code, could this be double checked, the relevant segments are shown below

    Code ( (Unknown Language)):
    1.  
    2. //Global declarations
    3.     const unsigned char slave_address_write = 0b00111010;
    4.     const unsigned char slave_address_read  = 0b00111011;
    5.     const unsigned char sub_address_whoami  = 0b00001111;
    6.  
    7.  
    8. void I2C_START ()
    9. {
    10. // SCL period = 100KHZ so 100 times slower then clock
    11. // so high time = low time = Delay10TCYx (5);
    12.  
    13.         PORTCbits.RC7 = 1;  //SDA
    14.         Delay100TCYx(1);
    15.         PORTCbits.RC6 = 1;  //SCL
    16.         Delay100TCYx(1);
    17.         PORTCbits.RC7 = 0;  //SDA
    18.         Delay100TCYx(1);
    19. }
    20. //-------------------------------------------------------------------------
    21. void I2C_STOP ()
    22. {
    23.         PORTCbits.RC6 = 0;  //SCL
    24.         PORTCbits.RC7 = 0;  //SDA
    25.         Delay100TCYx(1);
    26.         PORTCbits.RC6 = 1;  //SCL
    27.         Delay100TCYx(1);
    28.         PORTCbits.RC7 = 1;  //SDA
    29.         Delay100TCYx(1);
    30. }
    31. //-------------------------------------------------------------------------
    32. void I2C_SLAVE_ADD (unsigned char Slave_Address)
    33. {
    34. //Slave Address = 0011101
    35.  
    36.     unsigned char bit_sent;
    37.  
    38.     for ( bit_select = 7; bit_select >= 0 ; bit_select--)
    39.     {
    40.         PORTCbits.RC6 = 0;  //SCL  
    41.         Delay100TCYx(1);
    42.         bit_sent = (Slave_Address >> bit_select) & 0x01;
    43.         PORTCbits.RC7 = bit_sent;   //SDA
    44.         Delay100TCYx(1);
    45.         PORTCbits.RC6 = 1;  //SCL  
    46.         Delay100TCYx(1);
    47.     }
    48.  
    49.     // ACK
    50.         PORTCbits.RC6 = 0;  //SCL
    51.         Delay100TCYx(1);
    52.         PORTCbits.RC7 = 0;  //SDA
    53.         Delay100TCYx(1);
    54.         TRISC= 0x80;    // port C7 input
    55.  
    56.         PORTCbits.RC6 = 1;  //SCL
    57.         Delay100TCYx(1);
    58.         PORTCbits.RC6 = 0;  //SCL      
    59.         Delay100TCYx(1);
    60.         TRISC= 0x00;    // port C output    
    61. }
    62.  
    63. //------------------------------------------------------------------------
    64. void I2C_SUB_ADD (unsigned char Sub_Address)
    65. {
    66.  
    67. //MSB sent first, MSB = buffer = 0
    68. //LSB 7 bits = adderss
    69.        
    70.     unsigned char bit_sent;
    71.  
    72.     for ( bit_select = 7; bit_select >= 0 ; bit_select--)
    73.     {
    74.         PORTCbits.RC6 = 0;  //SCL  
    75.         Delay100TCYx(1);
    76.         bit_sent = (Sub_Address >> bit_select) & 0x01;
    77.         PORTCbits.RC7 = bit_sent;   //SDA
    78.         Delay100TCYx(1);
    79.         PORTCbits.RC6 = 1;  //SCL  
    80.         Delay100TCYx(1);
    81.     }
    82.  
    83.     // ACK
    84.         PORTCbits.RC6 = 0;  //SCL
    85.         Delay100TCYx(1);
    86.         PORTCbits.RC7 = 0;  //SDA
    87.         Delay100TCYx(1);
    88.         TRISC= 0x80;    // port C7 input
    89.  
    90.         PORTCbits.RC6 = 1;  //SCL      
    91.         Delay100TCYx(1);
    92.         PORTCbits.RC6 = 0;  //SCL
    93.         Delay100TCYx(1);
    94.         TRISC= 0x00;    // port C output    
    95.  
    96. }
    97. //-------------------------------------------------------------------------
    98. void I2C_READ ()
    99. {
    100. // READ
    101.         TRISC= 0x80;    // port C7 input
    102.  
    103.     for ( bit_select = 7; bit_select >= 0 ; bit_select--)
    104.     {
    105.         Delay100TCYx(1);
    106.         PORTCbits.RC6 = 0;  //SCL
    107.         Delay100TCYx(1);
    108.         PORTCbits.RC6 = 1;  //SCL
    109.         Delay100TCYx(1);
    110. // Read data now
    111.         ReadData = (PORTCbits.RC7 << bit_select) | ReadData;
    112. // Latch Data rising edge clock
    113.     } // end of for loop
    114.  
    115.     // ACK
    116.  
    117.         Delay100TCYx(1);
    118.         PORTCbits.RC6 = 0;  //SCL
    119.         Delay100TCYx(1);
    120.         PORTCbits.RC6 = 1;  //SCL      
    121.         Delay100TCYx(1);
    122.         PORTCbits.RC6 = 0;  //SCL
    123.    
    124.         TRISC= 0x00;    // port C output    
    125.    
    126. }
    127. //------------------------------------------------------------------------
    128.  
    129. In main:
    130. // call procedures
    131.     I2C_START ();
    132.     I2C_SLAVE_ADD (slave_address_write); // and Write = 0
    133.     I2C_SUB_ADD (sub_address_outxL); // buffer msb = 0
    134.     I2C_START ();
    135.     I2C_SLAVE_ADD (slave_address_read); // and Read = 1
    136.     I2C_READ ();    //expected return 00111010
    137.     I2C_STOP ();
    138.  
    139.  
    Any help will be appreciated, Thanks
     
    Last edited by a moderator: Mar 13, 2012
Loading...