Code:
#include <avr/io.h>
#include <util/delay.h>
void init_pwm()
{
  TCCR0|=(1<<WGM00)|(1<<WGM01)  //fast pwm
  |(1<<COM01)                  //Clear OC0 on compare match, set OC0 at BOTTOM
  |(1<<CS00);        //fcu/(No prescaling)
   //Set OC0 PIN as output. It is  PB3 on ATmega16
   DDRB|=(1<<PB3);
}
int main()
{
   uint8_t i=0;     // variable i is to control the brightness of led
   init_pwm();      //Initialize PWM Channel 0
   while(1)
   {
      // increasing brightness
      for(i=0;i<255;i++)
      {
         //Now Set The Brighness using PWM
        OCR0=i;
        //Now Wait For Some Time
         _delay_ms(10);
      }
      // decreasing brightness
      for(i=255;i>0;i--)
      {
         //Now Set The Brighness using PWM
         OCR0=i;
         //Now Wait For Some Time
         _delay_ms(10);
      }
   }
return (0);
}
If you connect a LED to PB3 you will notice the brightness of the led is increasing and decreasing gradually. there's my video:
is this a fake code ? not working on proteus ISIS
ReplyDelete