Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18f46J50 I2C issue

Status
Not open for further replies.

Jinzpaul4u

Full Member level 4
Joined
Feb 1, 2012
Messages
231
Helped
59
Reputation
118
Reaction score
60
Trophy points
1,328
Location
India
Activity points
2,822
Hi there,

Good wishes.


Do any one of you know why my i2c code ain't working?

I have been working on PIC18F46J50 microcontroller to interface with DAC7573 through I2C communication. I tried to get an ouput from it. But unfortunately there wasn't a I2C communication occurs.

Does anybody tell is there any thing wrong in configration of I2C module? or where did it go wrong?

Please guide me in clock settings, configuration bit settings and PLL settings. I need to use 40Mhz here and I am using 10Mhz crystal oscilator.



#include <p18F46J50.h> // Always include the header file
#include<delays.h>



//Defaut pragma code for 18F46J50
//#pragma config WDTEN = OFF //WDT disabled (enabled by SWDTEN bit)
#pragma config PLLDIV = 3 //Divide by 3 (12 MHz oscillator input)
#pragma config STVREN = ON //stack overflow/underflow reset enabled
//#pragma config XINST = OFF //Extended instruction set disabled
#pragma config CPUDIV = OSC1 //No CPU system clock divide
#pragma config CP0 = OFF //Program memory is not code-protected
//#pragma config OSC = HSPLL //HS oscillator, PLL enabled, HSPLL used by USB
#pragma config FCMEN = OFF //Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF //Two-Speed Start-up disabled
#pragma config WDTPS = 32768 //1:32768
#pragma config DSWDTOSC = INTOSCREF //DSWDT uses INTOSC/INTRC as clock
//#pragma config RTCOSC = T1OSCREF //RTCC uses T1OSC/T1CKI as clock
//#pragma config DSBOREN = OFF //Zero-Power BOR disabled in Deep Sleep
//#pragma config DSWDTEN = OFF //Disabled
#pragma config DSWDTPS = 8192 //1:8,192 (8.5 seconds)
//#pragma config IOL1WAY = OFF //IOLOCK bit can be set and cleared
#pragma config MSSP7B_EN = MSK7 //7 Bit address masking
#pragma config WPFP = PAGE_1 //Write Protect Program Flash Page 0
#pragma config WPEND = PAGE_0 //Start protection at page 0
#pragma config WPCFG = OFF //Write/Erase last page protect Disabled
#pragma config WPDIS = OFF //WPFP[5:0], WPEND, and WPCFG bits ignored
#pragma config T1DIG = ON //Sec Osc clock source may be selected
#pragma config LPT1OSC = OFF //high power Timer1 mode





void i2c_stop(void)
{
SSP1CON2bits.PEN=1;
while(!SSP1STATbits.P); //wait for setting stop bit.
Delay10TCYx(50);
}


unsigned char i2c_write(unsigned int data)
{
SSP1CON1bits.WCOL=0;
SSP1BUF=data;
if(SSP1CON1bits.WCOL) // NO COLLISION HAPPEND
{
return 1;
}
else
{
while(SSP1STATbits.BF); // TRANSMITED OR NOT?
while(SSP1CON2bits.ACKSTAT)//Acknowledge was received from slave or not?
Delay10TCYx(20);
return 0;
}
}

void i2c_start(void)
{
SSP1CON2bits.SEN=1; // START COMMUNICATION
while(SSP1STATbits.S); // START ENABLED ??
Delay10TCYx(50);
}
void i2c_idle(void)
{

SSPCON1 = 0x28;
Delay10TCYx(50);
}
void i2c_close(void)
{
SSP1CON2=0x00;
SSP1STAT=0x00;
Delay10TCYx(50);
}

void i2c_open(void)
{
//xx0x xxxx = SSPEN, Disable SSP
//xxxx 1000 = I2C Master mode, clock = FOSC / (4 * (SSPADD+1))
SSPCON1 = 0x08;
Delay10TCYx(50);
}
void init_i2c(void)
{
/////////////////////////////////////////////////
//Configure SSP Peripheral for I2C Master mode
//Set port B6 as an output
// TRISBbits.TRISB6 = 0;

//RB4 (I2C clock) and RB5 (I2C data) are configured as inputs at this stage
//TRISB |= 0b00011000;
//0xxx xxxx = Slew rate off, for high speed mode (400 kHz)
//x0xx xxxx = Disable SMBus specific inputs
SSPSTAT &= 0x3F; // power on state, and clear bits 6 and 7
//SSPSTAT |= 0x80; //1xxx xxxx = Slew rate on



SSPCON2 = 0x00; // power on state
SSPCON1bits.SSPEN = 1; //Enable SSP module, and configure port pins as serial port pins
SSPADD = 24; //Set Baud rate to 400,000. SSPADD = ( ((40,000,000/400,000) / 4) - 1 ) = 24

}




void main(void)
{


unsigned int i=0;
OSCTUNEbits.PLLEN=1;// enable PLL for 40MHz
while(!OSCCONbits.OSTS); // LOCK PLL CLOCK??
init_i2c();

while(1)// infinity loop
{
i2c_close(); // CLOSE I2C COMMUNICATION
Delay10TCYx(200); //DELAY

i2c_open(); //OPEN CHANNEL
i2c_idle();
Delay10TCYx(20);
i2c_start();
i2c_write(0x9C); // DAC iC slave address 0x9C
for(i=0;i<1000;i++);//delay
i2c_write(0x14);// DAC internal control register bits
for(i=0;i<1000;i++);//delay
i2c_write(0x1C);// DAC data
for(i=0;i<1000;i++);//delay
i2c_write(0xEE); // DAC View attachment dac7573 i2c.pdfdata
for(i=0;i<1000;i++);//delay
i2c_write(0x9C);
for(i=0;i<1000;i++);//delay
i2c_stop();
i2c_idle();
i2c_close();
Delay10TCYx(200);
}// end of while

}// end of main


Any kind of feedback would be appreciable !

Thanks in advance,
 
Last edited:

Hi,
See the attached files for I2C. Change the extension.
Enjoy!
 

Attachments

  • i2c.h.txt
    1.6 KB · Views: 56
  • i2c.c.txt
    1.2 KB · Views: 62

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top