Thursday, June 24, 2010

Session 2 - Flash LED on Press of Switch


Now it is time to think about flashing LEDs on the press of switches.  The connections can be done like  8 pins of PORTA to 8 switches and 8 pins of PORTB to 8 LEDs.  The switches should be arranged in such a way that when it is pressed, a high voltage should be shorted to the other end.  The connection of LED is as in the previous session.  Our aim is to turn on the  LED when the respective switch is pressed.  That is, when we press switch 1, LED1 should flash and so on.  This can be achieved by writing a very simple code as shown below:


Program-2

Code Explained:

The code can be understood almost completely by a simple look on it.  However I am going to explain it a little.  Here our code starts with some initialization of PORTA and PORTB.  First PORTA is defined as input (DDRA=0x00;) and then PORTB as output (DDRB=0xFF;).  This time we have used the Hexadecimal format for coding.  Always keep in mind that each digit in HEX format represent 4 binary bits.  So here 0xFF means 0b11111111.

The next code used here is the 'while' loop.  I dare to explain what is a loop.  However, as its meaning indicates, it is a code used for repeated works.  What we write inside a loop, will be repeated.  The question is how much times should it repeat?  In this program the code used is 'while(1)'.  What does this '1' inside the brackets indicate?  Actually a condition is expected at the place of this '1'.  The loop will work on the basis of the result of that condition.  The result can attain two values: True (1) or False (0).  If the answer is a True then the codes written under the loop will work.  As the name of the loop is 'while' its meaning can be predicted.  That is, WHILE the condition is true, the code inside the loop will work.  Or in other words, the codes will work repeatedly until the condition become wrong.  For example take a look at the code below:

a=1;
while(a <= 10)
{
PORTA=0xFF;
a=a+1;
}

This code will work repeatedly for 10 times.
Now let us come to our example.  We have given a '1' instead of a condition and it means a TRUE result always.  Hence this loop always gives true result and the codes inside it will repeat infinitely!  This is what is called an infinite loop.  Infinite loop is an important concept in embedded programming.  We use it in many situations.

Now think about the code written inside the infinite loop which is going to be repeated infinite times (The term infinite means the time, from the processor is turned on and till it is turned off).  It is 'PORTB=PINA'.  This is the first time we use this code 'PINA'.  When a port is used as input, we use the keyword PIN to represent it.  If it is an output we use PORT.  Here the B port is used as output and A port as input and hence PORTB and PINA.  So what does this code do?  It just copies the value of PINA to PORTB.  If we press the switch (connects to HIGH value) connected to the 0th pin of A port (PA0), that value will be copied to PORTB.  So the result is when we press 0th swtich, 0th LED will flash.  When 1st switch is pressed 1st LED will flash and so on.  Since it is inside the infinite loop, the value of PINA is copied to PORTB continuously.  Thus we have reached our target!

Wednesday, June 9, 2010

Session 1 - Blink LED - A Convention

As convention, we can start with a program which blinks LED connected to the processor. The program below does this job. The LEDs should be connected in such a way that when a High Voltage (5V) is connected to one terminal, it should turn on.  It will be very simple to use a development board like STK500 for simulating the program.  (If you are using STK500, its circuit arrangement is different.  It will flash the LED when a zero is fed.)


Program - 1

This simple program can turn on 8 LEDs connected to 8 pins of PORTA of the processor.  The pin names are PA0, PA1,.... PA7.  When you burn this program into the processor, the LEDs will be turned on.  There are a lot of things to be understood behind the working of this program.  We can go to it step by step.

#include...:
Here the statement #include.. is what is called the 'Header file'.  As its name indicates, it is the head of this program.  We include this head in the starting of program.  Because it contains informations about the 'commands' which we use in program.  In that header file, instructions are provided to the computer (or specifically Compiler) about the things-to-do when a command is executed.  There are lot of header files are available.  These are also called as Library files.  Depending on the variety of commands we use in the program, more header files are to be included.  Because all commands cannot be defined in a single header file.  In this program only one header file is enough and that is the one we included here - io.h. io stands for input-output.

