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

📄 flash.c

📁 2007年全国大学生电子设计大赛
💻 C
字号:
#include "flash.h"
#include "c8051f120.h"
#include "intrins.h"
sbit DF_SCK=P5^4;
sbit DF_SI=P5^6;
sbit DF_SO=P5^7;
sbit DF_CS=P5^0;
#define unchar unsigned char
#define unint unsigned int
unchar DF_R[256];//存放读取的值
unchar DF_R1[256];
/********************************
延时函数
*********************************/
void delayus(unint t)            
{
unint i;
for(i=0;i<t;i++);
{_nop_();_nop_();}	
}
/*******************************************************************************
**函数名称: DF_Send_Byte()
**函数功能: 发送字节	  
**入口参数: unchar word
**出口参数: 无 
*******************************************************************************/
void DF_Send_Byte(unchar S_data)    
{
   unchar i;
   unchar tempword;
   
   tempword = S_data;
   DF_CS=0;
   for (i=0;i<8;i++)
   {
     DF_SCK=0;                   //使能低	  
	 //delayus(1);
     if((tempword & 0x80)==0)   //!(tempword & 0x80)
       DF_SI=0;
     else
       DF_SI=1;
	
     DF_SCK=1;                    //使能高	  
     tempword = tempword << 1;
	 //delayus(1);
   } 
}

/*******************************************************************************
**函数名称: DF_Read_Byte(void)
**函数功能: 读取一个字节的内容
**入口参数: 无	  
**出口参数: UCHAR R_word
*******************************************************************************/

unchar DF_Read_Byte(void)
{
  unchar i;
  unchar R_word = 0;
  DF_CS=0;                //SELECT
  for(i=0;i<8;i++)
  {
    R_word = R_word << 1;
    DF_SCK=0;
	//delayus(1);
    DF_SCK=1;
	//delayus(1);
    if(DF_SO==1)                   //
     R_word =  R_word+1 ;              //R_word = R_word + 1;           
    DF_SCK=0;    
  }
  return (R_word);
}

/*******************************************************************************
**函数名称: void DF_Stop(void)
**函数功能: 停止操作	  
**入口参数: 无
**出口参数: 无
*******************************************************************************/

void DF_Stop(void)
{
  DF_CS=1;    //NOT SELECT
  DF_SCK=1;   //SCK 置1
}
/*******************************************************************************
**函数名称: unchar DF_Status(void)
**函数功能: 读取状态(读取忙标志)
**入口参数: 无
**出口参数: UCHAR status--1不忙,0忙   
*******************************************************************************/

unchar DF_Status(void)
{
  unchar status=0;
  
  DF_CS=0;
  DF_Send_Byte(0x57);
  status = DF_Read_Byte();
  DF_Stop();
  if(status&0x80)       //status&0x80
    return 1;
  else
    return 0;
}
/*******************************************************************************
**函数名称: void DF_Write_Buf()
**函数功能: 写Buffer
**入口参数: unchar type--1选择BUF1,2选择BUF2;
**          unsigned int addr--BUF地址(0~263)
**          unchar *W_data--要写入的数据首地址
**          unsigned int Length--待写入数据的长度
**出口参数: 无
*******************************************************************************/

void DF_Write_Buf(unchar type,unint addr,unchar *W_data,unint Length)
{
  unchar addr_H = (unchar)(addr>>8);
  unchar addr_L = (unchar)(addr&0x00ff);
  unchar temp;
  unint j;
  
  while(!DF_Status());
  if(type==1)
    temp = 0x84;
  else            //if(type == 2)
    temp = 0x87;
  DF_CS=0;
  DF_Send_Byte(temp);
  DF_Send_Byte(0x00);
  DF_Send_Byte(addr_H);
  DF_Send_Byte(addr_L);
  for(j=0;j<Length;j++)
  {
    DF_Send_Byte(W_data[j]);
  }
  DF_Stop();
}
/*******************************************************************************
**函数名称: void DF_Read_Buf()
**函数功能: 从BUF中读取数据,放入DF_R[]数组中
**入口参数: unchar type--1选择BUF1,2选择BUF2
**          unsigned int addr--读数据的地址
**          unsigned int Length--要读取的长度
**出口参数: 无
*******************************************************************************/

void DF_Read_Buf(unchar type,unint addr,unint Length)
{
  unchar addr_H = (unchar)(addr>>8);
  unchar addr_L = (unchar)(addr&0x00ff);
  unchar temp;
  unint j;
  
  while(!DF_Status());
  switch(type)
  {
  case 1:temp=0x54;break;
  case 2:temp=0x56;break;
  default:temp=0x54;break;
  }
  DF_CS=0;
  DF_Send_Byte(temp);
  DF_Send_Byte(0x00);
  DF_Send_Byte(addr_H);
  DF_Send_Byte(addr_L);
  DF_Send_Byte(0xff);
  for(j=0;j<Length;j++)
  {    
    DF_R[j] = DF_Read_Byte();
  }
  DF_Stop();
}

