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;
            }
        }
    }

  • 相关阅读:
    Java 中Timer和TimerTask 定时器和定时任务使用的例子
    PowerDesigner逆向工程导入MYSQL数据库总结
    Powerdesigner 连接mysql 在指定的DSN中,驱动程序和应用程序之间的体系结构不匹配 SQLSTATE = IM014
    关于web.xml中的<welcome-file-list>
    SQL查询四舍五入 解决方法
    HTML页面跳转的5种方法
    easyUI datagrid 列宽自适应(简单 图解)(转)
    navicat for mysql只导出数据表结构(转)
    每一位想有所成就的程序员都必须知道的15件事(走不一样的路,要去做,实践实践再实践,推销自己,关注市场)good
    2017除夕夜的感悟:学习工作不分家,工作生活不分家,读书用兵不分家
  • 原文地址:https://www.cnblogs.com/LoongEmbedded/p/5298699.html
Copyright © 2011-2022 走看看