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 に答える