Im trying to send a PWM signal on the OC0A (PB2) with ATTiny2313 but for some reason nothing is happen on Port B2. My code shows as following:
#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;
}
The LED on PB0 is blinking, but on oscilloscope is no PWM signal (on PB2) is shown and the led on PB2 is still off. Do i have missconfigured the MCU?
Similar code on ATTiny13A is still working.
Ok, i accidentally selected a wrong MCU architecture (in my case attiny13 instead of attiny2313) on project creation in Code::Blocs. I solved this by changing the configuration in ".cbp" and changed any attiny13 to attiny2313 (in my case in environment, compiler and linker config).
How i detected this: on trying to set
to test OC0B instead of OC0A as PWM output the compiler error is occured like DDRD can not be found.