第十节 蜂鸣器

来源:互联网 发布:带有知字的诗句 编辑:程序博客网 时间:2024/04/29 16:21
第十节  蜂鸣器

    蜂鸣器是一种常用的报警设备,常用的蜂鸣器有无源和有源两种类型,无源蜂鸣器需要用一定频率的方波驱动,从而发出不同频率的声音。而有源蜂鸣器只需要通电就会发出固定频率的声音,MT254xboard开发板上的蜂鸣器用的是无源蜂鸣器,因此我们需要用一定频率的方波来驱动。


硬件驱动方面,我们这里使用了PNP三极管来驱动蜂鸣器,BUZZ引脚为芯片的P2.0。对照IO复用表可知,此IO可以作为定时器4的匹配通道1输出。所以我们需要把定时器配置为PWM匹配输出模式:

<span style="font-size:18px;">PERCFG |= (0x01<<4);   // 选择定时器4匹配功能中的第2种IO口    P2DIR |= 0x01;          // p2.0  输出    P2SEL |= 0x01;          // p2.0  复用功能    T4CTL &= ~0x10;             // Stop timer 3 (if it was running)    T4CTL |= 0x04;              // Clear timer 3    T4CTL &= ~0x08;             // Disable Timer 3 overflow interrupts    T4CTL |= 0x03;              // Timer 3 mode = 3 - Up/Down    T4CCTL0 &= ~0x40;           // Disable channel 0 interrupts    T4CCTL0 |= 0x04;            // Ch0 mode = compare    T4CCTL0 |= 0x10;            // Ch0 output compare mode = toggle on compare</span>

这里仅仅是配置为匹配输出,具体输出什么样的波形还需要我们再通过计算得出。

<span style="font-size:18px;">void Buzzer_Start(uint16 frequency){    P2SEL |= 0x01;          // p2.0  复用功能    uint8 prescaler = 0;    // Get current Timer tick divisor setting    uint8 tickSpdDiv = (CLKCONSTA & 0x38)>>3;    // Check if frequency too low    if (frequency < (244 >> tickSpdDiv)){   // 244 Hz = 32MHz / 256 (8bit counter) / 4 (up/down counter and toggle on compare) / 128 (max timer prescaler)        Buzzer_Stop();                       // A lower tick speed will lower this number accordingly.    }    // Calculate nr of ticks required to achieve target frequency    uint32 ticks = (8000000/frequency) >> tickSpdDiv;      // 8000000 = 32M / 4;    // Fit this into an 8bit counter using the timer prescaler    while ((ticks & 0xFFFFFF00) != 0)    {        ticks >>= 1;        prescaler += 32;    }    // Update registers    T4CTL &= ~0xE0;    T4CTL |= prescaler;    T4CC0 = (uint8)ticks;    // Start timer    T4CTL |= 0x10;}</span>

这个函数是通过传入参数的形式,使P2.0口发出指定频率的方波。

<span style="font-size:18px;">void Buzzer_Stop(void){    T4CTL &= ~0x10;             // Stop timer 3    P2SEL &= ~0x01;    P2_0 = 1;}</span>

这个函数是使蜂鸣器停止,主要有三个动作,停止定时器,将P2.0配置为IO功能并且输出高电平,因为我们使用的是PNP三极管。
    我们在按键的程序上加上蜂鸣器的控制,当按下按键时,蜂鸣器响。松开后停止响。

<span style="font-size:18px;">int main(void){    char LCDBuf[21]={0};            // 显存    int KeyCnt = 0;    SysStartXOSC();    LCD12864_Init();    LCD12864_DisStr(1, "    Buzzer Test");    Buzzer_Init();    P0SEL &= ~0X01; // 设置为IO功能    P0DIR &= ~0X01; // 设置为输入功能    P0IEN |= 0X01;  // P0.0 设置为中断方式    PICTL |= 0X01;  // 下降沿触发    IEN1 |= 0X20;   // 允许P0口中断    P0IFG = 0x00;   // 清除中断标志位    EA = 1;         // 开总中断    sprintf(LCDBuf, "   Key Count : %d", KeyCnt++);    // 按键计数    LCD12864_DisStr(3, LCDBuf);    while(1)    {        if(KEY_DOWN == NewKeyValue)  // 按键按下        {            SoftWaitUs(25000);       // 延时防抖            if((P0&0X01) == 0X00)   // 再次确认按键是否按下            {                sprintf(LCDBuf, "   Key Count : %d", KeyCnt++);    // 按键计数                LCD12864_DisStr(3, "    Buzzer Start");                Buzzer_Start(2000);            }            else            {                NewKeyValue = KEY_UP;   // 按键松开                Buzzer_Stop();                LCD12864_DisStr(3, "    Buzzer Stop");            }        }    }    return 0;}</span>

本文章转载自
http://www.deyisupport.com/question_answer/wireless_connectivity/bluetooth/f/103/t/69222.aspx
请勿用于商业

0 0
原创粉丝点击