0

I don't know what's wrong with the code I have activated all the bits I need. I want the code to slowly increase the brightness of the bulb and then reset and then the same thing goes on. Please help me find the error in the code.

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

int duty_cycle;

int main()
{
 duty_cycle=50;
 DDRD=0b01000000;
 PORTD=0b01000000;

 TCCR0A=(1<<COM0A0)|(1<<WGM00)|(1<<WGM01);
 TIMSK0=(1<<TOIE0)|(1<<OCIE0A);
 OCR0A=duty_cycle;
 sei();
 TCCR0B=(1<WGM02)|(1<CS00);
 while(1)
 {}

 return 0;
 }
 ISR(TIMER0_COMPA_vect)
 {if(duty_cycle<250)
 {duty_cycle=duty_cycle+10;}
 else
 duty_cycle=50;
 }
  • Changing a variable alone does nothing. You have to do something with what is in that variable. – Majenko Apr 4 at 18:46
  • I don't get what u mean by "do something with what is in the variable" but I have assigned the variable to OCR0A. – user173379 Apr 4 at 18:58
  • No, you didn't. At one point in the execution of your program, you copied the content of the variable duty_cycle into the register OCR0A. – Majenko Apr 4 at 18:59
  • @Majenko so are u saying me to update OCR0A in ISR – user173379 Apr 4 at 19:09
  • 1
    You certainly need to update it somewhere. However is what you are doing in the ISR really what you want to do? That will change the duty cycle very fast. Too fast to see it do anything. – Majenko Apr 4 at 19:18

Your Answer

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

Browse other questions tagged or ask your own question.