欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4029|回復: 0
收起左側

arduino蜂鳴器運行代碼

[復制鏈接]
ID:265531 發表于 2017-12-23 00:34 來自觸屏版 | 顯示全部樓層 |閱讀模式

#include <mach/gpio.h>  
#include <linux/fs.h>  
#include <linux/cdev.h>  
#include <linux/device.h>  
  
static int beep_major = 0;  
static struct class *pClass = NULL;  
module_param(beep_major, int, 0);  
MODULE_AUTHOR("woshishui");  
MODULE_LICENSE("GPL");  
  
#define BEEP_MAGIC 'k'  
#define BEEP_START_CMD _IO (BEEP_MAGIC, 1)  
#define BEEP_STOP_CMD _IO (BEEP_MAGIC, 2)  
  
/*
* Open the device; in fact, there's nothing to do here.
*/  
int beep_open (struct inode *inode, struct file *filp)  
{  
    return 0;  
}  
  
ssize_t beep_read(struct file *file, char __user *buff, size_t count, loff_t *offp)  
{  
  
    *buff = gpio_get_value(S5PV210_GPD0(0));//獲取引腳的電平值  
  
    return 0;  
}  
  
ssize_t beep_write(struct file *file, const char __user *buff, size_t count, loff_t *offp)  
{  
    return 0;  
}  
  
void beep_stop( void )  
{  
    s3c_gpio_cfgpin(S5PV210_GPD0(0), S3C_GPIO_OUTPUT);//設置引進為輸出狀態  
    s3c_gpio_setpull(S5PV210_GPD0(0), S3C_GPIO_PULL_NONE);//不設置上拉  
    gpio_set_value(S5PV210_GPD0(0), 0);//設置引腳為低電平,即不要蜂鳴器響  
}  
  
void beep_start( void )  
{  
    s3c_gpio_cfgpin(S5PV210_GPD0(0), S3C_GPIO_OUTPUT);  
    s3c_gpio_setpull(S5PV210_GPD0(0), S3C_GPIO_PULL_NONE);  
    gpio_set_value(S5PV210_GPD0(0), 1);//設置引腳為高電平,即要蜂鳴器響  
}  
  
static int beep_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)  
{  
    switch ( cmd ) {  
        case BEEP_START_CMD: {  
            beep_start();   break;  
        }  
        case BEEP_STOP_CMD: {  
            beep_stop();    break;  
        }  
        default: {  
            break;  
        }  
    }  
    return 0;  
}  
  
static int beep_release(struct inode *node, struct file *file)  
{  
    return 0;  
}  
  
/*
* Our various sub-devices.
*/  
/* Device 0 uses remap_pfn_range */  
static struct file_operations beep_remap_ops = {  
    .owner   = THIS_MODULE,  
    .open    = beep_open,  
    .release = beep_release,  
    .read    = beep_read,  
    .write   = beep_write,  
    .ioctl   = beep_ioctl,   
};  
  
/*
* There's no need for us to maintain any
* special housekeeping info, so we just deal with raw cdevs.
*/  
static struct cdev BeepDevs;  
  
/*
* Module housekeeping.
*/  
static int beep_init(void)  
{  
    int result;  
    dev_t dev = MKDEV(beep_major, 0);  
      
    s3c_gpio_cfgpin(S5PV210_GPD0(0), S3C_GPIO_OUTPUT);  
    s3c_gpio_setpull(S5PV210_GPD0(0), S3C_GPIO_PULL_NONE);  
    gpio_set_value(S5PV210_GPD0(0), 0);  
  
    /* Figure out our device number. */  
    if (beep_major)  
        result = register_chrdev_region(dev, 1, "beep");  
    else {  
        result = alloc_chrdev_region(&dev, 0, 1, "beep");  
        beep_major = MAJOR(dev);  
    }  
    if (result < 0) {  
        printk(KERN_WARNING "beep: unable to get major %d\n", beep_major);  
        return result;  
    }  
    if (beep_major == 0)  
        beep_major = result;  
  
    /* Register a class_device in the sysfs. */  
    pClass = class_create(THIS_MODULE, "beep");//注冊一個類,使mdev可以在"/dev/"目錄下面建立設備節點,在設備中動態創建節點  
    if(NULL == pClass)  
    {  
        unregister_chrdev_region(dev, 1);  
        return -1;  
    }  
    device_create(pClass, NULL, dev, NULL, "beep");  
    /* Now set up cdev. */  
    cdev_init(&BeepDevs, &beep_remap_ops);  
    BeepDevs.owner = THIS_MODULE;  
    result = cdev_add (&BeepDevs, dev, 1);  
    if(result != 0)  
    {  
        device_destroy(pClass, dev);  
        class_destroy(pClass);  
        unregister_chrdev_region(dev, 1);  
        return -1;  
    }  
  
    printk("beep device installed, with major %d\n", beep_major);  
    return 0;  
}  
  
static void beep_cleanup(void)  
{  
    cdev_del(&BeepDevs);  
    unregister_chrdev_region(MKDEV(beep_major, 0), 1);  
    printk("beep device uninstalled\n");  
}  
  
module_init(beep_init);  
module_exit(beep_cleanup);  
EXPORT_SYMBOL(beep_major);  
  
//1.自動獲取主設備號、次設備號  
//2.動態生成設備節點(dev/  目錄下)  
//  動態卸載  

驅動程序的Makefile:
[cpp] view plain copy
ifeq ($(KERNELRELEASE),)  
  
KERNELDIR ?=/home/fs/linux-3.14 //內核編碼
  
#當前Makefile目錄  
PWD := $(shell pwd)  
  
#   -C $(KERNELDIR)指明跳轉到內核源碼目錄下讀取那里的Makefile  
#   M=$(PWD) 表明然后返回到當前目錄繼續讀入、執行當前的Makefile  
modules:  
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules  
  
modules_install:  
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install  
  
clean:  
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions  
  
.PHONY: modules modules_install clean  
  
else  
    obj-m := beep_drv.o  
endif  


應用程序:beep_test.c
[cpp] view plain copy
#include <stdio.h>  
#include <stdlib.h>  
#include <fcntl.h>  
#include <linux/ioctl.h>  
  
#define BEEP_MAGIC 'k'  
#define BEEP_START_CMD _IO (BEEP_MAGIC, 1)  
#define BEEP_STOP_CMD _IO (BEEP_MAGIC, 2)  
  
int main()  
{  
    int i = 0;  
    char buf;  
    int dev_fd;  
    dev_fd = open("/dev/beep",O_RDWR | O_NONBLOCK);  
    if ( dev_fd == -1 ) {  
        printf("Cann't open file /dev/beep\n");  
        exit(1);  
    }  
    while(1)  
    {  
        getchar();  
        ioctl (dev_fd, BEEP_START_CMD,0);  
        read(dev_fd,&buf,sizeof(buf));  
        printf("The value of the buzzer:%d\n",buf);  
         
        getchar();  
        ioctl (dev_fd, BEEP_STOP_CMD,0);  
        read(dev_fd,&buf,sizeof(buf));  
        printf("The value of the buzzer:%d\n",buf);  
    }  
    return 0;  
}
  
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表