Microbit Programable Logic Gates
by Gammawave in Circuits > Microcontrollers
1709 Views, 6 Favorites, 0 Comments
Microbit Programable Logic Gates
When you think of logic gates what comes to mind, small packages containing combinational or sequential logic elements, building blocks with which to build more complex circuits.
However, there construction is not limited to integrated circuits but could be made from any number of different elements; relays, optical or pneumatic.
But regardless of the construction the basic logic functions can be implemented and used to enable more complex functions and circuits.
In this case rather than using any of the options previously mentioned the logic gates will be created using a microcontroller.
The versatility of the microcontroller allows it to be used to create any of the logic functions not just virtually but physically and even replacing a gate in a circuit as a temporary solution.
All of the basic logic functions (NOT, AND, OR, XOR, NAND, NOR and XNOR)*, will be realised in a single Microbit with real time input and output status monitoring.
*Each function will be described individually.
Enabling the selection of a single logic function that the Microbit will emulate.
Connecting multiple Microbit's configured as different logic functions will enable more complex logic circuits to be created..
Consideration has to be given to identifying the different logic functions selected so that it clear to the user.
However, the 5x5 LED display is limited in the detail that can be presented and as such has dictated that displaying logic symbols be as simple as possible.
Electronic logic symbols were rejected in favour of Boolean algebra symbols which require less detail.
Boolean algebra is concerned with the expression binary decisions where the states are either 0 (false) or 1(true).
In addition the input and output states will also be displayed in real time.
Supplies
Microbit V1 & V2
Digital Logic
By digital logic we are referring to an electrical system that makes use of two distinct voltage levels to encode information.
In this case a binary system of 0's and 1's were 0 is a low voltage and 1 is a high voltage.
The 0's and 1's can be represented by switches which are either ON (1) or OFF (0) but beyond these simple actions these switches are combined to create more complex building blocks.
These building blocks are NOT, AND, OR, XOR
The NOT (negating/inverter), function when added to the output of the AND, OR & XOR creates the NAND, NOR & XNOR.
These logic functions are unidirectional meaning that changes at the input only affect the output.
All these basic functions only have one output.
The NOT gate only has one input whilst the other gates have a minimum of 2.
The NOT output from a digital logic perspective is in the opposite state to the input.
If the input is ON the output is OFF and if the input is OFF the output is ON.
Each function will be represented by symbols
Function Symbol
NOT -
AND ^
OR v
NAND ⊼
NOR ⊽
XOR ⊻
XNOR ⊙
{Multiple symbols exist for the same logic function the availability subject to fonts and whether graphics or text. This is particularly evident with the XOR/XNOR functions.}
For the other gates, combinations of the two inputs produce an output subject to the truth table
The truth table lists the input combinations and the corresponding output state.
Logic Levels
In this context we are referring to two distinct voltage states which represent the binary states of 0 and 1.
The 0 (Low = L), would be represented by a low voltage and the 1 (High = H), by a high voltage.
The voltage for the logic levels varies depending on the type of logic family.
In this case as we are using a Microbit with a supply voltage of 3.3V.
However, the inputs have logic thresholds which minimise the effect of variation in the voltage level due to finite variation in voltage accuracy and noise.
The logic input high level (VIH), is recognised as such if the input voltage is > ~1.8V*. (maximum 3.3V)
The logic input low level (VIL), is recognised as such if the input voltage is < ~1.4V*. (minimum 0V)
*Voltage levels as measured on an actual Microbit using a potentiometer to vary the input voltage.
If you are dealing with logic from the same family, logic thresholds will be compatible and can simply be reference as 0 = Low or 1 = High.
But if interfacing to different logic families the voltages become important and voltage level shifting ensures that the logic levels between the different logic families maintains correct function.
As we will be dealing purely with the Microbit no other logic families will be involved and therefore there will be no need for voltage level shifting.
Connections
The physical connections to the Microbit for the logic function use Analog inputs P0 and P1 and Digital output P2.
Enabling one or two input logic functions all with a single output.
Layout
The Microbit display is divided up into input, output and symbol area.
Button A - Selects a logic function and shows the symbol on the display.
Button B - Shows the truth table for the logic function selected with Button A
Button A+B - Enabled or disables the output at P2 whilst retaining live update at LED 4,4
Column 1 shows the inputs to the logic function with LED's at co-ordinates 0,1 (input a) & 0, 3 (input b)
Column 5 shows the status of the logic function output with LED at 4,2 (output).
When an input or output LED is lit it indicates a logic 1 (High) and when extinguished logic 0 (Low).
Columns 2 to 4 show the symbol for the selected logic function.
AND gate example:
This shows the input LED's with a logic 1 (High) at both inputs.
This shows the output LED with a logic 1 (High).
The symbol display area of 3x5 LED's shows the AND function.
Coding
Initialise variables.
The code cycles in a loop.
The selected mode from Button A determines which logic function* is executed.
*(NOT, AND, OR, XOR, NAND, NOR and XNOR)
The inputs are read and processed as per the logic function.
The outputs (display and P2), are updated accordingly.
[Digital read and write are used to interface the logic to the physical world.]
The loop repeats.
Button A selects a different logic function on each button press and sets the mode.
Button B displays the truth table based on the mode.
Button A+B enables or disables the output on P2.
Downloads
NOT
NOT (Inverter)
Follow the truth table for the NOT gate.
Press button B for the truth table of the gate currently selected.
For the NOT gate the output is the opposite logic state to the input.
Boolean logic symbol - Y = !A, !
The input for the NOT gate is on P0 and the output is on P2
AND
AND
Follow the truth table for the AND gate.
Press button B for the truth table of the gate currently selected.
For the AND gate the output is logic high(1) only when both inputs are high under all other conditions the output is low(0).
Boolean logic - Y = A.B, ^
The inputs for the AND gate are on P0 and P1 and the output is on P2.
OR
OR
Follow the truth table for the OR gate.
Press button B for the truth table of the gate currently selected.
For the OR gate the output is logic high(1) when any input is high and low when both inputs are low(0).
Boolean logic - Y = A+B, v
The inputs for the OR gate are on P0 and P1 and the output is on P2.
XOR
XOR (Exclusive OR)
Follow the truth table for the XOR gate.
Press button B for the truth table of the gate currently selected.
For the XOR gate the output is logic high(1) only when both inputs are in different states if both inputs are the same the output is low(0).
Boolean logic - Y = (A@B)
The inputs for the XOR gate are on P0 and P1 and the output is on P2..
The XOR gate is listed as a basic building block, however this is actually a compound gate made up of the other basic functions and as such there are various configurations that are applicable.
Its still a basic function requiring no clocking, feedback or memory being static rather than dynamic logic.
An example is attached for reference.
NAND
NAND (Negated AND)
A NAND (Negated AND), gate is equivalent to an AND gate followed by an inverter.
Follow the truth table for the AND gate and invert the output.
Press button B for the truth table of the gate currently selected.
For the NAND gate the output is logic low(0) only when both inputs are high under all other conditions the output is high(1).
Boolean logic - Y = !(A.B), !^
The inputs for the NAND gate are on P0 and P1 and the output is on P2.
NOR
NOR (Negated OR)
A NOR (Negated OR), gate is equivalent to an OR gate followed by an inverter.
Follow the truth table for the OR gate and invert the output.
Press button B for the truth table of the gate currently selected.
For the NOR gate the output is logic high(1) only when both inputs are high under all other conditions the output is low(0).
Boolean logic - Y =!(A+B), !v
The inputs for the NOR gate are on P0 and P1 and the output is on P2.
XNOR
XNOR (Negated XOR)
A XNOR (Negated XOR), gate is equivalent to an XOR gate followed by an inverter.
Follow the truth table for the XOR gate and invert the output.
Press button B for the truth table of the gate currently selected.
For the XNOR gate the output is logic low(0) only when both inputs are in different states if both inputs are the same the output is high(1).
Boolean logic - Y = !(A@B)
The inputs for the XNOR gate are on P0 and P1 and the output is on P2.
Usage
Power up the Microbit and load the Hex file.
Press button A to select the required gate and its Boolean symbol will be displayed.
An input left unconnected or connected to 0V(GND), represents a logic 0.
An input connected to ~3V represents a logic 1.
The input and output states will be displayed in real time.
The logic truth table for the selected function can be displayed at any time by pressing Button B, this will remain visible for 2 seconds than revert back to the live display.
Pressing buttons A+B enables or disables live changes to the output at P2 and is indicated by the LED at 4,4 lit when disabled and unlit when enabled.
The display will reflect the logic status whether the output at P2 is enabled or not.
A different gate can be selected by pressing button A.
With a single Microbit you can evaluate the basic functions in isolation.
The example shows a NOT gate with a low input and corresponding high output
If you wish to experiment with more complex functions you will need to use multiple Microbits.
Some examples:
1: Half adder
2: SR flip Flop
Each can be realised with just 2 gates (Microbits).
Finally
If you have a Microbit and have not experimented with physical logic functions, now is a perfect opportunity with no extra cost for the basic functions.
Hope you found it interesting.