Build an 8-bit Microcontroller - Part I.
by pityukecske in Circuits > Electronics
3235 Views, 26 Favorites, 0 Comments
Build an 8-bit Microcontroller - Part I.
My Instructables were mostly about MCU-related projects until now, this one will be no different, only this time around we will build the microcontroller ourselves. It's going to be a bigger project and so it will be divided into several separate units. You are reading the first one.
You will see a lot of components, PCBs, datasheets, measurements, and test equipment involved and some iterations might occur but the target is to create a programmable microcontroller and have it run stuff. Since I will build it myself, it isn't going to be as much micro, maybe macrocontroller is a better word for it.
We're going to start with simple circuits but we are going to turn them into complex assemblies later. A video series and a website stub are also in the making, I will link them so you can check them out (the video is still under editing..). Let's get to it!
Supplies
We will need a ton of stuff for this to work but the most important is patience and perseverance. For this project, we will need a lot of logic circuits, transistors, LEDs, buttons, and solder, but also instruments like a logic analyzer, oscilloscope, power supply, soldering iron, and signal generator. Not all of them are necessary, but all of them really helped me go forward. I have a well-equipped electronics hobbyist shop at home, and most components and instruments came to me with time. A slow but steady investment is what describes this shop the best, but I can't skip mentioning the contribution of the first outside supporters of the project R. and N. from Eleshop who figured out how to lend me a Zeroplus logic analyzer to debug most parts of the MCU. Without this help, all circuits with more than eight inputs and/or outputs would have been impossible to debug efficiently. I will show you the difference between the LAP-C and the other analyzers I have - a nice little comparison. We will need something a bit more complex in order to really see what these are good for, which will come with the Program Memory episode.
We will need a computer, of course, and some design software so we can design circuits, circuit boards, monitoring tools, and simulations. I also used Proteus quite extensively, it's a great tool and they have this free version you can play around with forever. It won't let you save unless you buy it, but hey - highly visual, intuitive, and free. It's a fair deal.
The place I work recently changed HQ and an extensive cleaning/sorting operation began to scrap all the electronics we had in storage but didn't use. We had this huge box full of stuff and I salvaged what I thought might still be useful. Most of the boards and components I disassembled were modules from an IEF Werner PLC and I knew I would find some use for them. The time is now, let's turn some trash into treasure.
Instructables Also Helped
Before I get to it, I wanted to mention a project here on Instructables I found while browsing - it was about an 8-bit adder and I immediately liked it. It is built with logic gates so everyone with the truth tables could figure out how it worked if given enough time. I read through it and found a link inside, to Ben Eater's video series: my mind was blown. It was the exact thing I wanted to build though I never realized it. I watched one video after the other until I put my phone down determined to build it for myself and for my boys to play around with. I began assembling but breadboards are not for me so after a botched attempt at creating the clock unit I decided to put some effort, do some design, and have something more robust in my hands at the end.
Common-emitter Amplifier With an NPN Transistor
Having desoldered a truckload of components from the IEF Werner PLCs, the timing felt right to turn them into something useful. To start we are going to look a bit at transistors. I have done a fair share of building amplifiers for myself, family, and friends back in high school but never really looked into how an amplifier design is done. It just wasn't my thing - I preferred to use schematics that were already tested. Right now designing an amplifier is not my main target but I must show you a very simple amplifier because it somewhat relates to our goal. The circuit is a super-simple, single-transistor amplifier built with components I desoldered or had lying around since high school. I soldered some THT components together by bending their pins around - I saved some time on creating a PCB for this particular part of the demo. You can do this as well, just bend the leads and solder them together while keeping the schematics in mind. My circuit resembles the schematic a lot in terms of component positioning. If you like this type of PCB-less design, take a look at this channel, it's crazy what this guy can do!
The amplifier I decided to build has a gain of around 5, the oscilloscope screenshot from this step shows what the voltages are on the input (CH1, yellow) and the output (CH2, blue). Measurements show a gain of 4.7 but that's good enough for the sake of the example. I made the screenshot while the circuit was powered from a PSU at 9V, and a signal generator was feeding in a 1V peak-to-peak signal. The amplifier amplifies!
While this is nice and I can actually hear a nice tone if I connect a small speaker, it's not how we're going to use transistors throughout our MCU build. We will drive them in saturation and have either 0V or 5V both at the input and the output. We only need to differentiate between these two values in order to have a binary system up and running - 5V will be a logical 1, and 0V will be a logical 0.
PCB Making and the Rise of Logic Gates
As said earlier, we will operate our NPN transistors in the saturation region. This means that the collector current will not be dependent on the base current anymore - it reaches a maximum set only by the collector resistance. I will use 5V in most of my circuits but we will vary that around. I did not measure the forward voltage of the LEDs and picked the current limiting resistors empirically. The LEDs were super bright if the input voltage was set to 5V so I played around a bit and brought it down to 3.3V. No more "dead pixels" in my eyes :).
I designed and built a custom PCB for this first part of the series, I will use it for demonstration purposes, to show you how you can build a transistor circuit with an NPN in saturation. The design tool I used was Altium Designer (I have access to it at work), but you can pretty much use anything, there is plenty of good free tools out there. I created the symbols, drew the schematics then routed a PCB. This PCB was designed to be homemade via the toner-transfer method so the traces were drawn a bit thicker. I saved the copper layers to a ratiometric PDF, printed it on my Brother printer to some glossy paper, and ironed it to the cleaned copper laminate. I used soap and warm water to tear the paper away, then threw the board into the sodium persulfate. In the end, I removed the toner with sandpaper and water, then sprayed the PCB with some flux for easier soldering. The strapping wires were soldered first, then I soldered everything else on and debugged a bit around.
There was a need for debugging because my DIY AND gate made no sense. I wanted to use the same input and output circuitry for all the gates, so I pushed through and made the PCB with a strange feeling about the transistor biasing. The circuit didn't work, so a small reconfiguration was needed. I ended up using the AND schematic shown on the hand drawing. Unfortunately, the AND gate was needed for the XOR as well so two hacks were needed to get back on track with the entire PCB. The emitter of the low-side NPN was grounded and the LED + current limiting resistor moved between +5V and the collector.
The single NPN circuit is an inherent logic inverter so we're slowly heading toward the logic gates. Let's see some of the well-known logic gates and the rules their output follows:
- INVERTER - output is the inverse of the input
- BUFFER - output is the same as the input
- AND - two inputs, both must be "1" for the output to be "1", otherwise the output is "0"
- NAND - two inputs, both must be "0" for the output to be "1", otherwise, the output is "0"
- OR - two inputs, if any of the two inputs is "1", the output is "1", otherwise the output is "0"
- NOR - two inputs, if any of the inputs is "0", the output is "1", otherwise the output is "0"
- XOR - two inputs, if inputs are different, the output is "1", otherwise the output is "0"
Imagine LEDs that light up if a logic value is "1" and go out when a logic value is "0" to have a better picture of what is going on. Take a look at the pictures to see some of the gates under test.
The gates from this list were all put together with NPN transistors. The input trigger was my finger for now but one could put these units one after the other and create combinational or sequential logic circuits that trigger each other - we will get to these soon. In order to avoid having hundreds of transistors later on in our future schematics, we can use the symbols for the logic gate itself, every one of them has one. I have drawn the symbols on the little piece of paper under the PCB.
In the picture with the three PCBs next to each other, I present you my logic gate demo boards. These are simple PCBs with a sole purpose: illustrate truth tables. Many times these gates come in packs of 4, crammed together into a single DIP14 package, most of the time the inputs and outputs fall on the same pin. This means that a PCB can display the truth table of more than one DIP14 chip.
- PCB 1: NAND-OR-AND-XOR
- PCB 2: NOR
- PCB 3: INVERTER
There are 4 gates in each DIP14 (6 in the inverter gate pack) and I packed 4 DIP switches on-board. Every DIP switch is connected to one particular gate input pair and there is an LED on the output. Configuring 00, 01, 10, and 11 on these DIP switches will show the truth table on the LEDs. This is a simple way to show how gates work. The same DIP switch input LED output rule applies to the INVERTER PCB but there are 6 channels so this PCB looks a bit different. In the coming video series, I will present each one and play around with the DIP switches as that is way easier to do on video.
Latches and Flip-flops
A latch is a simple storage element that can be used to store a bit of data. Many types exist but we're going to check the D latch and D flip-flop. A flip-flop differs from a latch in that the latch is level-triggered while the flip-flop is edge-triggered. I created a Master/Slave D-type flip flop entirely from NAND gates: a total number of 10 NAND gates were needed, and two remained unused (the total is 12 = 3 ICs * 4 NAND gates). The schematic can be seen attached to this step. There is a D or data input and there is a CLK or clock input, these are connected to the two buttons visible on the photo - pressing any of these two buttons will provide a logic "1" to the corresponding pin, and the flip-flop will drive its outputs accordingly.
Though this is not as easy to show with pictures as it is on video, the last two images show the two different states the outputs can take. The left button is the D input and the right button is the clock/enable button. The latch I designed and built will save the value set by the D button when the CLK button is pressed. Simple as that - this is one of the most basic storage elements in electronics and this is its size. One can imagine what this latch would look like if it was built with the DIY gates I showed you earlier... 30 transistors, right? Not necessarily - one can construct a very primitive latch using only two transistors in a bistable configuration. For now, we will stick to our overkill transistor count and DIY gates but we can return to this topic if we want to, later.
Now we have a 1-bit memory and it's huge. What can we do with it?
A 4-bit Latch and 4-bit Counter
Two transistors can be turned into a NAND gate. Ten NAND gates can be turned into a Master-Slave D Type flip-flop. What can we turn flip-flops into? Well, to provide examples, I created two circuits: a 4-bit counter and a 4-bit latch. To avoid using 20 NAND gates, I simply used two SN74LS74 ICs, each having two independent D-type flip-flops inside plus some additional circuitry for preset and clear functions. This means 2 ICs instead of six! We got some miniaturization going on already!
Three pictures show the counter in action. The output of one latch is simply fed into the input of the next and we have a counter. The button in this case is connected to the CLK input of the first latch in the chain. As the button is pressed, the 4-bit number visualized with the LEDs will increment once. Once the counter reaches 1111 or 15, it overflows and starts again from 0.
The last picture shows a 4-bit latch. The CLK inputs are tied together and the outputs are routed to LEDs. The inputs have a 4-way DIP switch and some biasing. When the button is pressed, all 4 inputs are saved onto the latch and the LEDs display the corresponding pattern. Had I used 4 ICs, this could have been turned into something that is usually called a register and you'll see how often this name will come up later in the series.
Counters are a very important part of this project and it is by no accident I decided to build one as a demo. We will soon see how the program counter module is created and you will find its behavior very similar to a latch mixed together with a counter. So these two can be combined into an SN74LS193N which we will talk about in the Program Counter part of the series which is coming soon.
Final Words
These words are actually only as much final as a Word document saved with the name "final". There is more coming, but for now, we will stop here. We have seen how we can take some transistors and turn them into an absolute must-have component of a microcontroller - this will be the way we will roll in the future as well.
I will finish the video version of this introductory part pretty soon so make sure you check out the YouTube channel.
See you next time with the Program Counter!