本帖最后由 forlinx2013 于 2014-1-24 09:44 编辑
欢迎大家来到飞凌爱板网专区,对嵌入式技术感兴趣的朋友不妨多多关注一下,我们提供了公司所有开发板的所有资料,也会更新大量技术文章,欢迎大家一块学习提高!!!
Wince6.0如何建立一个应用程序方法 开发工具:飞凌官方6410开发板一套 开发环境:vs2005 开发步骤: ① 打开vs2005,点击file-》new-》project,在project types选项中选择智能设备 smart device,在templates 项选择MFC smart device application,然后给工程起个名字,为led_cpp点击确定,点击下一步,选择TE6410 Board,选择下一步,选择 dialog based,选择中文,选择下一步,选择下一步,直到finish。 ② 工程建立完成后,可以在resource view选项中,选择你对话框的控件
在右图中添加按钮单击事件如下: ③ 修改button1的属性,右击button1,选择properties选项,如下设置: ④ 然后选择led_cpp的solution explorer项,点击led_cpp子菜单下source files下的led_cppDlg.cpp,首先添加led的宏定义:如下
//LED宏定义 #define IOCTL_LED_LED1_OPEN 0x04001060 #define IOCTL_LED_LED1_CLOSE 0x04001061 #define IOCTL_LED_LED2_OPEN 0x04001070 #define IOCTL_LED_LED2_CLOSE 0x04001071 #define IOCTL_LED_LED3_OPEN 0x04001080 #define IOCTL_LED_LED3_CLOSE 0x04001081 #define IOCTL_LED_LED4_OPEN 0x04001090 #define IOCTL_LED_LED4_CLOSE 0x0400109
⑤ 声明led的全局变量如下: //全局变量 HWND hWnd; HANDLE hLed;//LED句柄 HANDLE hLedEvent; //led 事件 HANDLE hThread; //led线程 DWORD flag_led = 0; //led状态标志位 ⑥ 在串口初始化函数中打开led驱动设备,创建led的线程,如下:
/打开LED hLed=CreateFile(TEXT("LED1:"),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); if (hLed == INVALID_HANDLE_VALUE) { printf ("can't open LED1\r\n"); } else { printf ("Open LED1\r\n"); }
//创建跑马灯线程 hThread = CreateThread (NULL, 0, LedThread, NULL, 0, NULL);
CreateFile和CreateThread函数的应用,大家可以从网上搜索下,讲解很详细的。
⑦ 添加LedThread的线程函数: ⑧ DWORD WINAPI LedThread (PVOID hdcMain) { int i; DWORD dwReturn = 0; DWORD dwControlCode; //char buf[4]={0x1, 0x2, 0x4, 0x8};
hLedEvent = CreateEvent(NULL, TRUE, FALSE, _T("TE6410_LED")); while(1) { WaitForSingleObject(hLedEvent, INFINITE); for(i=0; i<4; i++) { //OPEN dwControlCode = IOCTL_LED_LED1_OPEN + i*0x10; DeviceIoControl(hLed, dwControlCode, NULL, sizeof(DWORD), NULL, 0, &dwReturn, NULL);
Sleep(200); //CLOSE dwControlCode = IOCTL_LED_LED1_OPEN + i*0x10 + 1; DeviceIoControl(hLed, dwControlCode, NULL, sizeof(DWORD), NULL, 0, &dwReturn, NULL); } } return 0; } ⑨ 添加按钮事件,双击控件,在按钮事件中添加如下函数: if (flag_led == 0) { SetEvent(hLedEvent); } else { ResetEvent(hLedEvent); } flag_led = !flag_led;
⑩ 将led线程函数声明,在led_cppDlg.h中添加如下: DWORD WINAPI LedThread (PVOID hdcMain); 最后点击builed-》build solution 将生成的led_cpp.exe下载开发板。直接运行,可以看到,当你点击led按钮的时候,跑马灯开始,再点击一下,跑马灯停止。
全部源码:如下: // led_cppDlg.cpp : 实现文件 //
#include "stdafx.h" #include "led_cpp.h" #include "led_cppDlg.h"
#ifdef _DEBUG #define new DEBUG_NEW #endif
//LED宏定义 #define IOCTL_LED_LED1_OPEN 0x04001060 #define IOCTL_LED_LED1_CLOSE 0x04001061 #define IOCTL_LED_LED2_OPEN 0x04001070 #define IOCTL_LED_LED2_CLOSE 0x04001071 #define IOCTL_LED_LED3_OPEN 0x04001080 #define IOCTL_LED_LED3_CLOSE 0x04001081 #define IOCTL_LED_LED4_OPEN 0x04001090 #define IOCTL_LED_LED4_CLOSE 0x04001091
//全局变量 HWND hWnd; HANDLE hLed;//LED句柄 HANDLE hLedEvent; //led 事件 HANDLE hThread; //led线程 DWORD flag_led = 0; //led状态标志位
// Cled_cppDlg 对话框 Cled_cppDlg::Cled_cppDlg(CWnd* pParent /*=NULL*/) : CDialog(Cled_cppDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); }
void Cled_cppDlg: oDataExchange(CDataExchange* pDX) { CDialog: oDataExchange(pDX); }
BEGIN_MESSAGE_MAP(Cled_cppDlg, CDialog) #if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP) ON_WM_SIZE() #endif //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_BUTTON1, &Cled_cppDlg::OnBnClickedButton1) END_MESSAGE_MAP()
// Cled_cppDlg 消息处理程序
BOOL Cled_cppDlg::OnInitDialog() { CDialog::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
//打开LED hLed = CreateFile(TEXT("LED1:"),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); if (hLed == INVALID_HANDLE_VALUE) { printf ("can't open LED1\r\n"); } else { printf ("Open LED1\r\n"); } //创建跑马灯线程 hThread = CreateThread (NULL, 0, LedThread, NULL, 0, NULL);
return TRUE; // 除非将焦点设置到控件,否则返回TRUE }
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP) void Cled_cppDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/) { if (AfxIsDRAEnabled()) { DRA::RelayoutDialog( AfxGetResourceHandle(), this->m_hWnd, DRA::GetDisplayMode() != DRA: ortrait ? MAKEINTRESOURCE(IDD_LED_CPP_DIALOG_WIDE) : MAKEINTRESOURCE(IDD_LED_CPP_DIALOG)); } } #endif
void Cled_cppDlg::OnBnClickedButton1() { // TODO: Add your control notification handler code here
if (flag_led == 0) { SetEvent(hLedEvent); } else { ResetEvent(hLedEvent); } flag_led = !flag_led; }
DWORD WINAPI LedThread (PVOID hdcMain) { int i; DWORD dwReturn = 0; DWORD dwControlCode; //char buf[4]={0x1, 0x2, 0x4, 0x8};
hLedEvent = CreateEvent(NULL, TRUE, FALSE, _T("TE6410_LED")); while(1) { WaitForSingleObject(hLedEvent, INFINITE); for(i=0; i<4; i++) { //OPEN dwControlCode = IOCTL_LED_LED1_OPEN + i*0x10; DeviceIoControl(hLed, dwControlCode, NULL, sizeof(DWORD), NULL, 0, &dwReturn, NULL);
Sleep(200); //CLOSE dwControlCode = IOCTL_LED_LED1_OPEN + i*0x10 + 1; DeviceIoControl(hLed, dwControlCode, NULL, sizeof(DWORD), NULL, 0, &dwReturn, NULL); } } return 0; }
|