Saturday, 14 May 2016

Microcontrollers part II: Have a byte of ATtiny85

Microcontrollers come in a wide variety of models and packages. For my projects I'll use ones from Atmel's AVR family. After a long pondering, it turned out that AVR offers the best possible...erm, no. The true reason to pick AVR was that I've had one course in digital electronics that involved a project work using AVR chips. Thus I have a vague idea how to work with AVR controllers which makes my task a tad easier.

The model I'll start working with is ATtiny85. As the AVR family itself, the model was chosen after a comprehensive thought process i.e. I picked it randomly from the catalog while queuing at the local electronics store. I usually don't think too much beforehand; I tend to start with the first idea that comes to my mind and rethink if it fails.

ATtiny85 in DIP8 package
And here's the badass. To run this beast we need operating voltage of at least 2.7 volts but to get out the most of it we should crank it up to 5.5 V. With sufficient amount of volts, it can be run at the maximum clock frequency of 20 Mhz. It has 6 input/output pins, although usually one of them is reserved for other purpose. It has 8 KiB of flash memory to store the programs, and 512 bytes of RAM and EEPROM memory each to hold the run-time data and to store long term data, respectively. Such wow!

As bits and bytes are essential in digital electronics, I want to say a few words about them. A bit is the smallest unit of information in digital computing. It has two possible values: HIGH (ON, 1) or LOW (OFF, 0). In electronics these are practically implemented with voltage; either there is some or there is not. When we want to present numbers in binary, we put together multiple bits. For example with two bits we can present numbers from 0 to 3 with 0 = 00, 1 = 01, 2 = 10, and 3 = 11.

In 8-bit microprocessors the data is handled in the groups of 8 bits, which are also known as bytes. Each byte holds a value in the range of 0 to 255. Depending on the situation, bytes can represent other types of data as well, like characters or instructions for the processor. In ATtiny85, there is 8 KiB of memory for the program. KiB is often called kilobytes but the technically correct term would be kibibyte. One kibibyte is 1024 bytes, so 8 KiB would be 8192 bytes.

To put things into perspective, this is what it would approximately look like as an 8-bit grayscale bitmap (here I was forced to use PNG format which compresses it down). The picture contains 90 x 90 pixels. The "greyness" of each pixel is presented by one byte with the value in the range of 0 (black) to 255 (white). 512 bytes would be 16x32 sized rectangle to which Mario barely fits in. It is less than it would take to store this paragraph if every character was presented with a single byte! "Much" memory but sufficient for now.

Next we have to figure out how to connect the microcontroller to the rest of our chip. For this we need to get our hands on its datasheet. Datasheets are the source of all the necessary information regarding electronic components. Each component has a datasheet that tabulates its characteristics, like temperature behaviour or typical variation in parameter values, and most importantly for complicated components, how to use them. So in its datasheet, the pinout for ATtiny85 in DIP8 package looks like this:

ATtiny85 pinout from the datasheet
Holy fuck. Apparently this bastard is capable of doing way more stuff I knew. Honestly I have no idea what most of these acronymic abominations mean. Fortunately I don't have to. The essential thing for now is to know that VCC stands for positive operating voltage, GND for ground (i.e. negative end of the battery) and PB0 to PB4 are the input and output pins where we will eventually connect some buttons and displays and whatever our console needs.

According to the datasheet, PB5 is a special case. It behaves normally as the RESET pin, or technically its inverse. This means that if LOW signal is put to the RESET pin, it resets the chip and we get nowhere in our program. To prevent this we have to wire it to HIGH signal which is easiest to get from the operating voltage. Thus all I have to do is to connect the wires to the chip like this:


and voilĂ , it works! ...Or it would work, if it had a program in it. Now it does pretty much nothing but waste electricity. At this point we'll need to get out the Raspberry Pi but that'll be the topic of the next post.


References:

ATtiny25/45/85 datasheet

Original screenshot from the Super Mario Bros. from https://en.wikipedia.org/wiki/Super_Mario_Bros.

No comments:

Post a Comment