# Could you program a microcontroller without a computer?

Let's say you had a rather simple and small microcontroller and had no interfacing, no computer, no debugger, compiler, or assembler. Could you write your code in assembly, convert it (manually) to machine code, and then apply power to the appropriate pins using a voltage source?

I understand you would need appropriate I/O and memory to really do anything, but if you were so inclined and had the time, could you do this? I guess, historically, how was this done when there was no computer/compiler/assembler to begin with? Feel free to link me to an outside resource. Thanks! :)

• Have a look at the Altair 1. The front panel switches and manual machine programming were the only way of using it. – pjc50 Apr 8 '15 at 23:45
• @pj50 He asked about a microcontroller, not a microprocessor. Microcontrollers run their code our flash which is not accessible without an interface of some sort. – tcrosley Apr 8 '15 at 23:57
• How many programmed units do you need? Many manufacturers will supply their micros pre-programmed with your code; however, this is not practical if you need less than a full reel. – Oleg Mazurov Apr 9 '15 at 1:07
• Back in the 70s you would have built a panel of toggle switches and lights so you could enter the program. I never worked with one of the early microcontrollers, but worked with several different microprocessors, and that's how you did it, by default. And there was no fundamental difference between early microcontrollers and microprocessors other than the I/O capabilities and some of the details of how program memory was configured. – Hot Licks Apr 9 '15 at 3:12
• I'll leave this as a comment because I can't find the source for this story. I remember some time back a story of a kid who designed and built his own computer (just the CPU really) from transistors for a science fair purely based on his understanding of digital logic. He couldn't afford a computer at the time and programmed his creation via toggle switches (I think I remember the clock for the thing being just a button). One of the judges was so impressed that he bought the kid a computer. – slebetman Apr 9 '15 at 6:25

Could you write your code in assembly, convert it (manually) to machine code,

Yes!
Code can be written "out of your head" in binary, if you wish.
Long (long long) ago this is how I started using (then) microprocessors.
I and friends would write code in assembly language, compile it manually to machine code (something you can do "by inspection" after some practice) then enter it into the processor by various means. On one system we built we would set up the address on binary (on off) switches or use an auto increment feature of the processor, enter 8 data bits on binary switches and then press a "clock" switch to enter the data to memory.

The equivalent functionality could be achieved with even fewer switches on a modern microcontroller using serial SPI programming - see below.

... and then apply power to the appropriate pins using a voltage source?

Yes!
But it would be incredibly slow to do!
Many modern microcontrollers allow use of an "SPI" interface for programming.
This typically consists of input and output data lines and a "clock" line, and usually a reset line.

Many processors allow SPI clock and data to be "static" which means there is no limit on how long you can take to set up the data between bits. You could program such a processor using a data line and a clock line which were driven by manually operated switches. The clock line needs to be "bounce free" - you need to be able to set it high or low in a single transition per operation - so a minimum interface may need to include a Schmitt triggered gate. You may "get away with" just an RC delay and a push button switch, but a Schmitt triggered input is safer. The data line does not need to be bounce free as its state is only read at the clock edge.

Some interfaces are interactive - data is output by the processor during programming (eg data out = MISO = Master In Serial Out on AVR processors). To read this you'd need to add eg an LED and a resistor (and just maybe a buffer or transistor if the drive capability was REALLY low).

MC6800:

From semi-fading memory (almost 40 years!)

LDI A, $7F ...... 86 7F ...... 1000 0110 0111 1111 STA,$1234...... B7 12 34 ... 1011 0111 0001 0010 0011 0100
LDI X, \$2734... CE 27 34 ... 1100 1110 0010 0111 0011 0100
...

• +1 for mentioning the static design, allowing the clock to go all the way to DC. – pericynthion Apr 9 '15 at 6:40
• This brings back memories. I actually learned entering Z80 code using buttons back in school. That was around 1990. My teacher insisted that learning programming the hard way is valuable. 25 years later I have to agree. – Nils Pipenbrinck Apr 10 '15 at 2:13
• @NilsPipenbrinck I'm at this moment writing C code and applying lessons I first learned with Fortran :-) !!! - and which were then honed with embedded systems & assembly language. These lessons reduce the chance of errors by applying mental-typing to variable and port accesses which otherwise could do things which are allowed but not desired or useful. Assembler is the ultimate sharp weapon - a double ended sword with no hand guards. After that "higher level" languages seem less sharp :-). Z80 button entry acts similarly on your learning experiences ;-). – Russell McMahon Apr 10 '15 at 4:06
• @Erbureth Thanks :-). Yes ffff for 1111 was obvious brain fade. ... was just "you get the idea" but completing line doesn't hurt. – Russell McMahon Apr 10 '15 at 10:39
• Yes, I got the idea, but when I was at it... :) – Erbureth says Reinstate Monica Apr 13 '15 at 9:42

