Вы находитесь на странице: 1из 5

Food Living Outside Play Technology Workshop

Apple-style LED pulsing using a $1.30 MCU


by hardwarehank on February 21, 2011

Table of Contents

Apple-style LED pulsing using a $1.30 MCU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Intro: Apple-style LED pulsing using a $1.30 MCU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 1: Supplies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 2: Understanding PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 3: The datasheet - your new best friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 4: Bread it up, homie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 5: The code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 6: The result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

http://www.instructables.com/id/Apple-style-LED-pulsing-using-a-130-MCU/
Author:hardwarehank Schadenfreude
Just a dude who reads a lot of Instructables.

Intro: Apple-style LED pulsing using a $1.30 MCU


The Atmel ATTiny85 chip is an 8-pin MCU that is totally awesome. If you've been programming with the bigger boys (the ATMega series), these are a nice adventure -
you're rather limited in the number of output pins, but a creative design gives us a lot of flexibility in a very small package.

You've seen them - those "Apple computers." Probably in the hands of some Hipster in Portland, while riding his fixie and wearing those thick framed glasses. That
pulsating light when Apple laptops are asleep is so ... sooooothing. You just want to go to sleep watching it. You know you do.

Today, we're going to replicate that using our ATTiny85. It's really easy, and most of it can be implemented in hardware instead of code (!!!).

