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

📄 89c51prodlg.cpp

📁 单片机超级精华包.适合有一定基础的人,里面程序都较难,还包括一篇毕业设计,温度监控系统的设计.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 89c51ProDlg.cpp : implementation file
//

#include "stdafx.h"
#include "89c51Pro.h"
#include "89c51ProDlg.h"
#include "winio.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#define P3_6	6
#define P3_7	7
#define P2_7	0
#define P3_4	0
#define PROG	1
#define Vpp		2
UINT DrawState(LPVOID pParam)
{
	CMy89c51ProDlg * pdlg=(CMy89c51ProDlg *)pParam;
	CWnd* wnd=pdlg->GetDlgItem(IDC_pic);
	CClientDC dc(wnd);
	CPoint ptOrigin(0,0);
	for(;;)
	{
		pdlg->pic[0].Draw(&dc,ptOrigin);
		Sleep(50);	
		pdlg->pic[1].Draw(&dc,ptOrigin);
		Sleep(50);
		pdlg->pic[2].Draw(&dc,ptOrigin);
		Sleep(50);
	}
	return 0;
}
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy89c51ProDlg dialog

CMy89c51ProDlg::CMy89c51ProDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy89c51ProDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy89c51ProDlg)
	m_LPT = _T("LPT1(0x378)");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy89c51ProDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy89c51ProDlg)
	DDX_CBString(pDX, IDC_COMBO, m_LPT);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy89c51ProDlg, CDialog)
	//{{AFX_MSG_MAP(CMy89c51ProDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy89c51ProDlg message handlers

