EC-9.1 What a Microcontroller Is

en

What this is and why it exists

Every board module in this area begins by making a light blink. That works, and it teaches nothing at all about the part doing the work.

This topic is the missing first hour. One chip. One address space. Everything the chip can do sitting somewhere inside that space.

After it, a datasheet stops being a wall of tables and becomes a map. That change is worth more than any number of blinking lights. From here on you can answer questions about a part you have never used.

The vocabulary

  • Microcontroller — a processor with memory and peripherals on the same piece of silicon.
  • Address space — the full range of addresses the processor can name.
  • Memory map — the diagram saying which region of that space holds what.
  • Flash — program memory that keeps its contents without power.
  • Static memory — working memory that loses its contents at power-off.
  • Register file — the small set of locations the processor computes in directly.
  • Peripheral — a hardware unit such as a timer or a serial port, controlled through registers.
  • Reset vector — the fixed location holding the address of the first instruction to run.
  • Startup code — the code that prepares memory and clocks before your own function is called.

The mental model

A microcontroller is a whole computer on one piece of silicon. That is the entire difference from a processor, and every other difference follows from it.

A processor expects its memory and its peripherals to be somewhere else. It needs a board around it, with memory chips and support circuits, and it draws power accordingly. A microcontroller holds program memory, working memory, timers, converters and serial ports inside the package. That is why it can run for a year from a battery inside a product.

The price is scale. A microcontroller has kilobytes where a processor has gigabytes, and megahertz where a processor has gigahertz. Nearly every design decision in embedded work comes from that trade.

Now the idea that organises everything else. There is one address space, and program memory, working memory and every peripheral register live somewhere inside it. The memory map at the front of a datasheet says where each region begins and how large it is.

Once you have that map, questions become lookups. Where do variables live? In the static memory region. How do I turn on an output? Write to an address in the peripheral region. How much program will fit? Read the size of the flash region.

The three memories do different jobs and it is worth being exact about which. Flash holds your program and keeps it without power. It is slow to write, is written in large blocks, and survives only a limited number of erase cycles. That makes it a poor place for a counter you update often.

Static memory holds variables and the stack. It is fast, it can be written a word at a time, and it forgets everything the moment power is removed. The register file is smaller still: a handful of locations inside the processor where arithmetic actually happens.

The clock deserves more attention than beginners give it. One oscillator, divided and multiplied, sets the rate of the processor and of every peripheral inside the part.

Almost every timing figure you will later calculate is that frequency divided by something. A serial rate is the clock divided by a number you write into a register. A timer period is the clock divided by a prescaler and then by a limit. Knowing which oscillator is actually running, and at what speed, therefore explains a whole family of otherwise mysterious failures.

Most parts start on an internal oscillator that is convenient and not very accurate. Moving to an external crystal is a deliberate step. Forgetting to take it is why serial communication sometimes works at one rate and not another.

Finally, the path from power-on to your code. The part holds itself in reset until the supply is stable. It then reads a fixed location, the reset vector, which holds the address of the first instruction. That instruction is not yours.

Startup code runs first. It copies initial values of variables from flash into static memory, sets uninitialised memory to zero, prepares the stack and often configures the clock. Only then does it call your main function. Knowing this explains why a variable can have the wrong value before startup has finished. It also explains why code that runs earlier must be written very carefully.

Part numbers are the last small skill. A number encodes the family, the memory size, the package and the temperature rating. Parts within a family share their peripherals and their code. Moving to a member with more memory is usually a recompile rather than a rewrite.

What you should now be able to explain or do

  • State what a microcontroller has that a processor does not, and what it gives up in exchange.
  • Read a memory map and say where program, variables and peripheral registers live.
  • Choose between flash and static memory for a given piece of data, and say why.
  • Identify the clock source in use and derive a peripheral timing figure from it.
  • Describe the sequence from power-on through the reset vector and startup code to your own function.
  • Read a part number and say what changes between two members of one family.

Check yourself

Memory and peripherals are on the same piece of silicon. Everything else, including the small memory sizes, the low power and the ability to run standalone, follows from that.

It is written in large blocks, it is slow to write, and it survives only a limited number of erase cycles. Static memory or a dedicated non-volatile region is the right home.

The clock source. The part may be running on an internal oscillator that is a few per cent off. That error is tolerable at some rates and fatal at others.

The reset sequence, then startup code. That code copies initial values into memory, clears the uninitialised region, sets up the stack and often configures the clock.

Go deeper

Back to What a Microcontroller Is: work through the checklist