打印

OK6410使用VB.NET控制ARM11开发板LED程序

[复制链接]
2614|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
群龙舞十方|  楼主 | 2011-6-30 09:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我是一个从单片机思想转变到ARM上来的,一开始很不习惯使用ARM的开发方式,主要是自己对嵌入式的架构理解起来不那么快,受单片机开发影响,P0=0;即可点亮LED灯的影响,一直没太搞懂arm上LED驱动是怎么回事,总想拿VB.NET写个程序能控制板子上的小灯。借鉴了一下别人的成果,写了这个小程序,希望对像我这样在门口的朋友有个指导意义。另有几个概念我梳理下,希望高手指点一下我的思路是否正确。
1. 对于跑WINCE系统的arm板原则上不允许客户的C程序直接访问物理地址或者虚拟地址
2. 对于用于的C程序想访问设备的话,系统里面必须要有驱动,驱动应该作为内核级程序运行的
3. 想点亮LED时应用程序必须通过方法去掉系统api,然后由系统对应去打开驱动,再利用驱动里的方法对应去操作寄存器
4. 送数取数
5. 为什么这个程序既能跑在wince下又能跑在X86下呢?  请高手指点

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就可以对所有的LED进行操作
        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就可以对所有的LED进行操作
        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就可以对所有的LED进行操作
        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就可以对所有的LED进行操作
        Catch ex As Exception
            MsgBox("操作LED失败")
        End Try
    End Sub
End Class

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

77

主题

460

帖子

2

粉丝