經過幾天的研究,終于把OK210開發板wince 紅外驅動的部分給完成了。 開發工具:vs2005,飛凌官方OK210開發板,杜邦線3根,烙鐵一把,焊錫,4.7k電阻一個。 開發軟件:Win CE 6.0 下圖是所需的開發工具
從網上找了好久,不是不能用,就是騙子代碼,根本不能用,最可氣的就是掛羊頭賣狗肉,標著紅外驅動,用自己寶貴的積分,下載下來一看根本就不是,浪費了我很多時間,最后沒辦法只能自己動手了,這里充分體現了毛主席“自己動手,豐衣足食”這句話的重要性。好了廢話不多說吧我自己的這幾天的思路告訴大家讓大家以后少走彎路,在這里我也希望廣大網友能夠不吝惜自己的勞動成果,把自己的奮斗果實奉獻下,這樣能讓很多網友少走彎路,大家慶幸之余也會非常感激”挖井人“的。首先,我是按照單片機解碼的思路,開始制作的Win CE操作系統的紅外驅動,可把我害苦了。我用的是中斷+定時器。思路呢和網上所有的都一樣,但是總是卡死在定時器上,定時器我用的是微秒級別的延時,這樣已進入系統,總是不停地去響應定時器的中斷,這樣就卡死在線程了。系統連起都起不來了,害的我倒騰了好幾天,最后沒辦法只能放棄了,改用延時,網上有現成的函數。解碼的遙控器類型有很多,這里我只做了最通用的NEC遙控器,其他的只需要改一下高低電平時間就行了。NEC紅外協議,作為接收端來講,首先是引導碼,有9ms的低電平,然后是4.5ms的高電平,然后是32位的操作碼,對于按鍵來說我們只需要解第三組就可以了,其他的NEC協議的知識可以從網上搜索下有很多的。現在只做紅外接收模塊,將準備好的杜邦線插在紅外的三個引腳上,引腳順序,大家從網上搜索下,然后就是找一個直插的4.7k上拉電阻,接到hs0038的接受引腳和5v引腳中間。由于我的紅外解碼思路是中斷所以只能找中斷引腳了,但是通篇的找飛凌OK210開發板上中斷引腳真的是太難找了,(⊙o⊙)最后沒辦法只能找已經引出的引腳了,最后發現按鍵上每個引腳都能用,都是中斷引腳,最后我選擇了外部中斷6,開發板的k4按鍵,將按鍵的上拉電阻去掉,然后焊接出一條引線,接到紅外接收器的接收引腳。下面是自己焊的紅外接收模塊。
好了硬件準備完畢,現在開始寫驅動。1.包含的頭文件:- //made in 2013-6-7 by mr wang
- #include "stdafx.h"
- #include
- #include
- #include
- #include
- #include
- #include
- #include "pmplatform.h"
- #include "Pkfuncs.h"
- #include
- #include
- #include "gpioentry_reg.h"
- #include
- typedef enum
- {
- EINT_SIGNAL_LOW_LEVEL = 0,
- EINT_SIGNAL_HIGH_LEVEL,
- EINT_SIGNAL_FALL_EDGE,
- EINT_SIGNAL_RISE_EDGE,
- EINT_SIGNAL_BOTH_EDGE
- } EINT_SIGNAL_METHOD;
- typedef enum
- {
- EINT_FILTER_DISABLE = 0,
- EINT_FILTER_DELAY,
- EINT_FILTER_DIGITAL
- } EINT_FILTER_METHOD;
- static volatile GPIO_REG * g_pGPIOReg = NULL;
- static DWORD g_dwSysIntrHS38 = SYSINTR_UNDEFINED;
- static HANDLE g_hEventHS38 = NULL;
- static HANDLE g_hEventResetBtn = NULL;
- static HANDLE g_hThreadHS38 = NULL;
- static BOOL g_bExitThread = FALSE;
- static BOOL pwstatus=TRUE;
- void Delay_us(int n);//延時us
- BOOL IRS_date(void);//判斷遙控低電平
- static void InitInterrupt(void);//中斷初始化
- void IRS_port_init(void); //引腳初始化
- void IRS_enable_interrupt(void);//使能中斷
- void IRS_disable_interrupt(void);//不使能中斷
- void IRS_clear_interrupt_pending(void);//清楚中斷屏蔽位
- BOOL IRS_set_filter_method(EINT_FILTER_METHOD eMethod, unsigned int uiFilterWidth);//選擇中斷的filter方式
- BOOL IRS_set_interrupt_method(EINT_SIGNAL_METHOD eMethod);//設置中斷方式
- 2.解碼線程:
- INT WINAPI HS38Thread(void)
- {
- int i,j;
- byte ircode[4];//四位編碼
- while(!g_bExitThread)
- {
- WaitForSingleObject(g_hEventHS38, INFINITE);
- if(g_bExitThread)
- {
- break;
- }
- IRS_disable_interrupt(); // 禁止中斷
- IRS_clear_interrupt_pending(); // 清除中斷標志位
- InterruptDone(g_dwSysIntrHS38); //中斷完成,開始解碼
- Delay_us(8000);//延時掉ms的低電平,
- while(IRS_date());
- Delay_us(4000);//4.5ms高電平
- while(!IRS_date());
- for(j=0;j<4;j++)
- {
- for(i=0;i<8;i++)
- {
- while(IRS_date());
- Delay_us(700);
- if(IRS_date())// send 0
- {
- ircode[j]=ircode[j]>>1;
- }
- else
- {
- ircode[j]=ircode[j]|0x80;
- ircode[j]=ircode[j]>>1;
- Delay_us(1200);
- }
- }
- }
- Sleep(100);
- RETAILMSG(1,(TEXT("******* ircode[3] = %x\r\n"),ircode[3]));//打印第三組操作碼,便是按鍵的鍵碼
- IRS_enable_interrupt();
- }
- return 0;
- }
- 3.微妙延時函數
- void Delay_us(int n)
- {
- LARGE_INTEGER litmp;
- LONGLONG QPart1,QPart2;
- double dfMinus, dfFreq, dfTim;
- if(QueryPerformanceFrequency(&litmp)==FALSE)
- {
- return;
- }
- dfFreq = (double)litmp.QuadPart;
- QueryPerformanceCounter(&litmp);
- QPart1 = litmp.QuadPart;
- do
- {
- QueryPerformanceCounter(&litmp);
- QPart2=litmp.QuadPart;
- dfMinus=(double)(QPart2-QPart1);
- dfTim=dfMinus/dfFreq;
- }while(dfTim <0.000001*n);
- }
- 4.其他流驅動接口函數
- //設置寄存器地址
- BOOL HS38_initialize_register_address(void *pGPIOReg)//,void *pHSReg)
- {
- if (pGPIOReg == NULL)
- {
- return FALSE;
- }
- else
- {
- g_pGPIOReg = (GPIO_REG *)pGPIOReg;
-
- }
- return TRUE;
- }
- //初始化
- void IRS_port_init(void)
- {
- Set_PinFunction(g_pGPIOReg, GPH06_EXT_INT_6);
- Set_PinPullUD(g_pGPIOReg, GPH06_EXT_INT_6, sgip_PULL_UP);
- }
- //使能引腳中斷
- void IRS_enable_interrupt(void)
- {
- Unmask_EXTINT(g_pGPIOReg, EXT_INT_6);
- }
- //禁止引腳中斷
- void IRS_disable_interrupt(void)
- {
- Mask_EXTINT(g_pGPIOReg, EXT_INT_6);
- }
- //清除引腳中斷
- void IRS_clear_interrupt_pending(void)
- {
- Clear_EXTINT(g_pGPIOReg, EXT_INT_6);
- }
- //設置中斷方式
- BOOL IRS_set_interrupt_method(EINT_SIGNAL_METHOD eMethod)
- {
- BOOL Ret = TRUE;
- switch(eMethod)
- {
- case EINT_SIGNAL_LOW_LEVEL:
- Set_EXTINT_TRLVL(g_pGPIOReg, EXT_INT_6, sgip_LOW_LEVEL);
- break;
- case EINT_SIGNAL_HIGH_LEVEL:
- Set_EXTINT_TRLVL(g_pGPIOReg, EXT_INT_6, sgip_HIGH_LEVEL);
- break;
- case EINT_SIGNAL_FALL_EDGE:
- Set_EXTINT_TRLVL(g_pGPIOReg, EXT_INT_6, sgip_FALLING_EDGE);
- break;
- case EINT_SIGNAL_RISE_EDGE:
- Set_EXTINT_TRLVL(g_pGPIOReg, EXT_INT_6, sgip_RISING_EDGE);
- break;
- case EINT_SIGNAL_BOTH_EDGE:
- Set_EXTINT_TRLVL(g_pGPIOReg, EXT_INT_6, sgip_BOTH_EDGE);
- break;
- default:
- Ret = FALSE;
- break;
- }
- return Ret;
- }
- //設置中斷filter方式
- BOOL IRS_set_filter_method(EINT_FILTER_METHOD eMethod, unsigned int uiFilterWidth)
- {
- BOOL Ret =TRUE;
- switch(eMethod)
- {
- case EINT_FILTER_DISABLE:
- Clr_EXTINT_FILTER(g_pGPIOReg, EXT_INT_6);
- break;
- case EINT_FILTER_DELAY:
- Set_EXTINT_FILTER(g_pGPIOReg, EXT_INT_6, sgip_DELAY_FLT, 0);
- break;
- case EINT_FILTER_DIGITAL:
- Set_EXTINT_FILTER(g_pGPIOReg, EXT_INT_6, sgip_DIGITAL_FLT, uiFilterWidth);
- break;
- default:
- Ret = FALSE;
- break;
- }
- return Ret;
- }
- //判斷遙控接收引腳低電平,返回true
- BOOL IRS_date(void)
- {
- if(Get_PinData(g_pGPIOReg,GPH06_EXT_INT_6))
- {
- return FALSE; // Low Active Switch (Pull-up switch)
- }
- else
- {
- return TRUE;
- }
- }
- static BOOL AllocResources(void)
- {
- DWORD dwIRQ;
- PHYSICAL_ADDRESS ioPhysicalBase = {0,0};
- //------------------
- // GPIO Controller SFR
- //------------------
- ioPhysicalBase.LowPart = BASE_REG_PA_GPIO;
- g_pGPIOReg = (GPIO_REG *)MmMapIoSpace(ioPhysicalBase, sizeof(GPIO_REG), FALSE);
- if (g_pGPIOReg == NULL)
- {
- return FALSE;
- }
- dwIRQ = IRQ_EINT6;
- g_dwSysIntrHS38 = SYSINTR_UNDEFINED;
- g_hEventHS38 = NULL;
- if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &dwIRQ, sizeof(DWORD), &g_dwSysIntrHS38, sizeof(DWORD), NULL))
- {
- g_dwSysIntrHS38 = SYSINTR_UNDEFINED;
- return FALSE;
- }
- g_hEventHS38 = CreateEvent(NULL, FALSE, FALSE, NULL);
- if(NULL == g_hEventHS38)
- {
- return FALSE;
- }
- if (!(InterruptInitialize(g_dwSysIntrHS38, g_hEventHS38, 0, 0)))
- {
- return FALSE;
- }
- return TRUE;
- }
- static void ReleaseResources(void)
- {
- if (g_pGPIOReg != NULL)
- {
- MmUnmapIoSpace((PVOID)g_pGPIOReg, sizeof(GPIO_REG));
- g_pGPIOReg = NULL;
- }
- if (g_dwSysIntrHS38 != SYSINTR_UNDEFINED)
- {
- InterruptDisable(g_dwSysIntrHS38);
- }
- if (g_hEventHS38 != NULL)
- {
- CloseHandle(g_hEventHS38);
- }
- if (g_dwSysIntrHS38 != SYSINTR_UNDEFINED)
- {
- KernelIoControl(IOCTL_HAL_RELEASE_SYSINTR, &g_dwSysIntrHS38, sizeof(DWORD), NULL, 0, NULL);
- }
- g_pGPIOReg = NULL;
- g_dwSysIntrHS38 = SYSINTR_UNDEFINED;
- g_hEventHS38 = NULL;
- }
- static void InitInterrupt(void)
- {
- // 禁止中斷
- IRS_disable_interrupt();
- // I初始化端口
- IRS_port_init();
- // 設置中斷模式
- IRS_set_interrupt_method(EINT_SIGNAL_BOTH_EDGE);
- IRS_set_filter_method(EINT_FILTER_DELAY, 0);
- // 清除中斷標志位
- IRS_clear_interrupt_pending();
- // 使能中斷
- IRS_enable_interrupt();
- }
- BOOL WINAPI
- DllEntry(HANDLE hinstDLL,
- DWORD dwReason,
- LPVOID /* lpvReserved */)
- {
- switch(dwReason)
- {
- case DLL_PROCESS_ATTACH:
- DEBUGREGISTER((HINSTANCE)hinstDLL);
- return TRUE;
- case DLL_THREAD_ATTACH:
- break;
- case DLL_THREAD_DETACH:
- break;
- case DLL_PROCESS_DETACH:
- break;
- #ifdef UNDER_CE
- case DLL_PROCESS_EXITING:
- break;
- case DLL_SYSTEM_STARTED:
- break;
- #endif
- }
- return TRUE;
- }
- BOOL IRS_Deinit(DWORD hDeviceContext)
- {
- g_bExitThread = TRUE;
- if (g_hThreadHS38) // Make Sure if thread is exist
- {
- IRS_disable_interrupt();
- IRS_clear_interrupt_pending();
- // Signal Thread to Finish
- SetEvent(g_hEventHS38);
- // Wait for Thread to Finish
- WaitForSingleObject(g_hThreadHS38, INFINITE);
- CloseHandle(g_hThreadHS38);
- g_hThreadHS38 = NULL;
- }
- ReleaseResources();
-
- //RETAILMSG(1,(TEXT("USERLED: IRS_Deinit\r\n")));
- return TRUE;
- }
- DWORD IRS_Init(DWORD dwContext)
- {
- RETAILMSG(1,(TEXT("Key_Gpio_Setting----\r\n")));
- if (AllocResources() == FALSE)
- {
- goto CleanUp;
- }
- HS38_initialize_register_address((void *)g_pGPIOReg);
- InitInterrupt();
-
- g_hThreadHS38 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) HS38Thread, NULL, 0, NULL);
- if (g_hThreadHS38 == NULL )
- {
- goto CleanUp;
- }
- return TRUE;
- CleanUp:
- IRS_Deinit(0);
- return FALSE;
- }
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- BOOL IRS_IOControl(DWORD hOpenContext,
- DWORD dwCode,
- PBYTE pBufIn,
- DWORD dwLenIn,
- PBYTE pBufOut,
- DWORD dwLenOut,
- PDWORD pdwActualOut)
- {
- return TRUE;
- }
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- DWORD IRS_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
- {
- RETAILMSG(0,(TEXT("USERLED: IRS_Open\r\n")));
- return TRUE;
- }
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- BOOL IRS_Close(DWORD hOpenContext)
- {
- RETAILMSG(0,(TEXT("USERLED: IRS_Close\r\n")));
- return TRUE;
- }
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- void IRS_PowerDown(DWORD hDeviceContext)
- {
- RETAILMSG(0,(TEXT("USERLED: IRS_PowerDown\r\n")));
- }
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- void IRS_PowerUp(DWORD hDeviceContext)
- {
- RETAILMSG(0,(TEXT("USERLED: IRS_PowerUp\r\n")));
- }
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- DWORD IRS_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
- {
- RETAILMSG(0,(TEXT("USERLED: IRS_Read\r\n")));
- return TRUE;
- }
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- DWORD IRS_Seek(DWORD hOpenContext, long Amount, DWORD Type)
- {
- RETAILMSG(0,(TEXT("USERLED: IRS_Seek\r\n")));
- return 0;
- }
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- DWORD IRS_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes)
- {
- RETAILMSG(0,(TEXT("USERLED: IRS_Write\r\n")));
- return 0;
- }
復制代碼 6.實驗結果,當你按下遙控器上的按鍵時,dnw或超級終端上會顯示相應的按鍵碼如下:
長時間按某一個按鍵,會顯示同一個鍵值如下:
|