Difference between revisions of "Programming microcontrollers"

From Fab Lab Wiki - by NMÍ Kvikan
Jump to: navigation, search
Line 1: Line 1:
Programming microntrollers is about getting your code onto a microcontroller. Normally we take code from a computer that is written in [[Assembly]], [[compile]] that into a language the microcontroller understands and then put onto it using a serial DSB cable and the AVRdude and gavrasm software.
+
Programming microntrollers is about getting your code onto a microcontroller. Normally we take code from a computer that is written in [[Assembly]], [[compile]] that into a language the microcontroller understands and then put onto it using a parallel DSB cable and the AVRdude and gavrasm software.
  
 
== What you need ==
 
== What you need ==
Line 10: Line 10:
 
* gavrasm installed
 
* gavrasm installed
 
* avrdude installed
 
* avrdude installed
* the cable attached to the computer (where it fits)
+
* the programming cable attached to the computer (where it fits)
 
* your circuit ready, with a battery connected
 
* your circuit ready, with a battery connected
  

Revision as of 22:26, 15 January 2010

Programming microntrollers is about getting your code onto a microcontroller. Normally we take code from a computer that is written in Assembly, compile that into a language the microcontroller understands and then put onto it using a parallel DSB cable and the AVRdude and gavrasm software.

What you need

Programming hello-world microcontrollers through parallel port in Linux. There are multiple ways of putting your code on to the microcontroller. This tutorial is on how to do it through the BASH (shell/terminal) using gavrasm and avrdude, with the AMP MTA/DB25 (BSD) cable.

You should have:

  • a text file with assembly code with the extention .ASM
  • gavrasm installed
  • avrdude installed
  • the programming cable attached to the computer (where it fits)
  • your circuit ready, with a battery connected

You can get the Assembly code here


First

First you need to make your .ASM asembly file into a .HEX hexadecimal file. For this you use gavrasm. Open the terminal window, and navigate to where your file.ASM is. If it is on the desktop, you type "cd ~/Desktop". The syntax for gavrasm is straight forward:

 gavrasm file.ASM

The above command will return a file.HEX with the same name, in the same place as file.ASM -provided your file.ASM contains valid assembly code. If it does not, gavrasm will echo the errors back to you.


Second

The second step is to upload the file.HEX to the chip. Connect the programming cable to your circuit board. We are using the "in-circuit programming AMP MTA plug <-> DB25M parallel (bsd) cable" found in the shoebox (http://fab.cba.mit.edu/about/fab/). Make sure your board has power from a battery or powersuply, since this perticular cable don't carry power. We call avrdude, and give it the right parameters for the job. We need to state what chip we are uploading to, wich cable we are using and the method for storing our program on the chip. That should give us something like the folowing:

 avrdude -d t44 -c bsd -U flash:w:file.hex

Quick breakdown: We tell avrdude that -d (the device) is a tiny44, the -c (cable) is the "bsd" one, and after -U we say that we are using "flash" memory, "w" for write and then we give it the file to upload.

Problems with /dev/parport0? Try these three:

 sudo modprobe parport
 sudo modprobe ppdev
 sudo chmod 777 /dev/parport0

Then again with the avrdude...

The chip will start executing the code when you disconnect the programming cable.


The C/P-list

 tiny45 load hex file, bsd cable: avrdude -p t45 -c bsd -U flash:w:file.hex
 tiny45 load hex file, dasa cable: avrdude -p t45 -P /dev/ttyUSB0 -c dasa -U flash:w:file.hex
 tiny44 load hex file: avrdude -p t44 -c bsd -U flash:w:file.hex
 tiny44 use 20 MHz xtal: avrdude -p t44 -c bsd -U lfuse:w:0x7E:m
 mega88 load hex file: avrdude -p m88 -c bsd -U flash:w:file.hex
 mega88 use 20 MHz xtal: avrdude -p m88 -c bsd -U lfuse:w:0x76:m
 mega644 load hex file: avrdude -p m644 -c bsd -U flash:w:file.hex
 mega644 use 20 MHz xtal: avrdude -p m644 -c bsd -U lfuse:w:0x76:m


More details

Details on the MOSI/MISO way of communicating can be found here: http://en.wikipedia.org/wiki/SPI_bus

Troubleshooting

Problems that you might run into include:

  • The microcontroller being on the wrong way; make sure to check your schematics! The little dot is the '1' pin.
  • The microcontroller not working; it might have overheated or broken down. In that case you need to replace it.
  • You might have inverted the polarity of your power source. Make sure the current flows the right way.
  • Your tin might make a shortcircuit, make sure it doesn't flow from one wire to another.
  • Make sure your power source is connected to the microcontroller. You can use a voltmeter to check. It should recieve around 3-5V.
  • Make sure your cables are correct. You can check here if you don't know how your cables should be. Make sure to check with your schematics also.
  • Components can sometimes not make contact with the copper on the board despite their appearance to the contrary. Use a multimeter to make sure all your components are in contact with the board.