📄 motordlg.cpp.bak
字号:
// MotorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Motor.h"
#include "MotorDlg.h"
#include "Motordef.h"
#include "wait.h"
#include <pkfuncs.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
volatile GPIO_REGS *v_pGPIOReg = NULL;
#define StepBaseTime 2500
#define GPIO_81_PullHigh() v_pGPIOReg->GPSR_z|=GPIO_81 //用于直流电机
#define GPIO_81_PullLow() v_pGPIOReg->GPCR_z|=GPIO_81
#define GPIO_82_PullHigh() v_pGPIOReg->GPSR_z|=GPIO_82 //用于直流电机
#define GPIO_82_PullLow() v_pGPIOReg->GPCR_z|=GPIO_82
#define GPIO_83_PullHigh() v_pGPIOReg->GPSR_z|=GPIO_83 //用于产生步进电机脉冲
#define GPIO_83_PullLow() v_pGPIOReg->GPCR_z|=GPIO_83
#define GPIO_84_PullHigh() v_pGPIOReg->GPSR_z|=GPIO_84 //用于控制步进电机方向
#define GPIO_84_PullLow() v_pGPIOReg->GPCR_z|=GPIO_84
#define GPIO_53_PullHigh() v_pGPIOReg->GPSR_y|=GPIO_53//用于步进电机输出使能
#define GPIO_53_PullLow() v_pGPIOReg->GPCR_y|=GPIO_53
// CMotorDlg dialog
UINT StepMotorThread(LPVOID lpParam) //步进电机运行线程
{
CMotorDlg *pDlg=(CMotorDlg*)lpParam;
CWait waitTime;
WaitForSingleObject(pDlg->StepThreadBegin.m_hObject,INFINITE);
GPIO_53_PullLow();
while(1)
{
if(pDlg->m_StepMotorPositive.GetCheck())
GPIO_84_PullLow();
else
GPIO_84_PullHigh();
if(!pDlg->m_StepContinue.GetCheck())
{
pDlg->m_StepRunTime--;
if(pDlg->m_StepRunTime <=0)
break;
}
int result=::WaitForSingleObject(pDlg->StepThreadEnd.m_hObject,0);
if(result==WAIT_OBJECT_0)
break;
GPIO_83_PullHigh();
waitTime.usWait(pDlg->g_HighTimeA);
GPIO_83_PullLow();
waitTime.usWait(pDlg->g_LowTimeA);
}
GPIO_53_PullHigh();
return 0;
}
UINT DCMotorThread(LPVOID param)//直流电机运行线程
{
CMotorDlg *pDlg=(CMotorDlg*)param;
::WaitForSingleObject(pDlg->DCThreadBegin.m_hObject ,INFINITE);
while(1)
{
int result=::WaitForSingleObject(pDlg->DCThreadEnd.m_hObject ,0);
if(result==WAIT_OBJECT_0)
{
GPIO_82_PullHigh();
GPIO_81_PullHigh();
break;
}
if(pDlg->m_DCMotorPositive.GetCheck())
{
GPIO_81_PullLow();
GPIO_82_PullHigh();
}
else
{
GPIO_81_PullLow();
GPIO_82_PullHigh();
}
if(!pDlg->m_DCContinue.GetCheck())
{
Sleep(pDlg->m_DCRunTime);
GPIO_82_PullHigh();
GPIO_81_PullHigh();
break;
}
}
return 0;
}
CMotorDlg::CMotorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMotorDlg::IDD, pParent)
, m_DCRunTime(1000)
, m_StepRunTime(1000)
, m_RatioH(1)
, m_RatioL(1)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMotorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_DCTime, m_DCRunTime);
DDX_Text(pDX, IDC_StepTime, m_StepRunTime);
DDX_Text(pDX, IDC_StepRatioH, m_RatioH);
DDX_Text(pDX, IDC_StepRatioL, m_RatioL);
DDX_Control(pDX, IDC_DcMotorContinue, m_DCContinue);
DDX_Control(pDX, IDC_StepContinue, m_StepContinue);
DDX_Control(pDX, IDC_DCMotorPositive,m_DCMotorPositive);
DDX_Control(pDX, IDC_StepMotorPositive,m_StepMotorPositive);
}
BEGIN_MESSAGE_MAP(CMotorDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
ON_WM_SIZE()
#endif
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_StepMotorStart, &CMotorDlg::OnBnClickedStepmotorstart)
ON_BN_CLICKED(IDC_StepMotorStop, &CMotorDlg::OnBnClickedStepmotorstop)
ON_BN_CLICKED(IDC_DCMotorStart, &CMotorDlg::OnBnClickedDcmotorstart)
ON_BN_CLICKED(IDC_DCMotorStop, &CMotorDlg::OnBnClickedDcmotorstop)
END_MESSAGE_MAP()
// CMotorDlg message handlers
BOOL CMotorDlg::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
m_DCMotorPositive.SetCheck(1);
m_StepMotorPositive.SetCheck(1);
InitGPIO();
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CMotorDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
DRA::RelayoutDialog(
AfxGetInstanceHandle(),
this->m_hWnd,
DRA::GetDisplayMode() != DRA::Portrait ?
MAKEINTRESOURCE(IDD_MOTOR_DIALOG_WIDE) :
MAKEINTRESOURCE(IDD_MOTOR_DIALOG));
}
#endif
BOOL CMotorDlg::InitGPIO(void)
{
int retvalue;
// DWORD threadID;
retvalue=0;
if(!v_pGPIOReg) //
{
if(!(v_pGPIOReg=(volatile GPIO_REGS *)VirtualAlloc(0,0x1000,MEM_RESERVE,PAGE_NOACCESS)))
{
MessageBox(TEXT("VirtualAlloc() failed!\r\n"),NULL,MB_OK);
return FALSE;
}
else
retvalue=VirtualCopy((PVOID)v_pGPIOReg,(PVOID)(GPIO_BASE_U_VIRTUAL>>8),0x1000,PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL);
if(!retvalue)
{
VirtualFree((PVOID)v_pGPIOReg, 0, MEM_RELEASE);
v_pGPIOReg = NULL;
MessageBox(TEXT("VirtualCopy() failed!\r\n"),NULL,MB_OK);
return FALSE;
}
else
{
//set GPIO pin direction
v_pGPIOReg->GPDR_z |= GPIO_81;
v_pGPIOReg->GPDR_z |= GPIO_82;
v_pGPIOReg->GPDR_z |= GPIO_83;
v_pGPIOReg->GPDR_z |= GPIO_84;
v_pGPIOReg->GPDR_y |=GPIO_53;
GPIO_84_PullLow();
GPIO_83_PullLow();
GPIO_81_PullHigh();
GPIO_82_PullHigh();
GPIO_53_PullHigh();
return TRUE;
}
}
return TRUE;
}
void CMotorDlg::OnBnClickedStepmotorstart()
{
UpdateData(TRUE);
g_HighTimeA=m_RatioH*StepBaseTime/(m_RatioH+m_RatioL);
g_LowTimeA=m_RatioL*StepBaseTime/(m_RatioH+m_RatioL);
AfxBeginThread(StepMotorThread,this);
StepThreadBegin.SetEvent();
}
void CMotorDlg::OnBnClickedStepmotorstop()
{
StepThreadEnd.SetEvent();
}
void CMotorDlg::OnBnClickedDcmotorstart()
{
UpdateData(TRUE);
AfxBeginThread(DCMotorThread,this);
DCThreadBegin.SetEvent();
}
void CMotorDlg::OnBnClickedDcmotorstop()
{
DCThreadEnd.SetEvent();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -