Quantcast
Channel: Wireless Connectivity
Viewing all articles
Browse latest Browse all 116964

Forum Post: Modify buzzer.c in the keyfobdemo to use another pin

$
0
0

Hi,

I want change the buzzer output pin of keyfobdemo from P1_6(Timer 3 Channel 0 at alternate location 2) to P1_1(Timer 4 Channel 1 at alternate location 1)

I did the following changing,  but it does not work.  Did I miss something?

BTW,  I did have change it to P1_0(Timer 4 Channel 0 at alternate location 1) successfully. 

It seems that changing output Channel need some additional work.

Thanks a lot!

Jeff.

**************************************************************************************************/

#include <ioCC2541.h>
#include "buzzer.h"


/** \brief    Initialize buzzer
*
* This will initialize the buzzer
*
*/
void buzzerInit(void)
{
#if defined ( CC2540_MINIDK )  
    // Buzzer connected at P1_6
    // We will use Timer 3 Channel 0 at alternate location 2
    // Channel 0 will toggle on compare with 0 and counter will
    // count in up/down mode to T3CC0.
/*
    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
*/    
    // Buzzer connected at P1_1
    // We will use Timer 4 Channel 1 at alternate location 1
    // Channel 1 will toggle on compare with 0 and counter will
    // count in up/down mode to T4CC1.

    PERCFG &= ~0x10;            // Timer 4 Alternate location 1
    P1DIR |= 0x02;              // P1_1 = output
    P1SEL |= 0x02;              // Peripheral function on P1_1

    T4CTL &= ~0x10;             // Stop timer 4 (if it was running)
    T4CTL |= 0x04;              // Clear timer 4
    T4CTL &= ~0x08;             // Disable Timer 4 overflow interrupts
    T4CTL |= 0x03;              // Timer 4 mode = 3 - Up/Down

    T4CCTL1 &= ~0x40;           // Disable channel 1 interrupts
    T4CCTL1 |= 0x04;            // Ch1 mode = compare
    T4CCTL1 |= 0x10;            // Ch1 output compare mode = toggle on compare

    P2SEL |= 0x10;              // Port 1 peripheral priority control. Timer 4 has priority.
#endif
}

/** \brief    Starts the buzzer
*
* Starts the buzzer with given frequency
*
* \param[in]       frequency
*     The frequency in Hertz of the sound to output
* @return  1 successful - 0 if frequency invalid
*/
uint8 buzzerStart(uint16 frequency)
{
#if defined ( CC2540_MINIDK )  
    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;
*/
    
    // Update registers
    P1SEL |= 0x02;              // Peripheral function on P1_1
    
    T4CTL &= ~0xE0;
    T4CTL |= prescaler;
    T4CC1 = (uint8)ticks;

    // Start timer
    T4CTL |= 0x10;
    
#endif
    
    return 1;
}

/** \brief    Stops the buzzer
*
* Turn off the buzzer
*
*/
void buzzerStop(void)
{
#if defined ( CC2540_MINIDK )  
    //T3CTL &= ~0x10;             // Stop timer 3
    T4CTL &= ~0x10;             // Stop timer 4

    P1SEL &= ~0x02;              // GPIO function on P1_1

#endif
}


Viewing all articles
Browse latest Browse all 116964

Trending Articles