⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwmsindlg.cpp

📁 利用EVC编写一个可运行于Wince5.0的应用程序
💻 CPP
字号:
// PWMSinDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PWMSin.h"
#include "PWMSinDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPWMSinDlg dialog

CPWMSinDlg::CPWMSinDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPWMSinDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPWMSinDlg)
		// 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 CPWMSinDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPWMSinDlg)
	DDX_Control(pDX, IDC_SLIDER, m_Slider);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPWMSinDlg, CDialog)
	//{{AFX_MSG_MAP(CPWMSinDlg)
	ON_BN_CLICKED(IDC_OPEN_PWM, OnOpenPwm)
	ON_BN_CLICKED(IDC_CLOSE_PWM, OnClosePwm)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER, OnCustomdrawSlider)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPWMSinDlg message handlers

BOOL CPWMSinDlg::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, 10000, FALSE);	// 设置滑块调节范围
	m_Slider.SetPos(0);					/* 滑块处于0点 */
	m_Slider.EnableWindow(FALSE);		/* 滑块禁止操作 */
		
	return TRUE;  // return TRUE  unless you set the focus to a control
}


/***************************************************

		实    验    代    码

// 输出正弦波频率 18Hz 左右
****************************************************/
#include "pwm.h"						 /* 包含 PWM 命令代码头文件 */

HANDLE hPWMFile = INVALID_HANDLE_VALUE;	 /* PWM 驱动程序设备句柄 */

#define  FREQ_VALUE				360		 /* 定义 PWM 频率值*/
#define  VOL_MAX				3.3		 /* S3C2410 输出电压最大值*/

HANDLE gWaveThread;						 /* 波形发生线程句柄 */
volatile BOOL gWaveRun = FALSE;			 /* 线程是否运行 */
volatile DWORD dly = 0;					 /* 延时值 */

DWORD WaveThread(PVOID pArg);


// "打开PWM驱动"按键单击事件代码
void CPWMSinDlg::OnOpenPwm() 
{
	BOOL ret;
	BYTE prescale[2] = {0, 0};		
	BYTE divider[2] = {0, 2};
	DWORD IDThread;

	// 打开 PWM 驱动
	hPWMFile = CreateFile(TEXT("PWM1:"), GENERIC_READ | GENERIC_WRITE, 0, 
						  NULL, OPEN_EXISTING, 0, 0);
	if (hPWMFile == INVALID_HANDLE_VALUE)
	{
		MessageBox(_T("打开 PWM1 驱动失败!"));
		return;									/* 未打开, 不做其它事情 */
	}

	// 设置 PWM0 定时器预分频值
	ret = ::DeviceIoControl(hPWMFile, IOCTL_PWM_SET_PRESCALER, prescale, 2, 
							NULL, 0, NULL, NULL);
	if (ret != TRUE)
	{
		OnClosePwm();
		MessageBox(_T("设置 PWM0 定时器预分频值失败!"));
		return;
	}

	// 设置PWM0定时器分频值
	ret = ::DeviceIoControl(hPWMFile, IOCTL_PWM_SET_DIVIDER, divider, 2, 
							NULL, 0, NULL, NULL);
	if (ret != TRUE)
	{
		OnClosePwm();
		MessageBox(_T("设置 PWM0 定时器分频值失败!"));
		return;
	}

	gWaveRun = TRUE;
	UpdateData(TRUE);
	dly = m_Slider.GetPos();					/* 获取滑块当前位置 */

	// 创建波形发生器线程
	gWaveThread = CreateThread(0, 0, WaveThread, 0, 0, &IDThread);
	if (gWaveThread == NULL) 
	{
		OnClosePwm();
		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);									/* 滑块使能操作 */
	m_Slider.SetPos(0);												/* 滑块处于0点 */

	MessageBox(_T("打开 PWM1 驱动成功!"));
}


// "关闭PWM驱动"按键单击事件
void CPWMSinDlg::OnClosePwm() 
{
	gWaveRun = FALSE;							/* 通知波形发生器线程退出 */

	Sleep(200);
	if (hPWMFile != INVALID_HANDLE_VALUE)
	{
		// 关闭 PWM 驱动
		CloseHandle(hPWMFile);
		hPWMFile = INVALID_HANDLE_VALUE;
	}

	m_Slider.SetPos(0);												/* 滑块处于0点 */
	CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_PWM);		/* 取得控件指针 */
	CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_PWM);  
	pOpenButton->EnableWindow(TRUE);								/* 使能按键 */
	pCloseButton->EnableWindow(FALSE);								/* 禁止按键 */
}


// 对话框关闭退出处理函数
void CPWMSinDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	OnClosePwm();
}


// 正弦波波形发生器线程
DWORD WaveThread(PVOID pArg)
{
	double y;
	DWORD buff[3] = {0, FREQ_VALUE}, i, j;

	while(1)
	{
		if (gWaveRun == FALSE)							/* 线程退出标志 */
			return 0;

		for (i = 0; i <= 360; i++)						/* 0 ~ 360 度 */
		{
			double pi = 3.14159;
			
			y = sin(pi * i / 180) + VOL_MAX / 2;		/* 2.65 + Sinx */
			y = (y / VOL_MAX) * FREQ_VALUE;				/* 占空比当量  */
			buff[2] = (DWORD)y;
			// 改变 PWM 占空比
			BOOL ret = ::DeviceIoControl(hPWMFile, IOCTL_PWM_START, buff, 3, 
										 NULL, 0, NULL, NULL);
			if (ret != TRUE)
				return 0;

			for (j = 0; j < dly; j++);					/* 延时,用于改变波形周期 */
		}
	}
	
	return 0;
}


void CPWMSinDlg::OnCustomdrawSlider(NMHDR* pNMHDR, LRESULT* pResult) 
{
	UpdateData(TRUE);
	dly = m_Slider.GetPos();	/* 获取滑块当前位置 */

	*pResult = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -