zoukankan      html  css  js  c++  java
  • C# wince 蜂鸣器 发声 C#调用设备驱动函数

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Threading;

    namespace createfile
    {
        unsafe public partial class Form1 : Form
        {
            const UInt32 OPEN_EXISTING = 3;
            const UInt32 GENERIC_READ = 0x80000000;
            const UInt32 GENERIC_WRITE = 0x40000000;
            const Int32 INVALID_HANDLE_VALUE = -1;

            private IntPtr hPort;

            // PWM的控制字,来源TQ2440/Src/Drivers/PWMDriver/PWMDriver.h文件
            const UInt32 IOCTL_PWM_SET_PRESCALER = 1;
            const UInt32 IOCTL_PWM_SET_DIVIDER = 2;
            const UInt32 IOCTL_PWM_START = 3;
            const UInt32 IOCTL_PWM_GET_FREQUENCY = 4;

            [DllImport("coredll.dll")]
            public static extern IntPtr CreateFile(
                String lpFileName,
                UInt32 dwDesiredAccess,
                UInt32 dwShareMode,
                IntPtr lpSecurityAttributes,
                UInt32 dwCreationDisposition,
                UInt32 dwFlagsAndAttributes,
                IntPtr hTemplateFile
                );
            [DllImport("coredll.dll")]
            public static extern bool DeviceIoControl(
                IntPtr hDevice,
                UInt32 dwIoControlCode,
                UInt32[] lpInBuffer,
                UInt32 nInBufferSize,
                Byte[] lpOutBuffer,
                UInt32 nOutBufferSize,
                UInt32 lpBytesReturned,
                IntPtr lpOverlapped
                );

            public Form1()
            {
                InitializeComponent();
                hPort = CreateFile("PWM1:", GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0,

    IntPtr.Zero);

                if (hPort == (IntPtr)INVALID_HANDLE_VALUE)
                {
                    MessageBox.Show("Open PWM Driver Fail");
                }

            }

            private void button1_Click(object sender, EventArgs e)
            {
                UInt32[] buff = new UInt32[3] { 0, 488, 244 };//488,244来源
                /*int freq = 800;       // 工作频率初值
    #define S3C2440_PCLK 50000000    // PCLK是50MHz
    #define Prescaler0 15     // 预分频
    #define MUX0   8     // 定时器分频值
    #define TCNTB0   (S3C2440_PCLK/128/freq)   // 工作频率
    #define TCMPB0   (TCNTB0>>1)    // 占空比,默认是50%

    BYTE prescale[2] = {0, Prescaler0};
    BYTE divider[2] = {0, MUX0};
    DWORD buff[3] = {0, TCNTB0, TCMPB0};*/
                UInt32[] prescale = new UInt32[2] { 0, 15 };
                UInt32[] divider = new UInt32[2] { 0, 8 };
                //初始化硬件
                DeviceIoControl(hPort, IOCTL_PWM_SET_PRESCALER, prescale, 2, null, 0, 0, IntPtr.Zero);
                DeviceIoControl(hPort, IOCTL_PWM_SET_DIVIDER, divider, 2, null, 0, 0, IntPtr.Zero);

                //DeviceIoControl(hPort, IOCTL_PWM_START, buff, 3, null, 0, 0, IntPtr.Zero);
                Thread t = new Thread(hh);
                button2.Enabled = false;
                timer1.Enabled = true;
                t.Start();
                while (button2.Enabled == false) ;
                t.Abort();
            }
            public void hh()
            {
                UInt32[] buff = new UInt32[3] { 0, 488, 244 };//488,244来源
                DeviceIoControl(hPort, IOCTL_PWM_START, buff, 3, null, 0, 0, IntPtr.Zero);
            }

            private void timer1_Tick(object sender, EventArgs e)
            {
                button2.Enabled = true;
            }
        }
    }

  • 相关阅读:
    spring原理
    架构师和数学
    项目经理需要注意的地方
    如何快速掌握一门新技术
    项目管理要做啥
    编程原则
    架构设计的常用思想
    聊聊编程范式
    程序员与哲学家
    IT人员如何有效规划自己时间
  • 原文地址:https://www.cnblogs.com/liang123/p/6325674.html
Copyright © 2011-2022 走看看