快速PWM-OC0A(PB2)上的ATTiny2313

时间:2018-10-01 18:58:08

标签: c atmel avrdude attiny

我正在尝试使用ATTiny2313在OC0A(PB2)上发送PWM信号,但由于某些原因,端口B2上没有任何反应。我的代码如下所示:

#define F_CPU 8000000UL

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

/**
 * Initialize fast pwm on PB2 (OC0A)
 */
void pwmInit() {
    // Setup the output for PWM
    DDRB |= (1 << DDB2);

    // Set Timer/Counter0 prescaler to clock/1.
    // At 8MHz this is 8MHz.
    TCCR0B |= (1 << CS00);

    // Set to 'Fast PWM' mode
    TCCR0A |= (1 << WGM01) | (1 << WGM00);

    // Clear OC0A output on compare match, upwards counting.
    TCCR0A |= (1 << COM0A1);

    // If the value '128' is reached, the PWM signal will set to LOW
    OCR0A=128; // 128 = 50% duty cycle
}

void setup() {
    pwmInit();

    DDRB |= (1 << DDB0); // Setup the Output fon port B0
}

int main(void) {
    setup();

    while(1) {
        PORTB |= (1<<PB0);
        _delay_ms(500);

        PORTB &= ~(1<<PB0);
        _delay_ms(500);
    }

    return 0;
}

PB0上的LED闪烁,但示波器上未显示PWM信号(PB2上),并且PB2上的LED仍然熄灭。我是否未正确配置MCU?

ATTiny13A上的类似代码仍在工作。

1 个答案:

答案 0 :(得分:0)

好的,我在Code :: Blocs中创建项目时不小心选择了错误的MCU架构(在我的情况下为attiny13而不是attiny2313)。我通过更改“ .cbp”中的配置来解决此问题,并将所有attiny13更改为attiny2313(在我的环境中,是编译器和链接器配置)。

我如何检测到这一点:尝试设置

DDRD |= (1<<DD5);

要测试OC0B而不是OC0A作为PWM输出,则会发生编译器错误,例如找不到DDRD。