Course Materials




MUSIC 250A / CS377C
Human Computer Interaction Theory and Practice:
Designing New Devices

    About Microcontrollers



      A quick overview on Microcontroller architectures

    • Registers- All information in the microcontroller, from the program memory, the timer information, to the state on any of input or output pins, is stored in registers. Registers are like shelves in the bookshelf of processor memory. In a 8-bit processor, like the ATMega 163 we are using, the shelf can hold 8 books, where each book is a one bit binary number, a 0 or 1. Each shelf has an address in memory, so that the controller knows where to find it.
    • Bits and bytes- 8-bits is equal to one byte, and there are 256 unique possible values for each byte. All the information in the microcontroller is stored in byte-size chunks; since it would be tedious to write out all the 1's and 0's in binary format, we represent each byte of information as a two-digit hexadecimal number. For example, 11110011 in binary=243 in decimal=F3 in hexadecimal. We usually write 0xF3 to clue people in that the numbers are in base 16. Also, people sometimes write F3h, where h means hex.
    • Hex code- All the code you write is linked, assembled and otherwise compiled into hex code, which is a series of hexadecimal numbers strategically arranged to tell the controller what to do. The wonder of a nice high-level programming language like C is that these numbers are loaded in the appropriate places in memory without your having to know very much at all about how the logic in the controller is laid out.
    • IO Registers- In order to read and write to the input and output pins on the microcontroller, however, you will need to know a little about the input and output architecture. The reason that the 32 IO pins of the ATMega163 are divided into 4 ports of 8 pins is that this allows the state of the pins to be represented by 4 bytes, named PORTA, PORTB, PORTC and PORTD. Each physical IO pin corresponds to a logical bit on the port. The value of pin3 on Port D lives in slot 3 on the PORTD bookshelf.
    • Setting and Clearing IO- The term for assigning a logical high value (1) to a pin is setting, and the term for assigning a logical low value (0) is clearing. You can write to the IO registers one byte at a time, for example, by assigning the value 0xBB to PORTB, or by assigning one bit at a time, clearing PB2 (portB bit 2) and PB6, and setting the rest.
    • DDR Registers- Not all of the IO registers are physical pins. Since the IO pins are configurable to be either input or output, the controller needs some place to store the directionality of each bit. These are stored in the Data Direction Registers. Like all the other registers, the DDRs have 1's and 0's, but its 1's and 0's indicate whether the corresponding port pin is an input (1) or output (0). This register acts as a IO port librarian, controlling who is allowed to change the data on the shelves, bit by bit. So, if I set DDRA to 0xF0 (a.k.a. 1111 0000), this means that bits 7-4 on PORTA are set to input, and bits 3-0 are set to output. If PORTA is originally set to 0xFF (a.k.a 1111 1111) and I subsequently write 0xAA (a.k.a 1010 1010) to PORTA, I should read 0xFA on the PORTA.
    • Port Features- The pins on the different ports also have different features, much as each of the Superfriends had different super powers. For instance, PORT A can be bi-directional IO, but it can also be used for analog input. This functionality can be very useful for reading back sensor data. To enable the switching between analog and digital inputs, a special register called ADCSR (Analog to Digital Control & Status Register) is needed; each bit in ADCSR sets some aspect of the AD operations. You do not need to set these bits explicitly, but you need to be aware that you should run the library commands (such as a2dInit())in the appropriate library (a2d.h) to tell the processor to set these registers. A more complete description of all the ports and their special magical powers can be found in the ATMega 163 summary.

      Send comments about this page to gurevich@ccrma.stanford.edu
      Last modified: Thu Oct 3 18:31:16 PDT 2002