I2C problem with MPLABX (XC16)

Discussion in 'Embedded Systems and Microcontrollers' started by Zara Engineer, Nov 4, 2015.

  1. Zara Engineer

    Thread Starter Member

    Feb 6, 2015
    53
    0
    Hello everyone .

    I tried to use dspic30f3012 with i2c on ccs program and all's right.

    Now I'm trying to use the dspic with i2c on Mplabx.
    I used the mplabx with xc16 and this is the code :
    Code:
    Code (Text):
    1.  
    2. #include<i2c.h>
    3. void main(void )
    4. {
    5. unsigned int config2, config1;
    6. unsigned char *wrptr;
    7. unsigned char tx_data[] = {'M','I','C','R','O',’C’,'H','I','P','\0'};
    8. wrptr = tx_data;
    9. /* Baud rate is set for 100 kHz */
    10. config2 = 0x11;
    11. /* Configure I2C for 7 bit address mode */
    12. config1 = (I2C_ON & I2C_IDLE_CON & I2C_CLK_HLD &
    13. I2C_IPMI_DIS & I2C_7BIT_ADD &
    14. I2C_SLW_DIS & I2C_SM_DIS &
    15. I2C_GCALL_DIS & I2C_STR_DIS &
    16. I2C_NACK & I2C_ACK_DIS & I2C_RCV_DIS &
    17. I2C_STOP_DIS & I2C_RESTART_DIS &
    18. I2C_START_DIS);
    19. OpenI2C(config1,config2);
    20. LOOP:
    21. IdleI2C();
    22. StartI2C();
    23. /* Wait till Start sequence is completed */
    24. while(I2CCONbits.SEN);
    25. /* Clear interrupt flag */
    26. IFS0bits.MI2CIF = 0;
    27. /* Write Slave address and set master for transmission */
    28. MasterWriteI2C(0xE);
    29. /* Wait till address is transmitted */
    30. while(I2CSTATbits.TBF); // 8 clock cycles
    31. //while(!IFS0bits.MI2CIF); // Wait for 9th clock cycle
    32. //IFS0bits.MI2CIF = 0; // Clear interrupt flag
    33. //while(I2CSTATbits.ACKSTAT);
    34. /* Transmit string of data */
    35. MasterputsI2C(wrptr);
    36. StopI2C();
    37. /* Wait till stop sequence is completed */
    38. while(I2CCONbits.PEN);
    39. //CloseI2C();
    40. goto LOOP;
    41. }
    42.  
    without closing or interrupt or ack from i2c , I don't need ack I want only to see the output on oscilloscope. but nothing !

    Please help!
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    Do you have pullup resistors on SDA and SCL. I wouldn't have to ask if you had provided a schematic diagram.
     
    Zara Engineer likes this.
  3. Zara Engineer

    Thread Starter Member

    Feb 6, 2015
    53
    0
    Yes I pulled it , and connected because I'm seeing the 5v on the oscilloscope.

    about the schematic there the dspic with the 2 resistors because now I want only to have an output, no need for ack .

    thank you
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    Doesn't the hardware require the ACK at the end of the Command Word, to signify a valid address?
     
    Zara Engineer likes this.
  5. Zara Engineer

    Thread Starter Member

    Feb 6, 2015
    53
    0
    Yes the hardware needs that , but until now no address is on output!
    the first step is to solve the problem of i2c on the dspic and after that we can talk about ack.

    thank you
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    Have you configured the source of the clock for SCL?
    Are you looking at both SCL and SDA with the oscilliscope?
    Are you saying they are both high all the time?
     
    Zara Engineer likes this.
  7. Zara Engineer

    Thread Starter Member

    Feb 6, 2015
    53
    0
    Yes the config2 is equal to 19

    Crystal = 6 Mhz and PLL = 8 => Fcy=8*6/4 = 12Mhz.

    BRG = 12M/400K - 12M/1.1M = 19
    config2 = 19;

    thank you very much
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    Does that mean it is working now?
     
  9. Zara Engineer

    Thread Starter Member

    Feb 6, 2015
    53
    0
    No but I explained to you the clock rate of my module
     
  10. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    OK. So when you write a byte to the I2C hardware you should see eight clock pulses on the clock line (SCL), at the selected clock frequency, regardless of what the data line (SDA) is doing. You need to generate clock pulses before worrying about the data.

    Look into the requirements for the I2C pins being controlled by the I2C hardware instead of being dedicated to some other function.
     
    Zara Engineer likes this.
  11. Zara Engineer

    Thread Starter Member

    Feb 6, 2015
    53
    0
    Hello Papabravo

    I connected my dspic to debugger , so I conclude that there's an output but the frequency is only 13 Hz!!!

    and my configuration is 400 Khz!
     
  12. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    Then it sounds like one of your assumptions is incorrect. Recheck every choice that has to do with the frequency of anything. You might want to try sending characters out of your serial port to see if you can verify main oscillator and baudrate calculations.
     
Loading...