BOOL CMy89c51ProDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	ShutdownWinIo();
	InitializeWinIo();
	CPictureHolder myTmpPicture,myTmpPicture1,myTmpPicture2;
	myTmpPicture.CreateFromBitmap(IDB_BITMAP3);
	pic[0].Create32BitFromPicture(&myTmpPicture,40,40);
	myTmpPicture1.CreateFromBitmap(IDB_BITMAP4);
	pic[1].Create32BitFromPicture(&myTmpPicture1,40,40);
	myTmpPicture1.CreateFromBitmap(IDB_BITMAP5);
	pic[2].Create32BitFromPicture(&myTmpPicture1,40,40);
	BYTE MarkBitMapx[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
	BYTE MarkBitMapy[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
    memcpy(MarkBitMap0,MarkBitMapx,8);
	memcpy(MarkBitMap1,MarkBitMapy,8);
	memset(output,0,4097);
	memset(input,0,4097);

	hFile=NULL;
	hFile1=NULL;
	ADDR8139=0x0000;
	EPPADDR=0x278;
	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);
	pss=AfxBeginThread(DrawState,this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
	

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMy89c51ProDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		ShutdownWinIo();
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMy89c51ProDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMy89c51ProDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

int CMy89c51ProDlg::SetBit(UCHAR *data, int BitCount,int BitValue)
{
	if(BitValue!=0&&BitValue!=1)
		return 1;
	if(BitValue==0)
	{
		(*data)=(*data)&MarkBitMap0[BitCount];
	}
	else
	{
		(*data)=(*data)|MarkBitMap1[BitCount];
	}
	return 0;

}

int CMy89c51ProDlg::GetBit(UCHAR *data, int BitCount)
{
	if(BitCount>7)
		return 2;
	return ((*data)&MarkBitMap1[BitCount])>>BitCount;

}

/*void CRwDlg::OnButton7() 
{
AfxBeginThread(commx, this , THREAD_PRIORITY_NORMAL);	// TODO: Add your control notification handler code here
	
}
*/
int CMy89c51ProDlg::SaveCStringToFile(CString str)
{
	DWORD dwLength=0;
TCHAR  strPath[511];
GetModuleFileName(NULL,strPath,MAX_PATH);
TCHAR *str1,*str2;//建立out.lcd工作路径
str1=strPath;
str2=str1;
for(;str2!=NULL;)
{
	str1=str2+1;
    str2=strchr(str1,'\\');

}
	strcpy(str1,"烧写结果信息.txt");		   
	if(hFile1==NULL)
	{
		 hFile1 = CreateFile(                   //建立并打开文件
				   strPath,          // open MYFILE.TXT 
               GENERIC_READ|GENERIC_WRITE,              // open for reading 
                FILE_SHARE_WRITE,           // share for reading 
                NULL,                      // no security 
                CREATE_ALWAYS,             // existing file only 
                FILE_ATTRIBUTE_NORMAL,     // normal file 
                NULL);                     // no attr. template 
 
		if (hFile1 == INVALID_HANDLE_VALUE) 
		{ 
			AfxMessageBox("Could not creat *.*.");   // process error 
		} 
		WriteFile(hFile1,str.GetBuffer(str.GetLength()),str.GetLength(),&dwLength,NULL);
 		return 0;
	}
	WriteFile(hFile1,str.GetBuffer(str.GetLength()),str.GetLength(),&dwLength,NULL);
    return 1;
		
}

int CMy89c51ProDlg::WriteFlash(DWORD ADDR, UCHAR DATA)//写89C51某地址数据
{
	UCHAR ADDRL=0,ADDRH=0,Cortrol=0;
	DWORD State=0,buf32=0;
	ADDRL=ADDR;
	ADDRH=(ADDR>>8);
	SetBit(&ADDRH,6,1);
	SetBit(&ADDRH,7,1);
	SetPortVal(EPPADDR, 3, 1);//P2.7 ALE/PROG =H VPP=H
	Sleep(0);
	buf32=DATA;//output address and datta lines
	buf32=(buf32<<16)&0xffffff00;
	buf32=buf32|ADDRH;
	buf32=(buf32<<8)&0xffffff00;
	buf32=buf32|ADDRL;
	SetPortVal(ADDR8139+0xd4, buf32, 4);
	Sleep(0);
	SetPortVal(EPPADDR, 7, 1);//P2.7 ALE/PROG =H VPP=12V
	Sleep(0);
	//output 1 pulse to PROG
	SetPortVal(EPPADDR, 5, 1);//P2.7 ALE/PROG =L VPP=12V
	Sleep(1);
	SetPortVal(EPPADDR, 7, 1);//P2.7 ALE/PROG =H VPP=12V
	Sleep(2);
	SetPortVal(EPPADDR, 3, 1);//P2.7 ALE/PROG =H VPP=H
	Sleep(0);

return 0;
}

UCHAR CMy89c51ProDlg::ReadFlash(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,1);
	SetBit(&ADDRH,7,1);
	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(0);
//	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);

}
UINT Read(LPVOID pParam)//读89C51线程
{
	HANDLE hFile=NULL;
	DWORD dwLength=0;
	CString path;
  	CMy89c51ProDlg * pdlg=(CMy89c51ProDlg *)pParam;
	CWnd* wnd=pdlg->GetDlgItem(IDC_STATIC);
	DWORD FlashLength=4096;
	CString str;
	for(DWORD ADDR=0;ADDR<FlashLength;ADDR++)
	{
		pdlg->input[ADDR]=pdlg->ReadFlash(ADDR);
		str.Format("ADDR=%X,\tDATA=%X",ADDR,pdlg->input[ADDR]);
		wnd->SetWindowText(str);
		Sleep(0);
	}
	CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"二进制文件(*.bin)|*.bin",NULL);
	if(IDOK==dlg.DoModal())
	{ 
		 path=dlg.GetPathName();
		 hFile = CreateFile(                   //建立并打开文件
				path,           // open MYFILE.TXT 
                GENERIC_READ|GENERIC_WRITE,              // open for reading 
                FILE_SHARE_WRITE,           // share for reading 
                NULL,                      // no security 
                CREATE_ALWAYS,             // existing file only 
                FILE_ATTRIBUTE_NORMAL,     // normal file 
                NULL);                     // no attr. template 
 
		if (hFile == INVALID_HANDLE_VALUE) 
		{ 
			AfxMessageBox("Could not creat *.bin");   // process error 
		} 
		WriteFile(hFile,pdlg->input,4096,&dwLength,NULL);
	}
	CloseHandle(hFile);
	hFile=NULL;
	//CButton* wnd1=(CButton*)pdlg->GetDlgItem(IDC_BUTTON1);
	//wnd1->EnableWindow(TRUE);
	wnd->SetWindowText("读89C51结束");
	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 Write(LPVOID pParam)//写89C51线程
{
	CMy89c51ProDlg * pdlg=(CMy89c51ProDlg *)pParam;
	CWnd* wnd=pdlg->GetDlgItem(IDC_STATIC2);
	DWORD FlashLength=4096;

⌨️ 快捷键说明

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