Skip navigation
TechXchange
Digi-Key TechXchange Communities > Projects and Designs > Discussions
10880 Views 6 Replies Latest reply: Mar 30, 2012 8:59 AM by umadevi RSS
Currently Being Moderated

Mar 18, 2012 11:02 AM

How to write a program for controlling the brightness of LED using ATMega8 ?

I tried so many times but thats not controling the LED brightness. please help me .....

  • Robert Nelson designspecialist-micro 68 posts since
    Mar 21, 2011

    Hi Umadevi,

     

    What method's have you tried to control the brightness of an LED with the atmega8?

     

    With the atmega8, there's a couple options.. Using the PWM channels, or just using regular i/o with a timing loop turning on/off the port pins..

     

    Regards,

  • richcj10 Novice 8 posts since
    Mar 26, 2012

    Setting up timers are the best. Example code:

     

    #include <avr/io.h>
    #include <util/delay.h>
    
    /*
     * Assumptions:
     *      - LEDs connected to PORTB.7 and PORTB.6
     */
    
    int main (void)
    {
         /* Waveform Generation Mode 3 - Fast PWM */
         TCCR0A |= _BV(WGM01) | _BV(WGM00);
    
         /*
          * Compare Output Mode - fast PWM
          * Non-inverting mode drives the output high while the counter
          * is greater than OCRNx. Inverting mode drives the output low
          * while the counter is greater than OCRNx.
          */
         TCCR0A |= _BV(COM0A1) | _BV(COM0A0); /* inverting: fade down */
         TCCR0A |= _BV(COM0B1); /* non-inverting: fade up */
    
         /* reset all the timers and comparators */
         OCR0A = 0;
         OCR0B = 0;
         TCNT0 = 0;
    
         /*
          * Clock Source 1 - CLK. Setting this bit late allows us to
          * initialize the registers before the clocks start ticking
          */
         TCCR0B |= _BV(CS00);
    
         /*
          * Arduino pins 5 & 6 (PORTD.5 and PORTD.6) are PWM driven by TIMER0
          * "The setup of the OC0x should be performed before setting the Data
          * Direction Register for the port pin to output." -- S14.5.3
          */
         DDRD |= _BV(PORTD5) | _BV(PORTD6);
    
         while (1){
              /*
               * slowly crank up the compare register. since one output
               * is inverting, the net result is to fade from one channel
               * to the other.
               */
              OCR0A++;
              OCR0B++;
              _delay_ms(10); /* busy wait. could be done with timers too. */
         }
         return 0;
    }

More Like This

  • Retrieving data ...

Bookmarked By (0)