NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Simple 'Dimming an LED example'

August 05, 2009
by mcai8sh4
mcai8sh4's Avatar

Greetings fellow Nerdlings!!

There has been some interest in how to change the brightness for an LED, the answer to this is PWM (either using the facilities built into the chip or emulating it).

I just been talking to pH on the irc channel and decided to post a simple method (using PB1).

The following code simple makes an LED go brighter and dimmer... forever (or until the battery runs out or your cat eats it!)

If you read the code and check out the datasheet (essential reading folks - hard work, but essential), you should get the idea of how this works.

#define F_CPU 14745600
#include <stdio.h>
#include <avr/io.h>
#include "../libnerdkits/delay.h"

// Simply bright then dim an LED (on PB1)

int main(void)
{
            OCR1AH = 0;
            DDRB |= (1<<PB1);         //PortB 1 as output
            TCCR1A = (1<<COM1A1)|(1<<1);   // OC1A to zero, 8-Bit PWM
            TCCR1B = 1;             //Start PWM
            for (;;) {
                            int i;
                            for (i=0; i<255; i++) {
                                            OCR1AL = i;         //Change lowbyte of OCR1A
                                            delay_ms(15);
                            }
                            for (i=255; i>0; i--) {
                                            OCR1AL = i;
                                            delay_ms(15);
                            }
}
return 0;   //  never reached

Code can also be found HERE

NOTE : I only hacked this together from bits of the web/from the data sheet - I still don't fully understand it, so I probably won't be able to answer any technical questions, but hey I'll try!!

Another method you could use is similar to the Valentines Heart tutorial - I haven't looked into this yet, but at some point I will, then I'll post a simple example (as above) for that method.

August 05, 2009
by pedroh96
pedroh96's Avatar

I am AKA pH, :P

August 19, 2009
by Nerdful_com
Nerdful_com's Avatar

Here is an example that dims all portb pins high to low with adjustable speeds.

#include <avr/io.h>
#include <avr/interrupt.h>
/*
 * Illustrating PWM
 * www.NERDFUL.com was here!
 */
int main (void)
{
unsigned char ctr, brightness, speed;
/* set PORTB for output*/
DDRB = 0xFF;
ctr = 0;
for(brightness = 0; ; brightness++){ // brightness
for(ctr = 0; ctr < 255; ctr++){
for(speed = 0; speed < 128; speed++){ // ramp-up speed
if (ctr < brightness)
PORTB = 0xff;
else
PORTB = 0x00;
}
}
}
return 0;
}

// Hope this helps? Nerdful.com

Post a Reply

Please log in to post a reply.

Did you know that NerdKits has been featured on Slashdot, Hack A Day, Hacked Gadgets, the MAKE blog, and other DIY-oriented websites? Learn more...