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

📄 driver.c

📁 用keil开发的.单片机税控器程序.单片机用的是AT公司的.upsd3245
💻 C
📖 第 1 页 / 共 4 页
字号:
        	LcdDisplayDot(j,i,DisON);
//      	LcdDisplayDot(Dot_x2,i);
    }    
}
//画空芯矩形
void DisplayFullRec(uchar Dot_y1,uchar Dot_x1,uchar Dot_y2,uchar Dot_x2,uchar DisON)
{ unsigned char i;

  	for(i=Dot_x1;i<=Dot_x2;i++)
    { 
    	LcdDisplayDot(i,Dot_y1,DisON);
      	LcdDisplayDot(i,Dot_y2,DisON);
    }
  	for(i=Dot_y1;i<=Dot_y2;i++)
    { 
    	LcdDisplayDot(Dot_x1,i,DisON);
      	LcdDisplayDot(Dot_x2,i,DisON);
    }    
}

void Hint(uchar x,uchar y,uchar *str,uchar mode)
{
	ulong t=0;
	uchar k=0xFF;
	
	LcdDisplay(x,y,str,mode);	
	while(k==0xFF)
	{
		k=GetKey();
		t++;
		if(t>10000)break;
		BeepOn;
	}
	BeepOff;
}

void PowerOffHint(uchar *str,uchar *dat,uchar flg)
{
	I2cWrite(PwrFlgAdr,1,&flg);
	LcdDisplay(0,0,"操作过程中断电:",3);
	LcdDisplay(16,0,str,0);
	LcdDisplay(32,0,dat,0);
	while(1);
}

void ErrorHint(uchar *str,uint num)
{
	uchar dat[25];
	
	TR1=0;TR0=0;
	DisableCom;
	DisableCs;
	LcdDisplay(16,0,str,3);
	strcpy(dat,"故障代码:");
//	NumToStr(num,&dat[9],SwitchInt);
	BcdToStr(2,&num,&dat[9],0);
	LcdDisplay(32,0,dat,0);
	LcdDisplay(48,0,"按<合计>键程序复位!",0);
	while(KeyScan()!=K_HJ);
	WDRST=0x70;
	WDKEY=0;
	while(1);
}
  //---------------------------Lcd End----------------------------------------------------











//---------------------------Flash Begin---------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////
//                    Serial Flash Description
//    chip  D7   D6   D5   D4   D3   D2   D1   D0
//          0    0    0    0    X    X    X    X  ----->>M45PE80== 0x  0xAA
//          0    0    0    1    X    X    X    X  ----->>M25P80 == 1x  0x55
//          0    0    1    0    X    X    X    X  ----->>M25P16 == 2x
//          0    0    1    1    X    X    X    X  ----->>          3x  Hold
//    chip Hihg 4 bit = M25P80 or M45PE80  or  M25P16
///////////////////////////////////////////////////////////////////////////////
//Function: Select chip AND port Low
//Control = 1 Port Low   Control =0 Port High
//Compile: yongjibing
//Time:    2004-11-11 19:48
/////////////////////////////////////////////////////////////////////////////////
void Selectchip(uchar chip,uchar Control)
{
   GS yB;
   yB.bt=chip;
   FlashEnable;
   if(Control)
   {
     switch(yB.swap.bytHigh)
     {
      case 0:           //Select M45PE80
        SerialFlash[0]=0xAA;
        break;
      case 1:           //Select M25P80
        SerialFlash[0]=0x55;
        break;
      case 2:           //Select M25P16
        break;
      default :return;  // Input Unllity
     }
    switch(yB.swap.bytlow)
    {
      case 1:
          Mcs1_Enable;
        return;         //Select chip = 1
      case 2:
          Mcs2_Enable;
        return;         //Select chip = 2
      case 3:
          Mcs3_Enable;
        return;         //Select chip = 3
      case 4:
          Mcs4_Enable;
        return;         //Select chip = 4
      default:return;   //Input Unllity
    }
  }
  else FlashDisable;
}



