Four Bit Carry Adder/Subtractor Circuit

After creating my 1 bit full adder design found in a previous post, I decided to go for something a little more complicated. I wanted to prove to myself that the ripple carry system worked, so the obvious choice is to make a multi bit device. 4 bits seemed like a good amount, it’s a value used in some early ALU’s so it can be used in a future project. To make it more interesting I added in the ability to make the device a Subtractor at the same time. When you look at the schematic, it only requires one more device per adder, so it’s not even an expensive thing to implement, but adds lots of functionality. As with the 1 bit adder, I have attempted to build this adder using only single logic chips.

4 bit adder-subtractor circuit

The first stage is to know the logic circuit, its widely known and can be found pretty easily all over the web. I’m not going to explain how it’s created (I can always make a separate post on that) but I can describe how to use it. The aim is for the device to take two 4 bit inputs (0 – 15), along with a carry from another adder. So the adder needs to be able to output a value between 0 and 31. In binary this can be shown as 5 bits, so we have 2 outputs. This the S output is a 4 bit bus, and the Co output bumps this up to the 5 bits we need to make 31. A truth table can be made for this but it would be 32 lines long, so too much for this post. You could regard it as a personal challenge if you want to attempt it on your own.

So I got onto Altium and made a schematic of this circuit using some of the low voltage 7400 LVC series individual logic gates that I used on the previous adder I made. They come in SOT23-5 packages which are leaded a nice size to solder. Plus they are a size where it’s possible to probe the pins fairly easily. Luckily Altium shows the components as their logic symbols. Below I have shown the first two adders, the third and fourth are basically the same as the second one, which is the idea of the ripple carry adder.

The first two adders of the four found on the board

I also added a few LEDs to show what parts are on and off. This means the user can see the inputs and outputs. These LEDs run off the 5V input voltage, and have 220Ω current limiting resistors in series with them. Also, I have put in some 0.1 inch header pins so it can be attached into a breadboard and maybe even a micro.

The LEDs for the carry bits and outputs
The LEDs for the input bits

As a base of my circuit, I have decided on a double sided 100mm x 100mm board. This is quite big as you can see for the circuit I have made, but gives plenty of space for a soldering iron to get access. As well as this, it gives a nice amount of space for multimeter probes. I also tried to keep the individual logic chips in a similar arrangement as the schematic. This is meant to be used as a learning device, so it’s useful for the chips to line up with the diagram. The header pins for the inputs and outputs are placed on opposite sides of the board to make it more obvious for the user to see it. And the pins have designators written on the board so the user can see what each pin does. The input and output busses are placed in fairly logical places, and grouped together. There is no point having all the A inputs intertwined with the B inputs. The pins for the power and ground are on opposite sides with their own headers, only one needs to be connected for it to work. The LEDs that are directly attached to the pins are placed closer to the logic circuitry, but labeled clearly on the silkscreen. Most of the routing to the LEDs is on the underside of the board, else the top could get confusing. All the designators for components have been made half the normal size due to the small amount of parts used in the project. The below images show the PCB layout I created with the top copper being red, bottom copper being blue, and the silkscreen shown in yellow.

Top Copper

As you might be able to see, I have tried to keep all the power on the bottom side of the board. This leaves lots of space for the logic signals on the top, where the user is more likely to see. As you can see, most of the inputs and outputs of the circuit are also on the bottom side. This is because the way the busses work and input into the adder needs lots of crossing over and would add confusion into the design. This is why labels were used instead.

Bottom Copper

To make it easier to see, I made a larger image of the first and last adder in the series. As you can see, the only real difference in them is that the first has the add/subtract input shown by an LED, whereas the last shows the carry from the previous adder (C0). This is because the A/D bit is attached to all the adders, but the first bit doesn’t have a carry bit input. The carry on that adder is the input for the A/S. It serves the function of inverting the first bit, so that it works like 2’s complement when in subtract mode.

The layout of the first adder in the series
The layout of the last adder in the series

As noted above I used 7400 LVC series logic gates. The SOT23-5 package chips have the suffix of “BVD”. See the datasheets for each of the devices for more information. I have written a simple bill of materials below:

12x SN74LVC1G86DBVT – XOR gate
8x SN74LVC1G08DBVT – AND gate
4x SN74LVC1G32DBVT – OR gate
17x DO-214 LED’s
17x 0805 220Ω resistors
6x 5-pin 0.1″ header pins

The main downside to this type of adder is that is is very slow. Especially when you get to high bit amounts that you are trying to add. This adder will take at least 4 times as long as a single adder to add the two numbers together because the signal has to propage through 4 full adders. This problem is known as propagation delay, each logic chip will take a very short time to compute the output. Although this time is not perceivable by the human eye, if there are 100’s of logic gates in a row, then the delays start to add up and be a problem. If this circuit is to be used in a computer, it could need to make calculations thousands, or maybe millions of times a second, and a carry bit adder is not generally good at that. There are other, faster adders that I will show in a future post.

Why James Webb Was so Important

NASA Administrator James E. Webb
NASA Administrator James E. Webb. This was his official NASA photograph

There are not many people who know off the top of their head who James Webb is, even many lovers of space may not know who he was. Yet they are about to launch the James Webb Space Telescope into space to replace Hubble. James Webb wasn’t an engineer, or a physicist, or even really an academic; he was a lawyer and politician. He turned a small government research department into an organisation that had links to almost every state, and had control of 5% of the US federal budget. Webb’s NASA controlled the jobs of half a million workers across America, and he introduced new working practices and management techniques that are still used today.

If you were to go out and read the biographies of the astronauts, or histories of spaceflight, Webb doesn’t really come up. He was portrayed as just a bureaucrat in Washington, funnelling orders down the chain, living the politician life. In this new age of spaceflight, we see the Apollo years as some sort of poetic story, with NASA being the figurehead of the battle to win space against the evil russians. In 1961 though, America did not follow this narrative, nobody in America cared about space, least of all the brand new president, John F Kennedy. When he set up his first reshuffle of the cabinet they simply could not get anyone to run NASA, they asked 18 high level politicians, and everybody said no, space was a dead end job, and NASA was just a collection of squabbling mission centres. Eventually, JFK’s vice president, Lyndon B. Johnson suggested Jim Webb, a guy who had worked under the Roosevelt administration and had some experience with private businesses. When asked, by JFK personally, Webb agreed to run NASA, as long it was the way he wanted it. JFK, desperate for an administrator gladly agreed.

shaking hands with JFK
President Kennedy shakes hands with NASA Administrator James Webb

There had been heavy opposition to the idea of manned spaceflight. Up to this point, the head of the President’s Science Advisory Committee, Jerome Wiesner, had issued a critical report on project mercury. Kennedy, as a senator he had openly opposed the space program and wanted to terminate it. Kennedy put his vice president LBJ as the head of the National Aeronautics and Space Council because he had helped create NASA, but it was mainly to get him out of the way. Although Kennedy did try and reach out for international cooperation in space in his state of the union address in January 1961, he got nothing from Khrushchev. Kennedy was poised to dismantle the effort for space, purely because of the massive expense.

The space Council
Vice President Lyndon B. Johnson (seated, center) presides over a meeting of the National Aeronautics and Space Council.

He began his NASA administration on February 14th 1961. A month later on April 12th, Yuri Gagarin became the first man to orbit the earth. Reinforcing some fears that America was being left behind in a technological competition with the Soviet Union, America suddenly cared about space. Kennedy made a U-turn and space sped to the top of the list.  This lead to Kennedy making his famous speech on May 21st where he spoke those famous words “we will put a man on the moon before the decade is out”. Kennedy wanted to take lead in the space race. Suddenly, putting a man on the moon was the number one priority.

Kennedy Talking to Congress
MAy 1961, Kennedy proposes landing a man on the moon to congress. LBJ and Sam Rayburn sit behind him.

This meant that James Webb just got handed the opportunity to run the biggest single project the country had ever seen. Webb was told to go back to his engineers and figure out how much it will cost to get to the moon. His engineers came up with the number of $10 billion (a scary big number in the 1960’s), and sheepishly told Webb, expecting to be told to make cuts and slashes to the plan. Instead he told them to go higher, because he knew problems would come their way, and extra money will need to be spent, so they come back with the figure of $13 billion. Webb accepts the number, and goes to congress and tells them he needs $20 billion over the next 7 years. Jaws hit the floor, but he used this political knowledge to get a huge amount of leverage.

The key leverage he had was jobs, and he knew it. At its height, NASA employed half a million people in some form, that’s roughly the number of people living in Wyoming. The two biggest investments were in Cape Canaveral, FL and Houston, TX. The most controversial was the Manned Spaceflight Centre in Houston, donated by Rice University. Originally based in Langley Virginia, and named the Space Task Group, the senator didn’t care much for space. The entire operation was moved to Houston, LBJ’s home state. It was central, and had good universities surrounding it. There were many Texas based representatives in the space political landscapes at that time, such as Sam Rayburn, the speaker of the House of Representatives.

Johnson Space Centre
Manned Spaceflight Centre, Texas, one of the biggest employers in Texas for a long time. with over 3000 federal workers, and 100 buildings

One thing that Webb understood was what NASA needed to run. He implemented a very flat organisational structure, with very few middle managers. Webb was the very top, controlling Washington. He also had the head of NACA (precursor to NASA) Hugh L. Dryden as an associate director. He had overseen the development of the x-15, and understood the technical needs of Apollo. Also Robert Seamans, also an associate director, acted as the general manager of NASA, and oversaw the everyday running of the program. Using a team of people, each with their own particular strengths helped NASA, especially in the early growth years, much more so than any one of them could achieve on their own.

