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

📄 89c51prodlg.cpp

📁 单片机源程序超级精华包,好多资料
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	CString str,path;
	DWORD dwLength=0;
	int i=0,k=0;
	TCHAR strbuf[32];
	HANDLE hFile;
	strbuf[2]=0;
	DWORD j=0;
	CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"二进制文件(*.bin)|*.bin",NULL);
	if(IDOK==dlg.DoModal())
	{ 
		 path=dlg.GetPathName();
//AfxMessageBox(path);
		 hFile = CreateFile(                   //建立并打开文件
				   path,           // open MYFILE.TXT 
               GENERIC_READ|GENERIC_WRITE,              // open for reading 
                FILE_SHARE_WRITE,           // share for reading 
                NULL,                      // no security 
                OPEN_EXISTING,             // existing file only 
                FILE_ATTRIBUTE_NORMAL,     // normal file 
                NULL);                     // no attr. template 
 
		if (hFile == INVALID_HANDLE_VALUE) 
		{ 
			AfxMessageBox("Could not open *.jmu.");   // process error 
		} 
		else
		{
			DWORD sizeH;
			DWORD sizeL=GetFileSize(hFile,           // handle to file
			&sizeH  // high-order word of file size
			);
			if(sizeL>4096)
			{
				AfxMessageBox("*.bin文件不能大于4k");
				pdlg->GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
				pdlg->GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);
				pdlg->GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE);
				pdlg->GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);
				pdlg->GetDlgItem(IDC_BUTTON7)->EnableWindow(TRUE);
				return 0;
			}
			ReadFile(hFile,pdlg->output, sizeL, &dwLength, NULL) ;
			if(dwLength!=sizeL)
			{
				AfxMessageBox("读文件出错!");
				pdlg->GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
				pdlg->GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);
				pdlg->GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE);
				pdlg->GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);
				pdlg->GetDlgItem(IDC_BUTTON7)->EnableWindow(TRUE);
				return 0;
			}
			FlashLength=dwLength;
			CloseHandle(pdlg->hFile1);
			pdlg->hFile1=NULL;
			str.Format("89C51PRO\r\n版本号: 1.1\r\n本软件由桃源村软件制作\r\n本软件可免费使用并可任一修改源代码,但请不要用于商业目的,\r\nE-mail:yds_086@163.com\r\n-------------------------------------------------------------------\r\n\r\n\r\nBIN文件信息:烧录字节数%d.\r\n烧写开始地址:0x00\r\n",FlashLength);
			pdlg->SaveCStringToFile(str);
			Sleep(1000);
			pdlg->pss->ResumeThread();
			for(DWORD ADDR=0;ADDR<=FlashLength;ADDR++)
			{
				pdlg->WriteFlash(ADDR,pdlg->output[ADDR]);
				for(i=0;i<5;i++)
				{
					for(k=0;k<3;k++)
					{
						pdlg->input[ADDR]=pdlg->ReadFlash(ADDR);
						if(pdlg->input[ADDR]==pdlg->output[ADDR])
						{
							i=23;
							k=23;
						}
					}
					if(pdlg->input[ADDR]!=pdlg->output[ADDR])
					{
	 					pdlg->WriteFlash(ADDR,pdlg->output[ADDR]);
					}
					if(i==4)
					{
						str.Format("写地址0x%X失败,写数据0x%X,效验数据0x%X\r\n",ADDR,pdlg->output[ADDR],pdlg->input[ADDR]);
						pdlg->SaveCStringToFile(str);
					}
				}
				str.Format("ADDR=0x%X,DATA=0x%X",ADDR,pdlg->input[ADDR]);
				wnd->SetWindowText(str);
				Sleep(0);
		}
		str.Format("烧录结束:\r\n");
		pdlg->SaveCStringToFile(str);
		CloseHandle(pdlg->hFile1);
		pdlg->hFile1=NULL;
		}
		wnd->SetWindowText("烧录结束");
	}
	pdlg->pss->SuspendThread();
	pdlg->GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
	pdlg->GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);
	pdlg->GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE);
	pdlg->GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);
	pdlg->GetDlgItem(IDC_BUTTON7)->EnableWindow(TRUE);
	return 0;
}
UINT WriteHEX(LPVOID pParam)//写89C51线程
{
	CMy89c51ProDlg * pdlg=(CMy89c51ProDlg *)pParam;
	CWnd* wnd=pdlg->GetDlgItem(IDC_STATIC2);
	HEXBIN hex;
	DWORD FlashLength=0;
	CString str,path;
	DWORD dwLength=0;
	int i=0,k=0;
	TCHAR strbuf[32];
	HANDLE hFile;
	strbuf[2]=0;
	DWORD j=0;
	CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"十六进制文件(*.hex)|*.hex",NULL);
	if(IDOK==dlg.DoModal())
	{ 
		 path=dlg.GetPathName();
//AfxMessageBox(path);
		 UINT succ=hex.LoadHEXFile(path.GetBuffer(path.GetLength()));
		 if(succ==0)
		 {
			 AfxMessageBox("HEX文件格式错误");
			 pdlg->GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
		   	 pdlg->GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);
			 pdlg->GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE);
			 pdlg->GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);
			 pdlg->GetDlgItem(IDC_BUTTON7)->EnableWindow(TRUE);
			 return 0;
		 }
		if(hex.GetMaxAddr()>4096)
		{
			AfxMessageBox("*.hex文件不能大于4k");
			pdlg->GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
			pdlg->GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);
			pdlg->GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE);
			pdlg->GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);
			pdlg->GetDlgItem(IDC_BUTTON7)->EnableWindow(TRUE);
			return 0;
		}
	
		FlashLength=hex.GetMaxAddr();
		Data * pData=hex.GetNextData();
		CloseHandle(pdlg->hFile1);
		pdlg->hFile1=NULL;
		str.Format("89C51PRO\r\n版本号: 1.1\r\n本软件由桃源村软件制作\r\n本软件可免费使用并可任一修改源代码,但请不要用于商业目的,\r\nE-mail:yds_086@163.com\r\n-------------------------------------------------------------------\r\n\r\n\r\nHEX文件信息: 烧写数据数目%d.\r\n烧写开始地址:0x%X\r\n",hex.GetHexDataSize(),pData->address);
		pdlg->SaveCStringToFile(str);
		Sleep(500);
		pdlg->pss->ResumeThread();
		for(DWORD ADDR=pData->address,fag=0;pData->DataEOF==NODEOF||fag==0;hex.GetNextData())
		{
			ADDR=pData->address;
			if(pData->DataEOF==DEOF)
			{
				fag=1;
			}
			pdlg->WriteFlash(ADDR,pData->data);
				for(i=0;i<5;i++)
				{
					for(k=0;k<3;k++)
					{
						pdlg->input[ADDR]=pdlg->ReadFlash(ADDR);
						if(pdlg->input[ADDR]==pData->data)
						{
							i=23;
							k=23;
						}
					}
					if(pdlg->input[ADDR]!=pData->data)
					{
	 					pdlg->WriteFlash(ADDR,pData->data);
					}
					if(i==4)
					{
						str.Format("写地址0x%X失败,写数据0x%X,效验数据0x%X\r\n",ADDR,pData->data,pdlg->input[ADDR]);
						pdlg->SaveCStringToFile(str);
					}
				}
				str.Format("ADDR=0x%X,DATA=0x%X",ADDR,pdlg->input[ADDR]);
				wnd->SetWindowText(str);
				Sleep(0);
		}
		str.Format("烧录结束地址0x%X\r\n烧录结束\r\n",ADDR);
		pdlg->SaveCStringToFile(str);
		CloseHandle(pdlg->hFile1);
		wnd->SetWindowText("烧录结束");
		pdlg->hFile1=NULL;
		pdlg->pss->SuspendThread();
		pdlg->GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
		pdlg->GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);
		pdlg->GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE);
		pdlg->GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);
		pdlg->GetDlgItem(IDC_BUTTON7)->EnableWindow(TRUE);
		return 0;
	}
	else
	{
		pdlg->GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
		pdlg->GetDlgItem(IDC_BUTTON2)->EnableWindow(TRUE);
		pdlg->GetDlgItem(IDC_BUTTON3)->EnableWindow(TRUE);
		pdlg->GetDlgItem(IDC_BUTTON4)->EnableWindow(TRUE);
		pdlg->GetDlgItem(IDC_BUTTON7)->EnableWindow(TRUE);
		return 0;
	}
}

void CMy89c51ProDlg::OnButton1() 
{
	AfxBeginThread(Read, this , THREAD_PRIORITY_NORMAL);// TODO: Add your control notification handler code here
	GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON4)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON7)->EnableWindow(FALSE);
	
}

void CMy89c51ProDlg::OnButton2() 
{
	AfxBeginThread(Write, this , THREAD_PRIORITY_NORMAL);// TODO: Add your control notification handler code here
	GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON4)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON7)->EnableWindow(FALSE);
}

void CMy89c51ProDlg::OnButton3() 
{
	CWnd* wnd=GetDlgItem(IDC_STATIC1);
	CWnd* wnd1=GetDlgItem(IDC_STATIC3);
	CString str;
	str.Format("30H=%X,\t31H=%X,\t32H=%X",ReadSigNature(0x30),ReadSigNature(0x31),ReadSigNature(0x32));
	wnd->SetWindowText(str);
	if(ReadSigNature(0x30)==0x1e&&ReadSigNature(0x31)==0x51&&ReadSigNature(0x32)==0xff)
	{
		wnd1->SetWindowText("读特征字成功");
	}
	else
	{
		wnd1->SetWindowText("读特征字失败");
	}

	// TODO: Add your control notification handler code here
	
}

UCHAR CMy89c51ProDlg::ReadSigNature(DWORD ADDR)//读特征字
{
	UCHAR ADDRL=0,ADDRH=0,Cortrol=0,State=0;
	DWORD DATA0=0,DATA1=1,buf32=0;
	ADDRL=ADDR;
	ADDRH=ADDR>>8;
	SetBit(&ADDRH,6,0);
	SetBit(&ADDRH,7,0);
	buf32=0xff;
	buf32=(buf32<<16)&0xffffff00;
	buf32=buf32|ADDRH;
	buf32=(buf32<<8)&0xffffff00;
	buf32=buf32|ADDRL;
	SetPortVal(ADDR8139+0xd4, buf32, 4);//output address lines
	Sleep(1);
//	SetPortVal(ADDR8139+0xd5, ADDRH, 1); 
//	Sleep(1);
	SetPortVal(EPPADDR, 2, 1);//P2.7=L ALE/PROG =H VPP=H
	Sleep(0);
	for(;DATA0!=DATA1;)
	{
		GetPortVal(ADDR8139+0xd7, &DATA0, 1);
		Sleep(0);
		GetPortVal(ADDR8139+0xd7, &DATA1, 1);
		Sleep(0);
	}

	return (UCHAR)DATA0;
}

