C# wince 蜂鳴器 發聲 C#調用裝置驅動函數

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.