用普通PC主板的蜂鸣器给树莓派(RPI)加个报警器
这两天有点时间,捣鼓了下那闲置好久的树莓派,把普通PC主板的蜂鸣器作为树莓派的报警器用。
Raspberry Pi有许多的GPIO(General Purpose Input Output:通用输入/输出),可以用来控制和读取数字电路中TTL电平的逻辑0和逻辑1。
我们要使用RPi的GPIO首先要知其GPIO的定义,常用的有两种编号定义:WiringPi Pin和BCM GPIO。
GPIO的驱动库我这里介绍两种给大家,一种为C语言的WiringPi,另一种为python的RPi.GPIO,本例使用的是WiringPi。
接线比较简单,把蜂鸣器的红线接GPIO1口(pin:12,即BCM_GPIO 18),黑线接GND(pin:6)。
至于为什么接GPIO1口,主要是因为树莓派只有这个口支持PWM输出。
pin编号如下图所示:
具体代码如下:
alarm.h
#include <wiringPi.h> #define PIN_NO 1 int init(); void alarm(int count, int value, int interval); void stopAlarm();
alarm.c
#include <stdio.h>
#include <stdlib.h>
#include "alarm.h" static short isok; int init(){
if(!isok){
if(wiringPiSetup() == -){
printf("WiringPi setup failed!\n");
return ;
}else{
isok = ;
}
}
return ;
} void alarm(int count, int value, int interval){
if(!init())
return; //printf("Speaker pin: GPIO%d\n",PIN_NO); pinMode(PIN_NO,PWM_OUTPUT);//设置引脚模式,模式为INPUT、OUTPUT 或者 PWM_OUTPUT,注意:仅wiringPi引脚1(即BCM_GPIO 18)支持PWM输出
pwmSetMode(PWM_MODE_MS);
//pwmSetRange(1024); int c;
for(c = ;c < count; c++){
pwmWrite(PIN_NO,value);
//printf("%d\n",count - c);
delay(interval);
pwmWrite(PIN_NO,-);
delay(interval);
} stopAlarm();
} void stopAlarm(){
if(!init())
return; pwmWrite(PIN_NO,-);
//pwmSetMode(PWM_MODE_BAL);
}
alarm_test.c
#include <stdio.h>
#include <stdlib.h>
#include "alarm.h" int main(int argc, char *argv[])
{
if(argc < ){
printf("arg error! At least 3 parameters!\n");
return ;
}else{
int count = atoi(argv[]);
int value = atoi(argv[]);
int interval = atoi(argv[]);
printf("Count:%d\tValue:%d\tInterval:%d\n", count, value, interval);
printf("Alarm start!\n");
alarm(count, value, interval);
printf("Alarm complete!\n");
}
return ;
}
编译:
gcc -c -o alarm.o alarm.c -lwiringPi
gcc -o alarm_test alarm_test.c alarm.o -lwiringPi
执行:
sudo ./alarm_test
用普通PC主板的蜂鸣器给树莓派(RPI)加个报警器的更多相关文章
- 树莓派 Learning 002 装机后必要的操作 --- 08 实现PC端 远程登入 树莓派 --- 法2 远程登录树莓派的图形桌面
树莓派 装机后必要的操作 - 实现PC端 远程登入 树莓派 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 PC端系统:win10 x64 ...
- 树莓派 Learning 002 必备的操作 --- 08 实现PC端 远程登入 树莓派 --- 法1 远程登入树莓派的命令行状态
树莓派 必备的操作 - 实现PC端 远程登入 树莓派 - 法1 远程登入树莓派的命令行状态 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 ...
- PC远程传文件到树莓派(PSCP详细版)
1.下载pscp软件 下载地址:http://www.pc6.com/softview/SoftView_456976.html 百度云下载地址:https://pan.baidu.com/s/1bZ ...
- 树莓派 - RPi.GPIO
RPi.GPIO是通过Python/C API实现的,C代码操作底层寄存器, python通过Python/C API调用这些C接口. 这是关于RPi.GPIO项目的介绍. 其中提到了有python ...
- pc网页到移动端怎么自动加载适应移动端的css。
1.通过link标签判断加入 以前听说过在link标签中加media = "handheld",但这个用到安卓或苹果都不管用,后来尝试以下方法,是管用的. <link hre ...
- 树莓派RPi.GPIO+Flask构建WebApi实现远程控制
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import RPi.GPIO as GPIO from flask import Flask, requ ...
- Linux主机上实现树莓派的交叉编译及文件传输,远程登陆
0.环境 Linux主机OS:Ubuntu14.04 64位,运行在wmware workstation 10虚拟机 树莓派版本:raspberry pi 2 B型. 树莓派OS:官网下的的raspb ...
- 树莓派之web服务器搭建
树莓派之web服务器搭建 (一)使用ufw创建防火墙 设置目的:可以完全阻止对树莓派的访问也可以用来配置通过防火墙对特点程序的访问.使用防火墙更好的保护树莓派. 准备工作 1.带有5V电源的树莓派 2 ...
- 树莓派安装FLASK服务;并在端网页读取 GPIO状态和系统时间
做过一些物联网的作品:因为不想一直做APP来控制,因为不能每个人都去下载你自己做的APP,浏览器大家都是有的:那么每个人通过浏览器WEB来访问我们服务器,岂不是很简单和方便,采用flask+pytho ...
随机推荐
- GC的代机制
1.代为CLR垃圾回收器采用的一种机制,基于以下几点假设:对象越新,生存期越短:对象越老,生存期越长: 2.托管堆仅支持3代(GC.MaxGeneration,第0代.第1代.第2代),CLR初始化会 ...
- How To Call Stored Procedure In Hibernate
How To Call Stored Procedure In Hibernate In this tutorial, you will learn how to call a store proce ...
- SQL 中With as 的用法
转自:http://www.cnblogs.com/superyinhai/archive/2010/04/09/1708643.html 一.WITH AS的含义 WITHAS短语,也叫做子查询部分 ...
- Cannot generate SSPI context---MS SQL ERROR
http://www.cnblogs.com/newr2006/archive/2011/08/25/2153253.html Additional error information from SQ ...
- Java 单链表逆序
代码: package com.wangzhu.linkedlist; public class LinkedListDemo { /** * @param args */ public static ...
- 李洪强漫谈iOS开发[C语言-016]-变量的作用域
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)
一. 1.Spring MVC provides several ways that a client can pass data into a controller’s handler method ...
- 【HDOJ】4267 A Simple Problem with Integers
树状数组.Easy. /* 4267 */ #include <iostream> #include <string> #include <map> #includ ...
- 用U盘安装系统2
这种方式USB启动盘制作成功之后是可以往里面存放任何资料的,我喜欢用这一种 首先,在网站上下载一个你想要装的系统 (百度一下优优系统,大地系统,深度技术,MSDN我告诉你,都可以,看你自己喜欢了) 例 ...
- 如何从Win7中提取制作Windows PE3.0
在D盘新建文件夹winpe,在winpe中新建sources.pe3和new文件夹,把附件中提供的工具imagex连文件夹一起放到winpe目录中. 制作方法: 1.把windows7光盘(或光盘镜像 ...