Thursday, June 25, 2015

Building your own Computer with NAND gates

Have you ever wondered how our computers works, how they display colorful graphics and let us play some awesome games. What are those tiny black things inside my laptop?

Recently I came across an online course on building your own computer starting with NAND gates.
Ok, then What is NAND gates? Well NAND gates are basic building block for any digital device ex- Laptops, Mobiles, Camera and many other devices.

Ok, then how to make NAND gates? NAND gates are made using even smaller components called as Transistor, they are like switch which can be turned ON and OFF, but by flowing a small current through them not the we have traditional tick tack type switches.

Ok, so you are telling me that everything is made from Transistors? Yess you got it.. every digital device is made from Transistors.

So, lets talk about that course. The name of the course is nand2tetris in which we have to make our own computer and run own softwares all starting with simple NAND gates. There is also a course on coursera by the same professors with video lectures. I think is worth to see once that site and course for every engineer who is interested in computers and digital devices.

So, I decided to write my own blog on how to build our own computer. This is just a guide for people to help and share whatever i learnt using that course. I will follow materials from there and build my own HACK computer using the resources provided on nand2tetris website.

Though there is no pre-requisite for this course but, I think if you have taken any course on Digital Circuits or Basic Electronics, It will be easier to follow up the course.

The course goes like this:

  • First Project: Simple Gates and Computational Logic like AND, OR, NOT, Muxes and DeMuxes.
  • Second Project: Adders, Incrementer and full ALU.
  • Third Project: Memory elements like Registers and RAMs.
  • Fourth Project: Machine Language Programming i Assembly.
  • Fifth Project: Combining everything you made in First Second and Third project to full functional CPU.
  • Sixth Project: Writing an Assembler Program to translate code written in Fifth project to Machine Code which your CPU will understand. 

Then there are more projects which i will discuss in future blogs. I will start with building our simple gates in next blog. Thanks for reading please have a look at nand2tetris website.

Saturday, June 13, 2015

Computer Vision - SURF Feature Extraction and Object Tracking using MATLAB

I am currently working on a Mobile robot which will use Vision based Navigation for automatically finding Charging spots when the battery is low and get attached to itself.

For this task first the steps are broken down into following pieces:

  • Taking a number of reference images of the object to be detected. (For checking purpose of my code i took a "POW"(power) written on a notebook and tracking those characters. Depending on the location of "POW" in the image, i will generate the control command for the robot, whether it should go left or right and by how much amount using PID controller.) 

  • Detection and Extraction of SURF features (which is faster version of SIFT features) from the reference images and making a feature vector by stacking features from different reference images. In my case, I have only taken a single reference image shown below but you take multiple images and create the feature vector.
  • Detection and Extraction of SURF features from the images captured from camera in real-time.
  • Matching of those features using different algorithms. In MATLAB, there is only one feature matching function which is very robust, but if you using openCV, you can go for Brute-Force Matcher of FLANN based matcher. I have done this same thing in openCV. If anyone want the code using openCV, comment below in comment section.
  • Depending on the matched feature, Calculate centroid of the "POW" symbol and if the symbol is right half plane, generate right command to the motor otherwise left command.
The MATLAB code is embedded below,

Here is a working demo of my code:

For any suggestion, comment below. Thanks!

Friday, January 09, 2015

Arduino 101 - Blinking LED using Arduino

This tutorial is for the beginners who have just bought a arduino and want to start using it. Just like while learning programming we start with a simple program of Hello World!, in embedded systems we start by blinking an led. Before we start i want to give a brief introduction of how a arduino program or sketch looks like? For writing and uploading sketch to arduino you will need arduino IDE which can be downloaded from here

Arduino Sketch:

Every arduino program also called as sketch has 2 parts:
  1. Initialization Part : Where we define things and initialize any arduino peripheral like Serial or USB Connection and Input / Output Port.
  2. Infinite Loop : After initialization is complete there is a infinite loop in which arduino runs till it is not reset or switched off.
If things are not clear, after you will see the code below, i hope everything will be clear.

Code for Blinking Led: 

Some Explanation of Code: 

The two parts discussed above are implemented in code as setup() function and loop() function. In arduino led is connected to pin number 13 so we name a variable ledPin for our convenience.

The setup() block runs only once and initializes all the peripherals and assignments. Then the pinMode(pinNumber, direction) function assigns the mode of pin, (in our case pin number 13 or ledPin) as OUTPUT, because we want to output high or low voltage level so that our led lights up. If you are reading the voltage level of the pin then you have to choose the  direction as INPUT.

Now, the loop() function block, this block runs continuously till the reset or power off of the board. In this block there are two functions digitalWrite(pinNumber, Value) and delay(milliseconds). digitalWrite(pinNumber, Value) writes value (either LOW or HIGH) to the pinNumber and delay(milliseconds) is used to give accurate time delays between any two sequence of instruction.

Now upload this code using Arduino IDE to Arduino Board and you will see the led light blinking at an interval of 1sec.

Wednesday, July 30, 2014

UART communication on Tiva C Launchpad Microcontroller

Basics of UART: 
UART stands for Universal Asynchronous Receiver Transmitter. UART is used for serial communication between peripherals and other devices. UART takes bytes of data and transmits it through wire to another UART receiving end. UART device contains shift registers to convert parallel data into serial data and vice-versa. Data in UART is transferred serially as parallel transmission requires multiple wires. 
UART is asyncronous as no extra clock signal is required for synchronization. Synchronization is acheived by what is called as Baud Rate.

Monday, July 21, 2014

How to change System Clock Frequecy of Tiva C microcontroller Tutorial - using PLLs

Video Tutorial: How to change system clock of launchpad microcontroller using PLL and TivaWare
One of the very good feature of Tiva C microcontrollers are the full control over the operating frequency of the microcotrollers. We can set any frequency up to 80 MHz used Phase-Locked  Loop (PLL).  Clocking in Tiva C devices: Tiva C microcontrollers can be clocked from several sources
  • External Oscillator
  • Internal Oscillator (+/- 50%)
  • The main oscillator
  • Precision Internal Oscillator PISOC
The information about clock source tree could be found on page no. 220 of the datasheet.
The PLL can use any of these oscillator as its input but since the internal oscillator has a very large error generally it is not preferred for input. 
The PLL could be programmed from the Tiva Ware library very easily if you get started with tiva ware please see my another blog post :
The driver is contained in driverlib/sysctl.c, with driverlib/sysctl.h containg the API definitions. Changing Frequency: Let us first check the normal system clock frequency when the system boots up normally without using any PLL. Code is shown below:
 The first three lines of code include the important header files. I initialized a variable called clock_speed. uint32_t SysCtlClockGet(void) function: This function returns the system clock frequency so we get that and save it to clock_speed variable. Lets,  now  build and debug the code and see what's the frequency is    as you can see that the current operating frequency is 16 MHz. The frequency of the system clock could be set by use of function in Tiva Ware library SysCtlClockSet(unit32_t config) fucntion. This function configures the clocking of the system, the input crystal frequency, use of the PLL, oscillator to be used and the system clock divider SYS_DIV field. config parameter is the logical OR of many different values which are group into the set.Different values are as shown in the figure below:
  • The system clock divider is chosen with one of the following values: SYSCTL_SYSDIV_1,
  • The use of the PLL is chosen with either SYSCTL_USE_PLL or SYSCTL_USE_OSC. 
  • The external crystal frequency is chosen with one of the following values:
    below SYSCTL_XTAL_5MHZ are not valid when the PLL is in operation.  
  • The oscillator source is chosen with one of the following values: SYSCTL_OSC_MAIN,
    SYSCTL_OSC_EXT32 is only available on devices with the hibernate module, and then only when the hibernate module has been enabled.
Programming of PLL is shown below :   Different System Clock Divider values for different clock frequencies could be found from datasheet.
Lets now compile and Debug our code and see what is the new frequency after turning on the PLL

As you can see the clock speed is now changed to 80 MHz. So the PLL on Tiva C Launchpad provide the flexibility of changing the system clock frequency based on our need which is a great feature added in Tiva microcontrollers for real time execution.
Any comment to improve the article will be highly appreciated.