快速 PWM - OC0A (PB2) 上的 ATTiny2313
Fast PWM - ATTiny2313 on OC0A (PB2)
我正在尝试使用 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 上的类似代码仍然有效。
好的,我在 Code::Blocs 的项目创建中不小心选择了错误的 MCU 架构(在我的例子中是 attiny13 而不是 attiny2313)。我通过更改“.cbp”中的配置并将任何 attiny13 更改为 attiny2313 解决了这个问题(在我的情况下,在环境、编译器和链接器配置中)。
我是如何检测到的:在尝试设置时
DDRD |= (1<<DD5);
测试 OC0B 而不是 OC0A 作为 PWM 输出时出现编译器错误,如无法找到 DDRD。
我正在尝试使用 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 上的类似代码仍然有效。
好的,我在 Code::Blocs 的项目创建中不小心选择了错误的 MCU 架构(在我的例子中是 attiny13 而不是 attiny2313)。我通过更改“.cbp”中的配置并将任何 attiny13 更改为 attiny2313 解决了这个问题(在我的情况下,在环境、编译器和链接器配置中)。
我是如何检测到的:在尝试设置时
DDRD |= (1<<DD5);
测试 OC0B 而不是 OC0A 作为 PWM 输出时出现编译器错误,如无法找到 DDRD。