Blinkentree

From syn2cat - HackerSpace.lu
(Difference between revisions)
Jump to: navigation, search
(Flashing the microcontroller)
 
(36 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Diese Seite gibt es [[ Blinkentree_(de) | hier auch in deutsch]].
 
 
 
 
{{Project
 
{{Project
 
|type=hardware
 
|type=hardware
|intro=
+
|intro=We built our own blinking X-mas tree for the market in Strassen.
 
|logo=BlinkenWood.jpg
 
|logo=BlinkenWood.jpg
|status=running
+
|status=concluded
 +
|tracker=http://source.hacker.lu/projects/blinkentree
 
|meetings=none
 
|meetings=none
 
|contact=slopjong, prometheus
 
|contact=slopjong, prometheus
 
}}
 
}}
 +
Diese Seite gibt es [[ Blinkentree_(de) | hier auch in deutsch]].
 +
 +
[http://translate.google.de/translate?hl=de&sl=en&tl=fr&u=http%3A%2F%2Fhackerspace.lu%2Fwiki%2FBlinkentree Site en français]
 +
 +
  
 
== Introduction  ==
 
== Introduction  ==
  
What is this flashing green fir tree about?  
+
What is this flashing green fir tree all about?  
  
As creative hackers, we thought it would be necessary to support the Christmas market of the commune of Strassen with something tiny, but creative. Result? The Blinkentree. Obviously it represents a green fir tree that is dorned with flashing LEDs which should represent the baubles of a 'real' tree.
+
As creative hackers, we thought it would be great to support the Christmas market of the commune of Strassen with something tiny, but creative. Result? The Blinkentree. A green fir tree, adorned with flashing LEDs representing the ornaments of a 'real' tree.
  
'''A big thanks to all of you folks who purchsed a Blinkentree and thus supporting syn2cat Hackerspace.'''
+
Here are two links about [http://blog.hackerspace.lu/2010/12/07/33/ how the Blinkentree was made] and [http://blog.hackerspace.lu/2010/12/17/christmas-christmas-time-is-here/ where it was sold].
  
 +
'''A big thanks to all of you folks who purchsed a Blinkentree and thus supported syn<sub>2</sub>cat Hackerspace.'''
  
 
'''Hacky Christmas!'''
 
'''Hacky Christmas!'''
Line 28: Line 32:
 
-->
 
-->
  
We recommend to first solder the resistors, then the chip socket and then the LEDs because it's easier to start with the small devices and finally solder the biggest ones.  
+
If you didn't buy a kit but an already assembled Blinkentree, you can continue [https://www.hackerspace.lu/w/index.php?title=Blinkentree#How_to_program_it_on_your_own.3F here].
  
 +
Otherwise, follow the next instructions to assemble your kit. We recommend first soldering the resistors, then the chip socket, and finally the LEDs because it's easier to start with small devices and finish up with the biggest ones.
  
# The resisostors have all the same value and they don't have any polarity so it doesn't matter what direction they're put on the board. [[File:Front.png | thumb | none | Place the resistors as shown here. ]]
+
 
# The chip socket has an indentation which must be oriented down. [[File:Chip_socket.JPG | thumb | none | Chip direction. The indentation comes down. ]][[File:Chip.JPG | thumb | none | Chip direction. The indentation comes down. ]]
+
# The resistors all have the same value, and as they don't have a polarity it doesn't matter what direction they're put on the board. [[File:Front.png | thumb | none | Place the resistors as shown here. ]]
# The LEDs have a polarity. The long leg is positive (+) and the small one is negative (-). The current flows from the positive to the negative so you have to pay attention while you assemble the kit. The LEDs are flat on the negative side. You have to orientate the LEDs as shown on the second picture (click on it to resize it).<div>[[File:Led.png | border | left | 150px |  LED polarity]][[File:Front.png | border | 250px | Place the LEDs as shown here. ]]</div>
+
# The chip socket has an indentation which must be oriented downwards. [[File:Chip_socket.JPG | thumb | none | Chip direction. The indentation points downwards. ]][[File:Chip.JPG | thumb | none | Chip direction. The indentation points downwards. ]]
 +
# The LEDs have a polarity. The longer leg is positive (+) and the shorter one is negative (-). Moreover, the LEDs have a flat spot on the negative side. The current flows from the positive to the negative so you have to pay attention while you assemble the kit, or else the Blinkentree won't work! You have to mount the LEDs as shown on the second picture (click to resize).<div>[[File:Led.png | border | left | 150px |  LED polarity]][[File:Front.png | border | 250px | Place the LEDs as shown here. ]]</div>
  
 
== How to program it on your own?  ==
 
== How to program it on your own?  ==
  
We designed our Blinkentree in a way you can customize it by yourself. You can define your own flashing patterns using the C language that you can transfer to your chip using the USBasp programmer.  
+
The goal of our Blinkentree project was to create a christmas tree that you can reprogram. You can change the flashing patterns as you wish.  
  
=== Installing and confguring the tools  ===
+
Those who have never programmed before might start getting cold feet, but fear not! Nothing is hard when you want to learn something new. This howto is intended for everyone, even (and especially) by those who are new to programming. If you come across stuff that's too unclear, shoot us a quick email (electronics[at]hackerspace.lu) and we will be glad to help you .
  
This section describes what you need to program the chip. First you need the toolchain with the compiler which translates your program text into bits and bytes that the controller can execute. We are using the GCC compiler which is  part of the [http://winavr.sourceforge.net WinAVR] (Windows) and [http://www.obdev.at/products/crosspack/index-de.html CrossPack-AVR] (Mac).
+
<!--
 +
In this howto we use the C language, and thus we need a C compiler. (HUH?: but more that you can transfer to your chip using the USBasp programmer).  
 +
-->
  
If you use Windows download [http://sourceforge.net/projects/winavr/files/WinAVR/20100110/WinAVR-20100110-install.exe/download WinAVR] and follow the installtion guide. When the installer asks to make an entry in the PATH variable be sure this is checked.
+
If you're completely new to the microcontroller world, there are essentially four steps to be followed.
  
For a mac you have to download [http://www.obdev.at/downloads/crosspack/CrossPack-AVR-20100115.dmg CrossPack AVR]. The toolchain is installed into /usr/local/CrossPack-AVR <ref>/usr/local/CrossPack-AVR is just a symbolic link to /usr/local/CrossPack-AVR-20100115 or something like that.</ref>. Have a look if the installer did add an entry to the PATH variable by typing the following line in your shell (Terminal):
+
# Get the compiler tools
 +
# Write the program
 +
# Compile the program (compiling means turning it from "human" language into "machine" language)
 +
# Get the compiled program into the microcontroller
  
<pre>echo $PATH</pre>
+
=== Get the compiler toolchain  ===
  
If it doesn't contain ''/usr/local/CrossPack-AVR/bin'', execute this in your shell:
+
Before we start writing the program we must install the compiler. A compiler is a program which reads your program text (source code) and turns it into machine code which is a series of instructions that the controller will execute later. One instruction could be "set pin 3 of port B to high". Thus you could switch on an LED.
<pre>echo -e " \n \n export PATH=/usr/local/CrossPack-AVR/bin:$PATH" >> ~/.profile</pre>
+
  
 +
A very common compiler is the GCC compiler which is part of the [http://winavr.sourceforge.net WinAVR] (Windows) and [http://www.obdev.at/products/crosspack/index-de.html CrossPack-AVR] (Mac). You might already have guessed it, we are using the C language here.
 +
 +
Windows users download [http://sourceforge.net/projects/winavr/files/WinAVR/20100110/WinAVR-20100110-install.exe/download WinAVR] and mac users should download [http://www.obdev.at/downloads/crosspack/CrossPack-AVR-20100115.dmg CrossPack AVR] instead.
 +
 +
If you downloaded the windows version, the installer will ask you to make an entry in the PATH variable. Be sure that the checkbox in the dialog is checked.
 +
 +
If you downloaded the mac version, the installer will put the stuff into /usr/local/CrossPack-AVR <ref>/usr/local/CrossPack-AVR is just a symbolic link to /usr/local/CrossPack-AVR-20100115 or something like that.</ref>. Now the compiler should be on the PATH. You can check this by opening a terminal (shell) and typing in <pre>echo $PATH</pre>
 +
 +
In addition, on the Mac ''/usr/local/CrossPack-AVR/bin'' should be present. If not execute this in order to add it:
 +
<pre>echo -e " \n \n export PATH=/usr/local/CrossPack-AVR/bin:$PATH" >> ~/.profile</pre>
  
 
<!--
 
<!--
Line 57: Line 77:
 
Then download the [[AVR Studio || http://www.atmel.com/forms/software_download.asp?family_id=607&fn=dl_AvrStudio4Setup.exe]]
 
Then download the [[AVR Studio || http://www.atmel.com/forms/software_download.asp?family_id=607&fn=dl_AvrStudio4Setup.exe]]
 
-->
 
-->
 +
 +
=== Coding  ===
  
 
For both '''Windows''' and '''Mac''' chose a good editor like [http://notepad-plus-plus.org/ Notepad++] or [http://www.pnotepad.org/ Programmers Notepad] for Windows and [http://www.barebones.com/products/textwrangler/ TextWrangler] for Mac.
 
For both '''Windows''' and '''Mac''' chose a good editor like [http://notepad-plus-plus.org/ Notepad++] or [http://www.pnotepad.org/ Programmers Notepad] for Windows and [http://www.barebones.com/products/textwrangler/ TextWrangler] for Mac.
  
=== Coding  ===
+
Defining new flashing patterns for your Blinkentree is easy. Here is a sample code:
Programming new flashing patterns for your Blinkentree is very easy. Here is a sample code:
+
  
 
<syntaxhighlight lang="C" line="GESHI_NORMAL_LINE_NUMBERS">
 
<syntaxhighlight lang="C" line="GESHI_NORMAL_LINE_NUMBERS">
Line 89: Line 110:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Save this code in the file main.c because this filename will be used in next sections.
+
Save this code in the file '''main.c''' . Briefly, this program powers on and off all the LEDs by setting all output pins to HIGH (255 as value) or LOW (0 as value).
  
In the first two lines you include one library for input/output (I/O) operations and one library for delay operations. In line 4 you define the main function that the Blinkentree jumps to when you switch it on. The ''DDRD'' and ''DDRB'' functions are used to configure the ports D and B. They're set to 0b11111111 which are binary numbers where each 1 stands for one pin of the port which has 8 pins. 1 means that the pin is set as an output wheras a 0 would be used to configure the pin as an input.
+
In the first two lines, one library for input/output (I/O) operations and one library for delay operations are included.  
  
By setting the port B to ''high'' (255 = 0b11111111) in line 12 we turn on all the LEDs of that port. If you want to turn on only certain LEDs you have to set other values from the range 0 to 255 (0b00000000 to 0b11111111). You can use the [http://acc6.its.brooklyn.cuny.edu/~gurwitz/core5/nav2tool.html decimal-binary converter] to convert decimal numbers in binary numbers.
+
In line 4 you define the main (primary) function that will be executed by the Blinkentree as soon as it's powered on. Every C program always include a 'main'
  
The while loop is used to execute the flashing pattern repeatedly. Otherwise the microcontroller would stop at the end of the main function. In other words the flashing pattern would be executed only one time.
+
The ''DDRD'' and ''DDRB'' variables are defined to configure the ports (a grouping of pins on the microcontroller) D and B which could be either as input or as output. DDR stands for ''Data Direction Register'' and the following letter A, B, C or D defines the port. We initialize both, DDRD and DDRB, with 0b11111111 which are binary numbers where each 1 stands for one pin of the port. 1 means that the pin is set as an output wheras a 0 would be used to configure the pin as an input.
  
On [https://iamsuhasm.wordpress.com/tutsproj/avr-gcc-tutorial/ Suhas's blog] you find more information about the I/O operations. If we have some time we will update this site to provide you with a mini coding tutorial for AVR microcontrollers.
+
By setting the port B to ''HIGH'' (255 = 0b11111111) in line 12 we turn on all the LEDs of that port. If you want to turn on only certain LEDs you have to set other values from the range 0 to 255 (0b00000000 to 0b11111111). For some more advanced pin settings ,read this [http://winavr.scienceprog.com/avr-gcc-tutorial/accessing-avr-microcontroller-ports-with-winavr-gcc.html tutorial]. You can use the [http://acc6.its.brooklyn.cuny.edu/~gurwitz/core5/nav2tool.html decimal-binary converter] to convert decimal numbers into binary numbers and vice versa.
 +
 
 +
The 'while' loop is necessary in order to continue executing the flashing pattern. Otherwise the microcontroller would stop at the end of the main function. In other words the flashing pattern would be executed only one time. (In fact, a microcontroller must NEVER completely terminate the main loop, but that's a topic for another day.)
 +
 
 +
On [https://iamsuhasm.wordpress.com/tutsproj/avr-gcc-tutorial/ Suhas's blog] you find more information about the I/O operations.
 +
 
 +
The code the Blinkentree is shipped with can be found [https://source.hacker.lu/projects/blinkentree/repository/revisions/774ec25a38a85521f1000aa5b13b46825fa7eee0/entry/2D_with_LEDs/firmware/main.c here].
  
 
=== Compiling and linking ===
 
=== Compiling and linking ===
  
After you're done with the programming, the code must be translated into machine code that can be executed by the microcontroller. Open your command line and change into the directory where your code file is.
+
When the coding is done, translate it into machine code by opening your terminal (shell, command line or also known as cmd on Windows) and changing into the directory where your code file is.
  
There you you type in:
+
There you type:
  
 
<pre>
 
<pre>
Line 111: Line 138:
 
</pre>
 
</pre>
  
These commands are the same for Mac and Windows.
+
These commands are the same for Mac and Windows. We assume that you are not (yet) interested in what those lines mean exactly, so don't worry about them now. You can read this [http://www.hackerspace.lu/w/images/5/52/AVR_Tutorial.pdf tutorial] later when you're more familiar with compiling.  
  
After that you have main.hex which is the translated code that will be loaded into the chip later.
+
If nothing went wrong you should have main.hex which is the translated code. If something did go wrong, it's almost certainly because of a small typo that you made. Go back and check very carefully everything you've typed. There's not that much to check, so don't lose heart now, you're almost done!
 +
 
 +
The generated hex file will be loaded into your microcontroller in the next step.
  
 
=== Flashing the microcontroller ===
 
=== Flashing the microcontroller ===
To flash the controller you need a programmer like the [http://www.fischl.de/usbasp/ USBasp]. On that homepage you find all information you need to build your own programmer but I'd recommend to come tu auer hackerspace in Strassen where you get help and support. We have the equipment like an etching machine to make electronic stuff in a professional way. If you have questions about our space just write an email to electronics[at]hackerspace.lu (please replace [at] by @).
+
 
 +
Congratulations! You're not yet bored about this stuff :-)
 +
 
 +
Now it's getting a bit tricky. You need a programmer device to upload your code to the controller. We used the [http://www.fischl.de/usbasp/ USBasp programmer] which you can buy [http://shop.fundf.net/usbasp/bestellung.html here]. As the USBasp programmer is an open source project you can also build it yourself.
 +
 
 +
'''Important:'''
 +
If you buy the ATMega chip somewhere else it might be missing the required firmware. In this case you need another programmer to get the programmer firmware into yours so you'll probably run into the chicken-and-egg dilemma.
 +
 
 +
Mac and Linux users don't need any driver for the programmer but Windows users do. Download it [http://www.fischl.de/usbasp/usbasp-windriver.2009-02-28.zip here].
 +
 
 +
<!--
 +
To flash the controller you need a programmer like the [http://www.fischl.de/usbasp/ USBasp]. On that homepage you will find all the information you need to build your own programmer, but I'd recommend coming to our hackerspace in Strassen where you can get help and support. We have the equipment to make electronics in a professional way, like soldering irons and an etching machine. If you have questions about our space just write an email to electronics[at]hackerspace.lu (please replace [at] by @).
  
 
Windows requires a driver for the USBasp but not Linux and Mac OS. There you can work with it immediately.
 
Windows requires a driver for the USBasp but not Linux and Mac OS. There you can work with it immediately.
  
 
TODO: Driver installation for f***ing Windoze.
 
TODO: Driver installation for f***ing Windoze.
 +
-->
 +
 +
When you finally got a programmer, you need to solder a pin header on your board. Have a look at your tree, there are six holes that are reserved for this purpose. You can get such a pin header in any electronics store, for just a couple cents. In Luxembourg there are [http://sam.lu S.A.M.], [http://secto.lu Secto] and [http://schaller.lu Schaller], and perhaps others we didn't think to mention. In Germany [http://reichelt.de Reichelt] and [http://conrad.de Conrad] are well-known.
  
With the programmer you are now able to flash your controller. To connect the programmer with your Blinkentree you need to add a pin header on your board. [[File:ISP.JPG | thumb | none]]
+
[[File:ISP.JPG | thumb | none]]
  
 
The pins from left to right are:
 
The pins from left to right are:
Line 135: Line 178:
 
On the USBasp you have this connector:
 
On the USBasp you have this connector:
  
 +
<!--
 
[[File:Isp_10pin.png]]
 
[[File:Isp_10pin.png]]
 +
-->
 +
[[File:Connector.png]]
  
Connect all the pins with the same name. If you also connect VTG you have to switch '''off''' the Blinkentree and if you don't connect it you have to switch it '''on'''.
+
Important note: there is a difference between the USBasp and "original" ISP pin mappings. As ground use the two pins from the right.
  
Then on your Windows machine you execute
+
Connect all the pins with the same name. On the programmer device there are three jumpers.
 +
 
 +
[[File:USBasp.png]]
 +
 
 +
VERY IMPORTANT: If VTG of the programmer is connected with VTG of your Blinkentree and JP2 is set then your Blinkentree is powered by USB. Be sure there are no batteries in the battery holder and/or the switch is turned to '''off'''. If you don't connect it, you have to switch it '''on'''.
 +
JP1 is used to update the programmer firmware. Don't set it in the ''normal'' mode.
 +
JP3 slows down the ''programming'' clock. This allows to flash targets with an internal clock less than 1.5 MHz. As the Blinkentree uses the internal 1 MHz clock JP3 must not be set.
 +
 
 +
Go into your shell / command line and execute this
 
<pre>
 
<pre>
 
avrdude -c USBasp -p t2313 -U flash:w:main.hex -v -F
 
avrdude -c USBasp -p t2313 -U flash:w:main.hex -v -F
 
</pre>
 
</pre>
  
and on a mac
+
On a mac-- and probably under linux too-- you must execute it with '''sudo'''.
  
 
<pre>
 
<pre>
 
sudo avrdude -c USBasp -p t2313 -U flash:w:main.hex -v -F
 
sudo avrdude -c USBasp -p t2313 -U flash:w:main.hex -v -F
 
</pre>
 
</pre>
 +
 +
<!--
  
 
'''Note'''
 
'''Note'''
Line 155: Line 211:
  
 
<!--
 
<!--
After you built or got such a programmer you are able to get bits and bytes written into the microcontroller memory. Before you can load the actual compiled code into the microcontroller, it has to be configured first. This is done by setting the fuse bits. Thus you execute the following line:  
+
After you built or got such a programmer you are able to write bits and bytes into the microcontroller memory. Before you can load the actual compiled code into the microcontroller, it has to be configured first. This is done by setting the fuse bits. Thus you execute the following line:  
 
-->
 
-->
  
Then you have to execute
+
<!--
 
<pre>
 
<pre>
 
avrdude -cUSBasp -p t2313 -U lfuse:w:0x64:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m  
 
avrdude -cUSBasp -p t2313 -U lfuse:w:0x64:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m  
 
</pre>
 
</pre>
  
on Windoze
+
on Windows
  
 
and
 
and
Line 172: Line 228:
  
 
on a Mac.
 
on a Mac.
 +
-->
  
 
<!--
 
<!--
There are three fuse bytes ( = 24 bits in total). collection of bits stands for a base or a module configuration (Fuse Bits, page 160). Use the fusecalc to be sure to have the right ones.  
+
There are three fuse bytes ( = 24 bits in total). The collection of bits stands for a base or a module configuration (Fuse Bits, page 160). Use the fusecalc to be sure to have the right ones.  
 
-->
 
-->
 
== References ==
 
  
 
== Footnotes ==
 
== Footnotes ==
 
<references />
 
<references />

Latest revision as of 18:57, 31 October 2011

Add your Project
Crystal Project package graphics.png
Blinkentree
We built our own blinking X-mas tree for the market in Strassen.
BlinkenWood.jpg
Meetings: none
Type: hardware
Tracker: http://source.hacker.lu/projects/blinkentree
Status: concluded
Members:
Contact Person: slopjong (mail), prometheus (mail)
Tools
QrCode: QR-b1e7acf6b9edc7f5b6025eab63cef96d.png


Diese Seite gibt es hier auch in deutsch.

Site en français


Contents

[edit] Introduction

What is this flashing green fir tree all about?

As creative hackers, we thought it would be great to support the Christmas market of the commune of Strassen with something tiny, but creative. Result? The Blinkentree. A green fir tree, adorned with flashing LEDs representing the ornaments of a 'real' tree.

Here are two links about how the Blinkentree was made and where it was sold.

A big thanks to all of you folks who purchsed a Blinkentree and thus supported syn2cat Hackerspace.

Hacky Christmas!

[edit] Assembling the kit

If you didn't buy a kit but an already assembled Blinkentree, you can continue here.

Otherwise, follow the next instructions to assemble your kit. We recommend first soldering the resistors, then the chip socket, and finally the LEDs because it's easier to start with small devices and finish up with the biggest ones.


  1. The resistors all have the same value, and as they don't have a polarity it doesn't matter what direction they're put on the board.
    Place the resistors as shown here.
  2. The chip socket has an indentation which must be oriented downwards.
    Chip direction. The indentation points downwards.
    Chip direction. The indentation points downwards.
  3. The LEDs have a polarity. The longer leg is positive (+) and the shorter one is negative (-). Moreover, the LEDs have a flat spot on the negative side. The current flows from the positive to the negative so you have to pay attention while you assemble the kit, or else the Blinkentree won't work! You have to mount the LEDs as shown on the second picture (click to resize).
    LED polarity
    Place the LEDs as shown here.

[edit] How to program it on your own?

The goal of our Blinkentree project was to create a christmas tree that you can reprogram. You can change the flashing patterns as you wish.

Those who have never programmed before might start getting cold feet, but fear not! Nothing is hard when you want to learn something new. This howto is intended for everyone, even (and especially) by those who are new to programming. If you come across stuff that's too unclear, shoot us a quick email (electronics[at]hackerspace.lu) and we will be glad to help you .


If you're completely new to the microcontroller world, there are essentially four steps to be followed.

  1. Get the compiler tools
  2. Write the program
  3. Compile the program (compiling means turning it from "human" language into "machine" language)
  4. Get the compiled program into the microcontroller

[edit] Get the compiler toolchain

Before we start writing the program we must install the compiler. A compiler is a program which reads your program text (source code) and turns it into machine code which is a series of instructions that the controller will execute later. One instruction could be "set pin 3 of port B to high". Thus you could switch on an LED.

A very common compiler is the GCC compiler which is part of the WinAVR (Windows) and CrossPack-AVR (Mac). You might already have guessed it, we are using the C language here.

Windows users download WinAVR and mac users should download CrossPack AVR instead.

If you downloaded the windows version, the installer will ask you to make an entry in the PATH variable. Be sure that the checkbox in the dialog is checked.

If you downloaded the mac version, the installer will put the stuff into /usr/local/CrossPack-AVR [1]. Now the compiler should be on the PATH. You can check this by opening a terminal (shell) and typing in
echo $PATH

In addition, on the Mac /usr/local/CrossPack-AVR/bin should be present. If not execute this in order to add it:

echo -e " \n \n export PATH=/usr/local/CrossPack-AVR/bin:$PATH" >> ~/.profile


[edit] Coding

For both Windows and Mac chose a good editor like Notepad++ or Programmers Notepad for Windows and TextWrangler for Mac.

Defining new flashing patterns for your Blinkentree is easy. Here is a sample code:

  1. #include <avr/io.h> 
  2. #include <util/delay.h>
  3.  
  4. int main(void) { 
  5.  
  6.     // configure the ports as outputs 
  7.     DDRD = 0b11111111;
  8.     DDRB = 0b11111111;
  9.  
  10.     while(1)
  11.     {
  12.             PORTB = 255;
  13.             PORTD = 0;
  14.  
  15.             _delay_ms(400);
  16.  
  17.             PORTB = 0;
  18.             PORTD = 255;
  19.  
  20.             _delay_ms(400);
  21.     }
  22.  
  23. }

Save this code in the file main.c . Briefly, this program powers on and off all the LEDs by setting all output pins to HIGH (255 as value) or LOW (0 as value).

In the first two lines, one library for input/output (I/O) operations and one library for delay operations are included.

In line 4 you define the main (primary) function that will be executed by the Blinkentree as soon as it's powered on. Every C program always include a 'main'

The DDRD and DDRB variables are defined to configure the ports (a grouping of pins on the microcontroller) D and B which could be either as input or as output. DDR stands for Data Direction Register and the following letter A, B, C or D defines the port. We initialize both, DDRD and DDRB, with 0b11111111 which are binary numbers where each 1 stands for one pin of the port. 1 means that the pin is set as an output wheras a 0 would be used to configure the pin as an input.

By setting the port B to HIGH (255 = 0b11111111) in line 12 we turn on all the LEDs of that port. If you want to turn on only certain LEDs you have to set other values from the range 0 to 255 (0b00000000 to 0b11111111). For some more advanced pin settings ,read this tutorial. You can use the decimal-binary converter to convert decimal numbers into binary numbers and vice versa.

The 'while' loop is necessary in order to continue executing the flashing pattern. Otherwise the microcontroller would stop at the end of the main function. In other words the flashing pattern would be executed only one time. (In fact, a microcontroller must NEVER completely terminate the main loop, but that's a topic for another day.)

On Suhas's blog you find more information about the I/O operations.

The code the Blinkentree is shipped with can be found here.

[edit] Compiling and linking

When the coding is done, translate it into machine code by opening your terminal (shell, command line or also known as cmd on Windows) and changing into the directory where your code file is.

There you type:

avr-gcc -Wall -Os -std=c99 -DF_CPU=1000000 -mmcu=attiny2313 -c main.c -o main.o 
avr-gcc -Wall -Os -std=c99 -DF_CPU=1000000 -mmcu=attiny2313 -o main.elf main.o 
avr-objcopy -O ihex -R .eeprom main.elf main.hex 

These commands are the same for Mac and Windows. We assume that you are not (yet) interested in what those lines mean exactly, so don't worry about them now. You can read this tutorial later when you're more familiar with compiling.

If nothing went wrong you should have main.hex which is the translated code. If something did go wrong, it's almost certainly because of a small typo that you made. Go back and check very carefully everything you've typed. There's not that much to check, so don't lose heart now, you're almost done!

The generated hex file will be loaded into your microcontroller in the next step.

[edit] Flashing the microcontroller

Congratulations! You're not yet bored about this stuff :-)

Now it's getting a bit tricky. You need a programmer device to upload your code to the controller. We used the USBasp programmer which you can buy here. As the USBasp programmer is an open source project you can also build it yourself.

Important: If you buy the ATMega chip somewhere else it might be missing the required firmware. In this case you need another programmer to get the programmer firmware into yours so you'll probably run into the chicken-and-egg dilemma.

Mac and Linux users don't need any driver for the programmer but Windows users do. Download it here.


When you finally got a programmer, you need to solder a pin header on your board. Have a look at your tree, there are six holes that are reserved for this purpose. You can get such a pin header in any electronics store, for just a couple cents. In Luxembourg there are S.A.M., Secto and Schaller, and perhaps others we didn't think to mention. In Germany Reichelt and Conrad are well-known.

ISP.JPG

The pins from left to right are:

1. MOSI | 2. MISO | 3. SCK | 4. GND | 5. VTG | 6. RST

On the USBasp you have this connector:

Connector.png

Important note: there is a difference between the USBasp and "original" ISP pin mappings. As ground use the two pins from the right.

Connect all the pins with the same name. On the programmer device there are three jumpers.

USBasp.png

VERY IMPORTANT: If VTG of the programmer is connected with VTG of your Blinkentree and JP2 is set then your Blinkentree is powered by USB. Be sure there are no batteries in the battery holder and/or the switch is turned to off. If you don't connect it, you have to switch it on. JP1 is used to update the programmer firmware. Don't set it in the normal mode. JP3 slows down the programming clock. This allows to flash targets with an internal clock less than 1.5 MHz. As the Blinkentree uses the internal 1 MHz clock JP3 must not be set.

Go into your shell / command line and execute this

avrdude -c USBasp -p t2313 -U flash:w:main.hex -v -F

On a mac-- and probably under linux too-- you must execute it with sudo.

sudo avrdude -c USBasp -p t2313 -U flash:w:main.hex -v -F



[edit] Footnotes

  1. /usr/local/CrossPack-AVR is just a symbolic link to /usr/local/CrossPack-AVR-20100115 or something like that.
Personal tools
Namespaces

Variants
Actions
Navigation
syn2cat
Hackerspace
Activities
Initiatives
Community
Tools
Tools