kernel(八)PWM 蜂鸣器

PWM 蜂鸣器驱动: drivers/input/misc/pwm-beeper.c ,采用 input 子系统编写

需要在 mach-smdkv210.c 中为其构造平台设备,及平台数据,参考 samsung_bl_set 函数的处理过程


这里的 1 表示使用定时器 1,添加平台设备到 smdkv210_devices

注意: s3c_device_timer 一定要在 tq210_beeper 前面。

smdkv210_machine_init 中配置引脚为定时器输出功能

配置内核

System Type --->
        [*] PWM device support
Device Drivers --->
        Input device support --->
                [*] Miscellaneous devices --->
                            <*> PWM beeper support
重新编译,运行测试,启动时听到蜂鸣器响一声

蜂鸣器驱动的设备文件为

下面编写测试程序 beeper_ctl.c

[root@$Louis210: /]# ./beeper_ctl

Please enter the value(Hz) (0 is sop) :

1

freq = 1 Hz

0

freq = 0 Hz

[root@$Louis210: /]#

输入 1,蜂鸣器以 1Hz 的频率发出声音
输入
0,蜂鸣器停止发声
beeper_ctl.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/input.h>
#include <sys/fcntl.h>

int main(int argc, char *argv[])
{
	int fd = -1;
	int num;
	size_t wb;
	char name[20];
	struct input_event ev;
	
	if ((fd = open("/dev/input/event0", O_RDWR)) < 0)  //open device
	{
		perror("open error");
		exit(1);
	}

	printf("Please enter the value(Hz) (0 is stop) :\n");
	while (1)
	{
		ev.type = EV_SND;
		ev.code = SND_TONE;

		scanf("%d", &ev.value);

		printf("freq = %d Hz\n", ev.value);
		
		if (ev.value == 0)
			sleep(2);
		wb = write(fd, &ev, sizeof(struct input_event));
		if (wb < sizeof(struct input_event))
		{
			perror("write error");
			exit(1);
		}
		if (ev.value == 0)
			break;
	}
	close(fd);
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值