/*******************************************************************************
**函数名称: void DF_Write_Main()
**函数功能: 写主存,Buffer to Main Memory Page Program with Built_In Erase
**入口参数: unchar type--1选择BUF1,2选择BUF2
**          unsigned int addr--主存的地址
**          unchar *W_data--待写入的数据
**          unsigned int Length--待写入数据的长度
**出口参数: 无
*******************************************************************************/

void DF_Write_Main(unchar type,unint addr,unchar *W_data,unint Length)
{
  unchar addr_H = (unchar)(addr>>7);
  unchar addr_L = (unchar)((addr<<1) & 0x00FF);
  unchar type1;
  unchar type2;
  switch(type)
  {
  case 1:type1=0x84;type2=0x83;break;
  case 2:type1=0x87;type2=0x86;break;
  }
  while(!DF_Status());
  DF_CS=0;
  DF_Write_Buf(type,0,W_data,Length);
  while(!DF_Status());
  DF_CS=0;
  DF_Send_Byte(type2);
  DF_Send_Byte(addr_H);
  DF_Send_Byte(addr_L);
  DF_Send_Byte(0x00);  
  DF_Stop();
}
/*******************************************************************************
**函数名称: unchar *DF_Read_Main()
**函数功能: 读主存
**入口参数: unsigned int addr_P--主存页地址
**          unsigned int addr_B--主存页中字节地址
**          unsigned int Length--读取数据的长度
*******************************************************************************/
/*void DF_Read_Main(unint addr_P,unint addr_B,unint Length)
{
  unchar addr_PH = addr_P>>7;
  unchar addr_PL = ((addr_P<<1)&0x00ff)+(addr_B>>8);
  unchar addr_BL = addr_B & 0x00ff;
  unchar rdata[256] = {0};
  unint j;
  while(!DF_Status());
  DF_CS=0;
  DF_Send_Byte(0x52);
  DF_Send_Byte(addr_PH);
  DF_Send_Byte(addr_PL);
  DF_Send_Byte(addr_BL);
  DF_Send_Byte(0x00);
  DF_Send_Byte(0x00);
  DF_Send_Byte(0x00);
  DF_Send_Byte(0x00);
  for(j=0;j<Length;j++)
  {
    rdata[j] = DF_Read_Byte();
    DF_R1[j] = rdata[j];  
  }
  DF_Stop();
}*/
unchar *DF_Read_Main(unint addr_P,unint addr_B,unint Length)
{
  unchar addr_PH = addr_P>>7;
  unchar addr_PL = ((addr_P<<1)&0x00ff)+(addr_B>>8);
  unchar addr_BL = addr_B & 0x00ff;
  unchar rdata[256] = {0};
  unint j;
  while(!DF_Status());
  DF_CS=0;
  DF_Send_Byte(0x52);
  DF_Send_Byte(addr_PH);
  DF_Send_Byte(addr_PL);
  DF_Send_Byte(addr_BL);
  DF_Send_Byte(0x00);
  DF_Send_Byte(0x00);
  DF_Send_Byte(0x00);
  DF_Send_Byte(0x00);
  for(j=0;j<Length;j++)
  {
    rdata[j] = DF_Read_Byte();
    DF_R1[j] = rdata[j];  
  }
  DF_Stop();
  return(rdata);
} 
/*******************************************************************************
**函数名称: void DF_MaintoBuf()
**函数功能: 将主存中addr_P的内容复制到BUF中
**入口参数: unchar type--1选择BUF1,2选择BUF2
**          unsigned int addr_P--页地址
**出口参数: 无
*******************************************************************************/

void DF_MaintoBuf(unchar type,unint addr_P)
{
  unchar addr_PH = (unchar)(addr_P>>7);
  unchar addr_PL = (unchar)(addr_P<<1);
  switch(type)
  {
  case 1:type=0x53;break;
  case 2:type=0x55;break;
  }
  DF_CS=0;
  DF_Send_Byte(type);
  DF_Send_Byte(addr_PH);
  DF_Send_Byte(addr_PL);
  DF_Send_Byte(0x00);
  DF_Stop();
}
/*******************************************************************************
**函数名称: void DF_Port_Init(void)
**函数功能: 端口初始化
**入口参数: 无
**出口参数: 无
*******************************************************************************/

void DF_Port_Init(void)
{
  SFRPAGE=0x0F;
  P5 = 0XFF;
  DF_CS=1;    //NOT SELECT
  DF_SCK=1;   //SCK 置1
  DF_SO=1;   //设置s0为输入 

}

⌨️ 快捷键说明

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