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.

[PIC] I2C and PIC: write block EEPROM

Status
Not open for further replies.

wbenatti

Newbie level 1
Joined
Jul 5, 2017
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
9
Hi! I made functions for I2C comunication between EEPROM Atmel 24C64 and PIC18F46K80. Except writeBlock, all others functions work fine. My functions are:
writeChar
wiriteBlock
readChar
readBlock

PROBLEM:
For example, when I write a block from 0x0000 memory address, works fine. But, when I write a block from any other memory address, some disorder occurs in recorded data.

I appreciate any help.


writeBlock function:
Code:
/*==============================================================================
function  : signed char writeBlock_24C64
Descrição : Write a block of data from a memory address
Parameters:   deviceAddr : Address of the device on the I2C bus
              dataAddr   : Address of memory to be written
              blockLength: Number of bytes to be written
              *datBlock  : Pointer to the block of data to be written in memory.
Retorno:    ERROR_UNKNOW         : (0) Writing error
            SUCCESS_EXEC         : (1) Memories written successfully
            
--------------------------------------------------------------------------------
EXAMPLE USAGE:

    char i= 0;
    char set[100];
    char get[100];
    char val[4];

    writeBlock_24C64(0xA2, 0x0F0F, sizeof(set), set);
    readBlock_24C64(0xA2, 0x0F0F, sizeof(get), get);

    for (i = 0; i < sizeof(get); i++ ) {
        ByteToStr(get[i], val);
        UART1_Write_Text(val);
    }
            
==============================================================================*/
signed char writeBlock_24C64(char deviceAddr, unsigned int dataAddr, unsigned int blockLength, char *datBlock) {
    char aux;
    unsigned int i, count;

    // Begins the first block
    I2C1_Start();
    if (I2C1_Wr(deviceAddr)) return ERROR_UNKNOW;
    aux = (dataAddr >> 8) & 0x1F;
    if (I2C1_Wr(aux)) return ERROR_UNKNOW;
    aux = dataAddr & 0x00FF;
    if (I2C1_Wr(aux)) return ERROR_UNKNOW;

    i     = 0;
    count = 1;
    while (i < blockLength) {
        // Writes the current byte and advances to the next byte
        if (I2C1_Wr(*datBlock)) return ERROR_UNKNOW;
        datBlock++;
        i++;

        // Checks the memory page break
        if (i / (count * PAGE_BLOCK_WIDTH_24C64)) {
            // Ends the current block writing
            I2C1_Stop();
            count++;

            delay_ms(10);

            // Restarts the next data block for writing
            I2C1_Start();
            if (I2C1_Wr(deviceAddr)) return ERROR_UNKNOW;
            aux = ((dataAddr + i) >> 8) & 0x1F;
            if (I2C1_Wr(aux)) return ERROR_UNKNOW;
            aux = (dataAddr + i) & 0x00FF;
            if (I2C1_Wr(aux)) return ERROR_UNKNOW;
        }
    }

    I2C1_Stop();
    delay_ms(10);

    return SUCCESS_EXEC;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top