这两天有点时间,捣鼓了下那闲置好久的树莓派,把普通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)加个报警器的更多相关文章

  1. 树莓派 Learning 002 装机后必要的操作 --- 08 实现PC端 远程登入 树莓派 --- 法2 远程登录树莓派的图形桌面

    树莓派 装机后必要的操作 - 实现PC端 远程登入 树莓派 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 PC端系统:win10 x64 ...

  2. 树莓派 Learning 002 必备的操作 --- 08 实现PC端 远程登入 树莓派 --- 法1 远程登入树莓派的命令行状态

    树莓派 必备的操作 - 实现PC端 远程登入 树莓派 - 法1 远程登入树莓派的命令行状态 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 ...

  3. PC远程传文件到树莓派(PSCP详细版)

    1.下载pscp软件 下载地址:http://www.pc6.com/softview/SoftView_456976.html 百度云下载地址:https://pan.baidu.com/s/1bZ ...

  4. 树莓派 - RPi.GPIO

    RPi.GPIO是通过Python/C API实现的,C代码操作底层寄存器, python通过Python/C API调用这些C接口. 这是关于RPi.GPIO项目的介绍. 其中提到了有python ...

  5. pc网页到移动端怎么自动加载适应移动端的css。

    1.通过link标签判断加入 以前听说过在link标签中加media = "handheld",但这个用到安卓或苹果都不管用,后来尝试以下方法,是管用的. <link hre ...

  6. 树莓派RPi.GPIO+Flask构建WebApi实现远程控制

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- import RPi.GPIO as GPIO from flask import Flask, requ ...

  7. Linux主机上实现树莓派的交叉编译及文件传输,远程登陆

    0.环境 Linux主机OS:Ubuntu14.04 64位,运行在wmware workstation 10虚拟机 树莓派版本:raspberry pi 2 B型. 树莓派OS:官网下的的raspb ...

  8. 树莓派之web服务器搭建

    树莓派之web服务器搭建 (一)使用ufw创建防火墙 设置目的:可以完全阻止对树莓派的访问也可以用来配置通过防火墙对特点程序的访问.使用防火墙更好的保护树莓派. 准备工作 1.带有5V电源的树莓派 2 ...

  9. 树莓派安装FLASK服务;并在端网页读取 GPIO状态和系统时间

    做过一些物联网的作品:因为不想一直做APP来控制,因为不能每个人都去下载你自己做的APP,浏览器大家都是有的:那么每个人通过浏览器WEB来访问我们服务器,岂不是很简单和方便,采用flask+pytho ...

随机推荐

  1. java JNI 调试出现的错误

    java JNI 调试出现的错误 ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2JDW ...

  2. C# winform 弹出输入框

    Microsoft.VisualBasic.dll   引用using Microsoft.VisualBasic; string PM = Interaction.InputBox("提示 ...

  3. 今日网站突然报错,mysql的故障

    Access denied for user 'root'@'localhost' (using password: YES) 错误位置 FILE: /var/www/html/ThinkPHP/Li ...

  4. 中国海洋大学第四届朗讯杯高级组 A Rocky

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2718&cid=1203 题意:给你一个m乘n的格子阵,从一边进去,直线往前走,如果前边有 ...

  5. PHP strip_tags() 函数

    定义和用法 strip_tags() 函数剥去 HTML.XML 以及 PHP 的标签. 语法 strip_tags(string,allow) 参数 描述 string 必需.规定要检查的字符串. ...

  6. http://f.dataguru.cn/forum.php?mod=viewthread&tid=44212&fromuid=4771

    http://f.dataguru.cn/forum.php?mod=viewthread&tid=44212&fromuid=4771

  7. java super 隐式参数

    第41集 所有构造器里,第一句话就是super()           (隐式的,系统自动执行) 鸟构造器调用动物构造器,动物构造器调用object构造器. (系统默认的) tostring() 方法 ...

  8. org.dom4j.DocumentException unknown protocol h

    待解析文件的路径中有空格,把空格去掉就好了

  9. 基于ASP.NET的comet简单实现 http长连接,IAsyncResult

    http://www.cnblogs.com/hanxianlong/archive/2010/04/27/1722018.html 我潜水很多年,今天忽然出现.很久没写过博客了,不是因为不想写,而是 ...

  10. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-002-AOP术语解析

    一. 1.Advice Advice是切面的要做的操作,它定义了what.when(什么时候要做什么事) aspects have a purpose—a job they’re meant to d ...