uchar ReadFlashStatus(uchar chip,uchar type)
{
    uchar value=0xf;
    ulong Overtime;
    
    if(type == FlashBE || type == FlashSE || type == FlashPE)//块擦除,扇区擦除,页擦除
    	Overtime = 352000;		//22s
    if(type == FlashPP || type == FlashPW) //页写与页编程
    	Overtime = 32000;			//20ms

    Selectchip(chip,1);
    SpiSendChar(FlashRSR);//05
	while(--Overtime)
	{
	    if(value&0x01)
			value=SpiReceiveChar();
	    else	break;
	}

    Selectchip(chip,0);
   	if(Overtime != 0)
   		return 1;
   	else
   		return 0;				//超时
}


//write status Regrister only M25P80 use
//void WriteFlashStatus(uchar chip)
//{
//   Selectchip(chip,1);
//   SpiSendChar(FlashRSR);
//   SpiSendChar(0);
//   Selectchip(chip,0);
//}

/*----------------------------------------------------------------------
          write enable insturction
----------------------------------------------------------------------*/
void FlashWriteEnable(uchar chip)
{
 	Selectchip(chip,1);
 	SpiSendChar(FlashWE);
 	Selectchip(chip,0);
}

/*-----------------------------------------------------------------------
          write num data to Flash
-------------------------------------------------------------------------*/
uchar FlashWriteData( uchar chip, ulong address, uint Len, uchar *Dat)//向某个25p80写num个数据
{
  	uint  i;
  	gyt4 mydat1;
  	gyt2 mydat2;
  	
  	mydat1.dat=address;
  	mydat2.dat=Len;
  	FlashWriteEnable(chip);//wriet enable insturction
 	Selectchip(chip,1);

 	for(i=0;i<Len;i++)
  	TxdBuf[i]=Dat[i];
  	SerialFlash[1]=mydat2.str[1];//写数据长度高位1字节
  	SerialFlash[2]=mydat1.str[1];//地址3字节
  	SerialFlash[3]=mydat1.str[2];
  	SerialFlash[4]=mydat1.str[3];
  	
 	SpiSendData();
 	Selectchip(chip,0);
 	if(ReadFlashStatus(chip,FlashPP))
 		return SpiOK;
 	else 
 	{
 		ErrorHint("写Flash出错!",0x8100|chip);
 		return SpiErr;
 	}
}

/*---------------------------------------------------------------------------------
调用方法:  uchar SerialFlashWrite(uchar chip,ulong address,uint Len,uchar *Dat);
     ch:  选择操作芯片    address: 写入地址   Len:  写入长度    Dat[]: 写入数组
      返回  1  操作成功   返回   0  操作失败
----------------------------------------------------------------------------------*/
uchar SerialFlashWrite(uchar chip,ulong address,uint Len,uchar *Dat)
{
  	uint   n,k;
  	
//  	if(Len>256) return SpiWriteLenErr;
  	if(Len>256)Len=256;
  	n=256-(address%256);//一个扇区剩下多少字节
  	if(n>=Len)//本扇区所剩空间可以全写入本数组的内容
  	{
  	  	return FlashWriteData(chip,address,Len,Dat);
  	}
  	if(n<Len)//本扇区所剩空间不可以全写入本数组的内容
  	{
  	   	if(FlashWriteData(chip,address,n,Dat)!=SpiOK)
  	   		return SpiErr;
  	   	k=Len-n;
  	   	return FlashWriteData(chip,address+n,k,&Dat[n]);
  	}
}
/*------------------------------------------------------------------------------------
      form Flash FlashRD num data to *Dat
   调用方法:  read_data(uchar chip,ulong add,uint Len,uchar *Dat);
   说明:  chip: 选择芯片  address:  读起始地址  Len: 读取长度  Dat[]:  读取存放数据
--------------------------------------------------------------------------------------*/

void SerialFlashRead(uchar chip,ulong address,uint Len,uchar *Dat)//读某个25P80的num个字节数据
{
    uint i;
    uchar flag=0;
    gyt4 mydat1;
    gyt2 mydat2;
    
	if(Len>256)Len=256;
    mydat1.dat=address;
    mydat2.dat=Len;

    Selectchip(chip,1);
    SerialFlash[1]=mydat2.str[1];//读数据长度高位1字节
  	SerialFlash[2]=mydat1.str[1];//地址3字节
  	SerialFlash[3]=mydat1.str[2];
  	SerialFlash[4]=mydat1.str[3];

    SpiReceiveData();
    for(i=0;i<Len;i++)
      Dat[i]=RxdBuf[i];
    Selectchip(chip,0);
}

