📄 isp2dlg.cpp
字号:
// ISP2Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "ISP2.h"
#include "ISP2Dlg.h"
#include "winio.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CISP2Dlg dialog
CISP2Dlg::CISP2Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CISP2Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CISP2Dlg)
m_FileName = _T("");
m_sign = _T("02:02:06");
m_num = 0;
m_Instruction = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CISP2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CISP2Dlg)
DDX_Control(pDX, IDC_PROGRESS, m_Progress);
DDX_Text(pDX, IDC_FILENAME, m_FileName);
DDX_Text(pDX, IDC_SIGN, m_sign);
DDX_Text(pDX, IDC_NUM, m_num);
DDX_Text(pDX, IDC_INSTRUCTION, m_Instruction);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CISP2Dlg, CDialog)
//{{AFX_MSG_MAP(CISP2Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ERAS, OnEras)
ON_BN_CLICKED(IDC_PROG, OnProg)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_BN_CLICKED(IDC_READSIGN, OnReadSign)
ON_BN_CLICKED(IDC_FILEOPEN, OnFileOpen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CISP2Dlg message handlers
BOOL CISP2Dlg::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
m_Progress.SetRange(0,1000);
m_Progress.SetPos(0);
m_Progress.SetStep(10);
// TODO: Add extra initialization here
bool bResult;
bResult = InitializeWinIo();
if (!bResult)
{
AfxMessageBox("初始化并口I/O失败!");
exit(1);
}
SetPortVal(0x37A, 0X04, 1); //对话框开始运行时发出复位信号
SetPortVal(0x37A, 0X00, 1);
m_nPinMosi=14;//控制MOSI所用的并口引脚
m_nPinMiso=15;//控制MISO所用的并口引脚
m_nPinRst=16;//控制RST所用的并口引脚
m_nPinSck=1;//控制SCK所用的并口引脚
m_nPinLe=17;//控制器件锁存所用的并口引脚
return TRUE; // return TRUE unless you set the focus to a control
}
void CISP2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
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 CISP2Dlg::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 CISP2Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CISP2Dlg::OnEras()
{
PreparePro();
Erase();
ProOver();
BeepOk();
}
void CISP2Dlg::OnProg() //当点击编程按钮时调用此程序
{
CStdioFile m_File; //读.HEX文件用
CString str;
int addr,pos=0;
BYTE prodata;
m_Progress.SetPos(0);
m_num=0;
if(m_FileName=="")
{
MessageBox("请先选择要下载的文件!");
CString str;
CFileDialog fileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"*.hex|*.HEX",NULL);
fileDialog.DoModal();
str=fileDialog.GetPathName();
m_FileName=str;
};
m_File.Open(m_FileName,CFile::modeRead);
PreparePro(); //
Erase();
while(m_File.ReadString(str)) // 每次循环读一行
{
if(Char2ToByte(str.Mid(1,2))!=0) // 判是否是最后一行
{
addr = Char4ToByte(str.Mid(3,4)); // 取该行数据的下载首地址
for(int i=9;i<=Char2ToByte(str.Mid(1,2))*2+7;i+=2)
{
prodata = (BYTE)Char2ToByte(str.Mid(i,2)); //取下载的数据,每次取一个字节
if(prodata != 0xFF) // 若下载字节为0xFF,则不进行下载
{
Write(prodata,addr); // 下载一个非0xFF字节
}
addr++; // 地址加1
m_num++; // 下载字节个数加1
}
}
}
m_Instruction="写器件完成!";
m_Progress.SetPos(1000);
ProOver();// 编程结束
BeepOk();
UpdateData(FALSE);
}
BYTE CISP2Dlg::ReadState() //读状态
{
DWORD Val;
GetPortVal(0X379,&Val,1);
return (BYTE)Val;
}
BYTE CISP2Dlg::ReadCtrl()
{
DWORD Val;
GetPortVal(0X37A,&Val,1);
return (BYTE)Val;
}
CISP2Dlg::WriteCtrl(BYTE Ctrl)
{
SetPortVal(0X37A,(DWORD)Ctrl,1);
}
BOOL CISP2Dlg::SetPinH(int nPin)
{
if(nPin==0)
return FALSE;
switch(nPin)
{
case 1:
m_CtrlByte&=0xfe;
WriteCtrl(m_CtrlByte);
return TRUE;
case 14:
m_CtrlByte&=0xfd;
WriteCtrl(m_CtrlByte);
return TRUE;
case 16:
m_CtrlByte|=0x04;
WriteCtrl(m_CtrlByte);
return TRUE;
case 17:
m_CtrlByte&=0xf7;
WriteCtrl(m_CtrlByte);
return TRUE;
default:
return FALSE;
}
return FALSE;
}
BOOL CISP2Dlg::SetPinL(int nPin)
{
if(nPin==0)
return FALSE;
switch(nPin)
{
case 1:
m_CtrlByte|=0x01;
WriteCtrl(m_CtrlByte);
return TRUE;
case 14:
m_CtrlByte|=0x02;
WriteCtrl(m_CtrlByte);
return TRUE;
case 16:
m_CtrlByte&=0xfb;
WriteCtrl(m_CtrlByte);
return TRUE;
case 17:
m_CtrlByte|=0x08;
WriteCtrl(m_CtrlByte);
return TRUE;
default:
return FALSE;
}
return FALSE;
}
BOOL CISP2Dlg::SetPinLogic(int nPin,BOOL bLogic)
{
if(bLogic)
{
if(nPin==0)
return FALSE;
switch(nPin)
{
case 1:
m_CtrlByte&=0xfe;
WriteCtrl(m_CtrlByte);
return TRUE;
case 14:
m_CtrlByte&=0xfd;
WriteCtrl(m_CtrlByte);
return TRUE;
case 16:
m_CtrlByte|=0x04;
WriteCtrl(m_CtrlByte);
return TRUE;
case 17:
m_CtrlByte&=0xf7;
WriteCtrl(m_CtrlByte);
return TRUE;
default:
return FALSE;
}
}
else
{
if(nPin==0)
return FALSE;
switch(nPin)
{
case 1:
m_CtrlByte|=0x01;
WriteCtrl(m_CtrlByte);
return TRUE;
case 14:
m_CtrlByte|=0x02;
WriteCtrl(m_CtrlByte);
return TRUE;
case 16:
m_CtrlByte&=0xfb;
WriteCtrl(m_CtrlByte);
return TRUE;
case 17:
m_CtrlByte|=0x08;
WriteCtrl(m_CtrlByte);
return TRUE;
default:
return FALSE;
}
}
}
BOOL CISP2Dlg::GetPinLogic(int nPin)
{
switch(nPin)
{
case 1:
m_CtrlByte=ReadCtrl();
return !(m_CtrlByte & 0x01);
case 14:
m_CtrlByte=ReadCtrl();
return !(m_CtrlByte & 0x02);
case 15:
m_StatByte=ReadState();
return (m_StatByte & 0x08);
case 16:
m_CtrlByte=ReadCtrl();
return (m_CtrlByte & 0x04);
case 17:
m_CtrlByte=ReadCtrl();
return !(m_CtrlByte & 0x08);
default:
return FALSE;
}
return FALSE;
}
BOOL CISP2Dlg::GetMiso()//得到MISO引脚的电平
{
SetPinLogic(m_nPinLe,1);//开启锁存1
for(int n=0;n<=1000;n++)//延时
{
}
SetPinLogic(m_nPinLe,0);//关闭锁存1
return GetPinLogic(m_nPinMiso);
}
void CISP2Dlg::SetMosi(BOOL bLogic)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -