c - 在 ATtiny85 上设置高速 PWM

标签 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;

最佳答案

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

关于c - 在 ATtiny85 上设置高速 PWM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21654618/

相关文章:

node.js - libuv 和 Node.js 实际上是如何调度定时器的?

c - 做 while 循环过早退出

c - 如何仅更新微 Controller 中代码的某些部分或 API

ios - 从 objective-c 生成 key 并使用 OpenSSL 签名创建 CSR

c++ - 有比这更短的方式吗?(计算 C 中字符串中 char 的出现次数)

java - 停止一个线程并启动另一个线程?

windows - 内核模式驱动程序和用户模式应用程序之间的双向通信?

timer - Arduino PWM 衰落由 avra 引导

c - sscanf 使用 c 进行 float

c++ - 用于嵌入式 Linux 的非 GPL C/C++ XMPP 客户端库