博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# wince 蜂鸣器 发声 C#调用设备驱动函数
阅读量:4469 次
发布时间:2019-06-08

本文共 2625 字,大约阅读时间需要 8 分钟。

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

转载于:https://www.cnblogs.com/LoongEmbedded/archive/2012/08/16/5298699.html

你可能感兴趣的文章
Jquery:怎样让子窗体的div显示在父窗体之上
查看>>
01概率
查看>>
Shell脚本
查看>>
MatLab Load cv::Mat 导入数据
查看>>
html+css相关笔记(一)
查看>>
基于块流协议保证音频优先发送
查看>>
关于互联网的一些数据
查看>>
数据预处理:独热编码(One-Hot Encoding)
查看>>
python将对象名的字符串类型,转化为相应对象的操作方法
查看>>
【NLP新闻-2013.06.03】New Book Where Humans Meet Machines
查看>>
mongodb安装4.0(rpm)
查看>>
DispatcherServlet的url mapping为“/”时,对根路径访问的处理
查看>>
备忘pwnable.kr 之passcode
查看>>
好久没敲代码了,手有点生——一个小小的时钟
查看>>
运算符 AS和IS 的区别
查看>>
(转)详解C中volatile关键字
查看>>
easyui时的时间格式yyyy-MM-dd与yyyy-MM-ddd HH:mm:ss
查看>>
专题:动态内存分配----基础概念篇
查看>>
Codeforces Round #426 (Div. 2) (A B C)
查看>>
The Most Simple Introduction to Hypothesis Testing
查看>>