C8051F 内部Flash读写
2014-01-12 13:13
246 查看
#include "Head.h"
//-----------------------------------------------------------------------------
// FLASH_ByteWrite
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) FLADDR addr - address of the byte to write to
// valid range is 0x0000 to 0xFBFF for 64K Flash devices
// valid range is 0x0000 to 0x7FFF for 32K Flash devices
// 2) U8 byte - byte to write to Flash.
//
// This routine writes <byte> to the linear FLASH address <addr>.
//-----------------------------------------------------------------------------
U8 Flash_data[30];
void FLASH_ByteWrite (FLADDR addr, U8 byte)
{
bit EA_SAVE = EA; // Preserve EA
U8 xdata * data pwrite; // FLASH write pointer
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
EA = 0; // Disable interrupts
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
pwrite = (U8 xdata *) addr;
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL |= 0x01; // PSWE = 1 which enables writes
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
*pwrite = byte; // Write the byte
PSCTL &= ~0x01; // PSWE = 0 which disable writes
EA = EA_SAVE; // Restore interrupts
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// FLASH_ByteRead
//-----------------------------------------------------------------------------
//
// Return Value :
// U8 - byte read from Flash
// Parameters :
// 1) FLADDR addr - address of the byte to read to
// valid range is 0x0000 to 0xFBFF for 64K Flash devices
// valid range is 0x0000 to 0x7FFF for 32K Flash devices
//
// This routine reads a <byte> from the linear FLASH address <addr>.
//-----------------------------------------------------------------------------
U8 FLASH_ByteRead (FLADDR addr)
{
bit EA_SAVE = EA; // Preserve EA
U8 code * data pread; // FLASH read pointer
U8 byte;
EA = 0; // Disable interrupts
pread = (U8 code *) addr;
byte = *pread; // Read the byte
EA = EA_SAVE; // Restore interrupts
return byte;
}
//-----------------------------------------------------------------------------
// FLASH_PageErase
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) FLADDR addr - address of any byte in the page to erase
// valid range is 0x0000 to 0xF9FF for 64K Flash devices
// valid range is 0x0000 to 0x7DFF for 32K Flash devices
//
// This routine erases the FLASH page containing the linear FLASH address
// <addr>. Note that the page of Flash containing the Lock Byte cannot be
// erased from application code.
//-----------------------------------------------------------------------------
void FLASH_PageErase (FLADDR addr)
{
bit EA_SAVE = EA; // Preserve EA
U8 xdata * data pwrite; // FLASH write pointer
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
EA = 0; // Disable interrupts
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
pwrite = (U8 xdata *) addr;
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL |= 0x03; // PSWE = 1; PSEE = 1
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
*pwrite = 0; // Initiate page erase
PSCTL &= ~0x03; // PSWE = 0; PSEE = 0
EA = EA_SAVE; // Restore interrupts
SFRPAGE = SFRPAGE_save;
}
void FLASH_ReadData(U8 *buffer,U8 length)
{
U8 i;
for(i = 0; i < length ; i++){
buffer[i] = FLASH_ByteRead (start_address + i);
}
}
void FLASH_WriteData(U8 *buffer,U8 length)
{
U8 i;
for(i = 0; i < length; i ++){
FLASH_PageErase(start_address + i);
}
for(i = 0; i < length; i ++){
FLASH_ByteWrite (start_address + i, buffer[i]);
}
}
void Flash_data_init()
{
U8 i;
for(i = 0; i < 30; i++){
Flash_data[i] = 0x00;
}
}
//-----------------------------------------------------------------------------
// FLASH_ByteWrite
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) FLADDR addr - address of the byte to write to
// valid range is 0x0000 to 0xFBFF for 64K Flash devices
// valid range is 0x0000 to 0x7FFF for 32K Flash devices
// 2) U8 byte - byte to write to Flash.
//
// This routine writes <byte> to the linear FLASH address <addr>.
//-----------------------------------------------------------------------------
U8 Flash_data[30];
void FLASH_ByteWrite (FLADDR addr, U8 byte)
{
bit EA_SAVE = EA; // Preserve EA
U8 xdata * data pwrite; // FLASH write pointer
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
EA = 0; // Disable interrupts
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
pwrite = (U8 xdata *) addr;
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL |= 0x01; // PSWE = 1 which enables writes
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
*pwrite = byte; // Write the byte
PSCTL &= ~0x01; // PSWE = 0 which disable writes
EA = EA_SAVE; // Restore interrupts
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// FLASH_ByteRead
//-----------------------------------------------------------------------------
//
// Return Value :
// U8 - byte read from Flash
// Parameters :
// 1) FLADDR addr - address of the byte to read to
// valid range is 0x0000 to 0xFBFF for 64K Flash devices
// valid range is 0x0000 to 0x7FFF for 32K Flash devices
//
// This routine reads a <byte> from the linear FLASH address <addr>.
//-----------------------------------------------------------------------------
U8 FLASH_ByteRead (FLADDR addr)
{
bit EA_SAVE = EA; // Preserve EA
U8 code * data pread; // FLASH read pointer
U8 byte;
EA = 0; // Disable interrupts
pread = (U8 code *) addr;
byte = *pread; // Read the byte
EA = EA_SAVE; // Restore interrupts
return byte;
}
//-----------------------------------------------------------------------------
// FLASH_PageErase
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) FLADDR addr - address of any byte in the page to erase
// valid range is 0x0000 to 0xF9FF for 64K Flash devices
// valid range is 0x0000 to 0x7DFF for 32K Flash devices
//
// This routine erases the FLASH page containing the linear FLASH address
// <addr>. Note that the page of Flash containing the Lock Byte cannot be
// erased from application code.
//-----------------------------------------------------------------------------
void FLASH_PageErase (FLADDR addr)
{
bit EA_SAVE = EA; // Preserve EA
U8 xdata * data pwrite; // FLASH write pointer
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE_PAGE;
EA = 0; // Disable interrupts
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
pwrite = (U8 xdata *) addr;
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL |= 0x03; // PSWE = 1; PSEE = 1
VDM0CN = 0xA0; // Enable VDD monitor and high threshold
RSTSRC = 0x02; // Enable VDD monitor as a reset source
*pwrite = 0; // Initiate page erase
PSCTL &= ~0x03; // PSWE = 0; PSEE = 0
EA = EA_SAVE; // Restore interrupts
SFRPAGE = SFRPAGE_save;
}
void FLASH_ReadData(U8 *buffer,U8 length)
{
U8 i;
for(i = 0; i < length ; i++){
buffer[i] = FLASH_ByteRead (start_address + i);
}
}
void FLASH_WriteData(U8 *buffer,U8 length)
{
U8 i;
for(i = 0; i < length; i ++){
FLASH_PageErase(start_address + i);
}
for(i = 0; i < length; i ++){
FLASH_ByteWrite (start_address + i, buffer[i]);
}
}
void Flash_data_init()
{
U8 i;
for(i = 0; i < 30; i++){
Flash_data[i] = 0x00;
}
}
相关文章推荐
- STM32 对内部FLASH读写接口函数
- STM32 内部flash的读写程序
- stm32 读写内部Flash
- STM32 对内部FLASH读写接口函数(转)
- STM32F10X读写内部FLASH的完整工程
- C8051F的读写芯片FLASH的注意事项
- STM32 对内部FLASH读写接口函数
- STM32F4读写内部FLASH【使用库函数】
- 读写STM32内部flash读写代码
- 读写STM32内部flash读写代码
- STM32F103系列内部Flash读写程序
- STM32 对内部FLASH读写接口函数
- 第50章 读写内部FLASH—零死角玩转STM32-F429系列
- 读写STM32内部flash读写代码
- STM32内部FLASH读写-通用
- [nRF51822] 11、基础实验代码解析大全 · 实验16 - 内部FLASH读写
- STM32 对内部FLASH读写接口函数
- STM32读写内部Flash
- STM32 对内部FLASH读写接口函数
- STM32F4读写内部FLASH【使用库函数】