Webb in a Gemini Trainer
Webb in a Gemini Trainer

Part of what James Webb did, to the dislike of congress, was to invest in academia, specifically universities. $30 million dollars a year was put into the Universities Development Fund. A fund designed to help students get into engineering, and to develop talent, skills, and academics that could not only work for NASA, but help the science behind it. As it was taken from a fund that congress had no control over, the money continued to help 7000-8000 students a year get through university at a time where NASA needed engineers. Webb believed that NASA was more than just the one shot to the moon, and frequently fought with the presidents on that fact. He wanted NASA, and space exploration to benefit science, engineering and even society. He believed that this project could fix other problems not even related to space, such as poverty and disease. The management style of NASA, and the way these big projects were handled showed the impossible could be achieved. He frequently lectured on this subject, and universities became an important part of NASA.

Launch_Complex_34_Tour
Webb, Vice President Lyndon Johnson, Kurt Debus, and President John F. Kennedy receive a briefing on Saturn I launch operations

There was huge pressure from washington to spend all of NASA’s budget purely on the Apollo moonshot. Webb was instrumental in making sure that NASA and spaceflight was more than that. be made sure other projects like the Mariner and Pioneer space programs happened, and that JPL still functioned even with a terrible track record at the time. At the time, the academic community worked with NASA, in large part because of the importance Webb put on furthering science. Webb would frequently lecture at universities, and teach about the management styles that made NASA was. Unfortunately, some in Washington didn’t care for the extra spending, especially the states that did not have a mission centre or any of the major manufacturing plants located there. So when the Apollo 1 fire happened, there were a small group that were willing to use it as a way to make changes.

Closeup of James E. Webb, National Aeronautics and space administration

The Apollo 1 fire was a very unfortunate accident, and a national tragedy. For some, it highlighted some major problems with the Apollo program and how it had been run by the major contractor North American Aviation. Committees were set up, and Webb suddenly went from running NASA to trying to defend it. During the inquests, NASA still ran, it continued to fix problems and aim for the moon. This was because James Webb was there defending it. Left to just take the heat, some believe (me included) NASA’s funding would have been significantly cut, and we may have never got to the moon. Webb stood up in Washington and fought hard for the continuation of the project, defending the decisions that his team had made. At the end of it, he had used up most of his political sway, and called in so many favours that NASA was safe for the time being, and that Apollo was possible.

Webb presents NASA’s Group Achievement Award to Kennedy Space Center Director Kurt H. Debus, while Wernher von Braun (center) looks on

At this point, Johnson had decided not to run for re-election, Webb felt that he should step down to allow Nixon to choose his own administrator. On October 7, 1968 he stepped down from office. To put that into perspective, Apollo 11 landed on the moon July 20th, 1969, barely a year later. Webb went on to be a part of many advisory boards and served as regent for the Smithsonian institute. He died in 1992, and was buried in Arlington National cemetery.

This post was inspired by reading the book: The Man Who Ran The Moon by Piers Bizony. For anyone interested in the subject of how Webb actually made his dealings, and a much more detailed account of how NASA became what it is, I recommend this book. He also did a Lecture on Webb that I found on YouTube where he tells the story really well.

 

Interfacing a PIC and a 16×2 LCD

So in a recent project I had to implement a 16×2 LCD  on a PIC16F1827, but the system will work on most PIC microcontrollers, with slight changes to the code. For this project I am using MPLAB X v3.40, a free development environment, and a PICKIT 3, which can be bought at a number of stores online.

Setting up the MPLAB project

  1. Start off by loading up MPLAB X, if you don’t have it already, install it from the Microchip website microchip.com/mplab/mplab-x-ide.
  2. Start a new project, by going to File->New Project… or by pressing Ctrl+Shift+Nnew project
  3. The project we want is a standalone project, it should be chosen by default. Next choose the device we want, write in the box PIC16F1827 there should only be one. We want to use the PicKit 3 for the programmer. I am using XC8 as the compiler, this is available from the Microchip to9 stawebsite. Finally choose the name of the project, and where you want to keep it. Then click finish.
  4. It wont look like mush at the moment, but under Projects on the right, there should now be your newly created project, with a drop down, and a set of folders. Something like this: 
  5. To start the project, we need a main.c. So right click on Source Files, go to New->C Main File… then in the Dialog, change the name to main. After pressing okay, it should look like this: 

Connecting the LCD

LCDs have what is known as a parallel connection. This means that we send data 8 bits at a time, rather than serial where it is one at a time. The datasheet for the PIC is found on the microchip website here. The pinout is found on page 4.

