AVR Studio上的脉冲宽度调制(PWM)

mic*_*jiz 9 avr atmega width pulse

我正在尝试将PWM用于ATmega8上的LED,端口B的任何引脚.设置定时器一直很烦人,我不知道如何处理我的OCR1A.这是我的代码,我喜欢一些反馈.

我只想弄清楚如何使用PWM.我知道这个概念,OCR1A应该是我希望脉冲开启的整个计数器时间的一小部分.

#define F_CPU 1000000  // 1 MHz

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

int main(void){

    TCCR1A |= (1 << CS10) | (1 << CS12) | (1 << CS11);
    OCR1A = 0x0000;
    TCCR1A |= ( 0 << WGM11 ) | ( 1 << WGM10 ) | (WGM12 << 1) | (WGM13 << 0);
    TCCR1A |= ( 1 << COM1A0 ) | ( 0 << COM1A1 );
    TIMSK |= (1 << TOIE1); // Enable timer interrupt
    DDRB = 0xFF;
    sei(); // Enable global interrupts
    PORTB = 0b00000000;

    while(1)
    {
        OCR1A = 0x00FF; //I'm trying to get the timer to alternate being on for 100% of the time,
        _delay_ms(200);
        OCR1A = 0x0066; // Then 50%
        _delay_ms(200);
        OCR1A = 0x0000; // Then 0%
        _delay_ms(200);
    }
}

ISR (TIMER1_COMA_vect)  // timer0 overflow interrupt
{
    PORTB =~ PORTB;
}
Run Code Online (Sandbox Code Playgroud)

mic*_*jiz 6

您需要使用以下两行初始化OCR1A:

TCCR1A = (1 << WGM10) | (1 << COM1A1);
TCCR1B = (1 << CS10) | (1 << WGM12);
Run Code Online (Sandbox Code Playgroud)

然后用这个:

OCR1A = in
Run Code Online (Sandbox Code Playgroud)

并且知道范围是0-255.计算你的百分比,你有它!

#define F_CPU 1000000  // 1 MHz
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/interrupt.h>

int main(void){
    TCCR1A = (1 << WGM10) | (1 << COM1A1);
    TCCR1B = (1 << CS10) | (1 << WGM12);
    DDRB = 0xFF;
    sei(); // Enable global interrupts
    PORTB = 0b00000000;

    while(1)
    {
        OCR1A = 255;
        _delay_ms(200);
        OCR1A = 125;
        _delay_ms(200);
        OCR1A = 0;
        _delay_ms(200);
    }
}
Run Code Online (Sandbox Code Playgroud)


vsz*_*vsz 6

不,这不是你应该如何做PWM的方式.例如,如何设置PWM率,例如42%?此外,代码大小很大,可以以更有效的方式完成.此外,您浪费了16位定时器来执行8位操作.你有2x 8位定时器(定时器/计数器0和2)和一个16位定时器,Timer/Counter 1.

将未使用的portpins设置为输出也是一个坏主意.所有未连接任何东西的portpins都应作为输入.

ATmega8在定时器1和2上有一个内置PWM发生器,无需通过软件进行模拟.您甚至不必手动设置端口(您只需将相应的portpin设置为输出)

你甚至不需要任何中断.

#define fillrate OCR2A 


 //...

 // main()

PORTB=0x00;
DDRB=0x08;  //We use PORTB.3 as output, for OC2A, see the atmega8 reference manual

// Mode: Phase correct PWM top=0xFF
// OC2A output: Non-Inverted PWM
TCCR2A=0x81;
// Set the speed here, it will depend on your clock rate.
TCCR2B=0x02;

// for example, this will alternate between 75% and 42% PWM
while(1)
{
    fillrate = 191; // ca. 75% PWM
    delay_ms(2000);

    fillrate = 107; // ca. 42% PWM
    delay_ms(2000);
}
Run Code Online (Sandbox Code Playgroud)

请注意,您可以使用另一个带有另一个PWM的LED,使用相同的定时器并设置OCR2B而不是OCR2A.不要忘记设置TCCR2A以启用OCR2B作为PWM的输出,因为在此示例中仅允许OCR2A.