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.

CC2540 TIMER1

Other Parts Discussed in Thread: CC2540

你好,我们现在用KEYFOB例程时,硬件把BUZZER的io口从1-6该到1-2了,也就是说原来的timer3需要修改成timer1,在仿照timer3配置timer1时,发现没有register去设置timer1的stop和start。有没有timer1的使用实例?或者我要怎么配置timer1来达到我设置蜂鸣器频率的要求?

  • void buzzerInit(void)
    {
    #if defined ( CC2540_MINIDK )  
        // Buzzer connected at P1_2
        // We will use Timer 1 Channel 0 at alternate location 2
        // Channel 0 will toggle on compare with 0 and counter will
        // count in up/down mode to T1CC0.

        PERCFG |= 0x40;             // Timer 1 Alternate location 2
        P1DIR |= 0x04;              // P1_2 = output
        P1SEL |= 0x04;              // Peripheral function on P1_2

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

        T1CCTL0 &= ~0x40;           // Disable channel 0 interrupts
        T1CCTL0 |= 0x04;            // Ch0 mode = compare
        T1CCTL0 |= 0x10;            // Ch0 output compare mode = toggle on compare
    #endif
    }

    start函数里下面一段:

        // Update registers
        T1CTL &= ~0xE0;
        T1CTL |= prescaler;
        T1CC0L = (uint8)ticks;

        // Start timer
        T1CTL |= 0x10;

    void buzzerStop(void)
    {
    #if defined ( CC2540_MINIDK )  
        T1CTL &= ~0x10;             // Stop timer 1

    #endif
    }

    以上红色部分配置不正确,能帮忙指出该怎么修改吗?




  • Hi Wang wx,

    timer 1 跟 timer 3 寄存器结构有很大不一样.

    首先timer 1是16-bit的一个counter, timer 3是8-bit的.

    timer 1里面并没有clear的寄存器配置, 也没有overflow interrupts的配置.

    因此不推荐你使用timer 1.

    你可以用timer 4, 结构配置基本跟timer 3是一样的, 你完全可以参照timer 3的配置去写代码.

  • Hi Yan,

    硬件已经做成这样只能用timer 1了,通过看swru191c.pdf里timer 1的寄存器,软件修改如下:

    void buzzerInit(void)
    {
    #if defined ( CC2540_MINIDK )  
        // Buzzer connected at P1_2
        // We will use Timer 1 Channel 0 at alternate location 2
        // Channel 0 will toggle on compare with 0 and counter will
        // count in up/down mode to T1CC0.

        PERCFG |= 0x40;             // Timer 1 Alternate location 2
        P1DIR |= 0x04;              // P1_2 = output
        P1SEL |= 0x04;              // Peripheral function on P1_2

        T1STAT |= 0x20;             // Disable Timer 1 overflow interrupts
        T1CTL |= 0x03;              // Timer 1 mode = 1 - Up/Down

        T1CCTL0 &= ~0x40;           // Disable channel 0 interrupts
        T1CCTL0 |= 0x04;            // Ch0 mode = compare
        T1CCTL0 |= 0x10;            // Ch0 output compare mode = toggle on compare
    #endif
    }

    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 & 0xFFFF0000) != 0)
        {
            ticks >>= 1;
            prescaler += 4;
        }
        // Start timer
        T1CTL |= 0x03;              // Timer 1 mode  - Up/Down

        // Update registers
        T1CTL &= ~0x0C;
        T1CTL |= prescaler;
        T1CC0H = (uint8)(ticks>>8);
        T1CC0L = (uint8)ticks;        
    #endif
        
        return 1;
    }

    void buzzerStop(void)
    {
    #if defined ( CC2540_MINIDK )  
        T1CTL &= ~0x03;             // Stop timer 1
    #endif
    }
    发现PIO1-2上输出的波形是不联系的,单就timer 1的设置来看,这一段配置有什么问题吗?

  • Hi Yan,

         我使用timer 3的设置配置,看原来的io1-6,发现波形也是不连续的。

  • 外部clock正常吗?

  • 32M正常工作

  • 测量发现32M震荡不连续,换了几个晶振,现象一样,是不是IC问题?

  • 你是在IO1-6上用示波器测量的吗?波形是指那个PIN脚?

  • PIN38,直接2540IC脚上量的。

  • 这是个GPIO的输出,波形不连续是正常的(一直输出岂不是很耗电?要有数据传输的时候才输出时钟)。这不是晶振的直接输出。

  • 我的问题不是要一直输出,只是在我需要的这段时间内波形的连续输出。我的问题解决了,我发现软件里有控制晶振不让它切换到32K内部模式下的函数,一直输出32M就可以。感谢你的回答!

  • Hi,

    很高兴看到你能把问题自己解决了, 谢谢你的努力, 希望下次能再帮到你:)

  • 你好!

    我想请问一下,怎么控制晶振不切换到32K内部模式下,期待你的回复,谢谢!

x 出现错误。请重试或与管理员联系。