Originally, in programmable machines, the programs were manually punched into paper tape or cards. Later they used a kind of typewriter to do the punching, and even later still the computers (as they had come to be known by then) could punch their own paper tape.

It was, literally, sitting there with a card and manually making holes in it.

Each column is an instruction or bit of data that is interpreted by the CPU's state machine to perform tasks.

Now if course it's all flash memory, and yes, getting the data in there in the first place normally needs a computer to do it. But that's not strictly 100% true.

After all, what does a computer and hardware programmer do but set the state of IO pins at the right times? So, if you are really masochistic enough you can build a system which allows you to send the right instructions to the microcontroller's programming pins in the right way to install some instructions into the flash memory for execution.

Maybe something involving lots of switches, some shift registers, a clock generator, etc.

I remember at college we had a new-fangled Z80 based programming system - a unit the size of a suitcase with a Z80, some small amount of RAM, a bunch of 7-segment LED displays, and a big row of switches. Program it by manually loading instructions into RAM using the switches.

• The Z80 is not a valid example. He asked about a microcontroller, not a microprocessor. Microcontrollers run their code our flash which is not accessible without an interface of some sort. At least you did mention flash and a programming interface elsewhere in you answer. – tcrosley Apr 9 '15 at 0:05
• @tcrosley The Z80 was more for anecdotal purposes - illustrating how computers used to be programmed before Visual Studio took all the skills away. – Majenko Apr 9 '15 at 0:08
• While I'm unaware of any machines having been built to do so, it would not be difficult to add some circuitry to a card reader to program a microcontroller with code stored on a sequence of cards. Paper tape might be more practical, though. – supercat Apr 9 '15 at 0:12
• I used to cut holes manually in those. It is only feasible in extreme circumstances (10pm, term papers due tomorrow, one mainframe run left, punch room lady left for the day). Programming the same mainframe from the front panel (couple years later) was actually faster. – Oleg Mazurov Apr 9 '15 at 1:35
• @Majenko -- I've seen the reconstruction of Babbage's machine, and it definitely was not made from telephone equipment. Definitely noisy, though. – Hot Licks Apr 10 '15 at 1:11

You said "small microcontroller and had no interfacing, no computer, no debugger, compiler, or assembler.". Well you can get by without the computer, debugger, compiler, or assembler, but you have to have some sort of interface to connect with the microcontroller in order to load the program.

First of all, forget about any high level languages like C. Then the easiest way is to write your program in assembly language, but don't use the assembler to translate it to machine code. Instead, look up each of the assembly instructions in the programming manual, find their equivalent hex coding, and write that down.

You will have to allocate your own variables, so you can fill in the appropriate addresses in the operand portion of the instructions. Also forget about using the stack and heap, too much work. When done, you'll have a machine program that can be loaded directly into the flash memory of the microcontroller -- no linker needed either.

Because of the lack of external address and data busses on almost all microcontrollers (since they would take up too many pins which are needed by I/O ports and peripherals), virtually all microcontrollers are programmed via a special interface like this:

Normally the "programmer" on the left would be connected to a PC, which would download a machine file generated by a compiler/linker.

However if the programmer has a keyboard, like the one below:

then one can enter the hex codes for a manually generated program right into the programmer, and program the device without needing a compiler, linker, or PC which was what you were inquiring about. (The keyboard is also used for editing hex files, and generating checksums.)

Granted very few programmers have keyboards like this. This one would not be suitable for a hobbyist as it probably costs a couple thousand dollars.

This particular programmer programs parts before they are put onto a board; interfaces and sockets are available for several PICs and also Atmel AVRs among others.

