clock doesnt seem to work I2C dspic30f4011

Discussion in 'Embedded Systems and Microcontrollers' started by xluzx, Oct 31, 2013.

  1. xluzx

    Thread Starter New Member

    Oct 31, 2013
    1
    0
    So im trying to use i2c to get some data from a mpu6050, but whit the code that im using for some reason the clock does not work. on the osciloscope the scl line seems to be 1 all the time, and from diferent tests it seems that the pic stops waiting for clock signals.

    Code ( (Unknown Language)):
    1.  
    2. #include <libpic30.h>
    3. #include <timer.h>
    4. #include <stdlib.h>
    5. #include <stdio.h>
    6. #include <stdlib.h>
    7. #include <p30F4011.h>
    8. #include <i2c.h>
    9. #include <uart.h>
    10.  
    11. #define FCY     29491
    12.  
    13. //Configuration bits
    14. _FOSC(CSW_FSCM_OFF & XT_PLL16);  //oscilator
    15. //_FWDT(WDT_ON & WDTPSA_512 & WDTPSB_16);  //watchdog timer (interval of ~ 16s)
    16. _FWDT(WDT_OFF);
    17.  
    18. char RXbuffer[200];    //buffer used to store characters from serial port
    19. int str_pos = 0;     //position in the RXbuffer
    20.  
    21. int main() {
    22.   unsigned int config2, config1;
    23.   unsigned char *wrptr;
    24.   unsigned char tx_data[] = {'M','I','C','R','O','C','H','I','P','\0'};
    25.   unsigned int UMODEvalue, U2STAvalue; //auxiliary UART config variables
    26.  
    27.   //Configures every PORTB pin as digital I/Os (wont be using analog inputs for now)    
    28.     ADPCFG = 0xFFFF;
    29.  
    30.   /* Serial port config */
    31.     UMODEvalue = UART_EN & UART_IDLE_CON & UART_NO_PAR_8BIT; //activates the uart in continuos mode (no sleep) and 8bit no parity mode
    32.     U2STAvalue = UART_INT_TX & UART_TX_ENABLE & UART_INT_RX_CHAR & UART_RX_TX; //activates interrupt of pin Tx + enables Tx + enable Rx interrupt for every char
    33.      OpenUART2 (UMODEvalue, U2STAvalue, 15); //configures and activates UART2 at 115000 bps
    34.     U2STAbits.URXISEL = 1;
    35.     _U2RXIE = 1;                             //0-Interruption off, 1-Interruption on
    36.     U2MODEbits.LPBACK = 0;                     //disables hardware loopback on UART2. Enable only for tests
    37.  
    38.     __C30_UART = 2;                         //define UART2 as predefined for use with stdio library, printf etc
    39.  
    40.   TRISFbits.TRISF2=1;
    41.   TRISFbits.TRISF3=1;
    42.  
    43.   wrptr = tx_data;
    44.   /* Baud rate is set for 100 kHz */
    45.   config2 = 0xB5;
    46.   /* Configure I2C for 7 bit address mode */
    47.   config1 = (I2C_ON & I2C_IDLE_CON & I2C_CLK_HLD &
    48.              I2C_IPMI_DIS & I2C_7BIT_ADD &
    49.              I2C_SLW_DIS & I2C_SM_DIS &
    50.              I2C_GCALL_DIS & I2C_STR_DIS &
    51.              I2C_NACK & I2C_ACK_DIS & I2C_RCV_DIS &
    52.              I2C_STOP_DIS & I2C_RESTART_DIS &
    53.              I2C_START_DIS);
    54.  
    55.   printf("\n\rconfig1=%x config2=%x\n",config1,config2);
    56.  
    57.   OpenI2C(config1,config2);
    58.   //ConfigIntI2C(MI2C_INT_OFF & MI2C_INT_PRI_1 & SI2C_INT_ON & SI2C_INT_PRI_1);
    59.  
    60.   while(1){
    61.   IdleI2C();
    62.   StartI2C();
    63.   // Wait till Start sequence is completed
    64.   while(I2CCONbits.SEN);
    65.  
    66.   // Clear interrupt flag
    67.   IFS0bits.MI2CIF = 0;
    68.   // Write Slave address and set master for transmission
    69.   MasterWriteI2C(0xE);
    70.   // Wait till address is transmitted
    71.   while(I2CSTATbits.TBF);  // 8 clock cycles
    72.   while(!IFS0bits.MI2CIF); // Wait for 9th clock cycle
    73.  
    74.   IFS0bits.MI2CIF = 0;     // Clear interrupt flag
    75.   while(I2CSTATbits.ACKSTAT);
    76.   // Transmit string of data
    77.   MasterputsI2C(wrptr);
    78.  
    79.   StopI2C();
    80.   // Wait till stop sequence is completed
    81.   while(I2CCONbits.PEN);
    82.   CloseI2C();
    83. }
    84.   return 0;
    85. }
    86.  
    ty in advance for any help.
     
  2. Danm1

    Member

    Jul 19, 2010
    55
    7
    make sure your I2C interrupt is configured.
     
Loading...