/*----------------------------------------------------------------------------
   调用方式:  sector_erase(uchar chip,ulong FlashSE);
   说    明:  chip:  选择芯片  Sector:  扇区(0--15)
//sector erase function
// M45Pe80 and M25P80
-----------------------------------------------------------------------------*/
uchar SerialFlashSectorErase(uchar chip,uchar Sector)//sector value  form 0 to 15
{
  	ulong address;
  	gyt4 addr;
  	address=Sector*0x10000;
  	addr.dat = address;
  	FlashWriteEnable(chip);//wriet enable insturction
  	Selectchip(chip,1);
  	SpiSendChar(FlashSE);//FlashSE
  	SpiSendChar(addr.str[1]);
  	SpiSendChar(addr.str[2]);
  	SpiSendChar(addr.str[3]);
  	Selectchip(chip,0);
  	if(ReadFlashStatus(chip,FlashSE)) return SpiOK;
  	else 
  	{
  		ErrorHint("Flash扇区擦除出错!",0x8200|chip);
  		return SpiErr;
  	}
}

/*-----------------------------------------------------------------------------------
    调用方式: bulk_erase(uchar chip);
    说    明: chip:  选择芯片
//bulk erase instuction
------------------------------------------------------------------------------------*/
uchar SerialFlashBulkErase(uchar chip)
{
	FlashWriteEnable(chip);//wriet enable insturction
	Selectchip(chip,1);
	SpiSendChar(FlashBE);//FlashBE
	Selectchip(chip,0);
	if(ReadFlashStatus(chip,FlashBE)) return SpiOK;
	else 
	{
		ErrorHint("Flash整块擦除出错!",0x8300|chip);
		return SpiErr;
	}
}
/*
//M45P80页擦除函数
uchar M45P80PageErase(uchar chip,uint page)
{
	gyt4 addr;
	addr.dat=page*256;
	FlashWriteEnable(chip);//wriet enable insturction
	Selectchip(chip,1);
	SpiSendChar(FlashPE);//FlashPE
	SpiSendChar(addr.str[1]);
	SpiSendChar(addr.str[2]);
	SpiSendChar(addr.str[3]);
	Selectchip(chip,0);
	if(ReadFlashStatus(chip,FlashPE))return SpiOK;
	else return SpiErr;
}
*/

///////////////////////////////////////////
//      FlashRD serial flash electronic sign
//      Device Electronic Signature
//        M25P05-A         05h
//        M25P10-A         10h
//        M25P20           11h
//        M25P40           12h
//        M25P80           13h
//        M25P16           14h
//
//  test result: did not know why M25P80 have same ES 0x13h with M45PE80???? yongjibin
///////////////////////////////////////////
/////////////////////////////////
///////AT45DB161/////////////////
/////////////////////////////////



#define SFCSEN P4 |=9; // 片选

#define SFCSDI P4=0;   //取消片选
//#define uchar unsigned char
//#define uint unsigned int
//#define ulong unsigned long
sbit BUSY=P3^5;    //忙判断可以不要