• You have commented on various other answers re their invalidity, but this answer is arguably less valid than most you have commented on. He said " ... no interfacing, no computer, ...". At a minimum your solution is "a computer". What "no interfacing" means is moot. But, use some debounced switches on the SPI interface and you arguably have a solution. – Russell McMahon Apr 9 '15 at 1:26
• @RussellMcMahon Who says there is a computer in the programmer? Many of them just use FPGAs (which could be replaced by discrete logic.) How are you going to program a microcontroller via an SPI interface, unless it has a bootloader? If so, how do you program the bootloader? – tcrosley Apr 9 '15 at 1:44
• The essentially bootloader free standard low voltage serial programming interface on many (possibly most) microcontrollers uses a few lines in an essentially serial interface. MOSI MCLK (Vdd Gnd Reset) being utter minimum MISO if there is interaction. 3 switches (only one of which) needs to be bounce free) and an LED for MISO and "you are off". Very very very slowly. If an FPGA is allowed to not count as a "computer" then it would be no problem implementing many processor based systems. I suspect "no combinatorial logic" would met the spirit of his request. – Russell McMahon Apr 9 '15 at 5:46
• I have to say, fireworks is the wrong background to use when working with electronics equipment :) – Anonymous Penguin Apr 15 '15 at 3:12

As a practical matter, something like toggling JTAG pins manually would be very error-prone. To input a program of any decent size would take thousands of toggles. But theoretically, it's possible.

If you're allowed to prepare the microcontroller in advance (or write a boot ROM for it), you could easily implement something like the external toggle switches used on early mainframes. If you allow external logic, you could even use a hex keypad to speed things up.

Finally, although it's not as common these days, you can buy a ROM-based microcontroller with custom code in it. You'd have to offer some money up front and guarantee a certain volume, but I've seen customers do it to save money on high-volume production. In that case, there would be no external signals needed to program the microcontroller at all. The code would be built into the physical layout of the chip.

Of course, you can also use a microcontroller that runs code out of an external memory interface, but that's cheating. :-)

• In fact, I built an Imsai 8800 "clone" in 1976/77 and would routinely program it in binary using the toggle switches. I had a boot-load sequence that was around 40- or 60-odd bytes (this was a lifetime ago!) which would read a papertape from my ASR-33 teletype and get me into a keyboard monitor. Before the teletype worked I would program the 8085 (one of the differences between my "clone" and the Imsai,) strictly in binary. But that was in parallel, with a row of switches. I've used a telegraph key to do serial encoding and it is REALLY error-prone, as Adam has stated. – user6297 Apr 9 '15 at 5:56

Absolutely. In fact, the first microprocessor course I took (circa 87) used a Motorola 68000 board. We would work out the code in assembly, look up the hex for the assembly, and type the hex into a terminal to program the board. If you edited, you had to make sure the code you put in was shorter than the code you were replacing, and then buffer the rest with NOPs. If the coDE were longer, you would have to retype everything! We learned to add plenty of NOPs.

• The 68000 was not a microcontroller. He asked about a microcontroller, not a microprocessor. Microcontrollers run their code our flash which is not accessible without an interface of some sort. – tcrosley Apr 9 '15 at 0:04
• @tcrosley That's not strictly true. There are microcontrollers that require external ROM since they don't have any ROM or Flash of their own. What makes them a microcontroller isn't built in RAM or ROM, but built in peripherals (IO ports, UART, etc). – Majenko Apr 9 '15 at 0:17
• Very few though -- because of the I/O peripherals you just mentioned, very few have external address and data busses because they require two many pins. – tcrosley Apr 9 '15 at 0:29

Absolutely. Once you have a binary format of the actual instructions (easy to do for well-specified instruction sets), all you need is to implement the programming protocol.

For example, take something small like an AVR Tiny 4. Section 14 details the programming interface including the protocol and physical layer. It's relatively simple and you just need a few buttons and pullup/down resistors to send/recieve a 1 or a 0. If you're feeling ambitious, add some LED's to read back the output.

The last remaining issue is if you can do it fast enough, which luckily* for the AVR Tiny 4 (section 16), it doesn't have any minimum frequency when it comes to programming it.

*note: as far as the datasheet specifies. This may differ in reality... I doubt anyone has tried programming it at mHz clock speeds.

Feel free to automate it as much/little of the process as desired. This is what most programming devices do anyways: automate the process.

By whatever means you input the program to the uC, you are "interfacing" with it, because that's what interfacing means. Even a bunch of switches you flip manually are an "interface".

So I will treat this question as "how do I interface to a uC with largely -mechanical- (as opposed to electronic) interfacing hardware, and in as cool a way as possible?"

To which my answer is, input the program as a piano roll. The piano roll "tracker bar" from a player piano basically gives you at least 65 (depending on format) bits that you can turn on and off independently based on perforations in the paper. The rest is exacting work with an exacto knife. But at least your program, once running, is (somewhat) permanently stored (unlike a flip-the-switches-manually approach).

