⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash.c

📁 silicon lab 单片机C8051F320的片内flash 读写程序
💻 C
字号:
#include<c8051F320.h>
#include"flash.h"
//-----------------------------------------------------------------------------
// FLASH_ByteWrite
//-----------------------------------------------------------------------------
//
// This routine writes <byte> to the linear FLASH address <addr>.
//
// To do:
// -- optimize to skip 0xFF bytes
//
void FLASH_ByteWrite (int addr, char byte)
{
	bit EA_SAVE = EA; 						// preserve EA
	//AN201
	//Rev. 0.1 83
	char xdata * data pwrite; 				// FLASH write pointer
	EA = 0; 								// disable interrupts
											// change clock speed to slow, then restore later
	VDM0CN = 0x80; 							// enable VDD monitor
	RSTSRC = 0x02; 							// enable VDD monitor as a reset source
	pwrite = (char xdata *) addr;
	FLKEY = 0xA5; 							// Key Sequence 1
	FLKEY = 0xF1; 							// Key Sequence 2
	PSCTL |= 0x01; 							// PSWE = 1
	VDM0CN = 0x80; 							// enable VDD monitor
	RSTSRC = 0x02; 							// enable VDD monitor as a reset source
	*pwrite = byte; 						// write the byte
	PSCTL &= ~0x01; 						// PSWE = 0
	EA = EA_SAVE; 							// restore interrupts
}
//-----------------------------------------------------------------------------
// FLASH_ByteRead
//-----------------------------------------------------------------------------
//
// This routine reads a <byte> from the linear FLASH address <addr>.
//
unsigned char FLASH_ByteRead (int addr)
{
	bit EA_SAVE = EA; 						// preserve EA
	char code * data pread; 				// FLASH read pointer
	unsigned char byte;
	EA = 0; 								// disable interrupts
	pread = (char code *) addr;
	byte = *pread; 							// read the byte
	EA = EA_SAVE; 							// restore interrupts
	return byte;
}
//-----------------------------------------------------------------------------
// FLASH_PageErase
//-----------------------------------------------------------------------------
//
// This routine erases the FLASH page containing the linear FLASH address
// <addr>.
//
void FLASH_PageErase (int addr)
{
	bit EA_SAVE = EA; 							// preserve EA
	char xdata * data pwrite; 					// FLASH write pointer
	//AN201
	//84 Rev. 0.1
	EA = 0; 									// disable interrupts
	// change clock speed to slow, then restore later
	VDM0CN = 0x80; 								// enable VDD monitor
	RSTSRC = 0x02; 								// enable VDD monitor as a reset source
	pwrite = (char xdata *) addr;
	FLKEY = 0xA5; 								// Key Sequence 1
	FLKEY = 0xF1; 								// Key Sequence 2
	PSCTL |= 0x03; 								// PSWE = 1; PSEE = 1
	VDM0CN = 0x80; 								// enable VDD monitor
	RSTSRC = 0x02;		 						// enable VDD monitor as a reset source
	*pwrite = 0; 								// initiate page erase
	PSCTL &= ~0x03; 							// PSWE = 0; PSEE = 0
	EA = EA_SAVE; 								// restore inte
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -