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

📄 rd_wr_k9f5608.c

📁 C51读写K9F5608源程序
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "complier.h"

#include "build.h"

/*****************************************
1|?ü£o
?èê?£o
2?êy£o
·μ??£o
±?×¢£o
******************************************/
void main(void)
{      
   init_mcu();
   init_flash();
   while(1)
    {
      	//ST=0; //2aê?
		scan_key();
		while(SET_SC) 
		 {
		   //if(!FlashNum)          test_xram();
		   if(FlashNum)           write_flash(); //óDD§êy?Y′?′¢
		   scan_key();
		 }
		while(SET_TX)
		 {
		   scan_key();
		   if(PcAskMcu)             answer_pc_read();   //′??íêy?Yμ??????ú
		   else if(CountAdd)        count_rd_address(); //?????úìí3????tμ??eê?μ??·
		   else if(CheckFiles)      check_files_store();//?ì2éìí3????t??êy
		   else if(EraseAllBlock)   erase_all_block();  //2á3yìí3????t
		   else if(AskSysInfo)      answer_system_info();//ìí3?ò?D??¢?áè?
		 }
    }
}

/*****************************************
1|?ü£o3?ê??ˉμ¥???ú
?èê?£o?D??à?èè???ˉ£?3?ê??ˉμ¥???ú×′ì?±ê????
2?êy£o
·μ??£o
±?×¢£o
******************************************/
void init_mcu(void)
 {
   bit CY=1;
   watchdog();
   EA=0;
   PMR=0x40;    //?ú?÷?ü?ú?a4??CLK
   CKCON=0x00;  //TIME0 ?a12·??μ
   if(CY==1)
    {
    	//11.0592MHz
    	RCAP2H=0xFF; //????′??ú0,ó??÷3ìDòí¨D? 57600bps
   		RCAP2L=0xFA;
   		TH2=RCAP2H;
   		TL2=RCAP2L;
   		SCON=0x50;  //?êDí?óê?
   		T2CON=0x34; //?¨ê±?÷2×?′??ú02¨ì??ê·¢éú?÷,???ˉT2
   		
        WDCON|=0x80;
   		TMOD=0x21;
   		SCON1=0x50; 
   		TH1=0xFF;   //????′??ú1£?ó??????úí¨D? 57600bps
   		TL1=0xFF;   
  	    TR1=1;
   	}
   else
    {
    	//22.1184MHz
    	RCAP2H=0xFF; //????′??ú0,ó??÷3ìDòí¨D? 57600bps
   		RCAP2L=0xF4;
   		TH2=RCAP2H;
   		TL2=RCAP2L;
   		SCON=0x50;  //?êDí?óê?
   		T2CON=0x34; //?¨ê±?÷2×?′??ú02¨ì??ê·¢éú?÷,???ˉT2
   		
        TMOD=0x21;
   		SCON1=0x50; 
   		TH1=0xFF;   //????′??ú1£?ó??????úí¨D? 57600bps
   		TL1=0xFF;   
  	    TR1=1;

   	}	

	ET0=0; //Time1 50ms ??ê±×?±?
	TF0=0;
	TR0=0;
	TH0=0x4C;
	TL0=00;

    



 }




/*****************************************
1|?ü£oμè′y?D??
?èê?£o′??ú?D??0£oμè′yé?2?°?·¢à′μ?′?′¢êy?Y
      ′??ú?D??1£oμè′y?????ú·¢à′μ??üá?êy?Y
2?êy£o
·μ??£o
±?×¢£o
*****************************************
void wait_interrupt(void)
{
  // PCON|=0x01;
   NOP5();
   watchdog();
}

/*****************************************
1|?ü£oé¨?è??2é±ê/í¨D????ü?μ
?èê?£o?ù?Y2?í?μ?1|?ü£???±?2?í?μ?2¨ì??ê
2?êy£o
·μ??£o
±?×¢£o
******************************************/
void scan_key(void)
 {
   uchar i=0; 
   watchdog();
   
   
   if(ST && !SET_SC)        
    { 
          //2é±ê×′ì?      
		  //êy2é£-£-′??ú0
          SET_SC=1; 
     	  SET_TX=0;
     	  SP322E_OFF();
     	  ES=1;
          ES1=0;
          EA=1;
     }  
    else if(!ST && ! SET_TX) 
     {
     	 //í¨D?×′ì?
	 	 //í¨D?£-£-′??ú1
     	 SET_SC=0;
     	 SET_TX=1; 
     	 SP322E_ON();
     	 ES=0;
     	 ES1=1;
         EA=1;
     } 
 }

/*****************************************
1|?ü£o?D???óê?é?2?°?·¢à′μ?êy?Y
?èê?£o?ú4o??D??à?£??óê?é?2?°?·¢à′μ??ì2aêy?Y£?
      ?D±eoó′?′¢?úFLASH′?′¢?÷à?
2?êy£o
·μ??£o
±?×¢£o′??ú0
******************************************/