PIC pinout

Register Select (RS) pin, this decides which of the two registers that is getting written to. Either the instruction register (what the screen does) or the data register (what is shown on the screen). This is connected to A4 on the PIC.

Read/Write (RW) pin, this decides whether you are writing to or reading from the LCD. This is connected to pin A3.

Enable (E) pin, is to tell the LCD when data needs to be transferred. Pulsing this pin will write or read to the registers the data on the data pins. This is connected to pin  A2.

These pins are defined at the top of the code, to make life easier for us later on.

#define LCD_RS LATAbits.LATA4   //LCD Command/Data Control
#define LCD_RW LATAbits.LATA3   //LCD Read/Write Select
#define LCD_E LATAbits.LATA2    //LCD Enable Line

Data Pins (D0-D7), are the pins that transfer the information between the LCD and the PIC. These are connected to pins B0-B7. So B0 is connected to D0, and so on until B7 is connected to D7.

Vdd and Vss are connected to 5v and GND respectively.

Contrast (Vo) is connected to a 10k pot between the 5v  and  GND.

If the LCD you are using has connections for a backlight, follow the datasheet for instructions, on mine I connect it t0 5v and GND.

The Code

Below is the function I create to send data to the LCD.

#define LCD_CMD 0
#define LCD_TXT 1 

void LCD_DATA (unsigned char data, int type)
{
    __delay_ms(100);   // short delay

    LATB = 0x00;       // reset the register to 0
    LATB = data;       // set B output to the data we want to send
    __delay_ms(1);     // short delay for data to set

    if (type == LCD_CMD)
    {
        LCD_RS = 0;    // command mode
    }
    else
    {
        LCD_RS = 1;    // character/data mode
    }

    LCD_RW = 0;        // start the pulse
    __delay_ms(1);     // small delay
    LCD_E = 1;         // enable LCD data line
    __delay_ms(1);     // small delay
    LCD_E = 0;         // disable LCD data line
    __delay_ms(5);
}

Calling this function in the main, like follows will send data to the LCD. Notice the #define at the top, these are declaring LCD_CMD and LCD_TXT. Basically, when the type is LCD_CMD the LCD is sent into command mode, by setting the RS pin. Equally, sending LCD_TXT will clear the RS pin, putting the LCD in character mode.

The information on the data pins will then get written to the LCD, by clearing RW. To actually tell the LCD that it needs to be sent new instructions the enable pin needs to be pulsed. Once this happens, the screen should be updated with the new information.

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>

#define _XTAL_FREQ 500000

#define LCD_RS LATAbits.LATA4   //LCD Command/Data Control
#define LCD_RW LATAbits.LATA3   //LCD Read/Write Select
#define LCD_E LATAbits.LATA2    //LCD Enable Line

#define LCD_CMD 0
#define LCD_TXT 1 

void LCD_DATA (unsigned char data, int type)
{
    __delay_ms(100);   // short delay

    LATB = 0x00;       // reset the register to 0
    LATB = data;       // set B output to the data we want to send
    __delay_ms(1);     // short delay for data to set

    if (type == LCD_CMD)
    {
        LCD_RS = 0;    // command mode
    }
    else
    {
        LCD_RS = 1;    // character/data mode
    }

    LCD_RW = 0;        // start the pulse
    __delay_ms(1);     // small delay
    LCD_E = 1;         // enable LCD data line
    __delay_ms(1);     // small delay
    LCD_E = 0;         // disable LCD data line
    __delay_ms(5);
}


int main(int argc, char** argv) {
 
    // write "hello world!" on the first line
    LCD_DATA('h', LCD_TXT);
    LCD_DATA('e', LCD_TXT);
    LCD_DATA('l', LCD_TXT);
    LCD_DATA('l', LCD_TXT);
    LCD_DATA('o', LCD_TXT);
    LCD_DATA(' ', LCD_TXT);
    LCD_DATA('w', LCD_TXT);
    LCD_DATA('o', LCD_TXT);
    LCD_DATA('r', LCD_TXT);
    LCD_DATA('l', LCD_TXT);
    LCD_DATA('d', LCD_TXT);
    LCD_DATA('!', LCD_TXT);
 
    while (1)
    {
 
    }
 
 return (EXIT_SUCCESS);
}

We are Making a Foundry!

So the new term of university is here, and we have a couple of weeks to settle into our new house. As a house we felt we needed a big project to get us going, to inspire a bit of teamwork in us, and we think we have found it! After seeing this video by Grant Thompson, we have been inspired to make a similar forge. So, over the next couple of weeks, this is what we will be making. As much as this will be a little homemade enterprise, we should be learning about business skills, basic accounting, and some safety stuff along the way; so it wont all be fun! The idea is to achieve, maybe inspire, and to make something that works, but let’s just see how this ends up.