打印
[PIC®/AVR®/dsPIC®产品]

FLASH 读写问题,同一地址,写入后读出来数据不一样

[复制链接]
139|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Tobbie|  楼主 | 2024-12-26 08:58 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
同一地址,写入后读出来数据不一样
/******************************************************************************
* 函数名称:Memory_Check
* 功    能:**检测,防止重复写数据
* 返 回 值:
*******************************************************************************/
uint16_t Memory_Check(void)
{
    uint16_t temp;
    temp=FLASH_ReadWord(0x0020);
    if(temp==0x52)return 0;
    else
    {
        FLASH_WriteWord(dataAddr,Buf,Staus_Cont&0x00ff);//第一次写入档位
        FLASH_WriteWord(0x0020,Buf,0x52&0x00ff);

    }
    return 1;

}


主函数中初始化先进行**检查
    while(Memory_Check)//**检查,防止每次上电写数据
    {

         if(EUSART2_is_tx_ready())
        {
            EUSART2_Write(FLASH_ReadWord(0x0020));
        }

    }
    while (1)



使用特权

评论回复
沙发
Tobbie|  楼主 | 2024-12-26 09:03 | 只看该作者

使用特权

评论回复
板凳
Tobbie|  楼主 | 2024-12-26 10:08 | 只看该作者

uint16_t FLASH_ReadWord(uint16_t flashAddr)
{
    uint8_t GIEBitValue = INTCONbits.GIE;   // Save interrupt enable
   
    INTCONbits.GIE = 0;     // Disable interrupts
    NVMADRL = (flashAddr & 0x00FF);
    NVMADRH = ((flashAddr & 0xFF00) >> 8);

    NVMCON1bits.NVMREGS = 0;    // Deselect Configuration space
    NVMCON1bits.RD = 1;      // Initiate Read
    NOP();
    NOP();
    INTCONbits.GIE = GIEBitValue;        // Restore interrupt enable

    return ((uint16_t)((NVMDATH << 8) | NVMDATL));
}

void FLASH_WriteWord(uint16_t flashAddr, uint16_t *ramBuf, uint16_t word)
{
    uint16_t blockStartAddr = (uint16_t)(flashAddr & ((END_FLASH-1) ^ (ERASE_FLASH_BLOCKSIZE-1)));
    uint8_t offset = (uint8_t)(flashAddr & (ERASE_FLASH_BLOCKSIZE-1));
    uint8_t i;

    // Entire row will be erased, read and save the existing data
    for (i=0; i<ERASE_FLASH_BLOCKSIZE; i++)
    {
        ramBuf[i] = FLASH_ReadWord((blockStartAddr+i));
    }

    // Write at offset
    ramBuf[offset] = word;

    // Writes ramBuf to current block
    FLASH_WriteBlock(blockStartAddr, ramBuf);
}

使用特权

评论回复
地板
20062516| | 2024-12-26 13:54 | 只看该作者
FLASH 写之前要擦除

使用特权

评论回复
5
Tobbie|  楼主 | 2024-12-26 14:07 | 只看该作者
20062516 发表于 2024-12-26 13:54
FLASH 写之前要擦除

有做擦除的,在这个void FLASH_WriteWord函数中已经做了block擦除
int8_t FLASH_WriteBlock(uint16_t writeAddr, uint16_t *flashWordArray)
{
    uint16_t    blockStartAddr  = (uint16_t )(writeAddr & ((END_FLASH-1) ^ (ERASE_FLASH_BLOCKSIZE-1)));
    uint8_t     GIEBitValue = INTCONbits.GIE;   // Save interrupt enable
    uint8_t i;

   
    // Flash write must start at the beginning of a row
    if( writeAddr != blockStartAddr )
    {
        return -1;
    }

    INTCONbits.GIE = 0;         // Disable interrupts

    // Block erase sequence
    FLASH_EraseBlock(writeAddr);//擦除block

    // Block write sequence
    NVMCON1bits.NVMREGS = 0;    // Deselect Configuration space
    NVMCON1bits.WREN = 1;    // Enable wrties
    NVMCON1bits.LWLO = 1;    // Only load write latches

    for (i=0; i<WRITE_FLASH_BLOCKSIZE; i++)
    {
        // Load lower 8 bits of write address
        NVMADRL = (writeAddr & 0xFF);
        // Load upper 6 bits of write address
        NVMADRH = ((writeAddr & 0xFF00) >> 8);

        // Load data in current address
        NVMDATL = flashWordArray;
        NVMDATH = ((flashWordArray & 0xFF00) >> 8);

        if(i == (WRITE_FLASH_BLOCKSIZE-1))
        {
            // Start Flash program memory write
            NVMCON1bits.LWLO = 0;
        }

        NVMCON2 = 0x55;
        NVMCON2 = 0xAA;
        NVMCON1bits.WR = 1;
        NOP();
        NOP();

            writeAddr++;
    }

    NVMCON1bits.WREN = 0;       // Disable writes
    INTCONbits.GIE = GIEBitValue;   // Restore interrupt enable

    return 0;
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

18

主题

110

帖子

1

粉丝