Showing posts with label basic structure. Show all posts
Showing posts with label basic structure. Show all posts

Thursday, 1 August 2013

Looping in C - Basic Level

In every programming language, thus also in the C programming language, there are circumstances were you want to do the same thing many times. For instance you want to print the same words ten times. You could type ten printf function, but it is easier to use a loop. The only thing you have to do is to setup a loop that execute the same printf function ten times. 

There are three basic types of loops which are: 

For Loop 
While Loop 
Do while Loop 


Let’s look at the “for loop” from the example: We first start by setting the variable i to 0. This is where we start to count. Then we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++). 

In the example we used i++ which is the same as using i = i + 1. This is called incrementing. The instruction i++ adds 1 to i. If you want to subtract 1 from i you can use i–. It is also possible to use ++i or –i. The difference is is that with ++i (prefix incrementing) the one is added before the “for loop” tests if i < 10. With i++ (postfix incrementing) the one is added after the test i < 10. In case of a for loop this make no difference, but in while loop test it makes a difference. But before we look at a postfix and prefix increment while loop example, we first look at the while loop. 

Example code :
While Loop 

“while” statement is a sentinel controlled repetition which can be iterated indefinite number of times. Number of iterations is controlled using a sentinel variable (test expression).


Let’s take a look at the example: First you must always initialize the counter before the while loop starts ( counter = 1). Then the while loop will run if the variable counter is smaller then the variable “howmuch”. If the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive.

As said before (after the for loop example) it makes a difference if prefix incrementing (++i) or postfix incrementing (i++) is used with while loop. Take a look at the following postfix and prefix increment while loop example:


i++ will increment the value of i, but is using the pre-incremented value to test against < 5. That’s why we get 5 numbers. ++i will increment the value of i, but is using the incremented value to test against < 5. That’s why we get 4 numbers. 

Do while Loop 


“do..while” statement is a sentinel controlled repetition which is quite different from the other two statements we covered earlier. This statement runs the code first and then checks the test-expression, so there is always a guarantee that the code runs at least once. This type of loop is generally used for password checks and menus.

Do something first and then test if we have to continue. The result is that the loop always runs once. (Because the expression test comes afterward). Take a look at an example:

Next Topics will be soon .. 



Friday, 19 July 2013

The Multiplexers

        Multiplexing is the generic term used to describe the operation of sending one or more signals over a common transmission line at different times and as such, the device we use to do just that is called a Multiplexer.
A multiplexer is a combinational logic circuit design to switch one of several input lines through to one single output line by the application of a control signal. Multiplexers, often shortened to MUX, can be either digital circuits made from high speed logic gates used to switch digital or binary data or they can be analogue using transistors, mosfets or relays to switch one of the voltage or current inputs through to a single output.
Multiplexers also known as selectors because they can “select” each input line, are combinational logic circuits whose output condition either “HIGH” or “LOW” is determined at any time by its input state. In other words, multiplexers are switching circuits that just switch or route signals through themselves, and being a combinational circuit are memoryless as there is no signal feedback path.
The most basic type of multiplexer device is that of a one-way rotary switch as shown.

multiplexer
The rotary switch, also called a wafer switch as each layer of the switch is known as a wafer, is a mechanical device whose input is selected by rotating a shaft. In other words, the rotary switch is a manual switch that you can use to select individual data or signal lines simply by turning its inputs “ON” or “OFF”. So how can we select each data input automatically using a digital device.
The selection of each input line in a multiplexer is contro1led by an additional set of inputs called control lines. Normally, a multiplexer has 2N input lines and a set of n control or address lines are needed to select one of the 2N inputs to pass the data to the single output. Note that multiplexers are different in operation to Encoders. Encoders are able to switch an n-bit input pattern to multiple output lines that represent the binary coded (BCD) output equivalent of the active input.
We can build a simple 2-line to 1-line (2-to-1) multiplexer from basic logic NAND gates as shown.

2-input Multiplexer Design

multiplexer circuit

The input A of this simple 2-1 line multiplexer circuit constructed from standard NAND gates acts to control which input ( I0 or I1 ) gets passed to the output at Q.
From the truth table we can see that when data select input, A is LOW (logic 0), input I1 passes its data to the output while input I0 is blocked. When data select A is HIGH (logic 1), input I0 is passed to Q while input I0 is blocked.
So by the application of either a logic “0″ or a logic “1″ at Q we can select the appropriate input with the circuit acting a bit like a single pole double throw (SPDT) switch. Then in this simple example, the 2-input multiplexer connects one of two 1-bit sources to a common output, producing a 2-to-1-line multiplexer and we can confirm this in the following Boolean expression.
Q = A.I0.I1 + A.I0.I1 + A.I0.I1 + A.I0.I1
and for our 2-input multiplexer circuit above, this can be simplified too:
Q = A.I1 + A.I0
We can increase the number of data inputs to be selected further simply by following the same procedure and larger multiplexer circuits can be implemented using smaller 2-to-1 multiplexers as building blocks. For a 4-input multiplexer we would therefore require two data select lines as 4-inputs represents 22 data control lines give a circuit with four inputs, I0I1I2I3 and two data select lines A and B as shown.

4-input Multiplexer Design

4-input multiplexer circuit 

We can see from above that the input which appears at the output Q is the only one selected by the control inputs A and B. The x value in the truth table corresponds to a “don’t care” condition. This means that the particular input can be at either a logic “0″ or a logic “1″ having no effect on the output state.
Then we can show the selection of the data through the 4-input multiplexer as a function of the data select bits as shown.

multiplexer input selection

Adding more control address lines will allow the multiplexer to control more inputs by a value of 2n control lines, but each control line configuration will connect only ONE input to the output.
The Multiplexer is a very useful combinational logic device allowing multiple data lines to be connected to a single output and as such they can be used in many different applications such as signal routing, data communications and data bus control. In general, a multiplexer with n select lines can select one of 2n data inputs. Therefore, multiplexers are sometimes referred to as “data selectors”.

Thursday, 18 July 2013

Let commence with 8051.

Hay guys…
  Welcome to the micro world i.e.… 8051 one of the very familiar  stream of our branch . The whole story has its beginnings in the far 80s when Intel launched the first series of micro-controllers called the MCS 051. Even though these micro-controllers had quite modest features in comparison to the new ones, they conquered the world very soon and became a standard for what nowadays is called the micro-controller.
The main reason for their great success and popularity is a skillfully chosen configuration which satisfies different needs of a large number of users allowing at the same time constant expansions.

  • 4 Kb of ROM is not much at all.
  • 128b of RAM (including SFRs) satisfies the user's basic needs.
  • 4 ports having in total of 32 input/output lines are in most cases sufficient to make all necessary connections to peripheral environment.
The whole configuration is obviously thought of as to satisfy the needs of most programmers working on development of automation devices.
Here we are going for many more applications and interfacing of various devices e.g.- ADC , stepper motor etc .  and also share some tricks for configuring the various devices with 8051 .
The various tools available for the assessment of 8051 is keil, SPJ Terminal etc. We will also go for certain practical based implementation as assignment and much more.