Matrix - Buzzer

From FriendlyELEC WiKi
Revision as of 02:52, 24 July 2015 by Tzs (Talk | contribs)

Jump to: navigation, search

查看中文

1 介绍

Buzzer

Matrix-Buzzer主要器件是一个5V无源蜂鸣器。无源蜂鸣器内部不带震荡源,要使其鸣叫外部必须施加一个震荡信号。使用时V引脚接5V电源,G引脚接电源地,并向S引脚输出PWM信号。这个PWM信号通过一个三极管放大控制无源蜂鸣器的导通或关闭,使得蜂鸣器鸣叫。PWM信号可以是3.3V或者5V电平,蜂鸣器鸣叫的声音随着PWM频率的变化而变化。

2 特性

  • 5V Power Supply
  • 3.3V/5V PWM
  • 体积小巧, 带固定孔,方便嵌入到外壳
  • 2.54mm排针接口,接线方便,通用性强

3 使用方法

3.1 连接

  • 连接到Tiny4412 SDK (1506)
首先,你需要去掉Tiny4412 SDK 1506底板上面的BP1_EN的跳线帽
然后,将配件S针脚连接到GPIO PIN1, V接5V,G接地

3.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;
}

3.3 编译并运行示例

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

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

4 相关资料