I2C problem with MPLABX (XC16)

Thread Starter

Zara Engineer

Joined Feb 6, 2015
53
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:
#include<i2c.h>
void main(void )
{
unsigned int config2, config1;
unsigned char *wrptr;
unsigned char tx_data[] = {'M','I','C','R','O',’C’,'H','I','P','\0'};
wrptr = tx_data;
/* Baud rate is set for 100 kHz */
config2 = 0x11;
/* Configure I2C for 7 bit address mode */
config1 = (I2C_ON & I2C_IDLE_CON & I2C_CLK_HLD &
I2C_IPMI_DIS & I2C_7BIT_ADD &
I2C_SLW_DIS & I2C_SM_DIS &
I2C_GCALL_DIS & I2C_STR_DIS &
I2C_NACK & I2C_ACK_DIS & I2C_RCV_DIS &
I2C_STOP_DIS & I2C_RESTART_DIS &
I2C_START_DIS);
OpenI2C(config1,config2);
LOOP:
IdleI2C();
StartI2C();
/* Wait till Start sequence is completed */
while(I2CCONbits.SEN);
/* Clear interrupt flag */
IFS0bits.MI2CIF = 0;
/* Write Slave address and set master for transmission */
MasterWriteI2C(0xE);
/* Wait till address is transmitted */
while(I2CSTATbits.TBF); // 8 clock cycles
//while(!IFS0bits.MI2CIF); // Wait for 9th clock cycle
//IFS0bits.MI2CIF = 0; // Clear interrupt flag
//while(I2CSTATbits.ACKSTAT);
/* Transmit string of data */
MasterputsI2C(wrptr);
StopI2C();
/* Wait till stop sequence is completed */
while(I2CCONbits.PEN);
//CloseI2C();
goto LOOP;
}
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!
 

Thread Starter

Zara Engineer

Joined Feb 6, 2015
53
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
 

Thread Starter

Zara Engineer

Joined Feb 6, 2015
53
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
 

Papabravo

Joined Feb 24, 2006
12,389
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?
 

Thread Starter

Zara Engineer

Joined Feb 6, 2015
53
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
 

Papabravo

Joined Feb 24, 2006
12,389
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.
 

Thread Starter

Zara Engineer

Joined Feb 6, 2015
53
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!
 

Papabravo

Joined Feb 24, 2006
12,389
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.
 
Top