在ATtiny85上设置高速PWM

时间:2014-02-09 02:50:16

标签: c interrupt avr atmel pwm

我在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;

2 个答案:

答案 0 :(得分:0)

解决方案涉及CKDIV8保险丝。然而,为了正确编程该保险丝,需要HVSP“高压串行编程”。移除此保险丝后,器件工作在8 MHz,PWM输出为400 kHz。我希望其他人觉得这很有用!

答案 1 :(得分:0)

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