Saint Ann's Summer Workshop 2014
Day 1
- Stretches (9:00-9:15)
- Introduction (9:15-9:30)
- Introduction to TinkerCad (9:30-10:30)
-
Building Forms(10:45-11:30, 11:30-11:45, 11:45-12:00)
-
Modeling Forms (1:00-2:00)
-
Downloading, Slicing and Printing (2:00-2:25)
-
Connecting modules (2:30-3:00)
Introduction to TinkerCad
1 hourTinkercad is an easy-to-use tool for creating 3D models for 3D printing. It is part of the Autodesk family of products, which are free and exist to enable students, makers, and individuals from all walks of life to design and make the things they imagine.
- Navigate in a web-gl enabled browser (Chrome or Firefox) to tinkercad.com
- If you have not already signed up for a free account, do so now
- To exit the tutorial and start experimenting, exploring and modeling click on the Tinkercad logo:
- To start a new model click on Create new design
- For greater precision I generally change the Snap grid value to 0.1
- To add shapes, letters and numbers you drag your selection from the palette on the right over to the workplane:
- You can get the dimensions of an object by moving the cursor over the nodes or handles:
- You can also get and/or modify the dimensions by laying down the ruler:
- You can resize your form by dragging the corner points or using the ruler and editing the field:
- Dragging the corner point while holding shift will increase the X,Y and Z axis proportionally:
- You can copy and paste your forms using the standard keyboard commands. When you paste, Tinkercad will move your new form to the right:
- You can raise the height of your form by dragging the node below the up arrow
- You can subtract shapes from each other by placing the shape you want to remove, selecting it and then clicking on the the hole icon
- You can still adjust the location of the hole by dragging the form
-
When you want to remove the shape, drag your cursor over or Shift+Left Click on both objects (what you want to keep and what you want to remove)
- When both are selected, click on Group
- To view the hole, rotate with the mouse:
- You can easily rotate your objects by clicking on the rotation icons:
- You can move the workplane by placing it on a side of any of your forms. This allows you to add more forms on an angle:
- To reset the workplane, just drag the workplane from the palette to the work area:
- You can measure distances using the ruler
Place the ruler then click on objects:
- To align forms, select them and click on Adjust:
- Now select Align
- Click on the node you would like the forms to align with:
- To save the STL file, click on Design and select Download for 3D Printing:
Then select on the STL button: - Experiment, have fun, print
Building Forms
45 minutesWork in groups to design your own structures using a set of straws and connectors.
You may cut or bend your straws. When constructing your forms, think about triangles, hexagons, grids and Archimedean, Platonic and Johnson solids.
Archimedean solids are symmetric, semi-regular convex polyhedrons composed of two or more types of regular polygons meeting at identical vertices. Platonic solid are composed of only one type of polygon that meets at identical vertices, and Johnson solids have regular polygonal faces that do not meet in identical vertices.
Making the forms solid
15 minutesUse paper and tape to close off the shapes created by the straws and connectors
Sketching
15 minutesSketch the form on paper with a pencil. Show different perspectives.
Modeling Forms
1 hour- Place your model in a place where every member of your group can view it.
- Open Tinkercad
- Look at your form and your sketches and select the most successful or most interesting interpretation of your original structure.
- Reconstruct your interpretation as a 3D model.
Downloading, Slicing and Printing
20 Minutes- Click on the Design menu and select Properties.
- Rename your file and save the changes
- From the Design menu select Download for 3D Printing:
- Select on the STL button:
- Open MakerWare. If you have not installed this software, you can find it here.
- Click on the Add button and navigate to or just drag your stl file on to the virtual platform.
- If you see this window, click on the move to platform button
- To use any of the transformation tools, your STL file must be selected (i.e. highlighted in yellow)
- Press M to move the model on the platform:
- Press T to rotate the model on the platform:
- Press S to scale the model on the platform:
- Click on Uniform scaling to toggle this option. When deselected you can scale along each axis:
- Press L to change the view:
- Click on the Make Icon
. Generally the default settings are fine for most prints.
- Click on the Export button to save the x3g file. You can save this file on your computer or on an SD card. If you are tethered to your printer you should select Make It Now before pressing the Export button
- To save all your transformations, save the thing file by clicking on the Save icon.
Custom Connectors
30 minutes- Remove the paper from your structures.
- Select two structures to work with.
- Design a custom connector to join the two structure together.
Day 2
Light Toys
- Stretches (9:00-9:15)
- Introduction (9:15-9:30)
- Circuit (9:30-10:15)
- Tinkercad Demo (10:15-10:30)
- Modeling (10:40-12:00)
- Downloading, Slicing and Printing (1:00-3:00)
- Cat Toy (1:00-3:00)
- Documenting (1:30-3:00)
Introduction
A tilt sensor can be used to detect orientation. Inside the can are a pair of balls that make contact with the pins when the case is upright. Tilt the case over and the balls don't touch, thus not making a connection. You can use a tilt sensor as a switch.Image from learningaboutelectronics.com
Today's project is to create a flashlight that projects an image when you turn it horizontally and turns the light off in any other orientation.
Circuit
Parts
- Tilt sensor
- LED
- 3V Battery holder
- 3V battery
- 22 awg wire
- Double sided tape
Schematic
Another Tinkercad Demo
- Open Google Chrome and navigate to Tinkercad.com.
- Sign in or create an account.
- Drag a cube on to the workplane
- Drag the ruler on to the workplane
- Change the size of the cube to 35x35x74.
- Drag another cube on to the workplane
- Change the size of the cube to 32x32x84. The z should just be greater than 74. Turn the taller cuber into a hole by selecting just that cube and clicking on the hole option:
- Select both cubes and then click on Adjust. Select Align.
- Click on the central nodes on the groundplane to move the cubes along the X and Y axes
- Select both cubes and click on group. This will delete the hole from the outer cube
- Drag another cube on to the workplane
- Change the size of the cube to 35x35x1
- Drag another cube on to the workplane.
- Change the size of the cube to 31.75x31.75x10
- Drag another cube on to the workplane.
- Change the size of the cube to 30.74x30.74x20
- Turn the taller cuber into a hole by selecting just that cube and
clicking on the hole option
- Align the last two cubes and group them.
- Align the 35x35x1 cube with the last cube.
- Raise the taller cube 1mm up from the workplane.
- Group the cubes.
- Drag another cube on to the workplane.
- Change the size of the cube to 2x40x20
- Create a duplicate by pressing OPTION+D
- Rotate the duplicated copy 90°
- Group the two cubes.
- Turn them into a hole.
- Align with the last set of cubes
- Raise the hole 1.1mm above the workplane.
- Group the cubes
- Copy and paste the grouped cubes (Command+C, Command+V)
- Move the copy.
- Find an SVG (simple solid shapes work best).
- Import the file:
- Adjust the size
- Align with the last cube group
- Turn the extruded SVG into a hole and group to delete it
- Adjust the 3 cubes
- Rename
- Download for 3D printing
Time to model
Things to keep in mind:- Printing takes a long time, you want to conserve resources (use as little plastic as possible)
- Parts that fit together tightly should by offset from each other .5mm
- Parts that slide should by offset from each other 1mm
- Sometimes making a tightly fitting part less than .5mm with slits to compress work well
- The longer the tube, the better/clearer the projection
- When selecting SVGs, use the advanced search option and look for simple closed shapes
- Be mindful or your SVG's orientation if you use letters or numbers
Downloading, Slicing and Printing
20 Minutes- Click on the Design menu and select Properties.
- Rename your file and save the changes:
- From the Design menu select Download for 3D Printing:
- Select on the STL button:
- Open MakerWare. If you have not installed this software, you can find it here.
- Click on the Add button and navigate to or just drag your stl file on to the virtual platform.
- If you see this window, click on the move to platform button
- To use any of the transformation tools, your STL file must be selected (i.e. highlighted in yellow)
- Press M to move the model on the platform:
- Press T to rotate the model on the platform:
- Press S to scale the model on the platform:
- Click on Uniform scaling to toggle this option. When deselected you can scale along each axis:
- Press L to change the view:
- Click on the Make Icon
. Generally the default settings are fine for most prints.
- Click on the Export button to save the x3g file. You can save this file on your computer or on an SD card. If you are tethered to your printer you should select Make It Now before pressing the Export button
- To save all your transformations, save the thing file by clicking on the Save icon.
Cat Toy
Parts
- 3V Red Laser Module
- Push button
- 3V Battery holder
- 3V battery
- 22 awg wire
Design a laser beam cat toy that is fun to hold.
Documenting
Day 3
Motorbots
- Stretches (9:00-9:15)
- Introduction (9:15-9:30)
- First Circuit (9:30-10:15)
- Second Circuit (10:15-10:45)
- Introduction to 123D Design (10:45-11:15)
- Modeling (10:45-2:00)
-
Downloading, Slicing and Printing (1:00-3:00)
-
Documenting (1:30-3:00)
Introduction
BristleBots are simple and tiny "robots". The BristleBot is one version of a vibrobot, a simple category of robot that is controlled by a single vibrating (eccentric) motor.Image from: Evil Mad Scientist
First Circuit
Parts
- toothbrush
- 3V battery
- pager motor or other tiny motor with an unbalanced output shaft
- Small switch
- Double sided tape
Image from: Evil Mad Scientist
Assembly
- Cut off the handle of the toothbrush
- Solder wire leads to the motor terminals.
- Apply a small piece to the top of the toothbrush robotic platform, which will be used to hold the motor in place.
- Battery and motor placement, bristle shape (one stray long bristle can interfere with the motion, and motor rotation direction all influence the behavior. Try flipping the battery upside down if you have trouble getting your bot to go straight.
Image from: Evil Mad Scientist
Second Circuit
This second circut is a variation on the first bristlebot circuit. Instead of using a toothbrush and eccentric motor, this version uses two pcb boards from invent-abling.com. One board has a small motor attached, the other holds a rechargeable 3V coin cell battery.Time to model
Downloading, Slicing and Printing
Documenting
Day 4
Magnets
- Stretches (9:00-9:15)
- Introduction (9:15-9:30)
- Modeling and printing (9:30-11:00)
- Joule Thief (11:00-11:30)
- Cat Burglar (11:30-3:00)
- Documenting (1:30-3:00)
Introduction
There are forces that occur on earth which cannot be seen.Over 2,000 years ago, the Greeks discovered a mineral that attracted iron. Because this mineral was found in a part of Turkey called Magnesia, the Greeks called it magnetite. Today, any material that attracts iron or things made of iron is called a magnet. All magnets have certain properties:
- All magnets have two poles.
- Magnets exert forces on each other.These magnetic forces result from spinning electric charges in the magnets. The force can either push the magnets apart or pull them together. The magnetic effects exerted by a magnet is not the same throughout the magnet.
- Around every magnet there is an invisible field called a magnetic field. This field is what attracts items such as paper clips and nails. A magnetic field exists in the region around a magnet in which magnetic forces can act. The magnetic field lines spread out from the north pole and circle back around to the south pole.
Magnetic field display viewing film
This film is a translucent thin flexible sheet made of plastic, coated with micro-capsules containing nickel flakes suspended in oil that is used to visualize magnetic fields. The general principle behind the film is that magnetizable nickel particles are suspended in a colloidal form in a gelatinous material. This material allows enough freedom for the particles to align with an externally applied magnetic field. When this happens, the colored fluid is displaced and causes a color change at the surface of the film. In the presence of a strong field the rods that are embedded in the fluid align to the field. When lines of force are perpendicular to the film(i.e. you are viewing a pole against the surface), the flakes are edge-on, and the color of the film appears darker. When magnetic lines of force are parallel to the surface of the film, the surfaces of the flakes are reflective, and appear bright. This enables visualization of the field strength and behavior.The effect gets interesting when you have lots of magnets, or magnets that are polarized in a unique way. .
Vocabulary
Magnet: a metal that can attract certain other metals. Magnetic Properties - refers to an item which can attract or repel items as a magnet does.Poles : refers to the two areas of a magnet where the magnetic effects are the strongest. The poles are generally termed the north and south poles. Poles that are alike (both north or both south) will repel from each other, while poles that are different (one north, one south) will attract to each other.
Magnetic fields: In physical science, a "field of force " is a region or space in which an object can cause a push or pull. This field extends infinitely in all directions but gets weaker as you get farther from the source of the field. Magnetic lines of force show the strength and direction of this field.
Electromagnetism: Electric current flowing in a wire that creates a magnetic field around it.
The first circuit
Another Circuit
Magnetism can be used to step-up a voltage with a few electronic components./ / Simple Thief
A Joule thief allows you to boost the voltage of a dying battery.
Without the joule thief, you cannot light and led with a 1.5 volt battery
- Get the following parts along with some copper wire:
- Clip off 5-6 feet of wire and then fold in half. Twist the top of the folded end
- Make a knot on the top of one of the wires
- Make a knot at the bottom end of the wire with the knot on the top
- Using the loop end wrap the wire around the torroid
- Take one knotted end with a straight end and twist
- Take sandpaper and sand off ends
- Use a multimeter to test for continuity
- With soldering iron, tin the ends
- Bend the legs of the transistor
- Transistor:
Image from www.ecs.umass.edu/ece
Fold the base of the transistor under and solder it to the resistor - Take the LED and solder longer leg to the collector
- Solder the shorter side of the led to the emitter
- Solder the resistor to one single lead of the torroid
- Solder the other single leg from the torroid to the longer lead of the LED
and the transistor's collector:
- Cut three inches of wire and strip off both ends
- Solder one end of the wire to the emitter/short end of the LED
- Connect circuit to battery (wire to gnd and double wire from torroid to positive terminal) and let the light shine
So how does it work?
Imagine the condition when the battery is first connected. The transistor is off, but voltage can flow down the left side of the transformer (wound toroid) in the diagram, current limited by the resistor. This turns on the transistor, and a larger amount of current can flow through the other side of the transformer and through the resistor. Because the voltage applied across the LED is well below its forward voltage, no current flows through the LED, and the LED does not yet turn on.
The larger current flowing through the right side of the transformer induces a current in the left side of the transformer that opposes the existing current on the left side-- and the current on the left side quickly drops down to zero. When this happens, the transistor shuts off. With no current flowing in the left side of the transformer, the toroid acts like a simple inductor, which is like a flywheel for current. There is already current flowing though it, which cannot shut off immediately, and the resulting current (flow of electric charge) towards the collector of the shut-off transistor leads to a temporary increase in the voltage at that point, which continues until the voltage exceeds 3 V, at which point the current finds a way out-- through the LED, which lights up as current flows through it.
As the last of the current fades from the toroid, the LED shuts off again, and we again return to the initial state: no current flowing through either side of the toroid transformer. And the process begins again. This whole cycle happens at a typical rate of about 40 kHz, far too fast for you to see the blinking, and the light from the Joule Thief appears to be continuous. This is a simple example of a switching power supply driven by a transformer-coupled oscillator, made of only four components.
As a side note, this is not the most efficient circuit around; it's beauty is that it works with such a low voltage. So, this is a great circuit to use with a dead or dying battery, and less so for use with a brand new battery.
When the battery is connected current flow into the base of the transistor and the voltage across the base and emitter (Vbe) rises and the transistor is switched on. This means that a larger current can now flow through the primary winding and then through the transistor. This generates a magnetic field in the toroid opposite to the field created by the base current through the secondary winding. As soon as the primary current is greater than the secondary current the voltage on the secondary winding reverses switching the transistor off. The magnetic field collapses and the LED is switched on briefly and the whole process starts over again. The cycle repeats 40 000 times a second. Lastly the 1K resistor is there to limit the base current of the transistor.
/ / Cat Burglar
When the cat burglar gets his little paws on a battery his LED nose lights up until all the joules are gone.
Tools
- Scissors
- Embroidery Needle and Thread
- Soldering iron
- Glue gun
- Wire cutters
- Wire strippers
- Marker
Parts:
- Solid Core Ferrite Suppressors - Mouser - PN 875-28B0500-100
- Magnet wire
- Transistor -2N3904 or 2N2222
- LED
- 1KΩ resistor
- Conductive Fabric
- Felt
- Googlee eyes
- 2 small strong magnets
- Stuffing
- Fabric glue
- Use the template or make your own design. If you make your own design, keep the size the same
- Cut out two pieces of felt
- Cut out holes where the paws are:
- Use template to cut out conductive fabric for the front and back sides of the paws.
You'll need two fronts (circles) and two backs:
- Cut out your mask and nose
- Peel the paper backing off and place the frons and backs on the the paws. Iron them together
- Iron on the face
- Cut a hole in the nose-just enough to fit the LED
- Glue magnets to inside of paws. Cover with glue, but don't put glue between magnet and fabric.
Take care to keep the magnets apart during the gluing process—they might break. Make sure the magnets are positioned so that the opposite poles are facing one another. Why? Because when your cat burglar is finished, the paws will clasp together.
- Stick LED through fabric to create nose
- Glue to fix the thief in place and to also cover wires, preventing short circuits.
- Use conductive thread to sew the twisted strands to the inside paw. Use enough thread to make a good connection. Try not to go through the felt to the front side.
- Sew the other lead of the joule thief (the 3 inch wire) to the other paw.Use enough thread to make a good connection. Try not to go through the felt to the front side:
- Sew pads with conductive thread. This will connect the inside to the outside of the paws to complete you circuit. This time, go through the fabric:
- Sew the back and front together. Before completing, stuff. Then complete the sewing.
When you're stuffing thief, make sure you don't short or break any of the wires. - OPTIONAL:
-
Add a mouth with needle and pink thread.
- Add whiskers
- From scraps of black fabric sew up a tail. Just take two layers of fabric and sew up a kind of crooked tube. Cut the tube out and turn it inside out. Sew the tail to the body.
- stitch in a "+" symbol on the positive paw.
- Cut out small triangles from the pink felt and glue them to the ears.
-
Add a mouth with needle and pink thread.
-
Glue on some googlee eyes.
Makezine:The Sympathy Doll
Makezine:Joule Thief
http://blog.makezine.com/archive/2008/01/led_softie.html
http://www.emanator.demon.co.uk/bigclive/joule.htm
Day 5
Lamp
- Stretches (9:00-9:15)
- Introduction (9:15-9:30)
- Circuit (9:30-10:30)
- Modeling (10:30-12:00)
- Glow in the dark picture (11:30-12:00)
- UV Beads (1:00-2:00)
- Finishing up— all day
- Documenting— all day
- Show and tell— 2:00-3:00
Introduction
This circuit uses a microcontroller to control an RGB LED.An AVR chip is a member of a family of microcontrollers. These chips have a CPU, flash storage, RAM and EEPROM. An AVR's CPU is similar to the one found in your computer, but the difference between a microcontroller and your computer is that the AVR's CPU is much more simple and not as fast. An AVR's Flash storage is similar to the flash storage in an mp3 player or a digital camera card. It is where the programs are stored. RAM is used for temporary storage and EEPROM on can be used as long term storage, meaning, it doesn't get erased when the chip loses power.
Microcontrollers must contain an instruction set. The instruction set is a list of all commands and their corresponding functions.
During operation the microcontroller will step through its program. Each valid instruction of the program is a command to the micrcontroller to do a specific job. The instruction set and internal hardware are what differentiate one microcontroller from another.
What's inside a microcontroller
Inside the microcontroller is a collection of thousands of transistors that fit in a 1/10" square. These silicon chips are packed in plastic or ceramic carriers.
How to use a microcontroller?
- Write a program to tell the chip what to do.
- Compile the program—turn the program into machine code.
- Upload the program to the chip using a programmer.
- Test, debug, iterate,
/ /Mac setup
All of your files should end up in the /usr/local/bin directory on the hard driver. Unfortunately that directory is not your default path. This means that when you type avrdude into the terminal it won't be able to figure out where to look until you change it. In this prep-step you'll change the profile of your Terminal to add /usr/local/bin to the path.
- Open Terminal. It's in the Applications/Utilities folder.
- In the new Terminal window, type in echo $SHELL and press return.
-
If the output is /bin/bash then type the following command:
echo 'PATH=$PATH:/usr/local/bin' >> ~/.bash_profile
all on one line. Press return. -
If the output is /bin/csh or /bin/tcsh then type the following command:
class="em">echo 'set path = ($path /usr/local/bin)' ~/.cshrc
- Close any Terminal windows and open up a new one. This makes sure the .bash_profile or .cshrc is reloaded. Now type in echo $PATH (for bash) or echo $path (for t/csh) you should get something like the following:
The important thing is that at the end of the line is /usr/local/bin - Download CrossPack for AVR Development.
Avrdude
Avrdude is a command line program, which means you'll have to type in all the commands.- Open Terminal program to pull up a command line interface.
- Type in avrdude
You should see something like this: - There are a lot of options.
- -p <partno>: This tells which microcontroller avrdude is programming. For example, if you are programming an ATtiny2313, use attiny2313 as the partnumber
- -b <baudrate>: This is for overriding the serial baud rate for programmers like the STK500. Don't use this switch, the default is correct.
- -B <bitrate>: This is for changing the bitrate, which is how fast the programmer talks to the chip. If your chip is being clocked very slowly you'll need to talk slowly to it to let it keep up.
- -C <config-file>: The config file tells avrdude about all the different ways it can talk to the programmer. There's a default configuration file, so use that: don't use this command switch
- -c <programmer>: Here is where you specify the programmer type, if you're using an STK500 use stk500, if you're using a DT006 programmer use dt006, etc.
- -D: This disables erasing the chip before programming. Don't use this command switch.
- -P <port>: This is the communication port to use to talk to the programmer. It might be COM1 for serial or LPT1 for parallel or USB for USB.
- -F: This overrides the signature check to make sure the chip you think you're programming is that chip. The test is strongly recommended as it tests the connection, so don't use this switch.
- -e: This erases the chip, in general you don't use this because you will auto-erase the flash before programming.
- -U <memtype>:r|w|v:<filename>[:format]: OK this one is the important command. It's the one that actually does the programming. The <memtype> is either flash or eeprom (or hfuse, lfuse or efuse for the chip configuration fuses, but don't mess with those). the r|w|v means you can use r (read) w (write) or v (verify) as the command. The <filename> is the file that you want to write to or read from. and [:format] means there's an optional format flag. You will always be using "Intel Hex" format, so use i
For example: If you wanted to write the file test.hex to the flash memory, you would use -U flash:w:test.hex:i. If you wanted to read the eeprom memory into the file "eedump.hex" you would use -U eeprom:r:eedump.hex:i - -n: This means you don't actually write anything, it's good if you want to make sure you don't send any other commands that could damage the chip, sort of a 'safety lock'.
- -V: This turns off the auto-verify when writing. You want to verify when you write to flash so don't use this.
- -u: If you want to modify the fuse bits, use this switch to tell it you really mean it.
- -t: This is a 'terminal' mode where you can type out commands in a row. Don't use this, it is confusing to beginners.
- -E: This lists some programmer specifications, don't use it.
- -v: This gives you 'verbose' output...in case you want to debug something. If you want you can use it, but in general you won't.
- -q: This is the opposite of the above, makes less output. In general you won't use it.
-c <programmer>
To get a list of supported programmers, type in avrdude -c asdf (asdf is just some nonsense to get it to spit out the list of programmers) Here is LadyAda's output, yours may vary a little. Don't bother memorizing it, just glance through the list.
C:\>avrdude -c asdf
avrdude: Can't find programmer id "asdf"
Valid programmers are:
dasa3 = serial port banging, reset=!dtr sck=rts mosi=txd miso=cts [C:\WinAVR\bin\avrdude.conf:763]
dasa = serial port banging, reset=rts sck=dtr mosi=txd miso=cts [C:\WinAVR\bin\avrdude.conf:750]
siprog = Lancos SI-Prog <http://www.lancos.com/siprogsch.html> [C:\WinAVR\bin\avrdude.conf:737]
ponyser = design ponyprog serial, reset=!txd sck=rts mosi=dtr miso=cts [C:\WinAVR\bin\avrdude.conf:724]
frank-stk200 = Frank STK200 [C:\WinAVR\bin\avrdude.conf:689]
blaster = Altera ByteBlaster [C:\WinAVR\bin\avrdude.conf:676]
ere-isp-avr = ERE ISP-AVR <http://www.ere.co.th/download/sch050713.pdf> [C:\WinAVR\bin\avrdude.conf:666]
atisp = AT-ISP V1.1 programming cable for AVR-SDK1 from <http://micro-research.co.th/> [C:\WinAVR\bin\avrdude.conf:656]
dapa = Direct AVR Parallel Access cable [C:\WinAVR\bin\avrdude.conf:645]
xil = Xilinx JTAG cable [C:\WinAVR\bin\avrdude.conf:632]
futurlec = Futurlec.com programming cable. [C:\WinAVR\bin\avrdude.conf:615]
abcmini = ABCmini Board, aka Dick Smith HOTCHIP [C:\WinAVR\bin\avrdude.conf:605]
picoweb = Picoweb Programming Cable, http://www.picoweb.net/ [C:\WinAVR\bin\avrdude.conf:595]
sp12 = Steve Bolt's Programmer [C:\WinAVR\bin\avrdude.conf:584]
alf = Nightshade ALF-PgmAVR, http://nightshade.homeip.net/ [C:\WinAVR\bin\avrdude.conf:568]
bascom = Bascom SAMPLE programming cable [C:\WinAVR\bin\avrdude.conf:558]
dt006 = Dontronics DT006 [C:\WinAVR\bin\avrdude.conf:548]
pony-stk200 = Pony Prog STK200 [C:\WinAVR\bin\avrdude.conf:536]
stk200 = STK200 [C:\WinAVR\bin\avrdude.conf:520]
bsd = Brian Dean's Programmer, http://www.bsdhome.com/avrdude/ [C:\WinAVR\bin\avrdude.conf:509]
pavr = Jason Kyle's pAVR Serial Programmer [C:\WinAVR\bin\avrdude.conf:501]
dragon_dw = Atmel AVR Dragon in debugWire mode [C:\WinAVR\bin\avrdude.conf:494]
dragon_hvsp = Atmel AVR Dragon in HVSP mode [C:\WinAVR\bin\avrdude.conf:486]
dragon_pp = Atmel AVR Dragon in PP mode [C:\WinAVR\bin\avrdude.conf:478]
dragon_isp = Atmel AVR Dragon in ISP mode [C:\WinAVR\bin\avrdude.conf:470]
dragon_jtag = Atmel AVR Dragon in JTAG mode [C:\WinAVR\bin\avrdude.conf:462]
jtag2dw = Atmel JTAG ICE mkII in debugWire mode [C:\WinAVR\bin\avrdude.conf:454]
jtag2isp = Atmel JTAG ICE mkII in ISP mode [C:\WinAVR\bin\avrdude.conf:446]
jtag2 = Atmel JTAG ICE mkII [C:\WinAVR\bin\avrdude.conf:438]
jtag2fast = Atmel JTAG ICE mkII [C:\WinAVR\bin\avrdude.conf:430]
jtag2slow = Atmel JTAG ICE mkII [C:\WinAVR\bin\avrdude.conf:422]
jtagmkII = Atmel JTAG ICE mkII [C:\WinAVR\bin\avrdude.conf:414]
jtag1slow = Atmel JTAG ICE (mkI) [C:\WinAVR\bin\avrdude.conf:407]
jtag1 = Atmel JTAG ICE (mkI) [C:\WinAVR\bin\avrdude.conf:399]
jtagmkI = Atmel JTAG ICE (mkI) [C:\WinAVR\bin\avrdude.conf:391]
avr911 = Atmel AppNote AVR911 AVROSP [C:\WinAVR\bin\avrdude.conf:385]
avr109 = Atmel AppNote AVR109 Boot Loader [C:\WinAVR\bin\avrdude.conf:379]
butterfly = Atmel Butterfly Development Board [C:\WinAVR\bin\avrdude.conf:373]
usbtiny = USBtiny simple USB programmer [C:\WinAVR\bin\avrdude.conf:367]
usbasp = USBasp, http://www.fischl.de/usbasp/ [C:\WinAVR\bin\avrdude.conf:361]
avr910 = Atmel Low Cost Serial Programmer [C:\WinAVR\bin\avrdude.conf:355]
stk500hvsp = Atmel STK500 V2 in high-voltage serial programming mode [C:\WinAVR\bin\avrdude.conf:349]
stk500pp = Atmel STK500 V2 in parallel programming mode [C:\WinAVR\bin\avrdude.conf:343]
stk500v2 = Atmel STK500 Version 2.x firmware [C:\WinAVR\bin\avrdude.conf:337]
stk500v1 = Atmel STK500 Version 1.x firmware [C:\WinAVR\bin\avrdude.conf:331]
stk500 = Atmel STK500 [C:\WinAVR\bin\avrdude.conf:325]
avrisp2 = Atmel AVR ISP mkII [C:\WinAVR\bin\avrdude.conf:315]
avrispmkII = Atmel AVR ISP mkII [C:\WinAVR\bin\avrdude.conf:309]
avrispv2 = Atmel AVR ISP V2 [C:\WinAVR\bin\avrdude.conf:303]
avrisp = Atmel AVR ISP [C:\WinAVR\bin\avrdude.conf:297]
You'll note that the programmers mentioned before are listed here, including the avrisp, avrispv2, stk500, dragon, dasa/dasa3/ponyser (serial port bitbanging programmers), dapa/dt006/stk200 (parallel port bitbanging programmers)
Look up the name of the programmer you're using, and remember it!
-p <partno>
To get a list of parts supported by avrdude, type in avrdude -c avrisp (it doesnt matter if you're not useing an avrisp programmer) without a part number into the command line. Don't memorize this list, just glance over it to get an idea of the chips that are supported.
C:\>avrdude -c avrisp
avrdude: No AVR part has been specified, use "-p Part"
Valid parts are:
m6450 = ATMEGA6450 [C:\WinAVR\bin\avrdude.conf:10974]
m3250 = ATMEGA3250 [C:\WinAVR\bin\avrdude.conf:10785]
m645 = ATMEGA645 [C:\WinAVR\bin\avrdude.conf:10596]
m325 = ATMEGA325 [C:\WinAVR\bin\avrdude.conf:10407]
usb1287 = AT90USB1287 [C:\WinAVR\bin\avrdude.conf:10219]
usb1286 = AT90USB1286 [C:\WinAVR\bin\avrdude.conf:10030]
usb647 = AT90USB647 [C:\WinAVR\bin\avrdude.conf:9841]
usb646 = AT90USB646 [C:\WinAVR\bin\avrdude.conf:9651]
t84 = ATtiny84 [C:\WinAVR\bin\avrdude.conf:9468]
t44 = ATtiny44 [C:\WinAVR\bin\avrdude.conf:9286]
t24 = ATtiny24 [C:\WinAVR\bin\avrdude.conf:9104]
m2561 = ATMEGA2561 [C:\WinAVR\bin\avrdude.conf:8911]
m2560 = ATMEGA2560 [C:\WinAVR\bin\avrdude.conf:8718]
m1281 = ATMEGA1281 [C:\WinAVR\bin\avrdude.conf:8530]
m1280 = ATMEGA1280 [C:\WinAVR\bin\avrdude.conf:8341]
m640 = ATMEGA640 [C:\WinAVR\bin\avrdude.conf:8153]
t85 = ATtiny85 [C:\WinAVR\bin\avrdude.conf:7972]
t45 = ATtiny45 [C:\WinAVR\bin\avrdude.conf:7793]
t25 = ATtiny25 [C:\WinAVR\bin\avrdude.conf:7613]
pwm3 = AT90PWM3 [C:\WinAVR\bin\avrdude.conf:7431]
pwm2 = AT90PWM2 [C:\WinAVR\bin\avrdude.conf:7247]
t2313 = ATtiny2313 [C:\WinAVR\bin\avrdude.conf:7060]
m168 = ATMEGA168 [C:\WinAVR\bin\avrdude.conf:6872]
m88 = ATMEGA88 [C:\WinAVR\bin\avrdude.conf:6686]
m48 = ATMEGA48 [C:\WinAVR\bin\avrdude.conf:6499]
t861 = ATTINY861 [C:\WinAVR\bin\avrdude.conf:6311]
t461 = ATTINY461 [C:\WinAVR\bin\avrdude.conf:6122]
t261 = ATTINY261 [C:\WinAVR\bin\avrdude.conf:5933]
t26 = ATTINY26 [C:\WinAVR\bin\avrdude.conf:5776]
m8535 = ATMEGA8535 [C:\WinAVR\bin\avrdude.conf:5618]
m8515 = ATMEGA8515 [C:\WinAVR\bin\avrdude.conf:5460]
m8 = ATMEGA8 [C:\WinAVR\bin\avrdude.conf:5300]
m161 = ATMEGA161 [C:\WinAVR\bin\avrdude.conf:5160]
m32 = ATMEGA32 [C:\WinAVR\bin\avrdude.conf:4985]
m6490 = ATMEGA6490 [C:\WinAVR\bin\avrdude.conf:4792]
m649 = ATMEGA649 [C:\WinAVR\bin\avrdude.conf:4607]
m3290 = ATMEGA3290 [C:\WinAVR\bin\avrdude.conf:4424]
m329 = ATMEGA329 [C:\WinAVR\bin\avrdude.conf:4239]
m169 = ATMEGA169 [C:\WinAVR\bin\avrdude.conf:4059]
m163 = ATMEGA163 [C:\WinAVR\bin\avrdude.conf:3916]
m162 = ATMEGA162 [C:\WinAVR\bin\avrdude.conf:3720]
m644 = ATMEGA644 [C:\WinAVR\bin\avrdude.conf:3530]
m324 = ATMEGA324 [C:\WinAVR\bin\avrdude.conf:3338]
m164 = ATMEGA164 [C:\WinAVR\bin\avrdude.conf:3146]
m16 = ATMEGA16 [C:\WinAVR\bin\avrdude.conf:2968]
c128 = AT90CAN128 [C:\WinAVR\bin\avrdude.conf:2777]
m128 = ATMEGA128 [C:\WinAVR\bin\avrdude.conf:2599]
m64 = ATMEGA64 [C:\WinAVR\bin\avrdude.conf:2418]
m103 = ATMEGA103 [C:\WinAVR\bin\avrdude.conf:2278]
8535 = AT90S8535 [C:\WinAVR\bin\avrdude.conf:2157]
8515 = AT90S8515 [C:\WinAVR\bin\avrdude.conf:2043]
4434 = AT90S4434 [C:\WinAVR\bin\avrdude.conf:1960]
4433 = AT90S4433 [C:\WinAVR\bin\avrdude.conf:1836]
2343 = AT90S2343 [C:\WinAVR\bin\avrdude.conf:1712]
2333 = AT90S2333 [C:\WinAVR\bin\avrdude.conf:1627]
2313 = AT90S2313 [C:\WinAVR\bin\avrdude.conf:1514]
4414 = AT90S4414 [C:\WinAVR\bin\avrdude.conf:1401]
1200 = AT90S1200 [C:\WinAVR\bin\avrdude.conf:1286]
t15 = ATtiny15 [C:\WinAVR\bin\avrdude.conf:1153]
t13 = ATtiny13 [C:\WinAVR\bin\avrdude.conf:980]
t12 = ATtiny12 [C:\WinAVR\bin\avrdude.conf:847]
t11 = ATtiny11 [C:\WinAVR\bin\avrdude.conf:783]
That's all the chips that avrdude knows about. Almost all of them are ISP programmable.
Watch out: t2313 and 2313, m8 and m88, c128 and m128, etc look very similar but they are in fact quite different chips! For that reason I suggest you type out the name of the chip, that is, instead of t2313 use attiny2313 or m8 use atmega8. Avrdude is smart enough to know what you mean if you type out the full name.
You're going to use the ATtiny2313 so use the part number attiny2313 or (t2313)
Double check what chip you are using by looking at the top of the chip, these say ATTINY2313 and ATMEGA8 (respectively) the -20PI and -16PC are just the speed ratings and package descriptions so ignore them for now.
-P <port>
This switch tells avrdude where to look for your programmer. If you are using a USB connected device, you can just use -P usb or, leave it out. The programmer automatically knows when the programmer is a USB device.
For Mac's, there are no parallel ports. However, if you're using a USB-serial adapter (which lets you use an STK500 or AVRISP v1 with a Mac) then you'll need to specify the serial port. LadyAda doesn't know a foolproof way yet but the way I do it is in the Terminal I type in ls -l /dev/cu.* and it will spit out a bunch of stuff (I screwed up the screen capture, this image has another window in front of it, but just ignore that)
/dev/cu.Bluetooth is the built in bluetooth stuff, dont use that. /dev/cu.modem is the modem (if there is one), dont use that either. What you're looking for is something like /dev/cu.usbserial or /dev/cu.KeySerial1 or something similar. In this case its /dev/cu.usbserial-FTCTYG5U
-U <memtype>:r|w|v:<filename>[:format]
OK this is the important part. This is where you actually get around to telling avrdude how to put the data onto the chip. This command is rather complex, but break it down.
<memtype> - can be flash, eeprom, hfuse (high fuse), lfuse (low fuse), or efuse (extended fuse)
r|w|v - can be r (read), w (write), v (verify)
<filename> - the input (writing or verifying) or output file (reading)
[:format] - optional, the format of the file. You can leave this off for writing, but for reading use i for Intel Hex (the prevailing standard )
For example:
- To write a file called firmware.hex to the flash use the command: -U flash:w:firmware.hex
- To verify a file called mydata.eep from the eeprom use the command -U eeprom:v:mydata.eep
- To read the low fuse into a file use the command -U lfuse:r:lfusefile.hex:i
- -p <partno>: This tells which microcontroller avrdude is programming. For example, if you are programming an ATtiny2313, use attiny2313 as the partnumber
/ /Testing avrdude
Materials
- ATtiny2313-ZIF Mini Development Kit
- 6-Pin DIL Ribbon Cable,3"
- ISP Shield for Arduino
- USBtinyISP AVR Programmer Kit (USB SpokePOV Dongle) - v2.0
- Arduino
- LED
- 220Ω resistor
Pulling it together
- Solder together the ISP Shield for Arduino
- Solder together the ISP Shield for Arduino
- Solder togetherUSBtinyISP AVR Programmer Kit
-
Solder together the ATtiny2313-ZIF Mini Development Kit
- Your target board will be powered by your programmer. So make sure that the jumper is on the USBTiny
- Open BBEdit and paste the following in:
This is hex code ready to be uploaded to the microcontroller.
:1000000012C02AC029C028C027C026C025C024C0CD :1000100023C022C021C020C01FC01EC01DC01CC0E4 :100020001BC01AC019C011241FBECFEDCDBF10E0F8 :10003000A0E6B0E0E4E0F1E003C0C89531960D928F :10004000A036B107D1F710E0A0E6B0E001C01D92E4 :10005000A036B107E1F716C0D3CF61157105810550 :10006000910579F027E630E140E050E0245030403F :100070004040504057FFFACF615070408040904060 :10008000ECCF0895CFEDD0E0DEBFCDBF8FEF87BBC3 :1000900018BA81E088BB68EC70E080E090E0DDDFBA :1000A00082E088BB68EC70E080E090E0D6DF84E01E :1000B00088BB68EC70E080E090E0CFDF88E088BB30 :1000C00068EC70E080E090E0C8DF80E188BB68EC1D :1000D00070E080E090E0C1DF80E288BB68EC70E017 :1000E00080E090E0BADF80E488BB68EC70E080E0FC :1000F00090E0B3DF80E888BB68EC70E080E090E0DF :04010000ACDFC7CFDA :00000001FF
- Save the file as test_leds.hex on the Desktop.
- Find the path to your file by typing cd and dragging the hex file into terminal. Delete the file name and press RETURN.
- This next step is where you name what programmer you are using and which port it's connected to (in this example you'll be using a usbtinyisp. Since the usbtinyisp is a USB programmer you leave off the -P <port> switch.
Type in avrdude -c usbtiny -p attiny2313 -U flash:w:path_of_test_leds.hex - Avrdude should go through the following steps:
- Initializing the programmer (you wont see this if it works)
- Initializing the AVR device and making sure its ready for instructions
- Reading the device signature (0x1e910a) which confirms that the chip you specified in the command line (attiny2313) is in fact the chip that the programmer is connected to
- Erasing the chip
- Reading the file and verifying it's a valid file
- Writing the flash
- Verifying the flash
- Initializing the programmer (you wont see this if it works)
Burning fuses
Fuse memory is a seperate chunk of flash that is not written to when you update the firmware. Instead, the fuses tend to be set once (although they can be set as many times as you'd like). The fuses define things like the clock speed, crystal type, whether JTAG is enabled, what the brownout (minimum voltage) level is, etc. For more information on fuses you can read about them here.
First you'll want to calculate fuses using the very convenient AVR Fuse Calculator
To program the fuses, use:
avrdude -c usbtiny -p attiny2313 -U lfuse:w:<0xHH>:m
avrdude -c usbtiny -p attiny2313 -U hfuse:w:<0xHH>:m
avrdude -c usbtiny -p attiny2313 -U efuse:w:<0xHH>:m
Where <0xHH> is the desired fuse value, in hex. For example to set the high fuse to 0xDA:
avrdude -c usbtiny -p attiny2313 -U hfuse:w:0xDA:m
Setting the fuses incorrectly can 'brick' the chip - for example you can disable future programming, or make it so the chip is expecting an external crystal when there isn't one. For that reason I suggest triple-checking the fuse values. Then check again, make sure you aren't disabling ISP programming or the Reset pin or setting the clock speed to 32kHz. Then verify again that you have the correct chip for calculation. Then finally you can try writing them to the chip!
Remember: once you set the fuses you do not have to set them again
There are 3 bytes of permanent storage called the fuses. The fuses determine how the chip will act, whether it has a bootloader, what speed and voltage it likes to run at, etc.
Fuses are documented in the datasheets, but the best way to examine the fuses is to look at a fuse calculator
Clock selection
The first option is how the chip is clocked. By default, chips that come from the factory have the Internal 8 MHz clock with 14CK + 65ms Startup.Every CPU uses a clock, The clock keeps track of time for the chip, in general one assembly code instruction is run every clock cycle. If you look at the menu you'll see a huge list of options, but looking carefully you'll see there are two groupings, the Clock Source, the Clock Startup
The Clock Source can be either of the following:
- External Clock, Internal 8MHz clock, Internal 4MHz clock, Internal 128KHz clock, External Crystal (0.4-0.9 MHz), External Crystal (0.9MHz - 3.0MHz), External Crystal (3.0MHz - 8.0MHz)
- External Crystal (8.0MHz +)
- 14CK + 0 ms, 14CK + 4 ms
- 14CK + 65 ms
Internal Clock means that there's a little oscillator inside the chip, it's not very precise but good for most projects that don't have fine timing issues. The clock varies with temperature and the power supply voltage. You can chose from a 8MHz, 4MHz or 128KHz clock. The 128KHz clock is for very low power applications where running the chip very slowly helps conserve power. Having an internal oscillator means you don't need to wire up a crystal and you can use the clock pins for other purposes.
External Crystal
If you need a special clock rate, like 3.58MHz or 12MHz or a high precision clock that won't drift with the temperature, you'll want an external crystal or oscillator.
The Startup Time is just how long the clock source needs to calm down from when power is first applied. Always go with the longest setting 14CK + 65ms unless you know for a fact your clock source needs less time and 65ms is too long to wait.
Clock Output
The next option is Clock Out on PortD2 which means, basically, that whatever the clock input is (internal, external, crystal, etc) a square wave of the same frequency will appear on pin D2. This is useful if you're debugging the clock rate, or if you want to use the clock to drive another chip.By default, chips that come from the factory have this turned off.
Clock Divide
This fuse causes the chip to divide the clock rate by 8. So if the clock source is set to Internal 8MHz and you have this fuse set, then you'll really be running at 1MHz.By default, chips that come from the factory have this turned on.
Reset Disable
This fuse turns the Reset pin into a normal pin instead of a special pin. If you turn this fuse on you can't program the chip using ISP anymore. Never set this fuse unless you really mean to.By default, chips that come from the factory have this turned off (Reset is enabled)
Brown-out Detect (BOD)
These fuses set what voltage to turn the Brownout protection circuitry on. A brown-out in this case is the same as when the power-grid gets over-taxed by thousands of people running their air conditioners in the summer: the voltage drops and your clock radio maybe stops working. A brownout for a chip means that the power voltage is too low for it to run reliably at the speed of the clock.The attiny2313 can run as fast at 20MHz but only if the power voltage is between 4.5V and 5.5V. If the voltage is lower than that, it may behave erratically, erasing or overwriting the RAM and EEPROM. It may also start running random piece of the flash program. To keep it from doing that, set the brown-out voltage to 4.3V, then if the voltage dips, the chip will turn off until the voltage returns. It will then reset and start over.
If the chip is meant to run at 5V, set the brown-out to 4.3V.
If the chip can run as low as 3.3V you can set the brown-out to 1.8V.
If the chip is a low voltage compatible chip such as the attiny2313V (which can run as low as 1.8V if its clocked at 4MHz or less) then you can set the brown-out to 1.8V.
You can read the speed and voltage grades on the front page of the datasheet.
By default, chips that come from the factory don't have brown-out detect however you should set it. If you have a bootloader or are storing data in the EEPROM you must set it.
Stuff that can go wrong: AVR initilization failed
If the programmer is not properly connected to the chip, you'll get the following message: initialization failed, rc=-1 Double check connections and try again, or use -F to override this check
Don't use -F to override the check, even though it is suggested! This means that the programmer couldn't talk to the chip. It usually means the programmer is OK but it couldnt find the chip.
Check that the chip is powered, plugged into the socket or programmer properly, the programming cables are plugged in correctly, the header is wired correctly, etc. 99% of the time, it is a problem with wiring.
Stuff that can go wrong: Signature failure
Try running this command avrdude -c usbtiny -p atmega8 -U flash:w:test_leds.hex
You'll see that it stops at step 2, once the signature is different than the expected one it stops. This is because code that is compiled for an attiny2313 wont run on an atmega8 (this is true of most microcontrollers, the .hex files are not cross compatible)
Makefiles
You could compile and upload this program by manually typing the commands needed to properly run the compiler (avr-gcc) and uploaded (avrdude), but this gets tedious. Instead, you can place the rules for running these programs into another file, called a makefile, and use a program called make to run the programs for you.
Here's a basic Makefile that can compile and upload an AVR C program:
PROJECT=blink SOURCES=$(PROJECT).c MMCU=attiny2313 F_CPU=1000000 # The serial port of your Arduino board (if using Arduino as ISP): # - on Mac OS X, use: /dev/tty.usbserial-* # - on Windows, use the appropriate COM port, e.g.: COM3 # - on Linux, use: /dev/ttyUSB0 SERIAL=/dev/tty.usbserial-A6007WRw CFLAGS=-mmcu=$(MMCU) -DF_CPU=$(F_CPU) -Os $(PROJECT).hex: $(PROJECT).out avr-objcopy -j .text -O ihex $(PROJECT).out $(PROJECT).hex $(PROJECT).out: $(SOURCES) avr-gcc $(CFLAGS) -o $(PROJECT).out $(SOURCES) program-arduinoisp: $(PROJECT).hex avrdude -p $(MMCU) -P $(SERIAL) -c stk500v1 -b 19200 -U flash:w:$(PROJECT).hex program-avrisp2: $(PROJECT).hex avrdude -p $(MMCU) -P usb -c avrisp2 -U flash:w:$(PROJECT).hex program-usbtiny: $(PROJECT).hex avrdude -p $(MMCU) -c usbtiny -U flash:w:$(PROJECT).hex clean: rm $(PROJECT).out $(PROJECT).hex
Save it in a file called makefile in the same directory as your program. To compile the program, you simply type make at the command line.
To upload, you need to tell make to run the rule in the makefile that corresponds to the programmer you're using, for example:
- make program-avrisp2 to upload using an AVRISP mkII
- make program-usbtiny to upload with a USBtiny or FabISP
- make program-arduinoisp to upload using an Arduino as an ISP
Using USBTiny
- Place your ATTiny2313 in the target board. Add a .1µF capacitor between GND and VCC:
- Open up BBEdit and paste this program in:
This program sets pin PB0 as an output and then sets it high.
#include <avr/io.h> #define F_CPU 2000000UL // Some macros that make the code more readable #define output_low(port,pin) port &= ~(1<<pin) #define output_high(port,pin) port |= (1<<pin) #define set_output(portdir,pin) portdir |= (1<<pin) /** * Alias our color channel pinouts */ #define RED PB0 #define GREEN PB1 #define BLUE PB2 #define MOTOR PB3 /** * Create our rgb structure and declare it below as a global */ typedef struct { int red; int green; int blue; } rgb; rgb channels = { 0x00, 0x00, 0x00 }; void rainbow_step(void) { //Fade from blue to red if(channels.blue > 0x00 && channels.red == 0xFF && channels.green == 0x00) { channels.blue--; } if(channels.blue == 0xFF && channels.red < 0xFF && channels.green == 0x00) { channels.red++; } //Fade from green to blue if(channels.green > 0x00 && channels.blue == 0xFF && channels.red == 0x00) { channels.green--; } if(channels.green == 0xFF && channels.blue < 0xFF && channels.red == 0x00) { channels.blue++; } // Fade from red to green if(channels.red > 0x00 && channels.green == 0xFF && channels.blue == 0x00) { channels.red--; } if(channels.red == 0xFF && channels.green < 0xFF && channels.blue == 0x00) { channels.green++; } } // Software Pulse Width Modulation for RGB void do_pwm(int r_duty, int g_duty, int b_duty, int rate) { int i; while (rate != 0) { // Set our LED pins as output high PORTB |= (1<<RED) | (1<<GREEN) | (1<<BLUE) | (1<<MOTOR); for (i=0; i < 255; i++) { if (i == r_duty) output_low(PORTB, RED); if (i == g_duty) output_low(PORTB, GREEN); if (i == b_duty) output_low(PORTB, BLUE); } rate--; output_high(DDRB,MOTOR); } } void init(void) { channels.red = 0xFF; set_output(DDRB, RED); set_output(DDRB, GREEN); set_output(DDRB, BLUE); set_output(DDRB,MOTOR); } int main(void) { init(); while(1) { rainbow_step(); do_pwm(channels.red, channels.green, channels.blue, 8); } return 0; }
When you write a C program, you usually use existing code libraries that provide common functionality. To use the functions contained in the AVR Libc library, you need to include header files in our program. These header files contain specifications for the functions in the library, which allows the compiler to make sure that your program uses them correctly. To include a header file, you use a statement like:This particular header file contains definitions for the input and output (i/o) pins available for the various AVR microcontrollers.#include <avr/io.h>
The main() function
C programs include a function (block of code) called main(). This function is called when the program starts. The body of the function (the lines of code it contains) are delimited by curly braces {}. - Save the file as rgb.c on the Desktop
- Open Terminal
- Navigate to the Desktop using cd
- To get your program onto the microcontroller, you'll need to compile it (translate it into a machine-readable format) and upload it. When you compile the program you create a hex file. Paste the following after the prompt:
You should now have a hex file on the desktop.
avr-gcc -mmcu=attiny2313 -o rgb.out rgb.c avr-objcopy -O ihex rgb.out rgb.hex
- Connect the USBTiny to your target board (make sure the jumper connects the two power pins:
- You can upload this hex file with avrdude. You will use -U flash:w:main.hex to tell avrdude to upload the main.hex file and write it to the flash memory of the microcontroller:
avrdude -c usbtiny -p attiny2313 -U flash:w:rgb.hex
- You should see something like this:
- Attach an LED and see if it worked