TI论坛
直播中

王静

8年用户 684经验值
私信 关注
[问答]

怎么配置timer1来达到我设置蜂鸣器频率的要求?

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

回帖(13)

王静

2020-8-28 07:39:54
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
]
以上红色部分配置不正确,能帮忙指出该怎么修改吗?
举报

华农武

2020-8-28 07:51:28
引用: linlin10 发表于 2020-8-28 07:39
void buzzerInit(void)
[
#if defined ( CC2540_MINIDK )  

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的配置去写代码.
举报

王静

2020-8-28 08:01:55
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的设置来看,这一段配置有什么问题吗?
举报

王静

2020-8-28 08:15:36
引用: linlin10 发表于 2020-8-28 08:01
Hi Yan,
硬件已经做成这样只能用timer 1了,通过看swru191c.pdf里timer 1的寄存器,软件修改如下:
void buzzerInit(void)

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

更多回帖

发帖
×
20
完善资料,
赚取积分