Step 1: Supplies
1. Breadboard
2. Breadboard wires
3. 1 LED
4. ATTiny85 (the 45 or 25 will probably work, but I haven't tested them)
5. An ISP programmer (or other, if you know how to use it) - I use the USBtinyISP.
6. A computer with avrdude and avr-gcc
7. Optional: an oscilloscope

Step 2: Understanding PWM


In this guide, we're going to use Fast PWM, which is just one of the modes available on our little chippy friend. I've attached Atmel's very educational document on PWM -
I suggest you read it and familiarize yourself with the magic.

File Downloads

AVR131.Using.PWM.pdf (115 KB)


[NOTE: When saving, if you see .tmp as the file ext, rename it to 'AVR131.Using.PWM.pdf']
Step 3: The datasheet - your new best friend
The ATTiny 25/45/85 datasheet is the core of this project. Without it, we'd all be lost, poking our chips with sticks and screaming like chimpanzees. Well, most of us,
anyway.

Download it and go to section 11 and just take 30 minutes and read the entire thing. Trust me, it's worth it - it's a lot better than randomly paging through it for hours trying
to debug your code! :P

File Downloads

Atmel.ATTiny.25.45.85.Datasheet.pdf (4 MB)
[NOTE: When saving, if you see .tmp as the file ext, rename it to 'Atmel.ATTiny.25.45.85.Datasheet.pdf']

http://www.instructables.com/id/Apple-style-LED-pulsing-using-a-130-MCU/
Step 4: Bread it up, homie
Let's do some breaddage. I've attached a picture, but the rest is up to you, young padawan. Read the datasheet pinout and the pinout of your programmer and get that
mojo workin'. There's like a million guides on how to do this, so do your homework. This is a skill you should have - datasheets are the bomb, yo .

Step 5: The code


Here's my code. Hopefully it's commented well enough.
/* LED Pulsate with PWM
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/sleep.h>

int main()
{
// Turn on LED output pin
DDRB |= _BV(DDB0);

// Clear OC0A on match


TCCR0A |= _BV(COM0A1);

// Set timer to count with F_CPU / 64


TCCR0B |= _BV(CS10); // F_CPU
TCCR0B |= _BV(CS01) | _BV(CS00); // /64 prescale

// Use Fast PWM, OCRA TOP


TCCR0A |= _BV(WGM00);
TCCR0A |= _BV(WGM01);
//TCCR0B |= _BV(WGM02);

// Initial value for our pulse width is 0


OCR0A = 0x00;

uint8_t dir = 1; // Direction


uint16_t div = 500; // # of clocks per inc/decrement
uint16_t stall = 0; // Initial stall counter
uint16_t stall_time = 500; // # of clocks to stall at top/bottom
for(;;) {
// We only want to update every div counts
if(TCNT0 % div != 0) continue;

// Stall at the top and bottom, and change direction


if(OCR0A == 255 || OCR0A == 0) {
// Switch directon at top
if(OCR0A == 255) dir = -1;
else
{ // Disable LED at bottom
// And switch direction
DDRB & ~(_BV(DDB0));
dir = 1;
}
while(stall < stall_time)
{
++stall;
_delay_ms(10);
}
// Turn output back on if necessary
if(OCR0A == 0) DDRB |= _BV(DDB0);
stall = 0;
}
// General case - increment direction either way
OCR0A += dir;
}
return 0;
}

Hopefully, this code is self-documenting. Basically, we're changing the pulse width every few hundred clocks in one direction or the other. We have a special case for
when we get to the top or bottom (change direction and linger), just like the Apple stuff does. There's another special case when we want to completely turn off the LED at
the bottom - if we don't do this, it still has a (1/256)% duty cycle, and you can see it. Otherwise, the code should be rather concise. Constructive criticism encouraged.
http://www.instructables.com/id/Apple-style-LED-pulsing-using-a-130-MCU/
Step 6: The result
Here's a short video of the result. I'm pretty happy with it. Get some of the chips and go to town!

Related Instructables

Simplest and
Cheapest
Turn Your Arduino by
Arduino Into an smching Self-contained ATTiny85 Traffic Program an ATtiny
ISP by mr_mac3 16-Digit display Light by ATtiny with programming
- Arduino & nthdesign Arduino by with Arduino by
Attiny85 by randofo wirenut1980
Panici

Advertisements

Comments
6 comments Add Comment

M.Hawse says: Mar 19, 2012. 2:31 AM REPLY


Thank you HardwareHank for taking the time to post this jewel! Thanks also to jdelongpre & average_male for the Arduino conversion info!

I'm diving back into 'lectronics* prototyping again after a 5 year break. I just got an Arduino Mega R3 and am loving it.

This is a cool project that's looks easy to complete because it's well documented with plenty of tips and suggestions. Great job...you've made it easier and
fun for the rest of us.

*NOTE: 'lectronics - This is a term usually heard only in the US Southern States. Usually, it's being shouted over a 2W loudspeaker system in Walmart: " ...
'lectronics, Line 1...."

Dodge says: Mar 2, 2011. 6:01 PM REPLY


Nice effect! I like it. *favorited*

I'm just getting started with microcontrollers, and projects like this seem doable for a noob.

With some tweaking, this sketch would work on an Atmega chip as well, right?

http://www.instructables.com/id/Apple-style-LED-pulsing-using-a-130-MCU/
hardwarehank says: Mar 3, 2011. 6:11 AM REPLY
Thanks :).

It is definitely doable for someone inexperienced with microcontrollers who is just getting comfortable with the AVR platform. It could definitely be
adapted to an ATmega - the only changes would be register naming I think. Just find the equivalent PWM registers in your datasheet and everything
should work fine :).

Oh, and AFAIK, sketch refers to an Arduino program written in Processing. This is a program. It's fo' real, yo!

jdelongpre says: Oct 16, 2011. 2:23 PM REPLY

In Arduino, sketch refers to ANY program. They are called "sketches" to keep artsie folks from getting scared by the name.

To run this on an Arduino, you must change just one line:


DDRB |= _BV(DDB0); // for Attiny85 OC0A=PB0 (comment added)
-- to--:
DDRD |= _BV(DDB6); // for Arduino OC0A=PD6
-- or-- in the language of a sketch:
pinMode(6, OUTPUT); // enable OC0a output
The two forms do exactly the same thing and either works.

Now use digital pin 6 for your LED (with a current limiting resistor in series to ground)

average_male says: Jun 17, 2011. 4:41 PM REPLY


Is the comment still correct for the Fast PWM when the code has the assignment of the "TCCR0B" register commented out?

// Use Fast PWM, OCRA TOP


TCCR0A |= _BV(WGM00);
TCCR0A |= _BV(WGM01);
//TCCR0B |= _BV(WGM02);

Seeing how TCCR0B is not assigned, the comment should read as follows per the datasheet:

// Use Fast PWM, OCR0A = 0xFF


TCCR0A |= _BV(WGM00);
TCCR0A |= _BV(WGM01);

Or does that matter as your code happens to assign 0xFF (255) to OCR0A via the following line and then later have a check for this value elsewhere in the
code:

// General case - increment direction either way


OCR0A += dir;

Please advise,
Moe Howard (brother of Shemp)

hardwarehank says: Jun 18, 2011. 7:23 AM REPLY


I think you're right. It's been a while since I've looked at this, but I think that OCRA TOP part refers to the commented line involving TCCR0B. Cheers.

http://www.instructables.com/id/Apple-style-LED-pulsing-using-a-130-MCU/

Вам также может понравиться