void receive4top(void) interrupt 4 using 2
 {
    uchar i=0,checksum=0,temp=0;
    
	ES=0;EA=0;
    
    if(!RI)	{ TI=0;goto ExitReceive; }
    RI=0;
  
  if(SBUF==0xFF) 
   {
	RunTime0();//???ˉ?¨ê±?÷£?·à?1′í?ó?àμè′y
    
    while(!RI & !TF0) watchdog();
    if(TF0)	goto ExitReceive;
    RI=0;
    if(SBUF!=0xFF) goto ExitReceive;
    
    while(!RI & !TF0) watchdog();
    if(TF0)	goto ExitReceive;
    RI=0;
    if(SBUF!=0xFF) goto ExitReceive;
    
	while(i<6)
     {
       while(!RI & !TF0) watchdog();
       if(TF0)	goto ExitReceive;
       RI=0; 
       ReceiveBuff[i++]=SBUF;
      }
    CloseTime0();
    for(i=0;i<5;i++)
     {
       temp=ReceiveBuff[i];
       FlashBuff[FlashSerial][i]=temp;
       checksum+=temp;	
     }
	 
	checksum+=ReceiveBuff[5];

    if(checksum==0)
      {
      	 if(++FlashNum>5)    FlashNum=5;
      	 if(++FlashSerial>4) FlashSerial=0;
	 }	
   }
  else if(SBUF==0xEE)
   {
    RunTime0();//???ˉ?¨ê±?÷£?·à?1′í?ó?àμè′y
    
    while(!RI & !TF0) watchdog();
    if(TF0)	goto ExitReceive;
    RI=0;
    if(SBUF!=0xEE) goto ExitReceive;
    
    while(!RI & !TF0) watchdog();
    if(TF0)	goto ExitReceive;
    RI=0;
    if(SBUF!=0xEE) goto ExitReceive;
    
	while(i<6)
     {
       while(!RI & !TF0) watchdog();
       if(TF0)	goto ExitReceive;
       RI=0; 
       ReceiveBuff[i++]=SBUF;
      }
    CloseTime0();
    /*
	for(i=0;i<5;i++)
     {
       temp=ReceiveBuff[i];
       //FlashBuff[FlashSerial][i]=temp;
       checksum+=temp;	
     }
	 
	checksum+=ReceiveBuff[5];

    if(checksum==0)
      {
      	 if(++FlashNum>5)    FlashNum=5;
      	 if(++FlashSerial>4) FlashSerial=0;
      }
	*/
   }
  ExitReceive: if(TR0) CloseTime0(); 
               ES=1;EA=1;
  watchdog();       
 }
/*****************************************
1|?ü£o?D???óê??????úí¨D??üá?
?èê?£o?óê?μ?í¨D??üá?£??íêy?Yμ??????ú£??ò2á3y????′?′¢?÷?£
2?êy£o
·μ??£o
±?×¢£o×¢òaà??1£o1?£2á3yê±°′ò3???í×′ì?£???ê?2á3y???è
                2?£?a?ú?é2é′?′¢?÷èYá??¢ê£óàèYá?
                
******************************************/
void send2pc(void) interrupt 7  using 2
 {
   uchar data i=0;
   uchar idata checksum=0,temp=0;
   ES1=0;EA=0;
   
   if(!RI_1)	{ TI_1=0;   goto ExitSend2PC; }
   RI_1=0;
   
   if(SBUF1==0x65)  
   {
      //2é?ˉêy×é??
	  CheckFiles=1; 
    }
   else if(SBUF1==0x66) 
    {
       //ê?è????tDòo?
       RunTime0();
	   while(!RI_1 && !TF0);
       if(TF0) { CloseTime0(); goto ExitSend2PC; }
       RI_1=0;
       file_index=SBUF1;	
       CloseTime0();
	   CountAdd=1;
    }
   else  if(SBUF1==0x7F) 
    {
       //í¨D??aê?
	   RunTime0();
	   while(!RI_1 && !TF0);
       if(TF0) { CloseTime0(); goto ExitSend2PC; }
       RI_1=0;
       array_size=SBUF1;	
       CloseTime0();
	   PcAskMcu=1; //?????úòa?óé?′?êy?Y
   	} 
   else if(SBUF1==0xF7)
    {
	   //?áê?í¨D?
	   RunTime0();
	   while(!RI_1 && !TF0);
       if(TF0) { CloseTime0(); goto ExitSend2PC; }
       RI_1=0;
       i=SBUF1;	
       CloseTime0();
	   if(i==0xF8) { Send2PC(0x7D); }
	}
   else  if(SBUF1==0xF2)
    {
       RunTime0();
	   while(!RI_1 && !TF0);
       if(TF0) { CloseTime0(); goto ExitSend2PC; }
       RI_1=0;
       i=SBUF1;	
       CloseTime0();
       if(i==0xF3) { EraseAllBlock=1; }  //2á3y′?′¢?÷
	}    
   else  if(SBUF1==0xF0)
    {
	   RunTime0();
	   while(!RI_1 && !TF0);
       if(TF0) { CloseTime0(); goto ExitSend2PC; }
       RI_1=0;
       i=SBUF1;	
       CloseTime0();
       if(i==0xF1)  { AskSysInfo=1; }   //2é?ˉ2?êy
	}
   else 
    {
       i=SBUF1+1;   
	   Send2PC(0xFD);
	   Send2PC(i);	
       CloseTime0();
    }
  ExitSend2PC: ES1=1;EA=1;
  watchdog();
 }

/*****************************************
1|?ü£o?áFLASH
?èê?£o′óFLASHà??á3?ò?×éêyà′
2?êy£o1?£?aê?μ??·ò??a£??áê?μ??·ò??a?£2
·μ??£o
±?×¢£o
******************************************/
void read_flash(void)
 {
   uchar i=0;
   uchar buf[5];
   Nf_read_start(); //×?o??áêy×?±??£
   Nf_wait_busy();
   i=0;
   while(i<5)
     {
          ReceiveBuff[i++]=Nf_rd_byte();
     }
   
   rd_Column_Address+=5;          //??′?′??í5bytes
   if(rd_Column_Address>=250)     //μ??·D???
    {
            if(rd_Area==0)
               { 
                 //A--> B
			     rd_Area=1;
                 rd_Column_Address=0;
               }   
             else if(rd_Area==1)     
               {
                 rd_Area=0;
                 rd_Column_Address=0;
				 rd_L_Row_Address++;
				 if(rd_L_Row_Address%0x20==0)
				  {
				    if(rd_L_Row_Address==0) { rd_H_Row_Address++; }
                    if(bad_block)
					 {
					   //?ì2éê?·??μ?é
                       L_Row_Address=rd_L_Row_Address;
                       H_Row_Address=rd_H_Row_Address;
					ck:Area=0x50;
					   Column_Address=0;
                       Nf_ready_read();
	                   Nf_wait_busy();
	                   for(i=0;i<5;i++)
					    { buf[i]=Nf_rd_byte(); }
					  
                       if(buf[0]!=0xAA && (buf[4]!=0xEE && buf[4]!=0xFF))
					    {
						  L_Row_Address+=0x20;
						  if(L_Row_Address==0) H_Row_Address++;
						  if(H_Row_Address==0) lastblock=1;
						  goto ck;
						}
					   else
					    {
						  //D?μ??áêyμ??·?£
						  rd_L_Row_Address=L_Row_Address;
                          rd_H_Row_Address=H_Row_Address;
						  rd_Column_Address=0;
						  rd_Area=0;
						}
					 }//end if(bad_block)

				  }//end if(rd_l_row_address%20==0)
               }//end else if(rd_area==1)
    }//end if(rd_column_address>=250)
 } 
/*****************************************
1|?ü£oD′′?′¢?÷
?èê?£oD′è?′?′¢?÷£?·??aê?êy?Y?1ê?±ê??í·
2?êy£o
·μ??£o
±?×¢£o
******************************************/
void write_flash(void)
{
   uchar i=0;
   
   if(!firstdata)      { write_header();}  //ê×?èD′???tí·D??¢?£
   write_data();
   
   //?????o3???£?μ??·?????ó1
   while(i<5)          FlashBuff[FlashIndex][i++]=0;//?????o3???
   if(--FlashNum>5)    FlashNum=0;
   if(++FlashIndex>4)  FlashIndex=0;
}
/*****************************************
1|?ü£oD′FLASH
?èê?£o??ò?×éêy?Y′?′¢μ?FLASHà?
2?êy£oArea,Column,L_Row,H_Rowò??a£?D′íêoó?3DòμY???£
·μ??£o
±?×¢£o
******************************************/
void write_data(void)
 {
    uchar data i=0;
	uint  data Block_Add=0;
    uchar idata tmp_buf[5];
    do
     {
     	Nf_ready_write();
   	    
    	while(i<5)
     	{
       	   Nf_wr_byte(FlashBuff[FlashIndex][i++]);
     	}
        
        Nf_write_start();
        DelayXms(1);
        watchdog();
        Nf_wait_busy();
     } while(!nf_check_status());
   
    if(new_block)
	 {
	   //D′è??éê1ó?±ê?????
	   do
 		{
		  Area=0x50;
		  Column_Address=0;
          Nf_ready_write();
		  Nf_wr_byte(0xAA);
          Nf_write_start();
          DelayXms(1);
          watchdog();
          Nf_wait_busy();
        } while(!nf_check_status());   
       new_block=0;
	   Area=0;
	   Column_Address=0;
	 }

   Column_Address+=5;
   if(Column_Address==250)  
    {
     if(Area==0)
      {
         //A??D′?ú±ê??
	 	 do
 		  {
		  	Area=0x50;
		  	Column_Address=2;
          	Nf_ready_write();
		  	Nf_wr_byte(0xCC);
          	Nf_write_start();
          	DelayXms(1);
          	watchdog();
          	Nf_wait_busy();
          } while(!nf_check_status());
         
	     Column_Address=0;
         Area=1; //A?ú£????????òB??  £-£-£-??ó?μ?250£?250£?500??×??ú
      }
     else if(Area==1)
      { 
         //Bò3?ú±ê??
	     //B area full flag
	     do
 		  {
		  	Area=0x50;
		  	Column_Address=3;

⌨️ 快捷键说明

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