Not only is it possible to program a microcontroller without a computer, but you can actually purchase a computer today that is specifically designed to be programmed with hardware toggle switches. The kit is called a Membership Card, and it is a re-creation of the COSMAC Elf computer from the 1970's. The kit was described in a recent article in IEEE Spectrum magazine. It is also possible to program more recent versions of the Membership Card from a PC.

Okay. How about this: you write the program in assembler and look up the opcodes yourself, as previously suggested. How to get it in the computer: take an old tape player, remove the playback head, but leave the capstan and pinch roller. This will be your transport. If you have a reel-to-reel player you can leave the playback head in place.

Then take a long and narrow strip of paper, or even better, transparent tape. This is where you encode your program. You use as many tracks as you have pins to program. On one track you put black dots at regular intervals; this is your clock. On the other tracks you put black dots or strips and leave blanks according to the data and signals. You shine a light at the whole thing and detect it with small phototransistors (or shine one LED at the tape and detect the light with other, similar ones) and drive the clock and signal lines with transistors. This takes a little bit of space on the tape player, which is why you may need to remove the playback head.

Of course you can also encode the signals as audio, but it's a bit more complicated to decode. Or you can detect reflected instead of transmitted light. Or you can punch holes in dark tape, or paper tape, instead of making black marks on transparent tape. Whatever. The tape player drives the tape at a constant speed, so you can enter a clocked signal without a computer.

Yes. The main issue may be interacting with the programming interface. If you have a plain address and data bus, things are a bit easier. Have you ever seen some of the really early computers? The programs are entered one instruction at a time into RAM with switches on the front.

• He asked about a microcontroller, not a microprocessor. Microcontrollers run their code our flash which is not accessible without an interface of some sort. – tcrosley Apr 9 '15 at 0:06
• @tcrosley - The early uCs didn't have flash. They either had RAM or ROM for program store. – Hot Licks Apr 9 '15 at 3:14
• @HotLicks Correct. And with just a few exceptions, these were all microprocessors, not microcontrollers. That was my point, not whether the program store was flash, or ROM, or PROM, or EPROM, or EEPROM. The point was whether the program store was on chip (microcontroller) or not (microprocessor). Even the 8048, Intel's first micvrocontroller introduced in 1977, had internal ROM. – tcrosley Apr 9 '15 at 4:12
• @Tcrosley Who are you? And what have you done with the usually helpful and objectie tcrosley? :-) | His first observation was that you could interact with the programming interface - which will often but not always be valid. The "IF" re address and data bus is an IF. It applies to some large and borderline hybrid microcontrollers, but his original IF was fine enough (albeit hard and slow). – Russell McMahon Apr 9 '15 at 5:37
• Trying to maintain a hard distinction between microprocessor and microcontroller is more pedantic than useful, especially when it's in a comment to every single reply. – pjc50 Apr 9 '15 at 12:41

Digital Equipment's PDP-8 mincomputers of 40+ years ago had no automatic booting ability. Programs were usually loaded from punched paper tapes but the loader to load the paper tapes had to be manually entered on the front panel switches.

At Digital and PDP-8 customers' sites such as OEMs who used them as the embedded-controllers of the day, it wasn't uncommon for users to memorize the boot loaders and compete with one another for the fastest time taken to enter the boot loader into the machine. The machines used magnetic-core memories (hand strung in far-flung parts of the world, by ladies with tiny fingers, using needles and hair-fine wire).

Since magnetic memory contents is non-volatile the boot loaders survived a power-down and manual entry was only needed after initial assembly, the loader was wiped out by a program bug, or a program needed to use the loader's space.

• Many years ago back when we had to trudge to school barefoot in the snow uphill both ways, the PDP-8 in our high school had to be booted by toggling in the bootloader from the front panel as you describe. None of the teachers knew much about the computer, including the one that was responsible for it. He thought up was 0 and 1 was down, so could never toggle in the bootloader. I'd arrange for the computer to crash when I was in english class so I'd be called out to restart the computer. I never told him about his up versus down misconception - it was much too useful to me. – Olin Lathrop Apr 10 '15 at 16:36
• I loved the PDP-8. I had sole access to one for about three years -- so it was like having a personal computer long before the term PC was invented. Amazing what could fit into 4K of memory. Sure went through a lot of paper tape though (it had no DECtape), and Teletype paper. – tcrosley Apr 14 '15 at 20:59

The answer is yes, you could program a micro controller without a computer,debugger, etc. What you would need would be to provide the various voltages, data, clock, and programming sequences specified by the uC manufacturer (not an easy job).