int main()
Every C program contains this statement.  The execution of commands will start from this 'main' block.  What we write under this statement will be executed line by line when the program runs.  The two brackets - () can be called as function.  So here we will call it as Main function (main()).  The main() is isolated from other part of the program by providing braces ({}).

DDRB=0b11111111;
Now we are getting into the core of this program.  In this statement, a number is shown as '0b11111111'.  The first two digits - 0b, only indicates that the number following it is in binary format.  If you need to write the number in hexadecimal you can use the prefix '0x'.  If you need to use decimal itself, no need of any prefix.  Usually in programs we follow the hexadecimal format.  It is mainly because the processing of Atmega processors are done in Hex format.  So if we are using hexadecimal, we will feel that we are working very close to the processor.  So this code can be rewritten as:
DDRB=0xFF;
Now what does this DDRB mean?  DDR means the Data Direction Register.  That is the name given by Atmel.  We are going to look on it from our way.  Our aim is to make the processor communicable with the outside world.  For that purpose a lot of pins are provided in it.  What can we do with these pins?  It is to be noted that all logics are realized, through the two states of these pins.  The 'Zero' State and the 'One' State.
When we use an Air conditioner, we adjust the temperature by simply pressing desired switches.  When we press one switch, it will pass a one/zero to the desired pin of th we wante microcontroller.  The program is coded in it such that if a one/zero is received in that particular pin, its meaning is to change the temperature.  For that purpose the processor change the state of another pin which is connected to the temperature control unit of the Air conditioner.  This is what is actually happening in the base level.
In this example of Air conditioner, you can notice one difference in the type of signal.  When we press switch to change the temperature, a signal is given TO the processor.  When processor identifies that it is the signal to change the temperature, we receive signal FROM the processor.  So there are two functions for the pins.  One is to receive input from the outside world and the other is to give output to the outside world.  That is a pin can be used in two ways depending on its direction of flow of signal.  For that purpose we use the DDR - The 'Data Direction Register'.

In Atmega16/32, there are 32 input/output pins are available.  These are divided as PORTA, PORTB, PORTC and PORTD.  In our program the code is written as DDRB.  It represents the pins of PORTB.  The value in this register (Register is a memory space.  Here it contains 8 bits) determines which pins in PORTB are to be used as input and which are to be as outputs.  In this case the value of DDRB is declared as 0b11111111 (8 Bits).  It means all the pins (PORTB has 8 pins) are used as output.  For making these pins input, the value 0b00000000 can be used.  We can make some pins as inputs and some as outputs in a single PORT by adjusting this value.  1 defines an output pin and 0 defines an input pin.

We made all the pins as output because we are connecting LEDs to these pins and our aim is to turn on these LEDs.  So it is an output from the processor.

PORTB=0b11111111;
We have defined the direction of pins as outputs.  Now it is time to think about what values those pins should actually give.  As per the circuit, we need a HIGH (1) output to turn the LED on.  For that purpose the command PORTB=0b11111111 is used.  Here the value 1 will provide a HIGH (about 5V) output in that pin.  So the code here says the processor to make all the pins to HIGH Value.  So when the processor gets turned on, all the pins in PORTB will provide a 5V output.

That's it!  A working program is ready now!  Now a little bit more.  Beginners may feel a main difference between the programs in embedded C and normal C and it is, we do not have a start and stop in embedded.  The program is expected to be working whenever the processor is ON.  But in the case of normal computer programs, we are expected a start and a stop of the program.

Note:  The program can be simulated using the software AVR Studio which can be downloaded from the site of Atmel for free.  The complete details about installing tools, using AVR Studio for creating, compiling, debugging program etc. can be downloaded by clicking here.  It gives a step by step and complete information needed to work with the softwares.