Difference between revisions of "Matrix - Buzzer/zh"

From FriendlyELEC WiKi
Jump to: navigation, search
(连接)
Line 26: Line 26:
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <stdio.h>
 
#include <stdio.h>
 +
#include <unistd.h>
 +
#include <stdlib.h>
 
#include "libfahw.h"
 
#include "libfahw.h"
 
+
 
+
int parseCmd(int argc, char **argv, int *pin, int *Hz, int *duty)
 +
{
 +
    int num = atoi(argv[1]);
 +
    int hz = atoi(argv[2]);
 +
    int dt = atoi(argv[3]);
 +
 +
    if (hz<0) {
 +
        return -1;
 +
    }
 +
    if (dt<0 || dt>1000) {
 +
        return -1;
 +
    }
 +
    switch(num) {
 +
    case 0:
 +
        *pin = TINY4412_PWM0;
 +
        break;
 +
    case 1:
 +
        *pin = TINY4412_PWM1;
 +
        break;
 +
    default:
 +
        printf("Unsupported pin TINY4412_PWM%d\n", num);
 +
        num = 0;
 +
        *pin = TINY4412_PWM0;
 +
    }
 +
    *Hz = hz;
 +
    *duty = dt;
 +
    printf("Using config: pin=PWM%d freq=%dHz duty=%d\n", num, hz, dt);
 +
    return 0;
 +
}
 +
 +
int main(int argc, char ** argv)
 +
{
 +
    int pin = TINY4412_PWM0;
 +
    int Hz;
 +
    int duty;
 +
 +
    if (argc == 4) {
 +
        if (parseCmd(argc, argv, &pin, &Hz, &duty) == -1) {
 +
            return -1;
 +
        }
 +
    } else {
 +
        Hz = 1000;
 +
        duty = 500;
 +
        printf("Usage:%s PWM[0~1] freq duty[0~1000]\n", argv[0]);
 +
        printf("Using default config: pin=PWM0 freq=%dHz duty=%d\n", Hz, duty);
 +
    }
 +
 +
    if (PWMPlay(pin, Hz, duty) == -1) {
 +
        printf("Fail to output PWM\n");
 +
    } 
 +
    printf("Press enter to stop PWM\n");
 +
    getchar();
 +
    PWMStop(pin);
 +
    printf("Stopped PWM\n");
 +
    return 0;
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
===编译并运行示例===
 
===编译并运行示例===
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
+
git clone http://github.com/friendlyarm/fa-hardware.git
 +
cd fa-hardware
 +
cd demo
 +
cd matrix-buzzer
 +
make
 
</syntaxhighlight>
 
</syntaxhighlight>
 
将编译生成的Buzzer通过ftp上传到开发板上运行即可测试。
 
将编译生成的Buzzer通过ftp上传到开发板上运行即可测试。
  
 
==相关资料==
 
==相关资料==

Revision as of 08:55, 26 August 2015

English

1 介绍

Buzzer

给蜂鸣器5V供电,向它输出高电平即可发出声音。

2 特性

  • 使用标准的3 PIN接口
  • 尺寸为 16x24mm
  • PCB尺寸(mm):16x24

无源蜂鸣器PCB

3 工作原理

无源蜂鸣器没有内部驱动电路,因此无源蜂鸣器工作的理想信号是方波。如果给预直流信号蜂鸣器是不响应的,因为磁路恒定,钼片不能振动发音。所以GPIO驱动无源蜂鸣器需要把GPIO的值拉高以后再拉低来产生振荡,而振荡的频率由GPIO从高拉低之间的时间决定,用户可以通过改变这个时间使无源蜂鸣器发出不同的声音。

4 使用方法

4.1 连接

将配件S针脚连接到GPIO????, V接5V,G接地
  • 连接到NanoPi

NanoPi+buzzer

  • 连接到TINY4412 SDK 1506

tiny4412+buzzer

buzzer+tiny441201

4.2 Linux下的C示例

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "libfahw.h"
 
int parseCmd(int argc, char **argv, int *pin, int *Hz, int *duty)
{
    int num = atoi(argv[1]);
    int hz = atoi(argv[2]);
    int dt = atoi(argv[3]);
 
    if (hz<0) {
        return -1;
    }
    if (dt<0 || dt>1000) {
        return -1;
    }
    switch(num) {
    case 0:
        *pin = TINY4412_PWM0; 
        break;
    case 1:
        *pin = TINY4412_PWM1;
        break;
    default:
        printf("Unsupported pin TINY4412_PWM%d\n", num);
        num = 0;
        *pin = TINY4412_PWM0;
    }
    *Hz = hz;
    *duty = dt;
    printf("Using config: pin=PWM%d freq=%dHz duty=%d\n", num, hz, dt);
    return 0;
}
 
int main(int argc, char ** argv)
{
    int pin = TINY4412_PWM0;
    int Hz;
    int duty;
 
    if (argc == 4) {
        if (parseCmd(argc, argv, &pin, &Hz, &duty) == -1) {
            return -1;
        }
    } else {
        Hz = 1000;
        duty = 500;
        printf("Usage:%s PWM[0~1] freq duty[0~1000]\n", argv[0]);
        printf("Using default config: pin=PWM0 freq=%dHz duty=%d\n", Hz, duty);
    }
 
    if (PWMPlay(pin, Hz, duty) == -1) {
        printf("Fail to output PWM\n");
    }  
    printf("Press enter to stop PWM\n");
    getchar();
    PWMStop(pin);
    printf("Stopped PWM\n");
    return 0;
}

4.3 编译并运行示例

git clone http://github.com/friendlyarm/fa-hardware.git
cd fa-hardware
cd demo
cd matrix-buzzer
make

将编译生成的Buzzer通过ftp上传到开发板上运行即可测试。

5 相关资料