📄 dcmotordlg.cpp
字号:
// DCMotorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DCMotor.h"
#include "DCMotorDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDCMotorDlg dialog
CDCMotorDlg::CDCMotorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDCMotorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDCMotorDlg)
m_strPWMfreq = _T("");
m_strPWMduty = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDCMotorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDCMotorDlg)
DDX_Control(pDX, IDC_SLIDER_PWM, m_Slider);
DDX_Text(pDX, IDC_PWM_FREQ, m_strPWMfreq);
DDX_Text(pDX, IDC_PWM_DUTY, m_strPWMduty);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDCMotorDlg, CDialog)
//{{AFX_MSG_MAP(CDCMotorDlg)
ON_BN_CLICKED(IDC_OPEN_PWM, OnOpenPwm)
ON_BN_CLICKED(IDC_CLOSE_PWM, OnClosePwm)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_PWM, OnCustomdrawSliderPwm)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDCMotorDlg message handlers
/***************************************************
实 验 代 码
****************************************************/
#include "pwm.h" /* 包含 PWM 命令代码头文件 */
#include "gpio.h"
HANDLE hPWMFile = INVALID_HANDLE_VALUE; /* PWM 驱动程序设备句柄 */
HANDLE hGPIOFile = INVALID_HANDLE_VALUE; /* GPIO 驱动程序设备句柄 */
#define FREQ_VALUE 255 /* 定义 PWM 频率值*/
BOOL CDCMotorDlg::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
m_Slider.SetRange(0, FREQ_VALUE - 1, TRUE); /* 滑块控件滑动范围 */
m_Slider.SetPos(0); /* 滑块处于0点 */
m_Slider.EnableWindow(FALSE); /* 滑块禁止操作 */
m_strPWMfreq = "0"; /* 频率与占空比都为0 */
m_strPWMduty = "0%";
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
// "打开驱动" 按键单击事件代码
void CDCMotorDlg::OnOpenPwm()
{
BOOL ret;
BYTE pinnum = 9;
// 打开 PWM 驱动
hPWMFile = CreateFile(TEXT("PWM1:"), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, 0);
if (hPWMFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("打开 PWM 驱动失败!"));
return;
}
// 打开 GPIO 驱动
hGPIOFile = CreateFile(TEXT("PIO1:"), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, 0);
if (hGPIOFile == INVALID_HANDLE_VALUE)
{
OnClosePwm();
MessageBox(_T("打开 GPIO 驱动失败!"));
return;
}
// 设置 GPH9 为输出口
ret = ::DeviceIoControl(hGPIOFile, IOCTL_GPH_SET_PIN_OUT, &pinnum, 1,
NULL, 0, NULL, NULL);
if (ret != TRUE)
{
OnClosePwm();
MessageBox(_T("设置 GPH9 为输出失败!"));
return;
}
// 置 GPH9 为低电平
ret = ::DeviceIoControl(hGPIOFile, IOCTL_GPH_CLR_PIN, &pinnum, 1,
NULL, 0, NULL, NULL);
if (ret != TRUE)
{
OnClosePwm();
MessageBox(_T("设置 GPH9 为输出低电平失败!"));
return;
}
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_PWM); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_PWM);
pOpenButton->EnableWindow(FALSE); /* 禁止按键 */
pCloseButton->EnableWindow(TRUE); /* 使能按键 */
m_Slider.EnableWindow(TRUE); /* 滑块使能操作 */
MessageBox(_T("打开 PWM 和 GPIO 驱动成功!"));
}
// "关闭驱动" 按键单击事件代码
void CDCMotorDlg::OnClosePwm()
{
if (hPWMFile != INVALID_HANDLE_VALUE)
{
// 关闭 PWM 驱动
CloseHandle(hPWMFile);
hPWMFile = INVALID_HANDLE_VALUE;
}
if (hGPIOFile != INVALID_HANDLE_VALUE)
{
// 关闭 GPIO驱动
CloseHandle(hGPIOFile);
hGPIOFile = INVALID_HANDLE_VALUE;
}
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_PWM); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_PWM);
pOpenButton->EnableWindow(TRUE); /* 使能按键 */
pCloseButton->EnableWindow(FALSE); /* 禁止按键 */
}
// 对话框关闭退出处理函数
void CDCMotorDlg::OnDestroy()
{
CDialog::OnDestroy();
OnClosePwm();
}
// 移动滑块控件事件代码
int m_slider_pos = 0;
void CDCMotorDlg::OnCustomdrawSliderPwm(NMHDR* pNMHDR, LRESULT* pResult)
{
BOOL ret;
BYTE prescale[2] = {0, 97};
BYTE divider[2] = {0, 2};
UpdateData(TRUE);
if (((m_Slider.GetPos() - m_slider_pos) == 0) ||
(hPWMFile == INVALID_HANDLE_VALUE) ||
(hGPIOFile == INVALID_HANDLE_VALUE))
return; /* 滑块或驱动未打开, 不驱动电机 */
m_slider_pos = m_Slider.GetPos(); /* 获取滑块当前位置 */
// 设置PWM0定时器预分频值
ret = ::DeviceIoControl(hPWMFile, IOCTL_PWM_SET_PRESCALER, prescale, 2,
NULL, 0, NULL, NULL);
if (ret != TRUE)
{
MessageBox(_T("设置 PWM0 定时器预分频值失败!"));
return;
}
// 设置PWM0定时器分频值
ret = ::DeviceIoControl(hPWMFile, IOCTL_PWM_SET_DIVIDER, divider, 2,
NULL, 0, NULL, NULL);
if (ret != TRUE)
{
MessageBox(_T("设置 PWM0 定时器分频值失败!"));
return;
}
// 启动 PWM0 输出信号
DWORD buff[3] = {0, FREQ_VALUE, m_slider_pos};
ret = ::DeviceIoControl(hPWMFile, IOCTL_PWM_START, buff, 3,
NULL, 0, NULL, NULL);
if (ret != TRUE)
{
MessageBox(_T("启动 PWM0 信号输出失败!"));
return;
}
// 获取 PWM0 输出频率
DWORD timer = 0, curfreq, actlen;
ret = ::DeviceIoControl(hPWMFile, IOCTL_PWM_GET_FREQUENCY, &timer, 1,
&curfreq, 1, &actlen, NULL);
if (ret != TRUE)
{
MessageBox(_T("获取 PWM0 信号输出频率失败!"));
return;
}
m_strPWMfreq.Format(_T("%d"), curfreq); /* 显示 PWM0 频率 */
float duty = (float)m_slider_pos / ((float)FREQ_VALUE) * 100;
m_strPWMduty.Format(_T("%f"), duty);
m_strPWMduty += "%"; /* 显示 PWM0 点空比 */
UpdateData(FALSE); /* 更新显示 */
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -