:: dogiChow ::

06 // PóniLó morZe - MCU programming

This week the assignment was simple: redesign a PCB by adding an LED and Button, and program it so they do something cool. My design is Póniló morZe - a Morse encoder, which converts characters reaching it over the serial port to blinks of light.

The process is simple but requires quite a few steps:

  • Design the PCB. This consists of first adding semantical connections between elements (connected. to a PIN. through a NET. to a component that has a VALUE, etc.). In my previous life I used Eagle numerous times, thus I was happy that Ed Baafi did all the hard work for all of us and ported the design to Eagle. Here is the link to the Schematic and to the Board :). Thanks Ed! I added a button and a LED and checked my design against electrical design errors (ERC) and design rule violations (DRC), which for example tells me if the milling bit won't fit between two traces. Extremely helpful. I recommend using it when you are milling a board.

  • Next step is to materialize the board, through the steps I described during week 3. I choose to mill the board on the Mantis Machine. The tutorial is here for the Mantis 9. It was super-fast and easy to use, I was very happy with the result. During my PCB design I used a DRC check that corresponds to 17mils, which worked beautifully for my design, the Mantis cut wide enough traces everywhere. Therefore the PóniLó morZe was born.

  • Stuff the board a boring process. no comment.

  • Write the AVR code for the MCU. I will go over this in detail.

  • Compile and upload your program to your precious ATtiny44

The program, compiling and uploading

My program can be viewed here. Most MCU programs are quite easy to understand: first you setup all the ports and pins and their directions and properties, like the pull-up resistor that needs to be switched on if a button is connected. Then the programs goes to a Coma, where it can recover from when an interrupt arrives that is generated when a Serial event happens (a character arrives from the Serial line). If no interrupt is present you cannot even read a button periodically, because the whole program is concentrated on reading a serial value. Thus we like interrupts.

Some important revelations:

1. If you are toggling an LED and you measure it with a multimeter and it jitters between 3.6...4.8V that means you have not set it to output and you are pulling it through a transistor from the "wrong" direction.

2. In order to use the 20MHz external oscillator you have to TELL the MCU to use it. This is what fuse bits are for. This is part of the Uploading workflow NOT the Coding workflow. In other words you do it with the Makefile not the C or ASM file! Here is my Makefile that works on Mac OS X:

		F_CPU = 20000000

		CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU)

		$(PROJECT).hex: $(PROJECT).out
			avr-objcopy -j .text -O ihex $(PROJECT).out $(PROJECT).c.hex;\
		   avr-size --mcu=$(MMCU) --format=avr $(PROJECT).out

		$(PROJECT).out: $(SOURCES)
			avr-gcc $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES)

		program: $(PROJECT).hex
			avrdude -p t44 -c usbtiny -U flash:w:$(PROJECT).c.hex

		programfuses: $(PROJECT).hex
			avrdude -p t44 -c usbtiny -U lfuse:w:0x7E:m
To compile your code type (in the directory of your source):

>> make

To upload your program type:

>> make program

To program the fuse bits type:

>> make programfuses

Now you are using the 20Mhz crystal which is very precise and fast.

Dögi's How to make almost anything? photoset Dögi's How to make almost anything? photoset