This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2530之蜂鸣器设计问题。(如何对某一PIN脚以某频率触发?)

Other Parts Discussed in Thread: CC2530

大家好,请教一下有关蜂鸣器的程序问题。

我在P1.3上装上蜂鸣器,需要以1K~3K之频率触发它,使其可发出声响,请问该如何进行呢?

  • 利用I/O 定时翻转电平产生驱动波形对蜂鸣器进行驱动。设置PWM 的设置占空比、周期等产生符合蜂鸣器要求的频率的波形之后,只要打开PWM 输出就能输出该频率的方波,就可以驱动蜂鸣器了。

    2 可以通过PWM 输出口驱动。可以利用PWM 本身可以输出一定的方波来直接驱动蜂鸣器。利用定时器来做定时,通过定时翻转电平产生符合蜂鸣器要求的频率的波形,这个波形就可以用来驱动蜂鸣器了。

  • 感谢您的回复,

    由于我现在是使用P1.2作为Buzzer,故若要使用定时器似乎是要使用Timer1,然而我目前发现CC2530与Buzzer相关之资料都是使用Timer3去驱动Buzzer,请问使用Timer1会有影响吗?

  • 您好,

    之后我先将Buzzer改至P1_6使用Timer3来做测试,将Code分为下列三部分

     

    1. 初始化Buzzer之GPIO

     

    void buzzerInit(void)

    {

        PERCFG |= 0x20;             // Timer 3 Alternate location 2

        P1DIR |= 0x40;              // P1_6 = output

        P1SEL |= 0x40;              // Peripheral function on P1_6

     

        T3CTL &= ~0x10;             // Stop timer 3 (if it was running)

        T3CTL |= 0x04;              // Clear timer 3

        T3CTL &= ~0x08;             // Disable Timer 3 overflow interrupts

        T3CTL |= 0x03;              // Timer 3 mode = 3 - Up/Down

     

        T3CCTL0 &= ~0x40;           // Disable channel 0 interrupts

        T3CCTL0 |= 0x04;            // Ch0 mode = compare

        T3CCTL0 |= 0x10;            // Ch0 output compare mode = toggle on compare

    }

     

    2. 启动Buzzer

    uint8 buzzerStart(uint16 frequency)

    {

        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)

            buzzerStop();                       // A lower tick speed will lower this number accordingly.

            return 0;

        }

     

        // 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

        T3CTL &= ~0xE0;

        T3CTL |= prescaler;

        T3CC0 = (uint8)ticks;

     

        // Start timer

        T3CTL |= 0x10;

     

        return 1;

    }

     

    3. 停止Buzzer

    void buzzerStop(void)

    {

        T3CTL &= ~0x10;             // Stop timer 3

    }

     

    但当我按下触发键启动buzzerStart,还是无法使用蜂鸣器,请问我是有何处忽略了吗?

    P.S. 我frequency给2000

  • 参考下这个帖子 http://e2e.ti.com/support/low_power_rf/f/155/t/92359.aspx 

  • 再次感謝您的回覆

    後來我參考了http://e2e.ti.com/support/low_power_rf/f/158/t/291670.aspx?pi267162=1

    Buzzer已可發聲