int CMy89c51ProDlg::EraseChip()//擦除芯片
{
	SetPortVal(ADDR8139+0xd5, 0x0, 1);//Set p3.6 p3.7 to H;
	Sleep(1);
	SetPortVal(EPPADDR, 6, 1);
	Sleep(1);
	SetPortVal(EPPADDR, 4, 1);//output 1 pulse to PROG
	Sleep(100);
	SetPortVal(EPPADDR, 6, 1);
	Sleep(1);
	SetPortVal(EPPADDR, 2, 1);

	return 0;

}
int HextoDec1(TCHAR * p)
{
int r = 0;
TCHAR *str=p;
while(*str)
{
r = r<<4;
if((*str| 0x20 ) >= 'a' &&
(*str | 0x20) <= 'f')
r += ((*str| 0x20 ) - 'a' 
+ 10 
);
else
if(*str >= '0' &&
*str <= '9')
{
r += ((*str ) - '0');
}
str++;
}
return r;
}
void CMy89c51ProDlg::OnButton4() //擦除芯片
{
	EraseChip();// TODO: Add your control notification handler code here
	
}
void CMy89c51ProDlg::LoadConfinfo()//加载配置信息
{
PCIConfig fig;
if(fig.GetPCIConfig(0x10ec,0x8139)==1)
{
	ADDR8139=(fig.PCIConf.BaseAddressRegister[0])&0xfffffffe;
}
else
{
	ADDR8139=0xffff;
	AfxMessageBox("没有找到rtl8139");
	
}
TCHAR * p=m_LPT.GetBuffer(m_LPT.GetLength());
this->UpdateData(TRUE);
if(p[3]=='1')
{
	EPPADDR=0x378;
}
if(p[3]=='2')
{
	EPPADDR=0x278;
}

}



void CMy89c51ProDlg::OnButton5() 
{
		pss=AfxBeginThread(DrawState,this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
	// TODO: Add your control notification handler code here
}

void CMy89c51ProDlg::OnButton6() 
{
	CString path;
	HEXBIN hex;
	CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"intel十六制文件(*.hex)|*.hex",NULL);
	if(IDOK==dlg.DoModal())
	{ 
		 path=dlg.GetPathName();
		 UINT succ=hex.LoadHEXFile(path.GetBuffer(path.GetLength()));
		 if(succ==0)
		 {
			 AfxMessageBox("HEX文件格式错误,文件转换失败");
			 return;
		 }
		 // TODO: Add your control notification handler code here
	}
	else
	{
		return;
	}
	CFileDialog dlg1(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"二进制文件(*.bin)|*.bin",NULL);
	if(IDOK==dlg1.DoModal())
	{ 
		 path=dlg1.GetPathName();
		 UINT succ=hex.HEXTOBIN(path.GetBuffer(path.GetLength()));
		 if(succ==0)
		 {
			 AfxMessageBox("文件转换错误");
		 }
		 // TODO: Add your control notification handler code here
	}

}

void CMy89c51ProDlg::OnButton7() 
{
	AfxBeginThread(WriteHEX, this , THREAD_PRIORITY_NORMAL);
	GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON3)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON4)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON7)->EnableWindow(FALSE);
}

void CMy89c51ProDlg::OnButton8() 
{
	this->LoadConfinfo();
	CWnd* wnd=GetDlgItem(IDC_STATIC9);
	CString str;
	str.Format("rtl8139 I/O基地址:\t0x%X\nEPP 并口地址:\t\t0x%X",ADDR8139,EPPADDR);
	wnd->SetWindowText(str);// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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