OK6410使用VB.NET控制ARM11開(kāi)發(fā)板LED程序
我是一個(gè)從單片機(jī)思想轉(zhuǎn)變到ARM上來(lái)的,一開(kāi)始很不習(xí)慣使用ARM的開(kāi)發(fā)方式,主要是自己對(duì)嵌入式的架構(gòu)理解起來(lái)不那么快,受單片機(jī)開(kāi)發(fā)影響,P0=0;即可點(diǎn)亮LED燈的影響,一直沒(méi)太搞懂a(chǎn)rm上LED驅(qū)動(dòng)是怎么回事,總想拿VB.NET寫(xiě)個(gè)程序能控制板子上的小燈。借鑒了一下別人的成果,寫(xiě)了這個(gè)小程序,希望對(duì)像我這樣在門(mén)口的朋友有個(gè)指導(dǎo)意義。另有幾個(gè)概念我梳理下,希望高手指點(diǎn)一下我的思路是否正確。
1. 對(duì)于跑WINCE系統(tǒng)的arm板原則上不允許客戶(hù)的C程序直接訪問(wèn)物理地址或者虛擬地址
2. 對(duì)于用于的C程序想訪問(wèn)設(shè)備的話(huà),系統(tǒng)里面必須要有驅(qū)動(dòng),驅(qū)動(dòng)應(yīng)該作為內(nèi)核級(jí)程序運(yùn)行的
3. 想點(diǎn)亮LED時(shí)應(yīng)用程序必須通過(guò)方法去掉系統(tǒng)api,然后由系統(tǒng)對(duì)應(yīng)去打開(kāi)驅(qū)動(dòng),再利用驅(qū)動(dòng)里的方法對(duì)應(yīng)去操作寄存器
4. 送數(shù)取數(shù)
5. 為什么這個(gè)程序既能跑在wince下又能跑在X86下呢? 請(qǐng)高手指點(diǎn)
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Drawing
Imports System.Runtime.InteropServices
Public Class Form1
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const INVALID_HANDLE_VALUE = -1
Const LED_1_ON = &H4001060
Const LED_2_ON = &H4001070
Const LED_3_ON = &H4001080
Const LED_4_ON = &H4001090
Const LED_1_OFF = &H4001061
Const LED_2_OFF = &H4001071
Const LED_3_OFF = &H4001081
Const LED_4_OFF = &H4001091
Private hPort As IntPtr
Declare Function CreateFile Lib "coredll.dll" Alias "CreateFile" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As IntPtr) As IntPtr
Declare Function DeviceIoControl Lib "coredll.dll" Alias "DeviceIoControl" (ByVal hDevice As System.IntPtr, ByVal dwIoControlCode As Long, ByVal lpInBuffer() As Byte, ByVal nInBufferSize As Long, ByVal lpOutBuffer() As Byte, ByVal nOutBufferSize As Long, ByVal lpBytesReturned As Long, ByVal lpOverlapped As System.IntPtr) As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
hPort = CreateFile("LED1:", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
Dim led1() As Byte = {0}
Dim pout(10) As Byte
DeviceIoControl(hPort, LED_1_ON, led1, 0, pout, 0, 0, IntPtr.Zero) '在這里修改LED_1_ON就可以對(duì)所有的LED進(jìn)行操作
Catch ex As Exception
MsgBox("操作LED失敗")
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
hPort = CreateFile("LED1:", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
Dim led1() As Byte = {0}
Dim pout(10) As Byte
DeviceIoControl(hPort, LED_1_OFF, led1, 0, pout, 0, 0, IntPtr.Zero)
Catch ex As Exception
MsgBox("操作LED失敗")
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
hPort = CreateFile("LED1:", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
Dim led1() As Byte = {0}
Dim pout(10) As Byte
DeviceIoControl(hPort, LED_2_OFF, led1, 0, pout, 0, 0, IntPtr.Zero)
Catch ex As Exception
MsgBox("操作LED失敗")
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
hPort = CreateFile("LED1:", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
Dim led1() As Byte = {0}
Dim pout(10) As Byte
DeviceIoControl(hPort, LED_3_OFF, led1, 0, pout, 0, 0, IntPtr.Zero)
Catch ex As Exception
MsgBox("操作LED失敗")
End Try
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Try
hPort = CreateFile("LED1:", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
Dim led1() As Byte = {0}
Dim pout(10) As Byte
DeviceIoControl(hPort, LED_4_OFF, led1, 0, pout, 0, 0, IntPtr.Zero)
Catch ex As Exception
MsgBox("操作LED失敗")
End Try
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Try
hPort = CreateFile("LED1:", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
Dim led1() As Byte = {0}
Dim pout(10) As Byte
DeviceIoControl(hPort, LED_2_ON, led1, 0, pout, 0, 0, IntPtr.Zero) '在這里修改LED_1_ON就可以對(duì)所有的LED進(jìn)行操作
Catch ex As Exception
MsgBox("操作LED失敗")
End Try
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Try
hPort = CreateFile("LED1:", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
Dim led1() As Byte = {0}
Dim pout(10) As Byte
DeviceIoControl(hPort, LED_3_ON, led1, 0, pout, 0, 0, IntPtr.Zero) '在這里修改LED_1_ON就可以對(duì)所有的LED進(jìn)行操作
Catch ex As Exception
MsgBox("操作LED失敗")
End Try
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Try
hPort = CreateFile("LED1:", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
Dim led1() As Byte = {0}
Dim pout(10) As Byte
DeviceIoControl(hPort, LED_4_ON, led1, 0, pout, 0, 0, IntPtr.Zero) '在這里修改LED_1_ON就可以對(duì)所有的LED進(jìn)行操作
Catch ex As Exception
MsgBox("操作LED失敗")
End Try
End Sub
End Class |