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

📄 main.c

📁 一个使用USB下载51单片机程序的源程序
💻 C
字号:
#include <at89x52.h>
#include <intrins.h>
#include "common.h"
#include "pdiusbd12.h"
#include "Int_D12.h"
#include "AT89S52.h"
BYTE page;

void SystemInitial ()
{
 EA=0;
 PSW=0;
 P0=P1=P2=P3=0xFF;
 Ret_I();
 Ret_I();
 Delay_ms(100);
 Init_D12();									// Initialize USB chipset
 IE=0x81;
 B_RST=0;
 programed=0;	
}

void main(void)
{
	BYTE i;
	WORD p;

    SystemInitial ();
	while(1){	
		if(b_rx_done){
            //FlashLight ();

			DISABLE_USBINT						// Disable USB interrupt
			for(i=0; i<BUF_LEN; i++)
			   buf[i]=rx_buf[i];
			b_rx_done=0;
			ENABLE_USBINT						// Enable USB interrupt
			switch(buf[0]){
			case 0:	// 退出编程模式,目标CPU复位
				Reset_Target();
				Send_Data();
				break;
			case 1:	// 允许串行编程
                device=buf[6];
                if (programed)
                 {tx_buf[0]=1; tx_buf[1]=0xAA; tx_buf[2]=0xAA; Send_Data(); break;}
                Enable_Program();
				Send_Data();
				break;
			case 2:	// 芯片擦除
                if (!programed)                                 //芯片未进入编程模式
                  {
                   if (!Enable_Program())                       //芯片不能进入编程状态
                     {tx_buf[0]=2; tx_buf[1]=0xAA; tx_buf[2]=0xAA; Send_Data(); break;}
                   }  
				Chip_Erase();
				Send_Data();
				break;
			case 3:	// 字节方式读
				for(i=0; i<4; i++)
					tx_buf[i]=buf[i];
				p=buf[3]*256+buf[2];
				for(i=0; i<buf[1]; i++){
					tx_buf[i+4]=Flash_ReadByte(p);
					p++;
				}
				Send_Data();
				break;
			case 4:	// 字节方式写
				p=buf[3]*256+buf[2];
                switch (device){
                  case 0:
                  case 1:
				    for(i=0; i<buf[1];i++){
					  Flash_WriteByte(p, buf[i+4]);
					  p++;
                      //tx_buf[i]=buf[i];
				      }
                    tx_buf[0]=4;  tx_buf[1]=0xEE;  tx_buf[2]=0xEE;
				    Send_Data();
				    break;
                  case 2:
                    break;
                }
			case 5:	// 写加密位
				Write_LockBit(buf[1]);
				tx_buf[0]=5;  tx_buf[1]=0xEE;  tx_buf[2]=0xEE;
				Send_Data();
				break;
			case 6:	// 读加密位
				break;
			case 7:	// 读电子标签
				break;
			case 8:	// 页方式读
				break;
			case 9:	// 页方式写
                Flash_WritePage (buf[1],buf[2]);//buf[1]:mode,buf[2]:page,(ATMEGA8L:buf[3]->6位页内地址)
                tx_buf[0]=9;  tx_buf[1]=0xEE;  tx_buf[2]=0xEE;
				Send_Data();
				break;
            /*
            case 10:
                count=0;
	            for (page=0;page<128&&count<addrMax;page++){
		          pageaddr=count/64;
		          bCommand[2]=0;
		          bCommand[3]=pageaddr;
		          for (i=0;i<2;i++){
		            addrinpage=count%64;
		            bCommand[4]=addrinpage;
		            for (j=5;j<37&&count<addrMax;j++)
			          bCommand[j]=data[count++];
		            MyWriteFile(DeviceNo,bCommand, rbuf,1);
		            bCommand[2]=1;
		        }
            */
			}
		}
	}

}

⌨️ 快捷键说明

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