uchar StatusREG_Read(void)
{	
	uchar dat;
	SFCSEN;   //片选	
	SpiSendChar(0xd7);
	dat=SpiReceiveChar();	
	SFCSDI;
	return dat;
}
uchar Memory_Read(ulong adr,ulong len,uchar *dat)
{
	ulong pageadr=0,byteadr=0;	 
	uchar higeadr=0,midadr=0,lastadr=0;
	uint i;
	ulong temp;	
	pageadr=adr/528;
	byteadr=adr%528;
	temp=0;
	temp|=pageadr<<10;
	temp|=byteadr;	
	higeadr=temp>>16;	
	midadr=temp>>8;	
	lastadr=(uchar)temp;
	SFCSEN;				//片选
	SpiSendChar(0xe8);//ContinuousArrayRead
	SpiSendChar(higeadr);						
	SpiSendChar(midadr);
	SpiSendChar(lastadr);
	SpiSendChar(0x00);
	SpiSendChar(0x00);
	SpiSendChar(0x00);
	SpiSendChar(0x00);	
	for(i=0;i<(len);i++)
	{
		dat[i]=SpiReceiveChar();
	}
	SFCSDI;
	return 1;

}
uchar Memory_Page_Write(ulong adr,ulong len, uchar *dat)
{
	ulong pageadr,byteadr,pagenumber,bytenumber,sparelen,sparebyte;
	uchar higeadr,midadr,lastadr;
	ulong temp;
	uint i;	
	if(len>528)len=528;	
	pageadr=adr/528;
	byteadr=adr%528;
	temp=0;
	temp|=pageadr<<10;
	temp|=byteadr;	
	higeadr=temp>>16;	
	midadr=temp>>8;	
	lastadr=(uchar)temp;
	while((StatusREG_Read()&0x80)==0x00);
	SFCSEN;			   //片选
	SpiSendChar(0x53);//MemoryPageToBuffer1Transfer			
	SpiSendChar(higeadr);						
	SpiSendChar(midadr);
	SpiSendChar(lastadr);
	SFCSDI;	
	while((StatusREG_Read()&0x80)==0x00);
	SFCSEN;			   //片选
	SpiSendChar(0x84);//Buffer1Write	
	SpiSendChar(higeadr);						
	SpiSendChar(midadr);
	SpiSendChar(lastadr);
	for(i=0;i<len;i++)
	{				
		SpiSendChar(dat[i]);
	}
	SFCSDI;	
	while((StatusREG_Read()&0x80)==0x00);
	SFCSEN;				//片选
	SpiSendChar(0x83);//Buffer1ToMemoryProgramErase			
	SpiSendChar(higeadr);						
	SpiSendChar(midadr);
	SpiSendChar(lastadr);
	SFCSDI;	
	while((StatusREG_Read()&0x80)==0x00);
	SFCSEN;			   //片选
	SpiSendChar(0x60);//Buffer1ToMemoryProgramErase			
	SpiSendChar(higeadr);						
	SpiSendChar(midadr);
	SpiSendChar(lastadr);
	SFCSDI;	
	if((StatusREG_Read()&0x40)==0x00)
	{
		return 1;
	}
	else
	{
		return 0;
	}


}
uchar Memory_Write(ulong adr,ulong len,uchar *dat)
{
	ulong pageadr,byteadr,pagenumber,bytenumber,sparelen;
	uchar higeadr,midadr,lastadr;	
	ulong temp,spare=0;	
	ulong i,j,k;
	sparelen=528-adr%528;
	if(sparelen>=len)
	{
		if(	Memory_Page_Write(adr,len,dat)==1)
		{
			return 1;
		 }
		 else
		 {
			return 0;
		  }
	}
	if(Memory_Page_Write(adr,sparelen,dat)==0)return 0;
	adr +=sparelen;
	len -=sparelen;
	dat +=sparelen;
	pageadr=adr/528;
	byteadr=adr%528;
	for(i=0;i<pageadr;i++)
	{
		if(Memory_Page_Write(adr,528,dat)==0)return 0;
		adr +=528;
		dat +=528;
	}
	if(Memory_Page_Write(adr,byteadr,dat)==0)return 0;		
}

void Memory_Erase_All_Page()
{
	uint i;
	ulong pageadr,byteadr;	
	ulong temp;
	uchar higeadr,midadr,lastadr;		
	for(i=0;i<4096;i++)
	{
		
		pageadr=i;
		byteadr=0;
		temp=0;
		temp|=pageadr<<10;
		temp|=byteadr;	
		higeadr=temp>>16;	
		midadr=temp>>8;	
		lastadr=(uchar)temp;
		while((StatusREG_Read()&0x80)==0x00);	
		SFCSEN;			  //片选
		SpiSendChar(0x81);//PageErase		
		SpiSendChar(higeadr);						
		SpiSendChar(midadr);
		SpiSendChar(lastadr);
		SFCSDI;		
	 }
	
}

/*
uchar ReadFlashSign(uchar chip)
{
	uchar value=0;
	Selectchip(chip,1);
	SpiSendChar(0xAB);//FlashRDP
	SpiSendChar(0x55);
	SpiSendChar(0xAA);
	SpiSendChar(0x55);
	value=SpiReceiveChar();
	Selectchip(chip,0);
	return value;
}
*/
//---------------------------Flash End----------------------------------------------------

⌨️ 快捷键说明

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