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

📄 flash.c

📁 39LF040单片机片外flash读写程序
💻 C
字号:
/*
*********************************************************************************************************
* Flash(SST39LF040) Driver for C8051F340 MCU	
* version 1.0
* File : flash.c
* By   : WY		2006/09/08
*********************************************************************************************************
*/
#include "C8051F340.h"
#include <absacc.h>
#include <stdio.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long int
sbit flashCE=P2^6;
sbit flashAddr16=P1^0;
sbit flashAddr17=P1^1;
sbit flashAddr18=P1^2;
/*void flashWriteByte(unsigned long address,unsigned char aByte)
{
	data unsigned char blockAddr;
	data unsigned char addr15_8;
	data unsigned char addr7_0;

	blockAddr = (unsigned char)(address >> 16) & 0x1f;
	addr15_8 = (unsigned char)(address >> 8);
	addr7_0  = (unsigned char)(address);

	WR_HIGH;
	RD_HIGH;
//	flashALE = 1;

	P4 = (blockAddr | 0xe0) & (P4 | 0x1f);
//	flashEn = 1;

	P5 = addr15_8;
	P6 = addr7_0;
//	flashALE = 0;
//	flashALE = 1;
	P7 = aByte;
	WR_LOW;
	WR_LOW;
	WR_HIGH;

//	flashEn = 0;
}

unsigned char flashReadByte(unsigned long address)
{
	data unsigned char blockAddr;
	data unsigned char addr15_8;
	data unsigned char addr7_0;
	data unsigned char zz;

	blockAddr = (unsigned char)(address >> 16) & 0x1f;
	addr15_8 = (unsigned char)(address >> 8);
	addr7_0  = (unsigned char)(address);

	WR_HIGH;
	RD_HIGH;
//	flashALE = 1;

	P4 = (blockAddr | 0xe0) & (P4 | 0x1f);
//	flashEn = 1;

	P5 = addr15_8;
	P6 = addr7_0;
//	flashALE = 0;
//	flashALE = 1;
	RD_LOW;
	RD_LOW;
 	zz = P7;
	RD_HIGH;
//	flashEn = 0;

	return zz;
}*/
void flashWriteByte(ulong address,uchar aByte)
{
	idata uchar blockAddr;
	char xdata *idata pwrite;
	blockAddr = (uchar)(address >> 16) & 0x07;
	pwrite=(uint)(address);
	flashAddr16=blockAddr^0;
	flashAddr17=blockAddr^1;
	flashAddr18=blockAddr^2;
	flashCE=0;
	*pwrite=aByte;
    flashCE=1;
}
uchar flashReadByte(ulong address)
{
	uchar idata blockAddr;
	idata uchar x;
	char xdata *idata pRead;
    blockAddr = (uchar)(address >> 16) & 0x07;
	pRead = (uint)address;
    flashAddr16=blockAddr^0;
	flashAddr17=blockAddr^1;
	flashAddr18=blockAddr^2;
	flashCE = 0;
	x = *pRead;
	flashCE = 1;
	return x;
}
void flashChipErase(void)
{
	uchar idata x1,x2;

	flashWriteByte(0x5555,0xaa);	
	flashWriteByte(0x2aaa,0x55);
	flashWriteByte(0x5555,0x80);
	flashWriteByte(0x5555,0xaa);
	flashWriteByte(0x2aaa,0x55);
	flashWriteByte(0x5555,0x10);

	do												// check the toggling bit to 
	{												// ensure the operation is completed.
		x1 = flashReadByte(0x000) & 0x40;
		x2 = flashReadByte(0x000) & 0x40;
	 }while(x1 != x2); 									 												
}
void flashBlockErase(unsigned long blockAddress)
{
	unsigned char idata x1,x2;
	
	flashWriteByte(0x5555,0xaa);	
	flashWriteByte(0x2aaa,0x55);
	flashWriteByte(0x5555,0x80);
	flashWriteByte(0x5555,0xaa);
	flashWriteByte(0x2aaa,0x55);
	flashWriteByte(blockAddress,0x50);

	do
	{
		x1 = flashReadByte(blockAddress) & 0x40;
		x2 = flashReadByte(blockAddress) & 0x40;
	 }while(x1 != x2);
}
void flashSectorErase(ulong sectorAddress)
{
	uchar idata x1,x2;

	flashWriteByte(0x5555,0xaa);	
	flashWriteByte(0x2aaa,0x55);
	flashWriteByte(0x5555,0x80);
	flashWriteByte(0x5555,0xaa);
	flashWriteByte(0x2aaa,0x55);
	flashWriteByte(sectorAddress,0x30);

	do
	{
		x1 = flashReadByte(sectorAddress) & 0x40;
		x2 = flashReadByte(sectorAddress) & 0x40;
	 }while(x1 != x2);
}
void flashByteProgram(ulong address,uchar aByte)
{
	static uchar x1,x2;

	flashWriteByte(0x5555,0xaa);	
	flashWriteByte(0x2aaa,0x55);
	flashWriteByte(0x5555,0xa0);	

	flashWriteByte(address,aByte);

	do
	{
		x1 = flashReadByte(address) & 0x40;
		x2 = flashReadByte(address) & 0x40;
	 }while(x1 != x2);
}
ulong flashTest(void)
{
	static ulong xaddr;
	static uchar xx1;//				_at_ 0x0100;
	static uchar xx2;//				_at_ 0x0101;
	static ulong xx3;	//			_at_ 0x0102;
//	static data unsigned int xx4;	//			_at_ 0x0104;
	//flashSectorErase(0x1000);
	flashChipErase();
	for(xx1 = 0,xaddr = 0x00000;xaddr <= 0x7ffff;xaddr ++,xx1 ++)
	{
		flashByteProgram(xaddr,xx1);
	 }

	for(xx1 = 0,xx3 = 0,xaddr = 0x00000;xaddr <= 0x7ffff;xaddr ++,xx1 ++)
	{
		xx2 = flashReadByte(xaddr);
		if(xx1 != xx2) xx3 ++;
	 }
	return xx3;
}

⌨️ 快捷键说明

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