尝试使用C来使用ATTiny85,利用快速PWM来调整LED并使其随着时间变亮。代码应该正常工作,但事实并非如此

时间:2017-02-27 06:51:39

标签: c led pwm

尝试在AATiny85上使用快速PWM打开和关闭LED。尝试将此配置为打开然后关闭以及使用for循环逐渐关闭它。我们编译和编写代码时遇到的问题是代码在面包板内执行时,LED只是闪烁而没有任何频率或亮度的变化。如果代码工作正常,LED应根据变量值的当前值逐渐增加或减少亮度。'任何帮助将不胜感激。

代码:

#define F_CPU 20000000   


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


 double dutyCycle = 0;
 int d = 0;

 int main(void)
{
    DDRB = 1 << PORTB0;
    TCCR0A = (1 << COM0A1) | (1 << WGM00) | (1 << WGM01);

    TIMSK = (1 << TOIE0);
    OCR0A = (dutyCycle/100)*255;

    sei();

    TCCR0B = (1 << CS00) | (1 << CS02); // needs to be last or else clock starts

    while(1){
        _delay_ms(1000);
        dutyCycle += 10;
        if(dutyCycle > 100){
            dutyCycle = 0;
            for(d = 100; d >=0; d-=10){
                _delay_ms(10000);
                dutyCycle = (double)d;
            }
        }
    }
}

ISR(TIMER0_OVF_vect) //what happens on overflow    (interrupt)
{
    OCR0A = (dutyCycle/100)*255;
}

0 个答案:

没有答案