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.

CC2541 再打开Timer3 和Timer4时,会触发P1.2外部中断

Other Parts Discussed in Thread: CC2541

我目前使用CC2541的Timer3 和Timer4 作为PWM信号的timer,P1,2作为一个外部中断接口,接收外部中断。

目前遇到一个问题,就是当配置完Timer3,Timer4 以及中断接口P1.2后,开始Timer4 和Timer3 时,好像概率的会触发一个P1.2的中断。测试时已经将强制P1.2拉到地,还是莫名的会运行中断处理函数。

请问这是什么原因?下面是我的代码。

P1SEL |= BV(1); //P1.1 for pwm
P1DIR |= BV(1); //P1.1 as output.
// Init Timer4 for PWM
P2SEL |= 0x18; //P2SEL.PRI1P1 and P2SEL.PRI0P1;
PERCFG &= 0xef; //PERCFG.T4CFG, alt 0
T4CC1 = 0x80;
T4CCTL1 = 0x24; //00100100
T4CTL |= 0x60; //01100000 timer start
T4CTL |= 0x04;


P1SEL |= BV(3); //P1.3 for pwm
P1DIR |= BV(3); //P1.3 as output.
// Init Timer3 for PWM
P2SEL |= 0x60; //P2SEL.PRI2P1 , P2SEL.PRI3P1 ;
PERCFG &= 0xdf; //PERCFG.T4CFG, alt 0
T3CC0 = 0x80;
T3CCTL0 = 0x24; //00 100 1 00
T3CTL |= 0x60; //011 0 0 0 00 timer start
T3CTL |= 0x04;


// Set interupt for p1.2
P1SEL &= ~(BV(2)); /* Set pin function to GPIO */
P1DIR &= ~(BV(2)); /* Set pin direction to Input */
P1IEN |= BV(2);
P1IFG &= ~(BV(2));

PICTL &= ~(BV(1)); // set P1 Rising edge
IEN2 |= BV(4) ; // enable P1 interupt

// start PWM
T4CTL |= 0x10;
T3CTL |= 0x10;



  • /****************************************************************************** @file buzzer.c @brief Control of the buzzer of the keyfob board in the CC2540DK-mini kit. Group: WCS, BTS Target Device: CC2540, CC2541 ****************************************************************************** Copyright (c) 2009-2019, Texas Instruments Incorporated All rights reserved. IMPORTANT: Your use of this Software is limited to those specific rights granted under the terms of a software license agreement between the user who downloaded the software, his/her employer (which must be your employer) and Texas Instruments Incorporated (the "License"). You may not use this Software unless you agree to abide by the terms of the License. The License limits your use, and you acknowledge, that the Software may not be modified, copied or distributed unless embedded on a Texas Instruments microcontroller or used solely and exclusively in conjunction with a Texas Instruments radio frequency transceiver, which is integrated into your product. Other than for the foregoing purpose, you may not use, reproduce, copy, prepare derivative works of, modify, distribute, perform, display or sell this Software and/or its documentation for any purpose. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. Should you have any questions regarding your right to use this Software, contact Texas Instruments Incorporated at www.TI.com. ****************************************************************************** Release Name: ble_sdk_1.5.0.16 Release Date: 2019-04-18 08:53:32 *****************************************************************************/ #include "hal_mcu.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 #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 ) buzzerInit(); 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; #endif return 1; } /** \brief Stops the buzzer * * Turn off the buzzer * */ void buzzerStop(void) { #if defined ( CC2540_MINIDK ) T3CTL &= ~0x10; // Stop timer 3 P1SEL &= ~0x40; P1_6 = 0; #endif }

  • “好像概率的会触发一个P1.2的中断”

    也就是说不是每次都会进入中断?有没有在线调试过?

    关于PWM配置,建议您参考上面的方式