• AVR Freaks

Hot!dsPIC33EP512MU810 EEPROM by using I2c

Author
S12345S
S12345S
  • Total Posts : 9
  • Reward points : 0
  • Status: offline
2019/11/05 22:29:44 (permalink)
0

dsPIC33EP512MU810 EEPROM by using I2c

good morning
I'm using dspic33ep512mu810 microcontroller. I'm working on EEPROM(S-24C64C)IC with I2C .i not getting any data pulse at SDA pin.And even not getting any pulse on SCL(clock) pin.I'm using I2C2. I using 8Mhz clock with PLL of 40 mhz.

#pragma config GWRP = OFF
#pragma config GSS = OFF
#pragma config GSSK = OFF

// FOSCSEL
#pragma config FNOSC = FRC
#pragma config IESO = OFF

// FOSC
#pragma config POSCMD = XT
#pragma config OSCIOFNC = OFF
#pragma config IOL1WAY = OFF
#pragma config FCKSM = CSECMD

// FWDT
#pragma config WDTPOST = PS1024
#pragma config WDTPRE = PR32
#pragma config PLLKEN = ON
#pragma config WINDIS = OFF
#pragma config FWDTEN = OFF

// FPOR
#pragma config FPWRT = PWR128
#pragma config BOREN = ON
#pragma config ALTI2C1 = OFF
#pragma config ALTI2C2 = ON // Alternate I2C pins for I2C2 (SDA2/SCK2 pins are selected as the I/O pins for I2C2)

// FICD
#pragma config ICS = PGD1
#pragma config RSTPRI = PF
#pragma config JTAGEN = OFF

// FAS
#pragma config AWRP = OFF
#pragma config APL = OFF
#pragma config APLK = OFF


#define PAGESIZE 0x20

void InitClock( void )
{
PLLFBD = 38;
CLKDIVbits.PLLPOST = 0;
CLKDIVbits.PLLPRE = 0;
OSCTUN = 0;
RCONbits.SWDTEN = 0;


__builtin_write_OSCCONH( 0x03 );
__builtin_write_OSCCONL( OSCCON || 0x01 );
while( OSCCONbits.COSC != 0b011 );
while( OSCCONbits.LOCK != 1 )
{ };
}

//I2C2 pin configuration

void i2c2PinAndClkConfig()
{

TRISFbits.TRISF4 = 1;   //SDA pin -RF4
TRISFbits.TRISF5 = 0;  //SCL pin-RF5
I2C2CONbits.I2CEN =0;
I2C2CONbits.A10M = 0;
I2C2CONbits.SCLREL = 1;
I2C2BRG = 300;   //100 khz baudrate
I2C2ADD = 0x00;;
I2C2MSK = 0;
I2C2CONbits.I2CEN =1;
}


//EEPROM Read

int32_t eepRead(uint16_t addr, uint8_t *data, int32_t numbytes)
{

I2C2CONbits.SEN =1;
I2C2TRN = 0xA0;
if(I2C2STATbits.ACKSTAT == 0)
I2C2TRN=(addr & 0xFF00)>>8;
if(I2C2STATbits.ACKSTAT == 0)
I2C2TRN=(addr&0x00FF);
I2C2CONbits.RSEN = 1;
I2C2TRN = 0xA1;
if(I2C2STATbits.ACKSTAT == 0)
I2C2CONbits.RCEN =1;
while(numbytes)
{
if(numbytes<2)
{
I2C2CONbits.ACKEN == 0;
I2C2CONbits.PEN =1;
}
*data++ = (uint8_t) I2C2RCV;
numbytes--;
}
I2C2CONbits.ACKEN == 1;
I2C2CONbits.PEN =1;
return 0;
}

//EEPROM page Write

static int32_t pageWrite(uint16_t addr, uint8_t *data, int32_t numbytes)
{
I2C2CONbits.SEN =1;
I2C2TRN = 0xA0;
if(I2C2STATbits.ACKSTAT == 0)
I2C2TRN=(addr & 0xFF00)>>8;
if(I2C2STATbits.ACKSTAT == 0)
I2C2TRN=(addr&0x00FF);
if(I2C2STATbits.ACKSTAT == 0)
while(numbytes)
{
I2C2TRN = *data++;
numbytes--;
}
I2C2CONbits.PEN =1;
return 0;
}


//EEprom Write

int32_t eepWrite(uint16_t addr, uint8_t *data, int32_t numbytes)
{
int32_t count=0,i=0,stat;
int16_t saddr=0;
saddr=addr%PAGESIZE;
count=PAGESIZE-saddr;
if(numbytes<count)
{
count = numbytes;
stat = pageWrite(addr,data,count);
return stat;
}
if(count!=0)
{
stat = pageWrite(addr,data,count);
addr += count;
data +=count;
}
numbytes -= count;
while(numbytes>0)
{
if(numbytes <= PAGESIZE)
count = numbytes;
else
count = PAGESIZE;
for(i=0;i<1;i++)
delay_ns(1);
// usec();
stat = pageWrite(addr,data,count);
numbytes -= count;
addr += count;
data += count;
}
return stat;
}
 
Thank you
#1

3 Replies Related Threads

    JPortici
    Super Member
    • Total Posts : 876
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: online
    Re: dsPIC33EP512MU810 EEPROM by using I2c 2019/11/06 01:03:42 (permalink)
    0
    Many things wrong:
    1) use code tags to post code.
    2) the IO configuration is wrong. There is the risk of toggling the bus line before the peripheral does its work, then you'll have to reset the bus.
    3) the code is wrong because you don't understand how the peripheral works. You can't just set bits... for example, you set the bit to enable the start condition but you don't wait for the start condition to happen.
     
    2 and 3 are explained in the reference manual chapter on i2c (DS70000195) which conveniently describes the procedure and presents the code to communicate with an eeprom as an example
    #2
    S12345S
    S12345S
    • Total Posts : 9
    • Reward points : 0
    • Status: offline
    Re: dsPIC33EP512MU810 EEPROM by using I2c 2019/11/06 01:50:56 (permalink)
    0
    I didnt understand how to configure the SDA(RF4) and SCL(RF5) pins for I2C2 .please help how to to configure the pins as either input/output/open drain conditions.
     
    thank you
    #3
    JPortici
    Super Member
    • Total Posts : 876
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: online
    Re: dsPIC33EP512MU810 EEPROM by using I2c 2019/11/06 02:05:30 (permalink)
    0
    It's in the document i told you about (DS70000195) have you read it?
    #4
    Jump to:
    © 2020 APG vNext Commercial Version 4.5