📄 stepmotordlg.cpp
字号:
// StepMotorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "StepMotor.h"
#include "StepMotorDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStepMotorDlg dialog
CStepMotorDlg::CStepMotorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CStepMotorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CStepMotorDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CStepMotorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStepMotorDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStepMotorDlg, CDialog)
//{{AFX_MSG_MAP(CStepMotorDlg)
ON_BN_CLICKED(IDC_OPEN_PIO1, OnOpenPio1)
ON_BN_CLICKED(IDC_CLOSE_PIO1, OnClosePio1)
ON_BN_CLICKED(IDC_StepRun, OnStepRun)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStepMotorDlg message handlers
BOOL CStepMotorDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
/***************************************
实 验 代 码
****************************************/
// 包含命令码头文件
#include "gpio.h"
// 定义步进电机各相使用的 GPIO 编号
#define MOTOA 5
#define MOTOB 6
#define MOTOC 7
#define MOTOD 0
// 文件句柄
HANDLE hFile = INVALID_HANDLE_VALUE;
// "打开GPIO驱动" 按键单击事件代码
void CStepMotorDlg::OnOpenPio1()
{
BOOL ret;
// A,B,C,D 各相对就 GPIO 掩码
DWORD pinmask = (1 << MOTOA) + (1 << MOTOB) + (1 << MOTOC) + (1 << MOTOD);
// 打开 GPIO 驱动
hFile = CreateFile(TEXT("PIO1:"), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("打开 GPIO 驱动失败!"));
return;
}
else
MessageBox(_T("打开 GPIO 驱动成功!"));
// 设置 GPC0, GPC5, GPC6, GPC7 为输出口
ret = ::DeviceIoControl(hFile, IOCTL_GPC_SET_MULTI_PIN_OUT, &pinmask, 1, NULL, 0, NULL, NULL);
if (ret != TRUE)
{
OnClosePio1();
MessageBox(_T("设置 GPIO 引脚输出失败!"));
return;
}
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_PIO1); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_PIO1);
pOpenButton->EnableWindow(FALSE); /* 禁止按键 */
pCloseButton->EnableWindow(TRUE); /* 使能按键 */
}
// "关闭GPIO驱动" 按键单击事件代码
void CStepMotorDlg::OnClosePio1()
{
if (hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_PIO1); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_PIO1);
pOpenButton->EnableWindow(TRUE); /* 使能按键 */
pCloseButton->EnableWindow(FALSE); /* 禁止按键 */
}
// 对话框关闭退出处理函数
void CStepMotorDlg::OnDestroy()
{
CDialog::OnDestroy();
OnClosePio1();
}
/********************************************************************
函数名称: SetPinStatus()
功能描述: 设置 GPIO 引脚为高电平或低电平
输入参数: DWORD pinnum : 引脚编号, 取值为: MOTOA ~ MOTOD
BYTE status : 引脚状态, 1 -- 输出高电平, 0 -- 输出低电平
返 回 值: TRUE: 操作成功 FALSE: 操作失败
*********************************************************************/
BOOL SetPinStatus(DWORD pinnum, BYTE status)
{
BOOL ret;
if (status != 0)
ret = ::DeviceIoControl(hFile, IOCTL_GPC_SET_PIN, &pinnum, 1, NULL, 0, NULL, NULL);
else
ret = ::DeviceIoControl(hFile, IOCTL_GPC_CLR_PIN, &pinnum, 1, NULL, 0, NULL, NULL);
return ret;
}
/********************************************************************
函数名称: MotoMode2()
功能描述: 步进电机双四拍程序
控制时序为: AB-BC-CD-DA-AB
输入参数: DWORD dly: 每一步的延时控制, 值越大,延时越久
返 回 值: 无
*********************************************************************/
void MotoMode2(DWORD dly)
{
// AB 相有效
SetPinStatus(MOTOA, 1);
SetPinStatus(MOTOB, 1);
Sleep(dly);
SetPinStatus(MOTOA, 0);
SetPinStatus(MOTOB, 0);
// BC 相有效
SetPinStatus(MOTOB, 1);
SetPinStatus(MOTOC, 1);
Sleep(dly);
SetPinStatus(MOTOB, 0);
SetPinStatus(MOTOC, 0);
// CD 相有效
SetPinStatus(MOTOC, 1);
SetPinStatus(MOTOD, 1);
Sleep(dly);
SetPinStatus(MOTOC, 0);
SetPinStatus(MOTOD, 0);
// DA 相有效
SetPinStatus(MOTOD, 1);
SetPinStatus(MOTOA, 1);
Sleep(dly);
SetPinStatus(MOTOD, 0);
SetPinStatus(MOTOA, 0);
}
// "步进电机转动" 按键单击事件代码
void CStepMotorDlg::OnStepRun()
{
DWORD i;
if (hFile == INVALID_HANDLE_VALUE)
return;
// 步进电机运转四圈(电机步距为18度)
for (i = 0; i < 20; i++)
{
MotoMode2(20);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -