📄 dlgpwmsin.cpp
字号:
// DLGPWMSIN.cpp : implementation file
//
#include "stdafx.h"
#include "Magic2410.h"
#include "DLGPWMSIN.h"
#include "pwm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define FREQ_VALUE 101 /* 定义 PWM 频率值*/
/////////////////////////////////////////////////////////////////////////////
// DLGPWMSIN dialog
DLGPWMSIN::DLGPWMSIN(CWnd* pParent /*=NULL*/)
: CDialog(DLGPWMSIN::IDD, pParent)
{
//{{AFX_DATA_INIT(DLGPWMSIN)
m_strPWMduty = _T("");
m_strPWMfreq = _T("");
//}}AFX_DATA_INIT
}
void DLGPWMSIN::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(DLGPWMSIN)
// DDX_Control(pDX, IDC_SLIDER, m_SliderDAC);
DDX_Control(pDX, IDC_SLIDER_PWM2, m_Slider);
DDX_Text(pDX, IDC_PWM_DUTY, m_strPWMduty);
DDX_Text(pDX, IDC_PWM_FREQ2, m_strPWMfreq);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(DLGPWMSIN, CDialog)
//{{AFX_MSG_MAP(DLGPWMSIN)
ON_WM_DESTROY()
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_PWM2, OnCustomdrawSliderPwm2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// DLGPWMSIN message handlers
void DLGPWMSIN::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
// DWORD ExitCode;
// GetExitCodeThread(gWaveThread,&ExitCode);
// ExitThread(ExitCode);
DWORD buff[3] = {0, FREQ_VALUE, 0};
::DeviceIoControl(hPWMFile, IOCTL_PWM_START, buff, 3,
NULL, 0, NULL, NULL);
if (hPWMFile != NULL)
{
// 关闭 PWM 驱动
CloseHandle(hPWMFile);
hPWMFile = NULL;
}
}
void DLGPWMSIN::OnCustomdrawSliderPwm2(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
BOOL ret;
BYTE prescale[2] = {0, 97};
BYTE divider[2] = {0, 2};
UpdateData(TRUE);
if (((m_Slider.GetPos() - m_slider_pos) == 0) ||
(hPWMFile == NULL))
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;
}
BOOL DLGPWMSIN::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
// DWORD IDThread;
// 打开 PWM 驱动
hPWMFile = CreateFile(TEXT("PWM1:"), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, 0);
if (hPWMFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("Cannot Open PWM1!"));
return FALSE;
}
m_Slider.SetRange(0, FREQ_VALUE - 1, TRUE); /* 滑块控件滑动范围 */
m_Slider.SetPos(0); /* 滑块处于0点 */
m_strPWMfreq = "0"; /* 频率与占空比都为0 */
m_strPWMduty = "0%";
m_slider_pos = 0;
DWORD buff[3] = {0, FREQ_VALUE, 0};
::DeviceIoControl(hPWMFile, IOCTL_PWM_START, buff, 3,
NULL, 0, NULL, NULL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -