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

📄 main.c

📁 如何实现32M的FLASH在单片机上的存储功能的源代码
💻 C
字号:
/*
* Copyright (c) 2004,北京博创兴业科技有限公司
* All rights reserved.
* 
* 文件名称:main.c
* 文件标识:main
* 摘    要:主函数
* 
* 当前版本:2.0
* 作    者:Kent
* 完成日期:2004年5月20日
*
* 取代版本:1.1 
* 原作者  :Frank
* 完成日期:2003年8月10日
*/
#include <REGSND1.H>
#include "uart.h"
#include "flash.h"

unsigned char databuff[SectorLength];
unsigned char databuff1[128];

code unsigned char BPB_Data[512] = {
			0xeb, 0x3c, 0x90,//	Offset_U8_BS_jmpBoot		0
			'H', 'e', 'y', ',', 'K', 'e', 'n', 't',// Offset_U8_BS_OEMName		3
			0x00, 0x02,// Offset_short int_BPB_BytsPerSec	11	//Count of bytes per sector. This value may take on only the  //
//512									//following values: 512, 1024, 2048 or 4096. If maximum       //
										//compatibility is desired, only the value 512 should be used.//
			64,// Offset_U8_BPB_SecPerClus	13	//Number of sectors per allocation unit. This value must be a //
										//power of 2 that is greater than 0. The legal values are 1,  //
										//	2, 4, 8, 16, 32, 64,and 128.                                //
			0x1, 0x00,// Offset_short int_BPB_RsvdSecCnt	14	//Number of reserved sectors in the Reserved region of the    //
										//volume starting at the first sector of the volume.For FAT32 //
										//volumes, this value is typically 32.                        //
			0x02,// Offset_U8_BPB_NumFATs		16	//The count of FAT data structures on the volume.             //
			0xb0, 0x03,// Offset_short int_BPB_RootEntCnt	17	//Is only useful for FAT12 and FAT16 volumes.                 //
//										//For FAT32 volumes,this field must be set to 0.              //
//通过调整文件项为03b0,从而使数据扇区刚好从Block2Sector0开始,但
//文件分区表中根目录区并为标记为已占用。数据实际从Block4Sector0开始
			0xea, 0x7f,// Offset_short int_BPB_TotSec16		19	//For FAT12 and FAT16 volumes, this field contains the sector //
										//count, and BPB_TotSec32 is 0 if the total sector count fits //
										//(is less than 0x10000).                                     //
			0xf8,// Offset_U8_BPB_Media			21	//0xF8 is the standard value for fixed(non-removable) media.  //
										//For removable media, 0xF0 is frequently used.               //
			0x02, 0x00,// Offset_short int_BPB_FATSz16		22	//This field is FAT12/FAT16 16bit count of sectors occupied by//
										//	1 FAT.On FAT32 volumes this field must be 0, and BPB_FATSz32//
										//contains the FAT size count.                                //
			0x00, 0x00,// Offset_short int_BPB_SecPerTrk	24	//Sectors per track for interrupt 0x13. This field is only    //
										//relevant for media that have a geometry (volume is broken   //
										//down into tracks by multiple heads and cylinders) and are   //
										//visible on interrupt 0x13.This field contains the           //
										//"sectors per track" geometry value.                         //
			0x00, 0x00,// Offset_short int_BPB_NumHeads		26	//Number of heads for interrupt 0x13. This field is relevant  //
										//as discussed earlier for BPB_SecPerTrk.                     //
			0x01, 0x00, 0x00, 0x00,// Offset_U32_BPB_HiddSec		28	//Count of hidden sectors preceding the partition that        //
										//contains this FAT volume.                                   //
			0x00, 0x00, 0x00, 0x00,// Offset_U32_BPB_TotSec32		32	//This field is the new 32-bit total count of sectors on the  //
										//volume. This count includes the count of all sectors in all //
										//four regions of the volume. This field can be 0; if it is 0,//
										//then BPB_TotSec16 must be non-zero. For FAT32 volumes, this //
										//field must be non-zero. For FAT12/FAT16 volumes, this field //
										//contains the sector count if BPB_TotSec16 is 0 (count is    //
										//greater than or equal to 0x10000).//
			0x00,// Offset_U8_BS_DrvNum		36	//This field is the FAT32 32-bit count of sectors occupied by //
	//根据该值得到FAT的大小//				//ONE FAT. BPB_FATSz16 must be 0.                             //
	
			0x00,// Offset_U8_BS_Reserved		37	//    Bits 0-3 -- Zero-based number of active FAT.Only valid  //
										//                if mirroring is disabled.                   //
										//    Bits 4-6 -- Reserved.                                   //
										//    Bit 7    -- 0 means the FAT is mirrored at runtime into //
										//                  all FATs.                                 //
										//             -- 1 means only one FAT is active;it is the one//
										//                  referenced in bits 0-3.                   //
										//    Bits 8-15 -- Reserved.                                  //
			0x29,// Offset_U8_BS_BootSig		38	//High byte is major revision number.                         //
										//Low byte is minor revision number.                          //
			'N', 'A', 'M', 'E',// Offset_U32_BS_VolID		39	//This is set to the cluster number of the first cluster of   //
	//根据该值得到ROOT的地址//				//the root directory, usually 2 but not required to be 2.     //
	
			'N', 'O', ' ', 'N', 'A', 'M', 'E', ' ', ' ', ' ', ' ',// Offset_U2048_BS_VolLab		43	//Sector number of FSINFO structure in the reserved area of   //
										//the FAT32 volume.Usually 1.                                 //
			'F', 'A', 'T', '1', '6', ' ', ' ', ' ',// Offset_U256_BS_FilSysType	54	//If non-zero, indicates the sector number in the reserved    //
										//area of the volume of a copy of the boot record.            //
//Executable Code
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	

			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,	
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0,

			0x00,//active partion
			0x00,//head
			0x00,//partion begin
			0x00,//cylinder
			0x06,//is partion used
			0x00,//end head
			0x00,//partion end 
			0x00,//end cylinder
			0x00, 0x00, 0x00, 0x00,
			0x00, 0x80, 0x00, 0x00,//first partion

			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0,//second partion
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0,//third partion
			0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0, 0, 0, 0, 0,		0,//forth partion

			0x55, 0xAA	// Offset_short int_Signature		510	//0x55AA           

			};

void main()
{
	int i = 0, j = 0, k = 0, Writeok = 0;
	printu("UP-TECH FLASH Test!\n");
	//P3_5=0;
	Flash_Reset();
	//printu("Reset Over!\n");
	if (Check_Flash_Id())
	{
		printu("\nFlash is Found!\n");
		if (Check_Flash_Id() == 0xec75)
		{
			printu("Flash Type : K9F5608, Flash size : 32M!\n");
		}

		if (Check_Flash_Id() == 0xec73) 
		{
			printu("Flash Type : K9F2808, Flash size : 16M!\n");
		}

		i = SectorLength;
		while (i--) 
		{
			databuff[i] = 0;
			ReadPage(0, 0, databuff);
			i = 128;
		}

		while (i--)
		{
			if (databuff[i] != 0)
			{
				printu("Read Flash Test OK!\n");
				break;
			}
		}

		ReadPage(0xff, 0, databuff1);

		i = SectorLength;
		while (i--) 
		{
			databuff[i] = 0xa5;
			Erase_Cluster(0xff);
			WritePage(0xff, 0, databuff);
			ReadPage(0xff, 0, databuff);
		}
		
		i = 128;
		while (i--)
		{ 
			if ((databuff[i] != 0xa5) && (databuff[i] == databuff1[i])) 
			{
				printu("Can't Write to Flash, Check the resistor beside the Chip!\n");
				break;
			}
		}

		
		
		Erase_Cluster(0);
		WritePage(0, 0, BPB_Data);

		ReadPage(0, 0, databuff);
		if ((databuff[0] == 0xeb) && (databuff[1] == 0x3c) && (databuff[2] == 0x90)) 
		{
			printu("Write Flash Test Ok!\n"); 
			Writeok = 1;

			for (i=0; i<16; i++)
			{
				printuf("%x ", databuff[i]);
			}
		}
		else 
		{
			printu("\nFail to Write Flash, \n");
			printu("Please Check and make sure the Flash Chip have been properly joined!\n");
			printu("and Check Whether the Resistor R315 have been joined!\n");
		
		}
		
	}
	else 
	{
		printu("\nFLASH is Not Found!\n");
		printu("Please Check and make sure the Flash Chip have been properly joined!\n");
	}
	
	while (1);
}

⌨️ 快捷键说明

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