0

I am a beginner at AVR and programming. I am using ATtiny461 and asked to make code to use timer in PWM and CTC mode and use interrupts and pin toggle, this code worked for me with normal timer and toggle but I don't know how to make it work with CTC and PWM:

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

//Init PD6 pin as output
void InitPort(void)
{
    PORTB&=~(1<<PB1);//initial value 0
    DDRB|=(1<<PB1);
}
//Initialize Timer0
void InitTimer0(void)
{
    //Enable Timer0 overflow Interrupt
    TIMSK|=(1<<TOIE0);
    //Set Initial Timer value
    TCNT1=5;
}
void StartTimer0(void)
{
    //Set prescaller 64 and start timer
    TCCR0B|=(1<<CS01)|(1<<CS00);
    //Enable global interrupts
    sei();
}
//Timer0 Overflow ISR
ISR(TIMER0_OVF_vect)
{
    //Reset Initial Timer value
    TCNT1=5;
    //toggle PD6 pin
    PINB=(1<<PB1);
}

int main(void)
{
    InitPort();
    InitTimer0();
    StartTimer0();
    while(1)
    {
        //doing nothing
    }
}

and I know those commands for setting fast PWM and CTC:

TCCR1D |= (0<<WGM10)|(0<<WGM11);
TCCR1A |= (1<<PWM1A)|(1<<COM1A0)|(0<<COM1A1);
1

PWM is only supported for timer1 on the ATTiny461. Not in timer0.

Look at TCCR1A; specifically the COM1xn bits.

  • ok,thank you,good so i use timer1...but where can i include using PWM,TCCR1A und com1A,B bits in this code...since the code above worked for me but i didnt understand the whole sequnce..since the company asked me for results. – sumer fattoum Jul 29 '15 at 13:09
  • You can put it at the beginning of main. Or copy/replace the functions InitTimer0 and startTimer0 and change 0 to 1. Change the contents to apply to timer 1, and call those functions in main, just like with timer0 – Gerben Jul 29 '15 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.