Jeffery makes (almost) Anything

Project 7 : Programming Microcontrollers

Programs to Download and install

To compile the scripts for the board I needed avr-gcc. This can be installed once homebrew is installed on mac. In the website's words: "Homebrew installs the stuff you need that Apple didn’t." instead of us having to run many lines of code.

To install Homebrew I ran the following:

-e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then I make the repository and install avr-gcc:

brew tap osx-cross/avr
brew install avr-libc

To send the code to the board, I also need avr-dude, installed with:

install avrdude --with-usb

Once this is set up, I can use these directly or run them through makefiles. This is what I did, using this format (simply changing the name of the file at the top):

Embedded Text Document

With this makefile there are three terminal commands to remember:

make -f [makefile_name].make // compile the .c code and make hex file
make -f [makefile_name].make program-usbtiny // sending program
make -f [makefile_name].make program-usbtiny-fuses // sending program and set the external clock (only need to use once)

I also installed the Arduino software, which provides an environment for coding in C with additional libraries. In order to program ATtiny microcontrollers from the environment it was necessary to download some settings. I went to the tutorial on the High-Low Tech website. They show that we can put the following link in the "Additional Boards Manager URLs" (under Preferences):

'https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json'

Once that is done, do Tools > Board > ATtiny > install, then the board utilities can be used in Tools > Board.

First Try at Programming

At first I used the example code found in Rob's tutorial. I used the third version in the tutorial; however the pin numbers of my board were switched around compared to those in the example program so I had to change the macro PA5 to PA3 (for the button), PB2 to PA7 (for the LED), PORTA to PORTB, DDRA to DDRB (see ahead for an explanation of these terms).

Hooking up the cables right took some verifying:

  1. A mini-usb cable from the computer to the fabISP programmer
  2. The 6-pin FTDI cable connected to the hello-world board, making sure the direction is correct (ground is black, red is VCC)
  3. The 6 wire ribbon cable between 6 pin headers between the 2 boards (make sure ground connects to ground)

After making the right connections and make commands, the LED was turning on when I pressed the button: success!

Learning about programming

I used the first few chapters of the very helpful book "Make: AVR Programming". The author has a bunch of useful examples and header files on Github. I compiled several notes on the programming:

On the micro controllers in general (different models, pin organization, components, peripherals):

Embedded Text Document

Avrdude commands, errors and makefile commands:

Embedded Text Document

Makefile basics:

Embedded Text Document

Most important of all, AVR programming basics: Hardware registers, var types, and usefule header files

Embedded Text Document

Making my own Programs

After learning the basics, I made a few programs from scratch, trying out different methods, from bitwise manipulation to using macros. I also changed the effect: flashing when pressed, change in luminosity, a fade effect, and choosing a rnadom luminosity. Here are the different effects:

The first iteration (the flashing effect), used explicit bit assignment and manipulations:

Embedded Text Document

The next iteration explored the use of PMW to get different luminosities:

Embedded Text Document

The third program has the same effect but uses macros to change bits:

Embedded Text Document

The next iteration implements a fading effect: every press decreases the luminosity:

Embedded Text Document

The fifth version chooses the luminosities at random:

Embedded Text Document

The sixth version has the same fade effect but uses many more macros:

Embedded Text Document

With these programs I got to understand all the different layers of code. I also got to play around with PWM a bunch, but only with brute force explicit delays. Steps for the future include:

  1. I am hoping to define macros so as to make the their use and defining a bit less redundant. I could do this by looking at how to concatenate macros.
  2. How PWM would be set up explicitly without a loop (so that different frequencies can run at the same time and the chip can do other things at the same time)
  3. Learning to use Interupt loops as opposed to querying if something has happened at each loop
  4. Using the arduino environment and libraries
  5. Checking in more detail the functioning of avr-gcc. Apparently the command 'objdump' recomposes code from the assembly code avr-gcc made, hence showing how the compiler is making my code more efficient.

Files

Tools used