2

我在 ATtiny85 上设置高速 PWM 时遇到问题。我需要以 400 kHz 的速度使用 PCK。我相信我正确地遵循了数据表,但由于某种原因,定时器中断标志不起作用。

如果我对设备进行编程,相应引脚的输出是恒定的 5 V。

如果我注释掉 PCK 设置并改用系统时钟,则标志设置正确并且 PWM 工作正常。代码已发布。为什么没有设置标志并且 PWM 不工作?

#include <avr/io.h>
#include <avr/interrupt.h>


int main(void)
{
    PORTB = 0;        //Reset values on port B

    // After setting up the timer counter,
    // set the direction of the ports to output
    DDRB |= (1<<PB1) | (1<<PB0); // Set the direction of PB1 to an output

    // PLLCSR - PLL control and status register:
    // PLL is a clock multiplier - multiplies system     8 MHz by 8 to 64 MHz
    // PLL is enabled when:PLLE bit is enabled,
    // CKSEL fuse is programmed to 0001.  This clock is
    //   switched off in sleep modes!
    PLLCSR |= (1<<PLLE);    // PLL enable

    // Wait until the PLOCK bit is enabled
    // before allowing the PCK to be enabled
    //WaitForPLOCK();
    //unsigned int i = 0;

    while ((PLLCSR & (1<<PLOCK)) == 0x00)
    {
        // Do nothing until plock bit is set
    }

    PLLCSR |= (1<<PCKE); // Enable asynchronous mode, sets PWM clock source


    TCCR1 =
            (1<<CTC1)    | // Enable PWM
            (1<<PWM1A)   | // Set source to pck
            (1<<(CS10))  | // Clear the pin when match with ocr1x
            (1<<COM1A1);
    GTCCR =   (1<<PWM1B) | (1<<COM1B1);


    // Set PWM TOP value - counter will count and reset
    //  after reaching this value
    //            OCR1C
    // 400 kHz    159
    // 450 kHz    141
    // 500 kHz    127
    OCR1C = 159;


    // Enable Timer1 OVF interrupt
    TIMSK = (1<<OCIE1A) | (1<<TOIE1);

    sei();

    // This should set the duty cycle to about 75%
    OCR1A = 120;
4

2 回答 2

2

该解决方案涉及 CKDIV8 保险丝。但是,要正确编程此保险丝,需要 HVSP“高压串行编程”。在移除此保险丝以使设备以 8 MHz 运行后,PWM 提供 400 kHz 输出。我希望其他人觉得这很有用!

于 2014-02-11T01:45:23.267 回答
0

勘误表(数据表的第 27 节)指出“PLL 不会锁定在 6 MHz 以下”。列出的唯一解决方法是将时钟设置为 6 MHz 或更高。

于 2014-02-26T01:44:54.300 回答