Hi, I am trying to create a PWM on Attiny13, but it fail (PB0 pin - silent). What wrong? Thx.
- Code:
#define F_CPU 1200000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#define PWM_PIN PB0
#define LED_PIN PB4
#define BUTTON1_PIN PB2 //+
#define BUTTON2_PIN PB3 //-
int
main(void)
{
//TCCR0A = ((1<<WGM00) | (1<<WGM01) | (1 << COM0A1)) ; // Fast PWM mode 3, Clear OC0A on Compare Match, set OC0A at TOP
TCCR0A |= (1<<COM0A1);
TCCR0A |= (1<<WGM01)|(1<<WGM00); //FAST PWM
TCCR0B |= (0<<CS02)|(1<<CS01)|(0<<CS00); //F = F_CPU/(N * 256); F= 1200000Hz/(8*256)= 585.94 Hz //prescaler 8
// i/o
DDRB |=(1<<PWM_PIN);
PORTB&=~(1<<PWM_PIN);
//Led
DDRB |=(1<<LED_PIN);
PORTB&=~(1<<LED_PIN);
//Buttons
DDRB &=~(1<<BUTTON1_PIN);
PORTB|=(1<<BUTTON1_PIN);
DDRB &=~(1<<BUTTON2_PIN);
PORTB|=(1<<BUTTON2_PIN);
volatile uint8_t PWM = 0;
OCR0A = PWM;
while (1)
{
if ((PINB & (1<<BUTTON1_PIN))==0) //ADD +
{
if (PWM< 254)
{
PWM++;
}
while ((PINB & (1<<BUTTON1_PIN))==0)
{
}
}
if ((PINB & (1<<BUTTON2_PIN))==0) //SUB -
{
if (PWM>1)
{
PWM--;
}
while ((PINB & (1<<BUTTON2_PIN))==0)
{
}
}
if (OCR0A>9)
{
PORTB |=(1<<LED_PIN);
}
else
{
PORTB&=~(1<<LED_PIN);
}
OCR0A = PWM;
}
}