engineer khan
Member level 3
//using Master Mode I2C1 single Master, xc16 Compiler,8MHz,AT24c512 EEPORM, Please help me I am in trouble for about one due to this problem I waiting for reply please
/*I am using Alternate I2C1 ASDA1 , ASCL1 because of no success using SDA1 SCL1 */
#include <p33FJ128MC802.h>
////////////////////////////////Configuration Bits/////////////////////////////////////////////
#pragma config FNOSC = PRI // Oscillator Mode (Primary Oscillator (XT, HS, EC) w/ PLL)
#pragma config POSCMD = HS // Primary Oscillator Source (HS Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)
#pragma config ICS = PGD1 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config GWRP = OFF
#pragma config FWDTEN = OFF //WatchDogTimer Disable
#pragma config ALTI2C = ON
#pragma config JTAGEN = OFF
//#pragma config LPOL = OFF
//#pragma config HPOL = OFF
//#pragma config PWMPIN = OFF
//#pragma config FPWRT = PWR1
////////////////////////////////End of Configuration///////////////////////////////////////////
#define SlaveAddress 0x50
#define LED LATAbits.LATA0
void ConfigI2C1(void);
void _delay_ms(unsigned int);
unsigned char myData = 0;
unsigned int count = 0;
int main(void)
{
ConfigI2C1(); /* Enable and configure I2C module */
_delay_ms(5000);
while(1)
{
I2C1CONbits.SEN = 1; /*Start Enable*/
while(I2C1CONbits.SEN);
I2C1TRN = (SlaveAddress<<1)|0; /*Slave address with write indication*/
while(I2C1STATbits.TBF); /*Transmit buffer full status bit*/
while(I2C1STATbits.ACKSTAT); /*Slave acknowledge*/
I2C1TRN = 0x45; /*Memory Address Low byte*/
while(I2C1STATbits.TBF);
while(I2C1STATbits.ACKSTAT);
I2C1TRN = 0x55; /*Memory Address High byte*/
while(I2C1STATbits.TBF);
while(I2C1STATbits.ACKSTAT);
I2C1TRN = 'J'; /*Datato be written*/
while(I2C1STATbits.TBF);
while(I2C1STATbits.ACKSTAT);
I2C1CONbits.PEN = 1; /*Stopped*/
while(I2C1CONbits.PEN); /*Either stop complete*/
I2C1CONbits.RSEN = 1; /*Repeated Start Enabled*/
while(I2C1CONbits.RSEN);
I2C1TRN = (SlaveAddress<<1)|1; /*Slave Address with read indication*/
while(I2C1STATbits.TBF);
while(I2C1STATbits.ACKSTAT);
I2C1CONbits.RCEN = 1; /*Receive Enable*/
while(!I2C1STATbits.RBF); /*Wait untill RBF is set which ensures the I2C1RECV is Full*/ here it hang
I2C1CONbits.ACKEN = 1; /*Ack Enabled clear by hardware*/
myData = I2C1RCV;
I2C1CONbits.PEN = 1; /*Stopped*/
while(I2C1CONbits.PEN);
}
return 0;
}
void ConfigI2C1(void)
{
I2C1BRG = 37; /*100KHZ SCL1 Frequency*/
I2C1CONbits.I2CEN = 1; /*Enable I2C1 Module*/
I2C1CONbits.I2CSIDL = 0; /*Continue operation in idle mode*/
I2C1CONbits.IPMIEN = 0; /*Disable intelligent peripheral*/
I2C1CONbits.A10M = 0; /*7-bits Address Enable*/
I2C1CONbits.DISSLW = 0; /*Slew Rate Enabled*/
I2C1CONbits.SMEN = 1; /*Enable I/O pin thresholds compliant with SMbus specification*/
}
void _delay_ms(unsigned int i){
unsigned int x;
i = i*20;
for(x = 0; x<i; x++)
{
Nop();
}
}
//Note
/*I have made every possible change to this code while reading and manual and microchip forums datasheet still doesnt work and hang in while(!I2C1STATbits.RBF) please ; */
/*I am using Alternate I2C1 ASDA1 , ASCL1 because of no success using SDA1 SCL1 */
#include <p33FJ128MC802.h>
////////////////////////////////Configuration Bits/////////////////////////////////////////////
#pragma config FNOSC = PRI // Oscillator Mode (Primary Oscillator (XT, HS, EC) w/ PLL)
#pragma config POSCMD = HS // Primary Oscillator Source (HS Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)
#pragma config ICS = PGD1 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config GWRP = OFF
#pragma config FWDTEN = OFF //WatchDogTimer Disable
#pragma config ALTI2C = ON
#pragma config JTAGEN = OFF
//#pragma config LPOL = OFF
//#pragma config HPOL = OFF
//#pragma config PWMPIN = OFF
//#pragma config FPWRT = PWR1
////////////////////////////////End of Configuration///////////////////////////////////////////
#define SlaveAddress 0x50
#define LED LATAbits.LATA0
void ConfigI2C1(void);
void _delay_ms(unsigned int);
unsigned char myData = 0;
unsigned int count = 0;
int main(void)
{
ConfigI2C1(); /* Enable and configure I2C module */
_delay_ms(5000);
while(1)
{
I2C1CONbits.SEN = 1; /*Start Enable*/
while(I2C1CONbits.SEN);
I2C1TRN = (SlaveAddress<<1)|0; /*Slave address with write indication*/
while(I2C1STATbits.TBF); /*Transmit buffer full status bit*/
while(I2C1STATbits.ACKSTAT); /*Slave acknowledge*/
I2C1TRN = 0x45; /*Memory Address Low byte*/
while(I2C1STATbits.TBF);
while(I2C1STATbits.ACKSTAT);
I2C1TRN = 0x55; /*Memory Address High byte*/
while(I2C1STATbits.TBF);
while(I2C1STATbits.ACKSTAT);
I2C1TRN = 'J'; /*Datato be written*/
while(I2C1STATbits.TBF);
while(I2C1STATbits.ACKSTAT);
I2C1CONbits.PEN = 1; /*Stopped*/
while(I2C1CONbits.PEN); /*Either stop complete*/
I2C1CONbits.RSEN = 1; /*Repeated Start Enabled*/
while(I2C1CONbits.RSEN);
I2C1TRN = (SlaveAddress<<1)|1; /*Slave Address with read indication*/
while(I2C1STATbits.TBF);
while(I2C1STATbits.ACKSTAT);
I2C1CONbits.RCEN = 1; /*Receive Enable*/
while(!I2C1STATbits.RBF); /*Wait untill RBF is set which ensures the I2C1RECV is Full*/ here it hang
I2C1CONbits.ACKEN = 1; /*Ack Enabled clear by hardware*/
myData = I2C1RCV;
I2C1CONbits.PEN = 1; /*Stopped*/
while(I2C1CONbits.PEN);
}
return 0;
}
void ConfigI2C1(void)
{
I2C1BRG = 37; /*100KHZ SCL1 Frequency*/
I2C1CONbits.I2CEN = 1; /*Enable I2C1 Module*/
I2C1CONbits.I2CSIDL = 0; /*Continue operation in idle mode*/
I2C1CONbits.IPMIEN = 0; /*Disable intelligent peripheral*/
I2C1CONbits.A10M = 0; /*7-bits Address Enable*/
I2C1CONbits.DISSLW = 0; /*Slew Rate Enabled*/
I2C1CONbits.SMEN = 1; /*Enable I/O pin thresholds compliant with SMbus specification*/
}
void _delay_ms(unsigned int i){
unsigned int x;
i = i*20;
for(x = 0; x<i; x++)
{
Nop();
}
}
//Note
/*I have made every possible change to this code while reading and manual and microchip forums datasheet still doesnt work and hang in while(!I2C1STATbits.RBF) please ; */