1. 程式人生 > >資料大小端 儲存

資料大小端 儲存

void Write_EEPROM_u16(uint32_t Address, uint16_t Data)
{
    uint8_t  A, B;
    A = Data & 0xFF;
    B = (Data >> 8) & 0xFF;


    FLASH_Unlock(FLASH_MEMTYPE_DATA);
    //低位在前高位在後,通用小端模式
    FLASH_ProgramByte(Address, B);
    FLASH_ProgramByte(Address + 1, A);


    FLASH_Lock(FLASH_MEMTYPE_DATA);
}


uint16_t Read_EEPROM_u16(uint32_t Address)
{
    uint8_t  A, B;
    uint32_t Data = 0;


    B = FLASH_ReadByte(Address);
    A = FLASH_ReadByte(Address + 1);


    Data = Data + B;
    Data = (Data << 8) + A;
    return(Data);
}


void Write_EEPROM_u32(uint32_t Address, uint32_t Data)
{
    uint8_t  A, B, C, D;
    A = Data & 0xFF;
    B = (Data >> 8) & 0xFF;
    C = (Data >> 16) & 0xFF;
    D = (Data >> 24) & 0xFF;


    FLASH_Unlock(FLASH_MEMTYPE_DATA);
    //低位在前高位在後,通用小端模式
    FLASH_ProgramByte(Address, D);
    FLASH_ProgramByte(Address + 1, C);
    FLASH_ProgramByte(Address + 2, B);
    FLASH_ProgramByte(Address + 3, A);


    FLASH_Lock(FLASH_MEMTYPE_DATA);
}


uint32_t Read_EEPROM_u32(uint32_t Address)
{
    uint8_t  A, B, C, D;
    uint32_t Data = 0;


    D = FLASH_ReadByte(Address);
    C = FLASH_ReadByte(Address + 1);
    B = FLASH_ReadByte(Address + 2);
    A = FLASH_ReadByte(Address + 3);


    Data = Data + D;
    Data = (Data << 8) + C;
    Data = (Data << 8) + B;
    Data = (Data << 8) + A;
    return(Data);
}